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 |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 354 | # define FSTAT _Py_fstat |
| 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 | |
| 2473 | PyDoc_STRVAR(os_stat__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2474 | "stat($module, /, path, *, dir_fd=None, follow_symlinks=True)\n" |
| 2475 | "--\n" |
| 2476 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2477 | "Perform a stat system call on the given path.\n" |
| 2478 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2479 | " path\n" |
| 2480 | " Path to be examined; can be string, bytes, or open-file-descriptor int.\n" |
| 2481 | " dir_fd\n" |
| 2482 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2483 | " and path should be a relative string; path will then be relative to\n" |
| 2484 | " that directory.\n" |
| 2485 | " follow_symlinks\n" |
| 2486 | " If False, and the last element of the path is a symbolic link,\n" |
| 2487 | " stat will examine the symbolic link itself instead of the file\n" |
| 2488 | " the link points to.\n" |
| 2489 | "\n" |
| 2490 | "dir_fd and follow_symlinks may not be implemented\n" |
| 2491 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2492 | " NotImplementedError.\n" |
| 2493 | "\n" |
| 2494 | "It\'s an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 2495 | " an open file descriptor."); |
| 2496 | |
| 2497 | #define OS_STAT_METHODDEF \ |
| 2498 | {"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2499 | |
| 2500 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2501 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2502 | |
| 2503 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2504 | os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2505 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2506 | PyObject *return_value = NULL; |
| 2507 | static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL}; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2508 | path_t path = PATH_T_INITIALIZE("stat", "path", 0, 1); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2509 | int dir_fd = DEFAULT_DIR_FD; |
| 2510 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2511 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2512 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2513 | "O&|$O&p:stat", _keywords, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2514 | path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2515 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2516 | return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2517 | |
| 2518 | exit: |
| 2519 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2520 | path_cleanup(&path); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2521 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2522 | return return_value; |
| 2523 | } |
| 2524 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2525 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2526 | os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2527 | /*[clinic end generated code: output=0e9f9508fa0c0607 input=099d356c306fa24a]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2528 | { |
| 2529 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); |
| 2530 | } |
| 2531 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2532 | |
| 2533 | /*[clinic input] |
| 2534 | os.lstat |
| 2535 | |
| 2536 | path : path_t |
| 2537 | |
| 2538 | * |
| 2539 | |
| 2540 | dir_fd : dir_fd(requires='fstatat') = None |
| 2541 | |
| 2542 | Perform a stat system call on the given path, without following symbolic links. |
| 2543 | |
| 2544 | Like stat(), but do not follow symbolic links. |
| 2545 | Equivalent to stat(path, follow_symlinks=False). |
| 2546 | [clinic start generated code]*/ |
| 2547 | |
| 2548 | PyDoc_STRVAR(os_lstat__doc__, |
| 2549 | "lstat($module, /, path, *, dir_fd=None)\n" |
| 2550 | "--\n" |
| 2551 | "\n" |
| 2552 | "Perform a stat system call on the given path, without following symbolic links.\n" |
| 2553 | "\n" |
| 2554 | "Like stat(), but do not follow symbolic links.\n" |
| 2555 | "Equivalent to stat(path, follow_symlinks=False)."); |
| 2556 | |
| 2557 | #define OS_LSTAT_METHODDEF \ |
| 2558 | {"lstat", (PyCFunction)os_lstat, METH_VARARGS|METH_KEYWORDS, os_lstat__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2559 | |
| 2560 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2561 | os_lstat_impl(PyModuleDef *module, path_t *path, int dir_fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2562 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2563 | static PyObject * |
| 2564 | os_lstat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 2565 | { |
| 2566 | PyObject *return_value = NULL; |
| 2567 | static char *_keywords[] = {"path", "dir_fd", NULL}; |
| 2568 | path_t path = PATH_T_INITIALIZE("lstat", "path", 0, 0); |
| 2569 | int dir_fd = DEFAULT_DIR_FD; |
| 2570 | |
| 2571 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2572 | "O&|$O&:lstat", _keywords, |
| 2573 | path_converter, &path, FSTATAT_DIR_FD_CONVERTER, &dir_fd)) |
| 2574 | goto exit; |
| 2575 | return_value = os_lstat_impl(module, &path, dir_fd); |
| 2576 | |
| 2577 | exit: |
| 2578 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2579 | path_cleanup(&path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2580 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2581 | return return_value; |
| 2582 | } |
| 2583 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2584 | static PyObject * |
| 2585 | os_lstat_impl(PyModuleDef *module, path_t *path, int dir_fd) |
| 2586 | /*[clinic end generated code: output=85702247224a2b1c input=0b7474765927b925]*/ |
| 2587 | { |
| 2588 | int follow_symlinks = 0; |
| 2589 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); |
| 2590 | } |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2591 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2592 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2593 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2594 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2595 | |
| 2596 | path: path_t(allow_fd=True) |
| 2597 | Path to be tested; can be string, bytes, or open-file-descriptor int. |
| 2598 | |
| 2599 | mode: int |
| 2600 | Operating-system mode bitfield. Can be F_OK to test existence, |
| 2601 | or the inclusive-OR of R_OK, W_OK, and X_OK. |
| 2602 | |
| 2603 | * |
| 2604 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2605 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2606 | If not None, it should be a file descriptor open to a directory, |
| 2607 | and path should be relative; path will then be relative to that |
| 2608 | directory. |
| 2609 | |
| 2610 | effective_ids: bool = False |
| 2611 | If True, access will use the effective uid/gid instead of |
| 2612 | the real uid/gid. |
| 2613 | |
| 2614 | follow_symlinks: bool = True |
| 2615 | If False, and the last element of the path is a symbolic link, |
| 2616 | access will examine the symbolic link itself instead of the file |
| 2617 | the link points to. |
| 2618 | |
| 2619 | Use the real uid/gid to test for access to a path. |
| 2620 | |
| 2621 | {parameters} |
| 2622 | dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 2623 | on your platform. If they are unavailable, using them will raise a |
| 2624 | NotImplementedError. |
| 2625 | |
| 2626 | Note that most operations will use the effective uid/gid, therefore this |
| 2627 | routine can be used in a suid/sgid environment to test if the invoking user |
| 2628 | has the specified access to the path. |
| 2629 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2630 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2631 | |
| 2632 | PyDoc_STRVAR(os_access__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2633 | "access($module, /, path, mode, *, dir_fd=None, effective_ids=False,\n" |
| 2634 | " follow_symlinks=True)\n" |
| 2635 | "--\n" |
| 2636 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2637 | "Use the real uid/gid to test for access to a path.\n" |
| 2638 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2639 | " path\n" |
| 2640 | " Path to be tested; can be string, bytes, or open-file-descriptor int.\n" |
| 2641 | " mode\n" |
| 2642 | " Operating-system mode bitfield. Can be F_OK to test existence,\n" |
| 2643 | " or the inclusive-OR of R_OK, W_OK, and X_OK.\n" |
| 2644 | " dir_fd\n" |
| 2645 | " If not None, it should be a file descriptor open to a directory,\n" |
| 2646 | " and path should be relative; path will then be relative to that\n" |
| 2647 | " directory.\n" |
| 2648 | " effective_ids\n" |
| 2649 | " If True, access will use the effective uid/gid instead of\n" |
| 2650 | " the real uid/gid.\n" |
| 2651 | " follow_symlinks\n" |
| 2652 | " If False, and the last element of the path is a symbolic link,\n" |
| 2653 | " access will examine the symbolic link itself instead of the file\n" |
| 2654 | " the link points to.\n" |
| 2655 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2656 | "dir_fd, effective_ids, and follow_symlinks may not be implemented\n" |
| 2657 | " on your platform. If they are unavailable, using them will raise a\n" |
| 2658 | " NotImplementedError.\n" |
| 2659 | "\n" |
| 2660 | "Note that most operations will use the effective uid/gid, therefore this\n" |
| 2661 | " routine can be used in a suid/sgid environment to test if the invoking user\n" |
| 2662 | " has the specified access to the path."); |
| 2663 | |
| 2664 | #define OS_ACCESS_METHODDEF \ |
| 2665 | {"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__}, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2667 | static int |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2668 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2669 | |
| 2670 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2671 | os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2672 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2673 | PyObject *return_value = NULL; |
| 2674 | static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL}; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2675 | path_t path = PATH_T_INITIALIZE("access", "path", 0, 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2676 | int mode; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2677 | int dir_fd = DEFAULT_DIR_FD; |
| 2678 | int effective_ids = 0; |
| 2679 | int follow_symlinks = 1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2680 | int _return_value; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2681 | |
| 2682 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2683 | "O&i|$O&pp:access", _keywords, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2684 | path_converter, &path, &mode, FACCESSAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks)) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2685 | goto exit; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2686 | _return_value = os_access_impl(module, &path, mode, dir_fd, effective_ids, follow_symlinks); |
| 2687 | if ((_return_value == -1) && PyErr_Occurred()) |
| 2688 | goto exit; |
| 2689 | return_value = PyBool_FromLong((long)_return_value); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2690 | |
| 2691 | exit: |
| 2692 | /* Cleanup for path */ |
| 2693 | path_cleanup(&path); |
| 2694 | |
| 2695 | return return_value; |
| 2696 | } |
| 2697 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2698 | static int |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2699 | os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2700 | /*[clinic end generated code: output=dfd404666906f012 input=b75a756797af45ec]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2701 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2702 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2703 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2704 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2705 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2706 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2707 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2708 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2709 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2710 | #ifndef HAVE_FACCESSAT |
| 2711 | if (follow_symlinks_specified("access", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2712 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2713 | |
| 2714 | if (effective_ids) { |
| 2715 | argument_unavailable_error("access", "effective_ids"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2716 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2717 | } |
| 2718 | #endif |
| 2719 | |
| 2720 | #ifdef MS_WINDOWS |
| 2721 | Py_BEGIN_ALLOW_THREADS |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2722 | if (path->wide != NULL) |
| 2723 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2724 | else |
Christian Heimes | ebe83f9 | 2013-10-19 22:36:17 +0200 | [diff] [blame] | 2725 | attr = GetFileAttributesA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2726 | Py_END_ALLOW_THREADS |
| 2727 | |
| 2728 | /* |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2729 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2730 | * * we didn't get a -1, and |
| 2731 | * * write access wasn't requested, |
| 2732 | * * or the file isn't read-only, |
| 2733 | * * or it's a directory. |
| 2734 | * (Directories cannot be read-only on Windows.) |
| 2735 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2736 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2737 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2738 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2739 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2740 | #else |
| 2741 | |
| 2742 | Py_BEGIN_ALLOW_THREADS |
| 2743 | #ifdef HAVE_FACCESSAT |
| 2744 | if ((dir_fd != DEFAULT_DIR_FD) || |
| 2745 | effective_ids || |
| 2746 | !follow_symlinks) { |
| 2747 | int flags = 0; |
| 2748 | if (!follow_symlinks) |
| 2749 | flags |= AT_SYMLINK_NOFOLLOW; |
| 2750 | if (effective_ids) |
| 2751 | flags |= AT_EACCESS; |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2752 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2753 | } |
| 2754 | else |
| 2755 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2756 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2757 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2758 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2759 | #endif |
| 2760 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2761 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2764 | #ifndef F_OK |
| 2765 | #define F_OK 0 |
| 2766 | #endif |
| 2767 | #ifndef R_OK |
| 2768 | #define R_OK 4 |
| 2769 | #endif |
| 2770 | #ifndef W_OK |
| 2771 | #define W_OK 2 |
| 2772 | #endif |
| 2773 | #ifndef X_OK |
| 2774 | #define X_OK 1 |
| 2775 | #endif |
| 2776 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2777 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2778 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2779 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2780 | os.ttyname -> DecodeFSDefault |
| 2781 | |
| 2782 | fd: int |
| 2783 | Integer file descriptor handle. |
| 2784 | |
| 2785 | / |
| 2786 | |
| 2787 | Return the name of the terminal device connected to 'fd'. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2788 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2789 | |
| 2790 | PyDoc_STRVAR(os_ttyname__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2791 | "ttyname($module, fd, /)\n" |
| 2792 | "--\n" |
| 2793 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2794 | "Return the name of the terminal device connected to \'fd\'.\n" |
| 2795 | "\n" |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2796 | " fd\n" |
| 2797 | " Integer file descriptor handle."); |
| 2798 | |
| 2799 | #define OS_TTYNAME_METHODDEF \ |
| 2800 | {"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__}, |
| 2801 | |
| 2802 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2803 | os_ttyname_impl(PyModuleDef *module, int fd); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2804 | |
| 2805 | static PyObject * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2806 | os_ttyname(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2807 | { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2808 | PyObject *return_value = NULL; |
| 2809 | int fd; |
| 2810 | char *_return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2811 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2812 | if (!PyArg_ParseTuple(args, |
| 2813 | "i:ttyname", |
| 2814 | &fd)) |
| 2815 | goto exit; |
Larry Hastings | ed4a1c5 | 2013-11-18 09:32:13 -0800 | [diff] [blame] | 2816 | _return_value = os_ttyname_impl(module, fd); |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2817 | if (_return_value == NULL) |
| 2818 | goto exit; |
| 2819 | return_value = PyUnicode_DecodeFSDefault(_return_value); |
| 2820 | |
| 2821 | exit: |
| 2822 | return return_value; |
| 2823 | } |
| 2824 | |
| 2825 | static char * |
Larry Hastings | ebdcb50 | 2013-11-23 14:54:00 -0800 | [diff] [blame] | 2826 | os_ttyname_impl(PyModuleDef *module, int fd) |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 2827 | /*[clinic end generated code: output=cee7bc4cffec01a2 input=5f72ca83e76b3b45]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2828 | { |
| 2829 | char *ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2830 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2831 | ret = ttyname(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2832 | if (ret == NULL) |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2833 | posix_error(); |
| 2834 | return ret; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2835 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2836 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2837 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2838 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2839 | /*[clinic input] |
| 2840 | os.ctermid |
| 2841 | |
| 2842 | Return the name of the controlling terminal for this process. |
| 2843 | [clinic start generated code]*/ |
| 2844 | |
| 2845 | PyDoc_STRVAR(os_ctermid__doc__, |
| 2846 | "ctermid($module, /)\n" |
| 2847 | "--\n" |
| 2848 | "\n" |
| 2849 | "Return the name of the controlling terminal for this process."); |
| 2850 | |
| 2851 | #define OS_CTERMID_METHODDEF \ |
| 2852 | {"ctermid", (PyCFunction)os_ctermid, METH_NOARGS, os_ctermid__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2853 | |
| 2854 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2855 | os_ctermid_impl(PyModuleDef *module); |
| 2856 | |
| 2857 | static PyObject * |
| 2858 | os_ctermid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 2859 | { |
| 2860 | return os_ctermid_impl(module); |
| 2861 | } |
| 2862 | |
| 2863 | static PyObject * |
| 2864 | os_ctermid_impl(PyModuleDef *module) |
| 2865 | /*[clinic end generated code: output=277bf7964ec2d782 input=3b87fdd52556382d]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2866 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2867 | char *ret; |
| 2868 | char buffer[L_ctermid]; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2869 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2870 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2871 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2872 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2873 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2874 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2875 | if (ret == NULL) |
| 2876 | return posix_error(); |
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2877 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2878 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2879 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2880 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2881 | |
| 2882 | /*[clinic input] |
| 2883 | os.chdir |
| 2884 | |
| 2885 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') |
| 2886 | |
| 2887 | Change the current working directory to the specified path. |
| 2888 | |
| 2889 | path may always be specified as a string. |
| 2890 | On some platforms, path may also be specified as an open file descriptor. |
| 2891 | If this functionality is unavailable, using it raises an exception. |
| 2892 | [clinic start generated code]*/ |
| 2893 | |
| 2894 | PyDoc_STRVAR(os_chdir__doc__, |
| 2895 | "chdir($module, /, path)\n" |
| 2896 | "--\n" |
| 2897 | "\n" |
| 2898 | "Change the current working directory to the specified path.\n" |
| 2899 | "\n" |
| 2900 | "path may always be specified as a string.\n" |
| 2901 | "On some platforms, path may also be specified as an open file descriptor.\n" |
| 2902 | " If this functionality is unavailable, using it raises an exception."); |
| 2903 | |
| 2904 | #define OS_CHDIR_METHODDEF \ |
| 2905 | {"chdir", (PyCFunction)os_chdir, METH_VARARGS|METH_KEYWORDS, os_chdir__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2906 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 2907 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2908 | os_chdir_impl(PyModuleDef *module, path_t *path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2909 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2910 | static PyObject * |
| 2911 | os_chdir(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 2912 | { |
| 2913 | PyObject *return_value = NULL; |
| 2914 | static char *_keywords[] = {"path", NULL}; |
| 2915 | path_t path = PATH_T_INITIALIZE("chdir", "path", 0, PATH_HAVE_FCHDIR); |
| 2916 | |
| 2917 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2918 | "O&:chdir", _keywords, |
| 2919 | path_converter, &path)) |
| 2920 | goto exit; |
| 2921 | return_value = os_chdir_impl(module, &path); |
| 2922 | |
| 2923 | exit: |
| 2924 | /* Cleanup for path */ |
| 2925 | path_cleanup(&path); |
| 2926 | |
| 2927 | return return_value; |
| 2928 | } |
| 2929 | |
| 2930 | static PyObject * |
| 2931 | os_chdir_impl(PyModuleDef *module, path_t *path) |
| 2932 | /*[clinic end generated code: output=cc07592dd23ca9e0 input=1a4a15b4d12cb15d]*/ |
| 2933 | { |
| 2934 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2935 | |
| 2936 | Py_BEGIN_ALLOW_THREADS |
| 2937 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2938 | if (path->wide) |
| 2939 | result = win32_wchdir(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2940 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2941 | result = win32_chdir(path->narrow); |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 2942 | result = !result; /* on unix, success = 0, on windows, success = !0 */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2943 | #else |
| 2944 | #ifdef HAVE_FCHDIR |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2945 | if (path->fd != -1) |
| 2946 | result = fchdir(path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2947 | else |
| 2948 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2949 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2950 | #endif |
| 2951 | Py_END_ALLOW_THREADS |
| 2952 | |
| 2953 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2954 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2957 | Py_RETURN_NONE; |
| 2958 | } |
| 2959 | |
| 2960 | |
| 2961 | #ifdef HAVE_FCHDIR |
| 2962 | /*[clinic input] |
| 2963 | os.fchdir |
| 2964 | |
| 2965 | fd: fildes |
| 2966 | |
| 2967 | Change to the directory of the given file descriptor. |
| 2968 | |
| 2969 | fd must be opened on a directory, not a file. |
| 2970 | Equivalent to os.chdir(fd). |
| 2971 | |
| 2972 | [clinic start generated code]*/ |
| 2973 | |
| 2974 | PyDoc_STRVAR(os_fchdir__doc__, |
| 2975 | "fchdir($module, /, fd)\n" |
| 2976 | "--\n" |
| 2977 | "\n" |
| 2978 | "Change to the directory of the given file descriptor.\n" |
| 2979 | "\n" |
| 2980 | "fd must be opened on a directory, not a file.\n" |
| 2981 | "Equivalent to os.chdir(fd)."); |
| 2982 | |
| 2983 | #define OS_FCHDIR_METHODDEF \ |
| 2984 | {"fchdir", (PyCFunction)os_fchdir, METH_VARARGS|METH_KEYWORDS, os_fchdir__doc__}, |
| 2985 | |
| 2986 | static PyObject * |
| 2987 | os_fchdir_impl(PyModuleDef *module, int fd); |
| 2988 | |
| 2989 | static PyObject * |
| 2990 | os_fchdir(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 2991 | { |
| 2992 | PyObject *return_value = NULL; |
| 2993 | static char *_keywords[] = {"fd", NULL}; |
| 2994 | int fd; |
| 2995 | |
| 2996 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 2997 | "O&:fchdir", _keywords, |
| 2998 | fildes_converter, &fd)) |
| 2999 | goto exit; |
| 3000 | return_value = os_fchdir_impl(module, fd); |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3001 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3002 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3003 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3006 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3007 | os_fchdir_impl(PyModuleDef *module, int fd) |
| 3008 | /*[clinic end generated code: output=9f6dbc89b2778834 input=18e816479a2fa985]*/ |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3009 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3010 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 3011 | } |
| 3012 | #endif /* HAVE_FCHDIR */ |
| 3013 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3014 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3015 | /*[clinic input] |
| 3016 | os.chmod |
| 3017 | |
| 3018 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') |
| 3019 | Path to be modified. May always be specified as a str or bytes. |
| 3020 | On some platforms, path may also be specified as an open file descriptor. |
| 3021 | If this functionality is unavailable, using it raises an exception. |
| 3022 | |
| 3023 | mode: int |
| 3024 | Operating-system mode bitfield. |
| 3025 | |
| 3026 | * |
| 3027 | |
| 3028 | dir_fd : dir_fd(requires='fchmodat') = None |
| 3029 | If not None, it should be a file descriptor open to a directory, |
| 3030 | and path should be relative; path will then be relative to that |
| 3031 | directory. |
| 3032 | |
| 3033 | follow_symlinks: bool = True |
| 3034 | If False, and the last element of the path is a symbolic link, |
| 3035 | chmod will modify the symbolic link itself instead of the file |
| 3036 | the link points to. |
| 3037 | |
| 3038 | Change the access permissions of a file. |
| 3039 | |
| 3040 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3041 | an open file descriptor. |
| 3042 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3043 | If they are unavailable, using them will raise a NotImplementedError. |
| 3044 | |
| 3045 | [clinic start generated code]*/ |
| 3046 | |
| 3047 | PyDoc_STRVAR(os_chmod__doc__, |
| 3048 | "chmod($module, /, path, mode, *, dir_fd=None, follow_symlinks=True)\n" |
| 3049 | "--\n" |
| 3050 | "\n" |
| 3051 | "Change the access permissions of a file.\n" |
| 3052 | "\n" |
| 3053 | " path\n" |
| 3054 | " Path to be modified. May always be specified as a str or bytes.\n" |
| 3055 | " On some platforms, path may also be specified as an open file descriptor.\n" |
| 3056 | " If this functionality is unavailable, using it raises an exception.\n" |
| 3057 | " mode\n" |
| 3058 | " Operating-system mode bitfield.\n" |
| 3059 | " dir_fd\n" |
| 3060 | " If not None, it should be a file descriptor open to a directory,\n" |
| 3061 | " and path should be relative; path will then be relative to that\n" |
| 3062 | " directory.\n" |
| 3063 | " follow_symlinks\n" |
| 3064 | " If False, and the last element of the path is a symbolic link,\n" |
| 3065 | " chmod will modify the symbolic link itself instead of the file\n" |
| 3066 | " the link points to.\n" |
| 3067 | "\n" |
| 3068 | "It is an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 3069 | " an open file descriptor.\n" |
| 3070 | "dir_fd and follow_symlinks may not be implemented on your platform.\n" |
| 3071 | " If they are unavailable, using them will raise a NotImplementedError."); |
| 3072 | |
| 3073 | #define OS_CHMOD_METHODDEF \ |
| 3074 | {"chmod", (PyCFunction)os_chmod, METH_VARARGS|METH_KEYWORDS, os_chmod__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3075 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3076 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3077 | os_chmod_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int follow_symlinks); |
| 3078 | |
| 3079 | static PyObject * |
| 3080 | os_chmod(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3081 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3082 | PyObject *return_value = NULL; |
| 3083 | static char *_keywords[] = {"path", "mode", "dir_fd", "follow_symlinks", NULL}; |
| 3084 | path_t path = PATH_T_INITIALIZE("chmod", "path", 0, PATH_HAVE_FCHMOD); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3085 | int mode; |
| 3086 | int dir_fd = DEFAULT_DIR_FD; |
| 3087 | int follow_symlinks = 1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3088 | |
| 3089 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3090 | "O&i|$O&p:chmod", _keywords, |
| 3091 | path_converter, &path, &mode, FCHMODAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 3092 | goto exit; |
| 3093 | return_value = os_chmod_impl(module, &path, mode, dir_fd, follow_symlinks); |
| 3094 | |
| 3095 | exit: |
| 3096 | /* Cleanup for path */ |
| 3097 | path_cleanup(&path); |
| 3098 | |
| 3099 | return return_value; |
| 3100 | } |
| 3101 | |
| 3102 | static PyObject * |
| 3103 | os_chmod_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int follow_symlinks) |
| 3104 | /*[clinic end generated code: output=1e9db031aea46422 input=7f1618e5e15cc196]*/ |
| 3105 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3106 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3107 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3108 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3109 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3110 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 3111 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3112 | #ifdef HAVE_FCHMODAT |
| 3113 | int fchmodat_nofollow_unsupported = 0; |
| 3114 | #endif |
| 3115 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3116 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
| 3117 | if (follow_symlinks_specified("chmod", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3118 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3119 | #endif |
| 3120 | |
| 3121 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3122 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3123 | if (path->wide) |
| 3124 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3125 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3126 | attr = GetFileAttributesA(path->narrow); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 3127 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3128 | result = 0; |
| 3129 | else { |
| 3130 | if (mode & _S_IWRITE) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3131 | attr &= ~FILE_ATTRIBUTE_READONLY; |
| 3132 | else |
| 3133 | attr |= FILE_ATTRIBUTE_READONLY; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3134 | if (path->wide) |
| 3135 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3136 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3137 | result = SetFileAttributesA(path->narrow, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3138 | } |
| 3139 | Py_END_ALLOW_THREADS |
| 3140 | |
| 3141 | if (!result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3142 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3143 | } |
| 3144 | #else /* MS_WINDOWS */ |
| 3145 | Py_BEGIN_ALLOW_THREADS |
| 3146 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3147 | if (path->fd != -1) |
| 3148 | result = fchmod(path->fd, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3149 | else |
| 3150 | #endif |
| 3151 | #ifdef HAVE_LCHMOD |
| 3152 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3153 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3154 | else |
| 3155 | #endif |
| 3156 | #ifdef HAVE_FCHMODAT |
| 3157 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { |
| 3158 | /* |
| 3159 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! |
| 3160 | * The documentation specifically shows how to use it, |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3161 | * and then says it isn't implemented yet. |
| 3162 | * (true on linux with glibc 2.15, and openindiana 3.x) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3163 | * |
| 3164 | * Once it is supported, os.chmod will automatically |
| 3165 | * support dir_fd and follow_symlinks=False. (Hopefully.) |
| 3166 | * Until then, we need to be careful what exception we raise. |
| 3167 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3168 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3169 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3170 | /* |
| 3171 | * But wait! We can't throw the exception without allowing threads, |
| 3172 | * and we can't do that in this nested scope. (Macro trickery, sigh.) |
| 3173 | */ |
| 3174 | fchmodat_nofollow_unsupported = |
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 3175 | result && |
| 3176 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && |
| 3177 | !follow_symlinks; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3178 | } |
| 3179 | else |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 3180 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3181 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3182 | Py_END_ALLOW_THREADS |
| 3183 | |
| 3184 | if (result) { |
| 3185 | #ifdef HAVE_FCHMODAT |
| 3186 | if (fchmodat_nofollow_unsupported) { |
| 3187 | if (dir_fd != DEFAULT_DIR_FD) |
| 3188 | dir_fd_and_follow_symlinks_invalid("chmod", |
| 3189 | dir_fd, follow_symlinks); |
| 3190 | else |
| 3191 | follow_symlinks_specified("chmod", follow_symlinks); |
| 3192 | } |
| 3193 | else |
| 3194 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3195 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3196 | } |
| 3197 | #endif |
| 3198 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3199 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3202 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3203 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3204 | /*[clinic input] |
| 3205 | os.fchmod |
| 3206 | |
| 3207 | fd: int |
| 3208 | mode: int |
| 3209 | |
| 3210 | Change the access permissions of the file given by file descriptor fd. |
| 3211 | |
| 3212 | Equivalent to os.chmod(fd, mode). |
| 3213 | [clinic start generated code]*/ |
| 3214 | |
| 3215 | PyDoc_STRVAR(os_fchmod__doc__, |
| 3216 | "fchmod($module, /, fd, mode)\n" |
| 3217 | "--\n" |
| 3218 | "\n" |
| 3219 | "Change the access permissions of the file given by file descriptor fd.\n" |
| 3220 | "\n" |
| 3221 | "Equivalent to os.chmod(fd, mode)."); |
| 3222 | |
| 3223 | #define OS_FCHMOD_METHODDEF \ |
| 3224 | {"fchmod", (PyCFunction)os_fchmod, METH_VARARGS|METH_KEYWORDS, os_fchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3225 | |
| 3226 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3227 | os_fchmod_impl(PyModuleDef *module, int fd, int mode); |
| 3228 | |
| 3229 | static PyObject * |
| 3230 | os_fchmod(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3231 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3232 | PyObject *return_value = NULL; |
| 3233 | static char *_keywords[] = {"fd", "mode", NULL}; |
| 3234 | int fd; |
| 3235 | int mode; |
| 3236 | |
| 3237 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3238 | "ii:fchmod", _keywords, |
| 3239 | &fd, &mode)) |
| 3240 | goto exit; |
| 3241 | return_value = os_fchmod_impl(module, fd, mode); |
| 3242 | |
| 3243 | exit: |
| 3244 | return return_value; |
| 3245 | } |
| 3246 | |
| 3247 | static PyObject * |
| 3248 | os_fchmod_impl(PyModuleDef *module, int fd, int mode) |
| 3249 | /*[clinic end generated code: output=3c19fbfd724a8e0f input=8ab11975ca01ee5b]*/ |
| 3250 | { |
| 3251 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3252 | int async_err = 0; |
| 3253 | |
| 3254 | do { |
| 3255 | Py_BEGIN_ALLOW_THREADS |
| 3256 | res = fchmod(fd, mode); |
| 3257 | Py_END_ALLOW_THREADS |
| 3258 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3259 | if (res != 0) |
| 3260 | return (!async_err) ? posix_error() : NULL; |
| 3261 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3262 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3263 | } |
| 3264 | #endif /* HAVE_FCHMOD */ |
| 3265 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3266 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3267 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3268 | /*[clinic input] |
| 3269 | os.lchmod |
| 3270 | |
| 3271 | path: path_t |
| 3272 | mode: int |
| 3273 | |
| 3274 | Change the access permissions of a file, without following symbolic links. |
| 3275 | |
| 3276 | If path is a symlink, this affects the link itself rather than the target. |
| 3277 | Equivalent to chmod(path, mode, follow_symlinks=False)." |
| 3278 | [clinic start generated code]*/ |
| 3279 | |
| 3280 | PyDoc_STRVAR(os_lchmod__doc__, |
| 3281 | "lchmod($module, /, path, mode)\n" |
| 3282 | "--\n" |
| 3283 | "\n" |
| 3284 | "Change the access permissions of a file, without following symbolic links.\n" |
| 3285 | "\n" |
| 3286 | "If path is a symlink, this affects the link itself rather than the target.\n" |
| 3287 | "Equivalent to chmod(path, mode, follow_symlinks=False).\""); |
| 3288 | |
| 3289 | #define OS_LCHMOD_METHODDEF \ |
| 3290 | {"lchmod", (PyCFunction)os_lchmod, METH_VARARGS|METH_KEYWORDS, os_lchmod__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3291 | |
| 3292 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3293 | os_lchmod_impl(PyModuleDef *module, path_t *path, int mode); |
| 3294 | |
| 3295 | static PyObject * |
| 3296 | os_lchmod(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3297 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3298 | PyObject *return_value = NULL; |
| 3299 | static char *_keywords[] = {"path", "mode", NULL}; |
| 3300 | path_t path = PATH_T_INITIALIZE("lchmod", "path", 0, 0); |
| 3301 | int mode; |
| 3302 | |
| 3303 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3304 | "O&i:lchmod", _keywords, |
| 3305 | path_converter, &path, &mode)) |
| 3306 | goto exit; |
| 3307 | return_value = os_lchmod_impl(module, &path, mode); |
| 3308 | |
| 3309 | exit: |
| 3310 | /* Cleanup for path */ |
| 3311 | path_cleanup(&path); |
| 3312 | |
| 3313 | return return_value; |
| 3314 | } |
| 3315 | |
| 3316 | static PyObject * |
| 3317 | os_lchmod_impl(PyModuleDef *module, path_t *path, int mode) |
| 3318 | /*[clinic end generated code: output=2849977d65f8c68c input=90c5663c7465d24f]*/ |
| 3319 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3320 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3321 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3322 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3324 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3325 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3326 | return NULL; |
| 3327 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3328 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3329 | } |
| 3330 | #endif /* HAVE_LCHMOD */ |
| 3331 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3332 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3333 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3334 | /*[clinic input] |
| 3335 | os.chflags |
| 3336 | |
| 3337 | path: path_t |
| 3338 | flags: unsigned_long(bitwise=True) |
| 3339 | follow_symlinks: bool=True |
| 3340 | |
| 3341 | Set file flags. |
| 3342 | |
| 3343 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3344 | link, chflags will change flags on the symbolic link itself instead of the |
| 3345 | file the link points to. |
| 3346 | follow_symlinks may not be implemented on your platform. If it is |
| 3347 | unavailable, using it will raise a NotImplementedError. |
| 3348 | |
| 3349 | [clinic start generated code]*/ |
| 3350 | |
| 3351 | PyDoc_STRVAR(os_chflags__doc__, |
| 3352 | "chflags($module, /, path, flags, follow_symlinks=True)\n" |
| 3353 | "--\n" |
| 3354 | "\n" |
| 3355 | "Set file flags.\n" |
| 3356 | "\n" |
| 3357 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 3358 | " link, chflags will change flags on the symbolic link itself instead of the\n" |
| 3359 | " file the link points to.\n" |
| 3360 | "follow_symlinks may not be implemented on your platform. If it is\n" |
| 3361 | "unavailable, using it will raise a NotImplementedError."); |
| 3362 | |
| 3363 | #define OS_CHFLAGS_METHODDEF \ |
| 3364 | {"chflags", (PyCFunction)os_chflags, METH_VARARGS|METH_KEYWORDS, os_chflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3365 | |
| 3366 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3367 | os_chflags_impl(PyModuleDef *module, path_t *path, unsigned long flags, int follow_symlinks); |
| 3368 | |
| 3369 | static PyObject * |
| 3370 | os_chflags(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3371 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3372 | PyObject *return_value = NULL; |
| 3373 | static char *_keywords[] = {"path", "flags", "follow_symlinks", NULL}; |
| 3374 | path_t path = PATH_T_INITIALIZE("chflags", "path", 0, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3375 | unsigned long flags; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3376 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3377 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3378 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3379 | "O&k|p:chflags", _keywords, |
| 3380 | path_converter, &path, &flags, &follow_symlinks)) |
| 3381 | goto exit; |
| 3382 | return_value = os_chflags_impl(module, &path, flags, follow_symlinks); |
| 3383 | |
| 3384 | exit: |
| 3385 | /* Cleanup for path */ |
| 3386 | path_cleanup(&path); |
| 3387 | |
| 3388 | return return_value; |
| 3389 | } |
| 3390 | |
| 3391 | static PyObject * |
| 3392 | os_chflags_impl(PyModuleDef *module, path_t *path, unsigned long flags, int follow_symlinks) |
| 3393 | /*[clinic end generated code: output=2767927bf071e3cf input=0327e29feb876236]*/ |
| 3394 | { |
| 3395 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3396 | |
| 3397 | #ifndef HAVE_LCHFLAGS |
| 3398 | if (follow_symlinks_specified("chflags", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3399 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3400 | #endif |
| 3401 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3402 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3403 | #ifdef HAVE_LCHFLAGS |
| 3404 | if (!follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3405 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3406 | else |
| 3407 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3408 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3409 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3410 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3411 | if (result) |
| 3412 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3413 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3414 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3415 | } |
| 3416 | #endif /* HAVE_CHFLAGS */ |
| 3417 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3418 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3419 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3420 | /*[clinic input] |
| 3421 | os.lchflags |
| 3422 | |
| 3423 | path: path_t |
| 3424 | flags: unsigned_long(bitwise=True) |
| 3425 | |
| 3426 | Set file flags. |
| 3427 | |
| 3428 | This function will not follow symbolic links. |
| 3429 | Equivalent to chflags(path, flags, follow_symlinks=False). |
| 3430 | [clinic start generated code]*/ |
| 3431 | |
| 3432 | PyDoc_STRVAR(os_lchflags__doc__, |
| 3433 | "lchflags($module, /, path, flags)\n" |
| 3434 | "--\n" |
| 3435 | "\n" |
| 3436 | "Set file flags.\n" |
| 3437 | "\n" |
| 3438 | "This function will not follow symbolic links.\n" |
| 3439 | "Equivalent to chflags(path, flags, follow_symlinks=False)."); |
| 3440 | |
| 3441 | #define OS_LCHFLAGS_METHODDEF \ |
| 3442 | {"lchflags", (PyCFunction)os_lchflags, METH_VARARGS|METH_KEYWORDS, os_lchflags__doc__}, |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3443 | |
| 3444 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3445 | os_lchflags_impl(PyModuleDef *module, path_t *path, unsigned long flags); |
| 3446 | |
| 3447 | static PyObject * |
| 3448 | os_lchflags(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3449 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3450 | PyObject *return_value = NULL; |
| 3451 | static char *_keywords[] = {"path", "flags", NULL}; |
| 3452 | path_t path = PATH_T_INITIALIZE("lchflags", "path", 0, 0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3453 | unsigned long flags; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3454 | |
| 3455 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3456 | "O&k:lchflags", _keywords, |
| 3457 | path_converter, &path, &flags)) |
| 3458 | goto exit; |
| 3459 | return_value = os_lchflags_impl(module, &path, flags); |
| 3460 | |
| 3461 | exit: |
| 3462 | /* Cleanup for path */ |
| 3463 | path_cleanup(&path); |
| 3464 | |
| 3465 | return return_value; |
| 3466 | } |
| 3467 | |
| 3468 | static PyObject * |
| 3469 | os_lchflags_impl(PyModuleDef *module, path_t *path, unsigned long flags) |
| 3470 | /*[clinic end generated code: output=bb93b6b8a5e45aa7 input=f9f82ea8b585ca9d]*/ |
| 3471 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3472 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3473 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3474 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3475 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3476 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3477 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3478 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3479 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3480 | } |
| 3481 | #endif /* HAVE_LCHFLAGS */ |
| 3482 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3483 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3484 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3485 | /*[clinic input] |
| 3486 | os.chroot |
| 3487 | path: path_t |
| 3488 | |
| 3489 | Change root directory to path. |
| 3490 | |
| 3491 | [clinic start generated code]*/ |
| 3492 | |
| 3493 | PyDoc_STRVAR(os_chroot__doc__, |
| 3494 | "chroot($module, /, path)\n" |
| 3495 | "--\n" |
| 3496 | "\n" |
| 3497 | "Change root directory to path."); |
| 3498 | |
| 3499 | #define OS_CHROOT_METHODDEF \ |
| 3500 | {"chroot", (PyCFunction)os_chroot, METH_VARARGS|METH_KEYWORDS, os_chroot__doc__}, |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3501 | |
| 3502 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3503 | os_chroot_impl(PyModuleDef *module, path_t *path); |
| 3504 | |
| 3505 | static PyObject * |
| 3506 | os_chroot(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3507 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3508 | PyObject *return_value = NULL; |
| 3509 | static char *_keywords[] = {"path", NULL}; |
| 3510 | path_t path = PATH_T_INITIALIZE("chroot", "path", 0, 0); |
| 3511 | |
| 3512 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3513 | "O&:chroot", _keywords, |
| 3514 | path_converter, &path)) |
| 3515 | goto exit; |
| 3516 | return_value = os_chroot_impl(module, &path); |
| 3517 | |
| 3518 | exit: |
| 3519 | /* Cleanup for path */ |
| 3520 | path_cleanup(&path); |
| 3521 | |
| 3522 | return return_value; |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3523 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3524 | |
| 3525 | static PyObject * |
| 3526 | os_chroot_impl(PyModuleDef *module, path_t *path) |
| 3527 | /*[clinic end generated code: output=15b1256cbe4f24a1 input=14822965652c3dc3]*/ |
| 3528 | { |
| 3529 | int res; |
| 3530 | Py_BEGIN_ALLOW_THREADS |
| 3531 | res = chroot(path->narrow); |
| 3532 | Py_END_ALLOW_THREADS |
| 3533 | if (res < 0) |
| 3534 | return path_error(path); |
| 3535 | Py_RETURN_NONE; |
| 3536 | } |
| 3537 | #endif /* HAVE_CHROOT */ |
| 3538 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3539 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3540 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3541 | /*[clinic input] |
| 3542 | os.fsync |
| 3543 | |
| 3544 | fd: fildes |
| 3545 | |
| 3546 | Force write of fd to disk. |
| 3547 | [clinic start generated code]*/ |
| 3548 | |
| 3549 | PyDoc_STRVAR(os_fsync__doc__, |
| 3550 | "fsync($module, /, fd)\n" |
| 3551 | "--\n" |
| 3552 | "\n" |
| 3553 | "Force write of fd to disk."); |
| 3554 | |
| 3555 | #define OS_FSYNC_METHODDEF \ |
| 3556 | {"fsync", (PyCFunction)os_fsync, METH_VARARGS|METH_KEYWORDS, os_fsync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3557 | |
| 3558 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3559 | os_fsync_impl(PyModuleDef *module, int fd); |
| 3560 | |
| 3561 | static PyObject * |
| 3562 | os_fsync(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3563 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3564 | PyObject *return_value = NULL; |
| 3565 | static char *_keywords[] = {"fd", NULL}; |
| 3566 | int fd; |
| 3567 | |
| 3568 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3569 | "O&:fsync", _keywords, |
| 3570 | fildes_converter, &fd)) |
| 3571 | goto exit; |
| 3572 | return_value = os_fsync_impl(module, fd); |
| 3573 | |
| 3574 | exit: |
| 3575 | return return_value; |
| 3576 | } |
| 3577 | |
| 3578 | static PyObject * |
| 3579 | os_fsync_impl(PyModuleDef *module, int fd) |
| 3580 | /*[clinic end generated code: output=59f32d3a0b360133 input=21c3645c056967f2]*/ |
| 3581 | { |
| 3582 | return posix_fildes_fd(fd, fsync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3583 | } |
| 3584 | #endif /* HAVE_FSYNC */ |
| 3585 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3586 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3587 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3588 | /*[clinic input] |
| 3589 | os.sync |
| 3590 | |
| 3591 | Force write of everything to disk. |
| 3592 | [clinic start generated code]*/ |
| 3593 | |
| 3594 | PyDoc_STRVAR(os_sync__doc__, |
| 3595 | "sync($module, /)\n" |
| 3596 | "--\n" |
| 3597 | "\n" |
| 3598 | "Force write of everything to disk."); |
| 3599 | |
| 3600 | #define OS_SYNC_METHODDEF \ |
| 3601 | {"sync", (PyCFunction)os_sync, METH_NOARGS, os_sync__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3602 | |
| 3603 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3604 | os_sync_impl(PyModuleDef *module); |
| 3605 | |
| 3606 | static PyObject * |
| 3607 | os_sync(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 3608 | { |
| 3609 | return os_sync_impl(module); |
| 3610 | } |
| 3611 | |
| 3612 | static PyObject * |
| 3613 | os_sync_impl(PyModuleDef *module) |
| 3614 | /*[clinic end generated code: output=526c495683d0bb38 input=84749fe5e9b404ff]*/ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3615 | { |
| 3616 | Py_BEGIN_ALLOW_THREADS |
| 3617 | sync(); |
| 3618 | Py_END_ALLOW_THREADS |
| 3619 | Py_RETURN_NONE; |
| 3620 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3621 | #endif /* HAVE_SYNC */ |
| 3622 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3623 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3624 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3625 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3626 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
| 3627 | #endif |
| 3628 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3629 | /*[clinic input] |
| 3630 | os.fdatasync |
| 3631 | |
| 3632 | fd: fildes |
| 3633 | |
| 3634 | Force write of fd to disk without forcing update of metadata. |
| 3635 | [clinic start generated code]*/ |
| 3636 | |
| 3637 | PyDoc_STRVAR(os_fdatasync__doc__, |
| 3638 | "fdatasync($module, /, fd)\n" |
| 3639 | "--\n" |
| 3640 | "\n" |
| 3641 | "Force write of fd to disk without forcing update of metadata."); |
| 3642 | |
| 3643 | #define OS_FDATASYNC_METHODDEF \ |
| 3644 | {"fdatasync", (PyCFunction)os_fdatasync, METH_VARARGS|METH_KEYWORDS, os_fdatasync__doc__}, |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3645 | |
| 3646 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3647 | os_fdatasync_impl(PyModuleDef *module, int fd); |
| 3648 | |
| 3649 | static PyObject * |
| 3650 | os_fdatasync(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3651 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3652 | PyObject *return_value = NULL; |
| 3653 | static char *_keywords[] = {"fd", NULL}; |
| 3654 | int fd; |
| 3655 | |
| 3656 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3657 | "O&:fdatasync", _keywords, |
| 3658 | fildes_converter, &fd)) |
| 3659 | goto exit; |
| 3660 | return_value = os_fdatasync_impl(module, fd); |
| 3661 | |
| 3662 | exit: |
| 3663 | return return_value; |
| 3664 | } |
| 3665 | |
| 3666 | static PyObject * |
| 3667 | os_fdatasync_impl(PyModuleDef *module, int fd) |
| 3668 | /*[clinic end generated code: output=2335fdfd37c92180 input=bc74791ee54dd291]*/ |
| 3669 | { |
| 3670 | return posix_fildes_fd(fd, fdatasync); |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3671 | } |
| 3672 | #endif /* HAVE_FDATASYNC */ |
| 3673 | |
| 3674 | |
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3675 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3676 | /*[clinic input] |
| 3677 | os.chown |
| 3678 | |
| 3679 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') |
| 3680 | Path to be examined; can be string, bytes, or open-file-descriptor int. |
| 3681 | |
| 3682 | uid: uid_t |
| 3683 | |
| 3684 | gid: gid_t |
| 3685 | |
| 3686 | * |
| 3687 | |
| 3688 | dir_fd : dir_fd(requires='fchownat') = None |
| 3689 | If not None, it should be a file descriptor open to a directory, |
| 3690 | and path should be relative; path will then be relative to that |
| 3691 | directory. |
| 3692 | |
| 3693 | follow_symlinks: bool = True |
| 3694 | If False, and the last element of the path is a symbolic link, |
| 3695 | stat will examine the symbolic link itself instead of the file |
| 3696 | the link points to. |
| 3697 | |
| 3698 | Change the owner and group id of path to the numeric uid and gid.\ |
| 3699 | |
| 3700 | path may always be specified as a string. |
| 3701 | On some platforms, path may also be specified as an open file descriptor. |
| 3702 | If this functionality is unavailable, using it raises an exception. |
| 3703 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 3704 | and path should be relative; path will then be relative to that directory. |
| 3705 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 3706 | link, chown will modify the symbolic link itself instead of the file the |
| 3707 | link points to. |
| 3708 | It is an error to use dir_fd or follow_symlinks when specifying path as |
| 3709 | an open file descriptor. |
| 3710 | dir_fd and follow_symlinks may not be implemented on your platform. |
| 3711 | If they are unavailable, using them will raise a NotImplementedError. |
| 3712 | |
| 3713 | [clinic start generated code]*/ |
| 3714 | |
| 3715 | PyDoc_STRVAR(os_chown__doc__, |
| 3716 | "chown($module, /, path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n" |
| 3717 | "--\n" |
| 3718 | "\n" |
| 3719 | "Change the owner and group id of path to the numeric uid and gid.\\\n" |
| 3720 | "\n" |
| 3721 | " path\n" |
| 3722 | " Path to be examined; can be string, bytes, or open-file-descriptor int.\n" |
| 3723 | " dir_fd\n" |
| 3724 | " If not None, it should be a file descriptor open to a directory,\n" |
| 3725 | " and path should be relative; path will then be relative to that\n" |
| 3726 | " directory.\n" |
| 3727 | " follow_symlinks\n" |
| 3728 | " If False, and the last element of the path is a symbolic link,\n" |
| 3729 | " stat will examine the symbolic link itself instead of the file\n" |
| 3730 | " the link points to.\n" |
| 3731 | "\n" |
| 3732 | "path may always be specified as a string.\n" |
| 3733 | "On some platforms, path may also be specified as an open file descriptor.\n" |
| 3734 | " If this functionality is unavailable, using it raises an exception.\n" |
| 3735 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 3736 | " and path should be relative; path will then be relative to that directory.\n" |
| 3737 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 3738 | " link, chown will modify the symbolic link itself instead of the file the\n" |
| 3739 | " link points to.\n" |
| 3740 | "It is an error to use dir_fd or follow_symlinks when specifying path as\n" |
| 3741 | " an open file descriptor.\n" |
| 3742 | "dir_fd and follow_symlinks may not be implemented on your platform.\n" |
| 3743 | " If they are unavailable, using them will raise a NotImplementedError."); |
| 3744 | |
| 3745 | #define OS_CHOWN_METHODDEF \ |
| 3746 | {"chown", (PyCFunction)os_chown, METH_VARARGS|METH_KEYWORDS, os_chown__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3747 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3748 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3749 | os_chown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid, int dir_fd, int follow_symlinks); |
| 3750 | |
| 3751 | static PyObject * |
| 3752 | os_chown(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3753 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3754 | PyObject *return_value = NULL; |
| 3755 | static char *_keywords[] = {"path", "uid", "gid", "dir_fd", "follow_symlinks", NULL}; |
| 3756 | path_t path = PATH_T_INITIALIZE("chown", "path", 0, PATH_HAVE_FCHOWN); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3757 | uid_t uid; |
| 3758 | gid_t gid; |
| 3759 | int dir_fd = DEFAULT_DIR_FD; |
| 3760 | int follow_symlinks = 1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3761 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3762 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3763 | "O&O&O&|$O&p:chown", _keywords, |
| 3764 | path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid, FCHOWNAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 3765 | goto exit; |
| 3766 | return_value = os_chown_impl(module, &path, uid, gid, dir_fd, follow_symlinks); |
| 3767 | |
| 3768 | exit: |
| 3769 | /* Cleanup for path */ |
| 3770 | path_cleanup(&path); |
| 3771 | |
| 3772 | return return_value; |
| 3773 | } |
| 3774 | |
| 3775 | static PyObject * |
| 3776 | os_chown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid, int dir_fd, int follow_symlinks) |
| 3777 | /*[clinic end generated code: output=22f011e3b4f9ff49 input=a61cc35574814d5d]*/ |
| 3778 | { |
| 3779 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3780 | |
| 3781 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) |
| 3782 | if (follow_symlinks_specified("chown", follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3783 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3784 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3785 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
| 3786 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) |
| 3787 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3788 | |
| 3789 | #ifdef __APPLE__ |
| 3790 | /* |
| 3791 | * This is for Mac OS X 10.3, which doesn't have lchown. |
| 3792 | * (But we still have an lchown symbol because of weak-linking.) |
| 3793 | * It doesn't have fchownat either. So there's no possibility |
| 3794 | * of a graceful failover. |
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3795 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3796 | if ((!follow_symlinks) && (lchown == NULL)) { |
| 3797 | follow_symlinks_specified("chown", follow_symlinks); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3798 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3799 | } |
| 3800 | #endif |
| 3801 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3802 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3803 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3804 | if (path->fd != -1) |
| 3805 | result = fchown(path->fd, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3806 | else |
| 3807 | #endif |
| 3808 | #ifdef HAVE_LCHOWN |
| 3809 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3810 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3811 | else |
| 3812 | #endif |
| 3813 | #ifdef HAVE_FCHOWNAT |
| 3814 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3815 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3816 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
| 3817 | else |
| 3818 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3819 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3820 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3821 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3822 | if (result) |
| 3823 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3824 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3825 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3826 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3827 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3828 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3829 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3830 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3831 | /*[clinic input] |
| 3832 | os.fchown |
| 3833 | |
| 3834 | fd: int |
| 3835 | uid: uid_t |
| 3836 | gid: gid_t |
| 3837 | |
| 3838 | Change the owner and group id of the file specified by file descriptor. |
| 3839 | |
| 3840 | Equivalent to os.chown(fd, uid, gid). |
| 3841 | |
| 3842 | [clinic start generated code]*/ |
| 3843 | |
| 3844 | PyDoc_STRVAR(os_fchown__doc__, |
| 3845 | "fchown($module, /, fd, uid, gid)\n" |
| 3846 | "--\n" |
| 3847 | "\n" |
| 3848 | "Change the owner and group id of the file specified by file descriptor.\n" |
| 3849 | "\n" |
| 3850 | "Equivalent to os.chown(fd, uid, gid)."); |
| 3851 | |
| 3852 | #define OS_FCHOWN_METHODDEF \ |
| 3853 | {"fchown", (PyCFunction)os_fchown, METH_VARARGS|METH_KEYWORDS, os_fchown__doc__}, |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3854 | |
| 3855 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3856 | os_fchown_impl(PyModuleDef *module, int fd, uid_t uid, gid_t gid); |
| 3857 | |
| 3858 | static PyObject * |
| 3859 | os_fchown(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3860 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3861 | PyObject *return_value = NULL; |
| 3862 | static char *_keywords[] = {"fd", "uid", "gid", NULL}; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3863 | int fd; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3864 | uid_t uid; |
| 3865 | gid_t gid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3866 | |
| 3867 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3868 | "iO&O&:fchown", _keywords, |
| 3869 | &fd, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) |
| 3870 | goto exit; |
| 3871 | return_value = os_fchown_impl(module, fd, uid, gid); |
| 3872 | |
| 3873 | exit: |
| 3874 | return return_value; |
| 3875 | } |
| 3876 | |
| 3877 | static PyObject * |
| 3878 | os_fchown_impl(PyModuleDef *module, int fd, uid_t uid, gid_t gid) |
| 3879 | /*[clinic end generated code: output=687781cb7d8974d6 input=3af544ba1b13a0d7]*/ |
| 3880 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3881 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3882 | int async_err = 0; |
| 3883 | |
| 3884 | do { |
| 3885 | Py_BEGIN_ALLOW_THREADS |
| 3886 | res = fchown(fd, uid, gid); |
| 3887 | Py_END_ALLOW_THREADS |
| 3888 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 3889 | if (res != 0) |
| 3890 | return (!async_err) ? posix_error() : NULL; |
| 3891 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3892 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3893 | } |
| 3894 | #endif /* HAVE_FCHOWN */ |
| 3895 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3896 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3897 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3898 | /*[clinic input] |
| 3899 | os.lchown |
| 3900 | |
| 3901 | path : path_t |
| 3902 | uid: uid_t |
| 3903 | gid: gid_t |
| 3904 | |
| 3905 | Change the owner and group id of path to the numeric uid and gid. |
| 3906 | |
| 3907 | This function will not follow symbolic links. |
| 3908 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). |
| 3909 | [clinic start generated code]*/ |
| 3910 | |
| 3911 | PyDoc_STRVAR(os_lchown__doc__, |
| 3912 | "lchown($module, /, path, uid, gid)\n" |
| 3913 | "--\n" |
| 3914 | "\n" |
| 3915 | "Change the owner and group id of path to the numeric uid and gid.\n" |
| 3916 | "\n" |
| 3917 | "This function will not follow symbolic links.\n" |
| 3918 | "Equivalent to os.chown(path, uid, gid, follow_symlinks=False)."); |
| 3919 | |
| 3920 | #define OS_LCHOWN_METHODDEF \ |
| 3921 | {"lchown", (PyCFunction)os_lchown, METH_VARARGS|METH_KEYWORDS, os_lchown__doc__}, |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3922 | |
| 3923 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3924 | os_lchown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid); |
| 3925 | |
| 3926 | static PyObject * |
| 3927 | os_lchown(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3928 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3929 | PyObject *return_value = NULL; |
| 3930 | static char *_keywords[] = {"path", "uid", "gid", NULL}; |
| 3931 | path_t path = PATH_T_INITIALIZE("lchown", "path", 0, 0); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 3932 | uid_t uid; |
| 3933 | gid_t gid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3934 | |
| 3935 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 3936 | "O&O&O&:lchown", _keywords, |
| 3937 | path_converter, &path, _Py_Uid_Converter, &uid, _Py_Gid_Converter, &gid)) |
| 3938 | goto exit; |
| 3939 | return_value = os_lchown_impl(module, &path, uid, gid); |
| 3940 | |
| 3941 | exit: |
| 3942 | /* Cleanup for path */ |
| 3943 | path_cleanup(&path); |
| 3944 | |
| 3945 | return return_value; |
| 3946 | } |
| 3947 | |
| 3948 | static PyObject * |
| 3949 | os_lchown_impl(PyModuleDef *module, path_t *path, uid_t uid, gid_t gid) |
| 3950 | /*[clinic end generated code: output=bf25fdb0d25130e2 input=b1c6014d563a7161]*/ |
| 3951 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3952 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3953 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3954 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3955 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3956 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3957 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3958 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3959 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3960 | } |
| 3961 | #endif /* HAVE_LCHOWN */ |
| 3962 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3963 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3964 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3965 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3966 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3967 | char buf[1026]; |
| 3968 | char *res; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3969 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3970 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3971 | if (!use_bytes) { |
| 3972 | wchar_t wbuf[1026]; |
| 3973 | wchar_t *wbuf2 = wbuf; |
| 3974 | PyObject *resobj; |
| 3975 | DWORD len; |
| 3976 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3977 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3978 | /* If the buffer is large enough, len does not include the |
| 3979 | terminating \0. If the buffer is too small, len includes |
| 3980 | the space needed for the terminator. */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3981 | if (len >= Py_ARRAY_LENGTH(wbuf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3982 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3983 | if (wbuf2) |
| 3984 | len = GetCurrentDirectoryW(len, wbuf2); |
| 3985 | } |
| 3986 | Py_END_ALLOW_THREADS |
| 3987 | if (!wbuf2) { |
| 3988 | PyErr_NoMemory(); |
| 3989 | return NULL; |
| 3990 | } |
| 3991 | if (!len) { |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3992 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3993 | PyMem_RawFree(wbuf2); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3994 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3995 | } |
| 3996 | resobj = PyUnicode_FromWideChar(wbuf2, len); |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3997 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3998 | PyMem_RawFree(wbuf2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3999 | return resobj; |
| 4000 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 4001 | |
| 4002 | if (win32_warn_bytes_api()) |
| 4003 | return NULL; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4004 | #endif |
| 4005 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4006 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4007 | res = getcwd(buf, sizeof buf); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4008 | Py_END_ALLOW_THREADS |
| 4009 | if (res == NULL) |
| 4010 | return posix_error(); |
| 4011 | if (use_bytes) |
| 4012 | return PyBytes_FromStringAndSize(buf, strlen(buf)); |
Victor Stinner | 97c18ab | 2010-05-07 16:34:53 +0000 | [diff] [blame] | 4013 | return PyUnicode_DecodeFSDefault(buf); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4014 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4015 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4016 | |
| 4017 | /*[clinic input] |
| 4018 | os.getcwd |
| 4019 | |
| 4020 | Return a unicode string representing the current working directory. |
| 4021 | [clinic start generated code]*/ |
| 4022 | |
| 4023 | PyDoc_STRVAR(os_getcwd__doc__, |
| 4024 | "getcwd($module, /)\n" |
| 4025 | "--\n" |
| 4026 | "\n" |
| 4027 | "Return a unicode string representing the current working directory."); |
| 4028 | |
| 4029 | #define OS_GETCWD_METHODDEF \ |
| 4030 | {"getcwd", (PyCFunction)os_getcwd, METH_NOARGS, os_getcwd__doc__}, |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4031 | |
| 4032 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4033 | os_getcwd_impl(PyModuleDef *module); |
| 4034 | |
| 4035 | static PyObject * |
| 4036 | os_getcwd(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 4037 | { |
| 4038 | return os_getcwd_impl(module); |
| 4039 | } |
| 4040 | |
| 4041 | static PyObject * |
| 4042 | os_getcwd_impl(PyModuleDef *module) |
| 4043 | /*[clinic end generated code: output=d70b281db5c78ff7 input=f069211bb70e3d39]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4044 | { |
| 4045 | return posix_getcwd(0); |
| 4046 | } |
| 4047 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4048 | |
| 4049 | /*[clinic input] |
| 4050 | os.getcwdb |
| 4051 | |
| 4052 | Return a bytes string representing the current working directory. |
| 4053 | [clinic start generated code]*/ |
| 4054 | |
| 4055 | PyDoc_STRVAR(os_getcwdb__doc__, |
| 4056 | "getcwdb($module, /)\n" |
| 4057 | "--\n" |
| 4058 | "\n" |
| 4059 | "Return a bytes string representing the current working directory."); |
| 4060 | |
| 4061 | #define OS_GETCWDB_METHODDEF \ |
| 4062 | {"getcwdb", (PyCFunction)os_getcwdb, METH_NOARGS, os_getcwdb__doc__}, |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4063 | |
| 4064 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4065 | os_getcwdb_impl(PyModuleDef *module); |
| 4066 | |
| 4067 | static PyObject * |
| 4068 | os_getcwdb(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 4069 | { |
| 4070 | return os_getcwdb_impl(module); |
| 4071 | } |
| 4072 | |
| 4073 | static PyObject * |
| 4074 | os_getcwdb_impl(PyModuleDef *module) |
| 4075 | /*[clinic end generated code: output=75da47f2d75f9166 input=f6f6a378dad3d9cb]*/ |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 4076 | { |
| 4077 | return posix_getcwd(1); |
| 4078 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4079 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4080 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4081 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
| 4082 | #define HAVE_LINK 1 |
| 4083 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4084 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4085 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4086 | /*[clinic input] |
| 4087 | |
| 4088 | os.link |
| 4089 | |
| 4090 | src : path_t |
| 4091 | dst : path_t |
| 4092 | * |
| 4093 | src_dir_fd : dir_fd = None |
| 4094 | dst_dir_fd : dir_fd = None |
| 4095 | follow_symlinks: bool = True |
| 4096 | |
| 4097 | Create a hard link to a file. |
| 4098 | |
| 4099 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 4100 | descriptor open to a directory, and the respective path string (src or dst) |
| 4101 | should be relative; the path will then be relative to that directory. |
| 4102 | If follow_symlinks is False, and the last element of src is a symbolic |
| 4103 | link, link will create a link to the symbolic link itself instead of the |
| 4104 | file the link points to. |
| 4105 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 4106 | platform. If they are unavailable, using them will raise a |
| 4107 | NotImplementedError. |
| 4108 | [clinic start generated code]*/ |
| 4109 | |
| 4110 | PyDoc_STRVAR(os_link__doc__, |
| 4111 | "link($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None,\n" |
| 4112 | " follow_symlinks=True)\n" |
| 4113 | "--\n" |
| 4114 | "\n" |
| 4115 | "Create a hard link to a file.\n" |
| 4116 | "\n" |
| 4117 | "If either src_dir_fd or dst_dir_fd is not None, it should be a file\n" |
| 4118 | " descriptor open to a directory, and the respective path string (src or dst)\n" |
| 4119 | " should be relative; the path will then be relative to that directory.\n" |
| 4120 | "If follow_symlinks is False, and the last element of src is a symbolic\n" |
| 4121 | " link, link will create a link to the symbolic link itself instead of the\n" |
| 4122 | " file the link points to.\n" |
| 4123 | "src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n" |
| 4124 | " platform. If they are unavailable, using them will raise a\n" |
| 4125 | " NotImplementedError."); |
| 4126 | |
| 4127 | #define OS_LINK_METHODDEF \ |
| 4128 | {"link", (PyCFunction)os_link, METH_VARARGS|METH_KEYWORDS, os_link__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4129 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4130 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4131 | os_link_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int follow_symlinks); |
| 4132 | |
| 4133 | static PyObject * |
| 4134 | os_link(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4135 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4136 | PyObject *return_value = NULL; |
| 4137 | static char *_keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", "follow_symlinks", NULL}; |
| 4138 | path_t src = PATH_T_INITIALIZE("link", "src", 0, 0); |
| 4139 | path_t dst = PATH_T_INITIALIZE("link", "dst", 0, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4140 | int src_dir_fd = DEFAULT_DIR_FD; |
| 4141 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 4142 | int follow_symlinks = 1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4143 | |
| 4144 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4145 | "O&O&|$O&O&p:link", _keywords, |
| 4146 | path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd, &follow_symlinks)) |
| 4147 | goto exit; |
| 4148 | return_value = os_link_impl(module, &src, &dst, src_dir_fd, dst_dir_fd, follow_symlinks); |
| 4149 | |
| 4150 | exit: |
| 4151 | /* Cleanup for src */ |
| 4152 | path_cleanup(&src); |
| 4153 | /* Cleanup for dst */ |
| 4154 | path_cleanup(&dst); |
| 4155 | |
| 4156 | return return_value; |
| 4157 | } |
| 4158 | |
| 4159 | static PyObject * |
| 4160 | os_link_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int follow_symlinks) |
| 4161 | /*[clinic end generated code: output=53477662fe02e183 input=b0095ebbcbaa7e04]*/ |
| 4162 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4163 | #ifdef MS_WINDOWS |
| 4164 | BOOL result; |
| 4165 | #else |
| 4166 | int result; |
| 4167 | #endif |
| 4168 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4169 | #ifndef HAVE_LINKAT |
| 4170 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { |
| 4171 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4172 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4173 | } |
| 4174 | #endif |
| 4175 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4176 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4177 | PyErr_SetString(PyExc_NotImplementedError, |
| 4178 | "link: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4179 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4180 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4181 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 4182 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4183 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4184 | if (src->wide) |
| 4185 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4186 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4187 | result = CreateHardLinkA(dst->narrow, src->narrow, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4188 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 4189 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4190 | if (!result) |
| 4191 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4192 | #else |
| 4193 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 4194 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4195 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
| 4196 | (dst_dir_fd != DEFAULT_DIR_FD) || |
| 4197 | (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4198 | result = linkat(src_dir_fd, src->narrow, |
| 4199 | dst_dir_fd, dst->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4200 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
| 4201 | else |
| 4202 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4203 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4204 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 4205 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4206 | if (result) |
| 4207 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4208 | #endif |
| 4209 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4210 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 4211 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4212 | #endif |
| 4213 | |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 4214 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4215 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4216 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4217 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4218 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4219 | PyObject *v; |
| 4220 | HANDLE hFindFile = INVALID_HANDLE_VALUE; |
| 4221 | BOOL result; |
| 4222 | WIN32_FIND_DATA FileData; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 4223 | char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4224 | char *bufptr = namebuf; |
| 4225 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 4226 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4227 | PyObject *po = NULL; |
| 4228 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4229 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4230 | if (!path->narrow) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4231 | WIN32_FIND_DATAW wFileData; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4232 | wchar_t *po_wchars; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4233 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4234 | if (!path->wide) { /* Default arg: "." */ |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 4235 | po_wchars = L"."; |
| 4236 | len = 1; |
| 4237 | } else { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4238 | po_wchars = path->wide; |
| 4239 | len = wcslen(path->wide); |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 4240 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4241 | /* The +5 is so we can append "\\*.*\0" */ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4242 | wnamebuf = PyMem_New(wchar_t, len + 5); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4243 | if (!wnamebuf) { |
| 4244 | PyErr_NoMemory(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4245 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4246 | } |
Martin v. Löwis | c9e1c7d | 2010-07-23 12:16:41 +0000 | [diff] [blame] | 4247 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4248 | if (len > 0) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4249 | wchar_t wch = wnamebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 4250 | if (wch != SEP && wch != ALTSEP && wch != L':') |
| 4251 | wnamebuf[len++] = SEP; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4252 | wcscpy(wnamebuf + len, L"*.*"); |
| 4253 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4254 | if ((list = PyList_New(0)) == NULL) { |
| 4255 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4256 | } |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 4257 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4258 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 4259 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4260 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 4261 | int error = GetLastError(); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4262 | if (error == ERROR_FILE_NOT_FOUND) |
| 4263 | goto exit; |
| 4264 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4265 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4266 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4267 | } |
| 4268 | do { |
| 4269 | /* Skip over . and .. */ |
| 4270 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
| 4271 | wcscmp(wFileData.cFileName, L"..") != 0) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4272 | v = PyUnicode_FromWideChar(wFileData.cFileName, |
| 4273 | wcslen(wFileData.cFileName)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4274 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4275 | Py_DECREF(list); |
| 4276 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4277 | break; |
| 4278 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4279 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4280 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4281 | Py_DECREF(list); |
| 4282 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4283 | break; |
| 4284 | } |
| 4285 | Py_DECREF(v); |
| 4286 | } |
| 4287 | Py_BEGIN_ALLOW_THREADS |
| 4288 | result = FindNextFileW(hFindFile, &wFileData); |
| 4289 | Py_END_ALLOW_THREADS |
| 4290 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 4291 | it got to the end of the directory. */ |
| 4292 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4293 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4294 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4295 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4296 | } |
| 4297 | } while (result == TRUE); |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 4298 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4299 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4300 | } |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4301 | strcpy(namebuf, path->narrow); |
| 4302 | len = path->length; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4303 | if (len > 0) { |
| 4304 | char ch = namebuf[len-1]; |
Tim Golden | 781bbeb | 2013-10-25 20:24:06 +0100 | [diff] [blame] | 4305 | if (ch != '\\' && ch != '/' && ch != ':') |
| 4306 | namebuf[len++] = '\\'; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4307 | strcpy(namebuf + len, "*.*"); |
| 4308 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4309 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4310 | if ((list = PyList_New(0)) == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4311 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4312 | |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 4313 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4314 | hFindFile = FindFirstFile(namebuf, &FileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 4315 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4316 | if (hFindFile == INVALID_HANDLE_VALUE) { |
| 4317 | int error = GetLastError(); |
| 4318 | if (error == ERROR_FILE_NOT_FOUND) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4319 | goto exit; |
| 4320 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4321 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4322 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4323 | } |
| 4324 | do { |
| 4325 | /* Skip over . and .. */ |
| 4326 | if (strcmp(FileData.cFileName, ".") != 0 && |
| 4327 | strcmp(FileData.cFileName, "..") != 0) { |
| 4328 | v = PyBytes_FromString(FileData.cFileName); |
| 4329 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4330 | Py_DECREF(list); |
| 4331 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4332 | break; |
| 4333 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4334 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4335 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4336 | Py_DECREF(list); |
| 4337 | list = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4338 | break; |
| 4339 | } |
| 4340 | Py_DECREF(v); |
| 4341 | } |
| 4342 | Py_BEGIN_ALLOW_THREADS |
| 4343 | result = FindNextFile(hFindFile, &FileData); |
| 4344 | Py_END_ALLOW_THREADS |
| 4345 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if |
| 4346 | it got to the end of the directory. */ |
| 4347 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4348 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4349 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4350 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4351 | } |
| 4352 | } while (result == TRUE); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4353 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4354 | exit: |
| 4355 | if (hFindFile != INVALID_HANDLE_VALUE) { |
| 4356 | if (FindClose(hFindFile) == FALSE) { |
| 4357 | if (list != NULL) { |
| 4358 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4359 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4360 | } |
| 4361 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4362 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 4363 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4364 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4365 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4366 | } /* end of _listdir_windows_no_opendir */ |
| 4367 | |
| 4368 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ |
| 4369 | |
| 4370 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4371 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4372 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4373 | PyObject *v; |
| 4374 | DIR *dirp = NULL; |
| 4375 | struct dirent *ep; |
| 4376 | int return_str; /* if false, return bytes */ |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4377 | #ifdef HAVE_FDOPENDIR |
| 4378 | int fd = -1; |
| 4379 | #endif |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4380 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4381 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4382 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4383 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4384 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 4385 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 4386 | if (fd == -1) |
| 4387 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4388 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4389 | return_str = 1; |
| 4390 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4391 | Py_BEGIN_ALLOW_THREADS |
| 4392 | dirp = fdopendir(fd); |
| 4393 | Py_END_ALLOW_THREADS |
| 4394 | } |
| 4395 | else |
| 4396 | #endif |
| 4397 | { |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4398 | char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4399 | if (path->narrow) { |
| 4400 | name = path->narrow; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4401 | /* only return bytes if they specified a bytes object */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4402 | return_str = !(PyBytes_Check(path->object)); |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4403 | } |
| 4404 | else { |
| 4405 | name = "."; |
| 4406 | return_str = 1; |
| 4407 | } |
| 4408 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4409 | Py_BEGIN_ALLOW_THREADS |
| 4410 | dirp = opendir(name); |
| 4411 | Py_END_ALLOW_THREADS |
| 4412 | } |
| 4413 | |
| 4414 | if (dirp == NULL) { |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4415 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4416 | #ifdef HAVE_FDOPENDIR |
| 4417 | if (fd != -1) { |
| 4418 | Py_BEGIN_ALLOW_THREADS |
| 4419 | close(fd); |
| 4420 | Py_END_ALLOW_THREADS |
| 4421 | } |
| 4422 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4423 | goto exit; |
| 4424 | } |
| 4425 | if ((list = PyList_New(0)) == NULL) { |
| 4426 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4427 | } |
| 4428 | for (;;) { |
| 4429 | errno = 0; |
| 4430 | Py_BEGIN_ALLOW_THREADS |
| 4431 | ep = readdir(dirp); |
| 4432 | Py_END_ALLOW_THREADS |
| 4433 | if (ep == NULL) { |
| 4434 | if (errno == 0) { |
| 4435 | break; |
| 4436 | } else { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4437 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4438 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4439 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | if (ep->d_name[0] == '.' && |
| 4443 | (NAMLEN(ep) == 1 || |
| 4444 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) |
| 4445 | continue; |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 4446 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 4447 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
| 4448 | else |
| 4449 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4450 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4451 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4452 | break; |
| 4453 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4454 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4455 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4456 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4457 | break; |
| 4458 | } |
| 4459 | Py_DECREF(v); |
| 4460 | } |
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 4461 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4462 | exit: |
| 4463 | if (dirp != NULL) { |
| 4464 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4465 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4466 | if (fd > -1) |
| 4467 | rewinddir(dirp); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 4468 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4469 | closedir(dirp); |
| 4470 | Py_END_ALLOW_THREADS |
| 4471 | } |
| 4472 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4473 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4474 | } /* end of _posix_listdir */ |
| 4475 | #endif /* which OS */ |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 4476 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4477 | |
| 4478 | /*[clinic input] |
| 4479 | os.listdir |
| 4480 | |
| 4481 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
| 4482 | |
| 4483 | Return a list containing the names of the files in the directory. |
| 4484 | |
| 4485 | path can be specified as either str or bytes. If path is bytes, |
| 4486 | the filenames returned will also be bytes; in all other circumstances |
| 4487 | the filenames returned will be str. |
| 4488 | If path is None, uses the path='.'. |
| 4489 | On some platforms, path may also be specified as an open file descriptor;\ |
| 4490 | the file descriptor must refer to a directory. |
| 4491 | If this functionality is unavailable, using it raises NotImplementedError. |
| 4492 | |
| 4493 | The list is in arbitrary order. It does not include the special |
| 4494 | entries '.' and '..' even if they are present in the directory. |
| 4495 | |
| 4496 | |
| 4497 | [clinic start generated code]*/ |
| 4498 | |
| 4499 | PyDoc_STRVAR(os_listdir__doc__, |
| 4500 | "listdir($module, /, path=None)\n" |
| 4501 | "--\n" |
| 4502 | "\n" |
| 4503 | "Return a list containing the names of the files in the directory.\n" |
| 4504 | "\n" |
| 4505 | "path can be specified as either str or bytes. If path is bytes,\n" |
| 4506 | " the filenames returned will also be bytes; in all other circumstances\n" |
| 4507 | " the filenames returned will be str.\n" |
| 4508 | "If path is None, uses the path=\'.\'.\n" |
| 4509 | "On some platforms, path may also be specified as an open file descriptor;\\\n" |
| 4510 | " the file descriptor must refer to a directory.\n" |
| 4511 | " If this functionality is unavailable, using it raises NotImplementedError.\n" |
| 4512 | "\n" |
| 4513 | "The list is in arbitrary order. It does not include the special\n" |
| 4514 | "entries \'.\' and \'..\' even if they are present in the directory."); |
| 4515 | |
| 4516 | #define OS_LISTDIR_METHODDEF \ |
| 4517 | {"listdir", (PyCFunction)os_listdir, METH_VARARGS|METH_KEYWORDS, os_listdir__doc__}, |
| 4518 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4519 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4520 | os_listdir_impl(PyModuleDef *module, path_t *path); |
| 4521 | |
| 4522 | static PyObject * |
| 4523 | os_listdir(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4524 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4525 | PyObject *return_value = NULL; |
| 4526 | static char *_keywords[] = {"path", NULL}; |
| 4527 | path_t path = PATH_T_INITIALIZE("listdir", "path", 1, PATH_HAVE_FDOPENDIR); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4528 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4529 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4530 | "|O&:listdir", _keywords, |
| 4531 | path_converter, &path)) |
| 4532 | goto exit; |
| 4533 | return_value = os_listdir_impl(module, &path); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4535 | exit: |
| 4536 | /* Cleanup for path */ |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4537 | path_cleanup(&path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4538 | |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 4539 | return return_value; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 4540 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4541 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4542 | static PyObject * |
| 4543 | os_listdir_impl(PyModuleDef *module, path_t *path) |
| 4544 | /*[clinic end generated code: output=e159bd9be6909018 input=09e300416e3cd729]*/ |
| 4545 | { |
| 4546 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
| 4547 | return _listdir_windows_no_opendir(path, NULL); |
| 4548 | #else |
| 4549 | return _posix_listdir(path, NULL); |
| 4550 | #endif |
| 4551 | } |
| 4552 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4553 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4554 | /* A helper function for abspath on win32 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4555 | /* AC 3.5: probably just convert to using path converter */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4556 | static PyObject * |
| 4557 | posix__getfullpathname(PyObject *self, PyObject *args) |
| 4558 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4559 | const char *path; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 4560 | char outbuf[MAX_PATH]; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4561 | char *temp; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4562 | PyObject *po; |
| 4563 | |
| 4564 | if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po)) |
| 4565 | { |
| 4566 | wchar_t *wpath; |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 4567 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4568 | wchar_t *wtemp; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4569 | DWORD result; |
| 4570 | PyObject *v; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4571 | |
| 4572 | wpath = PyUnicode_AsUnicode(po); |
| 4573 | if (wpath == NULL) |
| 4574 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4575 | result = GetFullPathNameW(wpath, |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4576 | Py_ARRAY_LENGTH(woutbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4577 | woutbuf, &wtemp); |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4578 | if (result > Py_ARRAY_LENGTH(woutbuf)) { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4579 | woutbufp = PyMem_New(wchar_t, result); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4580 | if (!woutbufp) |
| 4581 | return PyErr_NoMemory(); |
| 4582 | result = GetFullPathNameW(wpath, result, woutbufp, &wtemp); |
| 4583 | } |
| 4584 | if (result) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4585 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4586 | else |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4587 | v = win32_error_object("GetFullPathNameW", po); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4588 | if (woutbufp != woutbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 4589 | PyMem_Free(woutbufp); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4590 | return v; |
| 4591 | } |
| 4592 | /* Drop the argument parsing error as narrow strings |
| 4593 | are also valid. */ |
| 4594 | PyErr_Clear(); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4595 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4596 | if (!PyArg_ParseTuple (args, "y:_getfullpathname", |
| 4597 | &path)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4598 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4599 | if (win32_warn_bytes_api()) |
| 4600 | return NULL; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 4601 | if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf), |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4602 | outbuf, &temp)) { |
| 4603 | win32_error("GetFullPathName", path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4604 | return NULL; |
| 4605 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4606 | if (PyUnicode_Check(PyTuple_GetItem(args, 0))) { |
| 4607 | return PyUnicode_Decode(outbuf, strlen(outbuf), |
| 4608 | Py_FileSystemDefaultEncoding, NULL); |
| 4609 | } |
| 4610 | return PyBytes_FromString(outbuf); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4611 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4612 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 4613 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4614 | /*[clinic input] |
| 4615 | os._getfinalpathname |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 4616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4617 | path: unicode |
| 4618 | / |
| 4619 | |
| 4620 | A helper function for samepath on windows. |
| 4621 | [clinic start generated code]*/ |
| 4622 | |
| 4623 | PyDoc_STRVAR(os__getfinalpathname__doc__, |
| 4624 | "_getfinalpathname($module, path, /)\n" |
| 4625 | "--\n" |
| 4626 | "\n" |
| 4627 | "A helper function for samepath on windows."); |
| 4628 | |
| 4629 | #define OS__GETFINALPATHNAME_METHODDEF \ |
| 4630 | {"_getfinalpathname", (PyCFunction)os__getfinalpathname, METH_VARARGS, os__getfinalpathname__doc__}, |
| 4631 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4632 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4633 | os__getfinalpathname_impl(PyModuleDef *module, PyObject *path); |
| 4634 | |
| 4635 | static PyObject * |
| 4636 | os__getfinalpathname(PyModuleDef *module, PyObject *args) |
| 4637 | { |
| 4638 | PyObject *return_value = NULL; |
| 4639 | PyObject *path; |
| 4640 | |
| 4641 | if (!PyArg_ParseTuple(args, |
| 4642 | "U:_getfinalpathname", |
| 4643 | &path)) |
| 4644 | goto exit; |
| 4645 | return_value = os__getfinalpathname_impl(module, path); |
| 4646 | |
| 4647 | exit: |
| 4648 | return return_value; |
| 4649 | } |
| 4650 | |
| 4651 | static PyObject * |
| 4652 | os__getfinalpathname_impl(PyModuleDef *module, PyObject *path) |
| 4653 | /*[clinic end generated code: output=4563c6eacf1b0881 input=71d5e89334891bf4]*/ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4654 | { |
| 4655 | HANDLE hFile; |
| 4656 | int buf_size; |
| 4657 | wchar_t *target_path; |
| 4658 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4659 | PyObject *result; |
| 4660 | wchar_t *path_wchar; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4661 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4662 | path_wchar = PyUnicode_AsUnicode(path); |
| 4663 | if (path_wchar == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4664 | return NULL; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4665 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4666 | hFile = CreateFileW( |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4667 | path_wchar, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4668 | 0, /* desired access */ |
| 4669 | 0, /* share mode */ |
| 4670 | NULL, /* security attributes */ |
| 4671 | OPEN_EXISTING, |
| 4672 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ |
| 4673 | FILE_FLAG_BACKUP_SEMANTICS, |
| 4674 | NULL); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4675 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4676 | if(hFile == INVALID_HANDLE_VALUE) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4677 | return win32_error_object("CreateFileW", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4678 | |
| 4679 | /* We have a good handle to the target, use it to determine the |
| 4680 | target path name. */ |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 4681 | buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4682 | |
| 4683 | if(!buf_size) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4684 | return win32_error_object("GetFinalPathNameByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4685 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4686 | target_path = PyMem_New(wchar_t, buf_size+1); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4687 | if(!target_path) |
| 4688 | return PyErr_NoMemory(); |
| 4689 | |
Steve Dower | 2ea51c9 | 2015-03-20 21:49:12 -0700 | [diff] [blame] | 4690 | result_length = GetFinalPathNameByHandleW(hFile, target_path, |
| 4691 | buf_size, VOLUME_NAME_DOS); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4692 | if(!result_length) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4693 | return win32_error_object("GetFinalPathNamyByHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4694 | |
| 4695 | if(!CloseHandle(hFile)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4696 | return win32_error_object("CloseHandle", path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4697 | |
| 4698 | target_path[result_length] = 0; |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4699 | result = PyUnicode_FromWideChar(target_path, result_length); |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 4700 | PyMem_Free(target_path); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4701 | return result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4702 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4703 | |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 4704 | PyDoc_STRVAR(posix__isdir__doc__, |
| 4705 | "Return true if the pathname refers to an existing directory."); |
| 4706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4707 | /* AC 3.5: convert using path converter */ |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4708 | static PyObject * |
| 4709 | posix__isdir(PyObject *self, PyObject *args) |
| 4710 | { |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4711 | const char *path; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4712 | PyObject *po; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4713 | DWORD attributes; |
| 4714 | |
| 4715 | if (PyArg_ParseTuple(args, "U|:_isdir", &po)) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 4716 | wchar_t *wpath = PyUnicode_AsUnicode(po); |
| 4717 | if (wpath == NULL) |
| 4718 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4719 | |
| 4720 | attributes = GetFileAttributesW(wpath); |
| 4721 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4722 | Py_RETURN_FALSE; |
| 4723 | goto check; |
| 4724 | } |
| 4725 | /* Drop the argument parsing error as narrow strings |
| 4726 | are also valid. */ |
| 4727 | PyErr_Clear(); |
| 4728 | |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4729 | if (!PyArg_ParseTuple(args, "y:_isdir", &path)) |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4730 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 4731 | if (win32_warn_bytes_api()) |
| 4732 | return NULL; |
Brian Curtin | 9c669cc | 2011-06-08 18:17:18 -0500 | [diff] [blame] | 4733 | attributes = GetFileAttributesA(path); |
| 4734 | if (attributes == INVALID_FILE_ATTRIBUTES) |
| 4735 | Py_RETURN_FALSE; |
| 4736 | |
| 4737 | check: |
| 4738 | if (attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 4739 | Py_RETURN_TRUE; |
| 4740 | else |
| 4741 | Py_RETURN_FALSE; |
| 4742 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4743 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4744 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4745 | /*[clinic input] |
| 4746 | os._getvolumepathname |
| 4747 | |
| 4748 | path: unicode |
| 4749 | |
| 4750 | A helper function for ismount on Win32. |
| 4751 | [clinic start generated code]*/ |
| 4752 | |
| 4753 | PyDoc_STRVAR(os__getvolumepathname__doc__, |
| 4754 | "_getvolumepathname($module, /, path)\n" |
| 4755 | "--\n" |
| 4756 | "\n" |
| 4757 | "A helper function for ismount on Win32."); |
| 4758 | |
| 4759 | #define OS__GETVOLUMEPATHNAME_METHODDEF \ |
| 4760 | {"_getvolumepathname", (PyCFunction)os__getvolumepathname, METH_VARARGS|METH_KEYWORDS, os__getvolumepathname__doc__}, |
| 4761 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4762 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4763 | os__getvolumepathname_impl(PyModuleDef *module, PyObject *path); |
| 4764 | |
| 4765 | static PyObject * |
| 4766 | os__getvolumepathname(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4767 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4768 | PyObject *return_value = NULL; |
| 4769 | static char *_keywords[] = {"path", NULL}; |
| 4770 | PyObject *path; |
| 4771 | |
| 4772 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4773 | "U:_getvolumepathname", _keywords, |
| 4774 | &path)) |
| 4775 | goto exit; |
| 4776 | return_value = os__getvolumepathname_impl(module, path); |
| 4777 | |
| 4778 | exit: |
| 4779 | return return_value; |
| 4780 | } |
| 4781 | |
| 4782 | static PyObject * |
| 4783 | os__getvolumepathname_impl(PyModuleDef *module, PyObject *path) |
| 4784 | /*[clinic end generated code: output=ac0833b6d6da7657 input=7eacadc40acbda6b]*/ |
| 4785 | { |
| 4786 | PyObject *result; |
| 4787 | wchar_t *path_wchar, *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4788 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4789 | BOOL ret; |
| 4790 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4791 | path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen); |
| 4792 | if (path_wchar == NULL) |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4793 | return NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4794 | buflen += 1; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4795 | |
| 4796 | /* Volume path should be shorter than entire path */ |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4797 | buflen = Py_MAX(buflen, MAX_PATH); |
| 4798 | |
| 4799 | if (buflen > DWORD_MAX) { |
| 4800 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
| 4801 | return NULL; |
| 4802 | } |
| 4803 | |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4804 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4805 | if (mountpath == NULL) |
| 4806 | return PyErr_NoMemory(); |
| 4807 | |
| 4808 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4809 | ret = GetVolumePathNameW(path_wchar, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4810 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4811 | Py_END_ALLOW_THREADS |
| 4812 | |
| 4813 | if (!ret) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4814 | result = win32_error_object("_getvolumepathname", path); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4815 | goto exit; |
| 4816 | } |
| 4817 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); |
| 4818 | |
| 4819 | exit: |
| 4820 | PyMem_Free(mountpath); |
| 4821 | return result; |
| 4822 | } |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4823 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4824 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4825 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4826 | |
| 4827 | /*[clinic input] |
| 4828 | os.mkdir |
| 4829 | |
| 4830 | path : path_t |
| 4831 | |
| 4832 | mode: int = 0o777 |
| 4833 | |
| 4834 | * |
| 4835 | |
| 4836 | dir_fd : dir_fd(requires='mkdirat') = None |
| 4837 | |
| 4838 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ |
| 4839 | |
| 4840 | Create a directory. |
| 4841 | |
| 4842 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 4843 | and path should be relative; path will then be relative to that directory. |
| 4844 | dir_fd may not be implemented on your platform. |
| 4845 | If it is unavailable, using it will raise a NotImplementedError. |
| 4846 | |
| 4847 | The mode argument is ignored on Windows. |
| 4848 | [clinic start generated code]*/ |
| 4849 | |
| 4850 | PyDoc_STRVAR(os_mkdir__doc__, |
| 4851 | "mkdir($module, /, path, mode=511, *, dir_fd=None)\n" |
| 4852 | "--\n" |
| 4853 | "\n" |
| 4854 | "Create a directory.\n" |
| 4855 | "\n" |
| 4856 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 4857 | " and path should be relative; path will then be relative to that directory.\n" |
| 4858 | "dir_fd may not be implemented on your platform.\n" |
| 4859 | " If it is unavailable, using it will raise a NotImplementedError.\n" |
| 4860 | "\n" |
| 4861 | "The mode argument is ignored on Windows."); |
| 4862 | |
| 4863 | #define OS_MKDIR_METHODDEF \ |
| 4864 | {"mkdir", (PyCFunction)os_mkdir, METH_VARARGS|METH_KEYWORDS, os_mkdir__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4865 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4866 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4867 | os_mkdir_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4868 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4869 | static PyObject * |
| 4870 | os_mkdir(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 4871 | { |
| 4872 | PyObject *return_value = NULL; |
| 4873 | static char *_keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 4874 | path_t path = PATH_T_INITIALIZE("mkdir", "path", 0, 0); |
| 4875 | int mode = 511; |
| 4876 | int dir_fd = DEFAULT_DIR_FD; |
| 4877 | |
| 4878 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 4879 | "O&|i$O&:mkdir", _keywords, |
| 4880 | path_converter, &path, &mode, MKDIRAT_DIR_FD_CONVERTER, &dir_fd)) |
| 4881 | goto exit; |
| 4882 | return_value = os_mkdir_impl(module, &path, mode, dir_fd); |
| 4883 | |
| 4884 | exit: |
| 4885 | /* Cleanup for path */ |
| 4886 | path_cleanup(&path); |
| 4887 | |
| 4888 | return return_value; |
| 4889 | } |
| 4890 | |
| 4891 | static PyObject * |
| 4892 | os_mkdir_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd) |
| 4893 | /*[clinic end generated code: output=55c6ef2bc1b207e6 input=e965f68377e9b1ce]*/ |
| 4894 | { |
| 4895 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4896 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4897 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4898 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4899 | if (path->wide) |
| 4900 | result = CreateDirectoryW(path->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4901 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4902 | result = CreateDirectoryA(path->narrow, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4903 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4904 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4905 | if (!result) |
| 4906 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4907 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4908 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4909 | #if HAVE_MKDIRAT |
| 4910 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4911 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4912 | else |
| 4913 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4914 | #if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4915 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4916 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4917 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4918 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4919 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4920 | if (result < 0) |
| 4921 | return path_error(path); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4922 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4923 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4924 | } |
| 4925 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4926 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4927 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
| 4928 | #if defined(HAVE_SYS_RESOURCE_H) |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4929 | #include <sys/resource.h> |
| 4930 | #endif |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4931 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4932 | |
| 4933 | #ifdef HAVE_NICE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4934 | /*[clinic input] |
| 4935 | os.nice |
| 4936 | |
| 4937 | increment: int |
| 4938 | / |
| 4939 | |
| 4940 | Add increment to the priority of process and return the new priority. |
| 4941 | [clinic start generated code]*/ |
| 4942 | |
| 4943 | PyDoc_STRVAR(os_nice__doc__, |
| 4944 | "nice($module, increment, /)\n" |
| 4945 | "--\n" |
| 4946 | "\n" |
| 4947 | "Add increment to the priority of process and return the new priority."); |
| 4948 | |
| 4949 | #define OS_NICE_METHODDEF \ |
| 4950 | {"nice", (PyCFunction)os_nice, METH_VARARGS, os_nice__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4951 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4952 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4953 | os_nice_impl(PyModuleDef *module, int increment); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4955 | static PyObject * |
| 4956 | os_nice(PyModuleDef *module, PyObject *args) |
| 4957 | { |
| 4958 | PyObject *return_value = NULL; |
| 4959 | int increment; |
| 4960 | |
| 4961 | if (!PyArg_ParseTuple(args, |
| 4962 | "i:nice", |
| 4963 | &increment)) |
| 4964 | goto exit; |
| 4965 | return_value = os_nice_impl(module, increment); |
| 4966 | |
| 4967 | exit: |
| 4968 | return return_value; |
| 4969 | } |
| 4970 | |
| 4971 | static PyObject * |
| 4972 | os_nice_impl(PyModuleDef *module, int increment) |
| 4973 | /*[clinic end generated code: output=c360dc2a3bd8e3d0 input=864be2d402a21da2]*/ |
| 4974 | { |
| 4975 | int value; |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4976 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4977 | /* There are two flavours of 'nice': one that returns the new |
| 4978 | priority (as required by almost all standards out there) and the |
| 4979 | Linux/FreeBSD/BSDI one, which returns '0' on success and advices |
| 4980 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4981 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4982 | If we are of the nice family that returns the new priority, we |
| 4983 | need to clear errno before the call, and check if errno is filled |
| 4984 | before calling posix_error() on a returnvalue of -1, because the |
| 4985 | -1 may be the actual new priority! */ |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4986 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4987 | errno = 0; |
| 4988 | value = nice(increment); |
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4989 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4990 | if (value == 0) |
| 4991 | value = getpriority(PRIO_PROCESS, 0); |
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4992 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4993 | if (value == -1 && errno != 0) |
| 4994 | /* either nice() or getpriority() returned an error */ |
| 4995 | return posix_error(); |
| 4996 | return PyLong_FromLong((long) value); |
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4997 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4998 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4999 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5000 | |
| 5001 | #ifdef HAVE_GETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5002 | /*[clinic input] |
| 5003 | os.getpriority |
| 5004 | |
| 5005 | which: int |
| 5006 | who: int |
| 5007 | |
| 5008 | Return program scheduling priority. |
| 5009 | [clinic start generated code]*/ |
| 5010 | |
| 5011 | PyDoc_STRVAR(os_getpriority__doc__, |
| 5012 | "getpriority($module, /, which, who)\n" |
| 5013 | "--\n" |
| 5014 | "\n" |
| 5015 | "Return program scheduling priority."); |
| 5016 | |
| 5017 | #define OS_GETPRIORITY_METHODDEF \ |
| 5018 | {"getpriority", (PyCFunction)os_getpriority, METH_VARARGS|METH_KEYWORDS, os_getpriority__doc__}, |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5019 | |
| 5020 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5021 | os_getpriority_impl(PyModuleDef *module, int which, int who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5022 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5023 | static PyObject * |
| 5024 | os_getpriority(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5025 | { |
| 5026 | PyObject *return_value = NULL; |
| 5027 | static char *_keywords[] = {"which", "who", NULL}; |
| 5028 | int which; |
| 5029 | int who; |
| 5030 | |
| 5031 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5032 | "ii:getpriority", _keywords, |
| 5033 | &which, &who)) |
| 5034 | goto exit; |
| 5035 | return_value = os_getpriority_impl(module, which, who); |
| 5036 | |
| 5037 | exit: |
| 5038 | return return_value; |
| 5039 | } |
| 5040 | |
| 5041 | static PyObject * |
| 5042 | os_getpriority_impl(PyModuleDef *module, int which, int who) |
| 5043 | /*[clinic end generated code: output=81639cf765f05dae input=9be615d40e2544ef]*/ |
| 5044 | { |
| 5045 | int retval; |
| 5046 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5047 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5048 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5049 | if (errno != 0) |
| 5050 | return posix_error(); |
| 5051 | return PyLong_FromLong((long)retval); |
| 5052 | } |
| 5053 | #endif /* HAVE_GETPRIORITY */ |
| 5054 | |
| 5055 | |
| 5056 | #ifdef HAVE_SETPRIORITY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5057 | /*[clinic input] |
| 5058 | os.setpriority |
| 5059 | |
| 5060 | which: int |
| 5061 | who: int |
| 5062 | priority: int |
| 5063 | |
| 5064 | Set program scheduling priority. |
| 5065 | [clinic start generated code]*/ |
| 5066 | |
| 5067 | PyDoc_STRVAR(os_setpriority__doc__, |
| 5068 | "setpriority($module, /, which, who, priority)\n" |
| 5069 | "--\n" |
| 5070 | "\n" |
| 5071 | "Set program scheduling priority."); |
| 5072 | |
| 5073 | #define OS_SETPRIORITY_METHODDEF \ |
| 5074 | {"setpriority", (PyCFunction)os_setpriority, METH_VARARGS|METH_KEYWORDS, os_setpriority__doc__}, |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5075 | |
| 5076 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5077 | os_setpriority_impl(PyModuleDef *module, int which, int who, int priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5078 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5079 | static PyObject * |
| 5080 | os_setpriority(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5081 | { |
| 5082 | PyObject *return_value = NULL; |
| 5083 | static char *_keywords[] = {"which", "who", "priority", NULL}; |
| 5084 | int which; |
| 5085 | int who; |
| 5086 | int priority; |
| 5087 | |
| 5088 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5089 | "iii:setpriority", _keywords, |
| 5090 | &which, &who, &priority)) |
| 5091 | goto exit; |
| 5092 | return_value = os_setpriority_impl(module, which, who, priority); |
| 5093 | |
| 5094 | exit: |
| 5095 | return return_value; |
| 5096 | } |
| 5097 | |
| 5098 | static PyObject * |
| 5099 | os_setpriority_impl(PyModuleDef *module, int which, int who, int priority) |
| 5100 | /*[clinic end generated code: output=ddad62651fb2120c input=710ccbf65b9dc513]*/ |
| 5101 | { |
| 5102 | int retval; |
| 5103 | |
| 5104 | retval = setpriority(which, who, priority); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 5105 | if (retval == -1) |
| 5106 | return posix_error(); |
| 5107 | Py_RETURN_NONE; |
| 5108 | } |
| 5109 | #endif /* HAVE_SETPRIORITY */ |
| 5110 | |
| 5111 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5112 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5113 | 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] | 5114 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5115 | char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5116 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5117 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5118 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5119 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 5120 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5121 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5122 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 5123 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5124 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5125 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
| 5126 | (dst_dir_fd != DEFAULT_DIR_FD); |
| 5127 | #ifndef HAVE_RENAMEAT |
| 5128 | if (dir_fd_specified) { |
| 5129 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5130 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5131 | } |
| 5132 | #endif |
| 5133 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5134 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5135 | PyErr_Format(PyExc_ValueError, |
| 5136 | "%s: src and dst must be the same type", function_name); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5137 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | #ifdef MS_WINDOWS |
| 5141 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5142 | if (src->wide) |
| 5143 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5144 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5145 | result = MoveFileExA(src->narrow, dst->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5146 | Py_END_ALLOW_THREADS |
| 5147 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5148 | if (!result) |
| 5149 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5150 | |
| 5151 | #else |
| 5152 | Py_BEGIN_ALLOW_THREADS |
| 5153 | #ifdef HAVE_RENAMEAT |
| 5154 | if (dir_fd_specified) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5155 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5156 | else |
| 5157 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5158 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5159 | Py_END_ALLOW_THREADS |
| 5160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5161 | if (result) |
| 5162 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5163 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5164 | Py_RETURN_NONE; |
| 5165 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5166 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5167 | |
| 5168 | /*[clinic input] |
| 5169 | os.rename |
| 5170 | |
| 5171 | src : path_t |
| 5172 | dst : path_t |
| 5173 | * |
| 5174 | src_dir_fd : dir_fd = None |
| 5175 | dst_dir_fd : dir_fd = None |
| 5176 | |
| 5177 | Rename a file or directory. |
| 5178 | |
| 5179 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 5180 | descriptor open to a directory, and the respective path string (src or dst) |
| 5181 | should be relative; the path will then be relative to that directory. |
| 5182 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 5183 | If they are unavailable, using them will raise a NotImplementedError. |
| 5184 | [clinic start generated code]*/ |
| 5185 | |
| 5186 | PyDoc_STRVAR(os_rename__doc__, |
| 5187 | "rename($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n" |
| 5188 | "--\n" |
| 5189 | "\n" |
| 5190 | "Rename a file or directory.\n" |
| 5191 | "\n" |
| 5192 | "If either src_dir_fd or dst_dir_fd is not None, it should be a file\n" |
| 5193 | " descriptor open to a directory, and the respective path string (src or dst)\n" |
| 5194 | " should be relative; the path will then be relative to that directory.\n" |
| 5195 | "src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n" |
| 5196 | " If they are unavailable, using them will raise a NotImplementedError."); |
| 5197 | |
| 5198 | #define OS_RENAME_METHODDEF \ |
| 5199 | {"rename", (PyCFunction)os_rename, METH_VARARGS|METH_KEYWORDS, os_rename__doc__}, |
| 5200 | |
| 5201 | static PyObject * |
| 5202 | os_rename_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd); |
| 5203 | |
| 5204 | static PyObject * |
| 5205 | os_rename(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5206 | { |
| 5207 | PyObject *return_value = NULL; |
| 5208 | static char *_keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 5209 | path_t src = PATH_T_INITIALIZE("rename", "src", 0, 0); |
| 5210 | path_t dst = PATH_T_INITIALIZE("rename", "dst", 0, 0); |
| 5211 | int src_dir_fd = DEFAULT_DIR_FD; |
| 5212 | int dst_dir_fd = DEFAULT_DIR_FD; |
| 5213 | |
| 5214 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5215 | "O&O&|$O&O&:rename", _keywords, |
| 5216 | path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd)) |
| 5217 | goto exit; |
| 5218 | return_value = os_rename_impl(module, &src, &dst, src_dir_fd, dst_dir_fd); |
| 5219 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5220 | exit: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5221 | /* Cleanup for src */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5222 | path_cleanup(&src); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5223 | /* Cleanup for dst */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5224 | path_cleanup(&dst); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5225 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5226 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 5229 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5230 | os_rename_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd) |
| 5231 | /*[clinic end generated code: output=c936bdc81f460a1e input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 5232 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5233 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 5234 | } |
| 5235 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5236 | |
| 5237 | /*[clinic input] |
| 5238 | os.replace = os.rename |
| 5239 | |
| 5240 | Rename a file or directory, overwriting the destination. |
| 5241 | |
| 5242 | If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 5243 | descriptor open to a directory, and the respective path string (src or dst) |
| 5244 | should be relative; the path will then be relative to that directory. |
| 5245 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 5246 | If they are unavailable, using them will raise a NotImplementedError." |
| 5247 | [clinic start generated code]*/ |
| 5248 | |
| 5249 | PyDoc_STRVAR(os_replace__doc__, |
| 5250 | "replace($module, /, src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n" |
| 5251 | "--\n" |
| 5252 | "\n" |
| 5253 | "Rename a file or directory, overwriting the destination.\n" |
| 5254 | "\n" |
| 5255 | "If either src_dir_fd or dst_dir_fd is not None, it should be a file\n" |
| 5256 | " descriptor open to a directory, and the respective path string (src or dst)\n" |
| 5257 | " should be relative; the path will then be relative to that directory.\n" |
| 5258 | "src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n" |
| 5259 | " If they are unavailable, using them will raise a NotImplementedError.\""); |
| 5260 | |
| 5261 | #define OS_REPLACE_METHODDEF \ |
| 5262 | {"replace", (PyCFunction)os_replace, METH_VARARGS|METH_KEYWORDS, os_replace__doc__}, |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 5263 | |
| 5264 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5265 | os_replace_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5266 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5267 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5268 | os_replace(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5269 | { |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5270 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5271 | static char *_keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL}; |
| 5272 | path_t src = PATH_T_INITIALIZE("replace", "src", 0, 0); |
| 5273 | path_t dst = PATH_T_INITIALIZE("replace", "dst", 0, 0); |
| 5274 | int src_dir_fd = DEFAULT_DIR_FD; |
| 5275 | int dst_dir_fd = DEFAULT_DIR_FD; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5276 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5277 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5278 | "O&O&|$O&O&:replace", _keywords, |
| 5279 | path_converter, &src, path_converter, &dst, dir_fd_converter, &src_dir_fd, dir_fd_converter, &dst_dir_fd)) |
| 5280 | goto exit; |
| 5281 | return_value = os_replace_impl(module, &src, &dst, src_dir_fd, dst_dir_fd); |
| 5282 | |
| 5283 | exit: |
| 5284 | /* Cleanup for src */ |
| 5285 | path_cleanup(&src); |
| 5286 | /* Cleanup for dst */ |
| 5287 | path_cleanup(&dst); |
| 5288 | |
| 5289 | return return_value; |
| 5290 | } |
| 5291 | |
| 5292 | static PyObject * |
| 5293 | os_replace_impl(PyModuleDef *module, path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd) |
| 5294 | /*[clinic end generated code: output=224e4710d290d171 input=25515dfb107c8421]*/ |
| 5295 | { |
| 5296 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); |
| 5297 | } |
| 5298 | |
| 5299 | |
| 5300 | /*[clinic input] |
| 5301 | os.rmdir |
| 5302 | |
| 5303 | path: path_t |
| 5304 | * |
| 5305 | dir_fd: dir_fd(requires='unlinkat') = None |
| 5306 | |
| 5307 | Remove a directory. |
| 5308 | |
| 5309 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 5310 | and path should be relative; path will then be relative to that directory. |
| 5311 | dir_fd may not be implemented on your platform. |
| 5312 | If it is unavailable, using it will raise a NotImplementedError. |
| 5313 | [clinic start generated code]*/ |
| 5314 | |
| 5315 | PyDoc_STRVAR(os_rmdir__doc__, |
| 5316 | "rmdir($module, /, path, *, dir_fd=None)\n" |
| 5317 | "--\n" |
| 5318 | "\n" |
| 5319 | "Remove a directory.\n" |
| 5320 | "\n" |
| 5321 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 5322 | " and path should be relative; path will then be relative to that directory.\n" |
| 5323 | "dir_fd may not be implemented on your platform.\n" |
| 5324 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 5325 | |
| 5326 | #define OS_RMDIR_METHODDEF \ |
| 5327 | {"rmdir", (PyCFunction)os_rmdir, METH_VARARGS|METH_KEYWORDS, os_rmdir__doc__}, |
| 5328 | |
| 5329 | static PyObject * |
| 5330 | os_rmdir_impl(PyModuleDef *module, path_t *path, int dir_fd); |
| 5331 | |
| 5332 | static PyObject * |
| 5333 | os_rmdir(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5334 | { |
| 5335 | PyObject *return_value = NULL; |
| 5336 | static char *_keywords[] = {"path", "dir_fd", NULL}; |
| 5337 | path_t path = PATH_T_INITIALIZE("rmdir", "path", 0, 0); |
| 5338 | int dir_fd = DEFAULT_DIR_FD; |
| 5339 | |
| 5340 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5341 | "O&|$O&:rmdir", _keywords, |
| 5342 | path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
| 5343 | goto exit; |
| 5344 | return_value = os_rmdir_impl(module, &path, dir_fd); |
| 5345 | |
| 5346 | exit: |
| 5347 | /* Cleanup for path */ |
| 5348 | path_cleanup(&path); |
| 5349 | |
| 5350 | return return_value; |
| 5351 | } |
| 5352 | |
| 5353 | static PyObject * |
| 5354 | os_rmdir_impl(PyModuleDef *module, path_t *path, int dir_fd) |
| 5355 | /*[clinic end generated code: output=70b9fdbe3bee0591 input=38c8b375ca34a7e2]*/ |
| 5356 | { |
| 5357 | int result; |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5358 | |
| 5359 | Py_BEGIN_ALLOW_THREADS |
| 5360 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5361 | if (path->wide) |
| 5362 | result = RemoveDirectoryW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5363 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5364 | result = RemoveDirectoryA(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5365 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 5366 | #else |
| 5367 | #ifdef HAVE_UNLINKAT |
| 5368 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5369 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5370 | else |
| 5371 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5372 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5373 | #endif |
| 5374 | Py_END_ALLOW_THREADS |
| 5375 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5376 | if (result) |
| 5377 | return path_error(path); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5379 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5380 | } |
| 5381 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5382 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5383 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5384 | #ifdef MS_WINDOWS |
| 5385 | /*[clinic input] |
| 5386 | os.system -> long |
| 5387 | |
| 5388 | command: Py_UNICODE |
| 5389 | |
| 5390 | Execute the command in a subshell. |
| 5391 | [clinic start generated code]*/ |
| 5392 | |
| 5393 | PyDoc_STRVAR(os_system__doc__, |
| 5394 | "system($module, /, command)\n" |
| 5395 | "--\n" |
| 5396 | "\n" |
| 5397 | "Execute the command in a subshell."); |
| 5398 | |
| 5399 | #define OS_SYSTEM_METHODDEF \ |
| 5400 | {"system", (PyCFunction)os_system, METH_VARARGS|METH_KEYWORDS, os_system__doc__}, |
| 5401 | |
| 5402 | static long |
| 5403 | os_system_impl(PyModuleDef *module, Py_UNICODE *command); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5404 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5405 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5406 | os_system(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5407 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5408 | PyObject *return_value = NULL; |
| 5409 | static char *_keywords[] = {"command", NULL}; |
| 5410 | Py_UNICODE *command; |
| 5411 | long _return_value; |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 5412 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5413 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5414 | "u:system", _keywords, |
| 5415 | &command)) |
| 5416 | goto exit; |
| 5417 | _return_value = os_system_impl(module, command); |
| 5418 | if ((_return_value == -1) && PyErr_Occurred()) |
| 5419 | goto exit; |
| 5420 | return_value = PyLong_FromLong(_return_value); |
Victor Stinner | cfa7278 | 2010-04-16 11:45:13 +0000 | [diff] [blame] | 5421 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5422 | exit: |
| 5423 | return return_value; |
| 5424 | } |
| 5425 | |
| 5426 | static long |
| 5427 | os_system_impl(PyModuleDef *module, Py_UNICODE *command) |
| 5428 | /*[clinic end generated code: output=29fe699c0b2e9d38 input=303f5ce97df606b0]*/ |
| 5429 | { |
| 5430 | long result; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5431 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5432 | result = _wsystem(command); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5433 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5434 | return result; |
| 5435 | } |
| 5436 | #else /* MS_WINDOWS */ |
| 5437 | /*[clinic input] |
| 5438 | os.system -> long |
| 5439 | |
| 5440 | command: FSConverter |
| 5441 | |
| 5442 | Execute the command in a subshell. |
| 5443 | [clinic start generated code]*/ |
| 5444 | |
| 5445 | PyDoc_STRVAR(os_system__doc__, |
| 5446 | "system($module, /, command)\n" |
| 5447 | "--\n" |
| 5448 | "\n" |
| 5449 | "Execute the command in a subshell."); |
| 5450 | |
| 5451 | #define OS_SYSTEM_METHODDEF \ |
| 5452 | {"system", (PyCFunction)os_system, METH_VARARGS|METH_KEYWORDS, os_system__doc__}, |
| 5453 | |
| 5454 | static long |
| 5455 | os_system_impl(PyModuleDef *module, PyObject *command); |
| 5456 | |
| 5457 | static PyObject * |
| 5458 | os_system(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5459 | { |
| 5460 | PyObject *return_value = NULL; |
| 5461 | static char *_keywords[] = {"command", NULL}; |
| 5462 | PyObject *command = NULL; |
| 5463 | long _return_value; |
| 5464 | |
| 5465 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5466 | "O&:system", _keywords, |
| 5467 | PyUnicode_FSConverter, &command)) |
| 5468 | goto exit; |
| 5469 | _return_value = os_system_impl(module, command); |
| 5470 | if ((_return_value == -1) && PyErr_Occurred()) |
| 5471 | goto exit; |
| 5472 | return_value = PyLong_FromLong(_return_value); |
| 5473 | |
| 5474 | exit: |
| 5475 | /* Cleanup for command */ |
| 5476 | Py_XDECREF(command); |
| 5477 | |
| 5478 | return return_value; |
| 5479 | } |
| 5480 | |
| 5481 | static long |
| 5482 | os_system_impl(PyModuleDef *module, PyObject *command) |
| 5483 | /*[clinic end generated code: output=5be9f3c40ead3bad input=86a58554ba6094af]*/ |
| 5484 | { |
| 5485 | long result; |
| 5486 | char *bytes = PyBytes_AsString(command); |
| 5487 | Py_BEGIN_ALLOW_THREADS |
| 5488 | result = system(bytes); |
| 5489 | Py_END_ALLOW_THREADS |
| 5490 | return result; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5491 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 5492 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5493 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5494 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5495 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5496 | /*[clinic input] |
| 5497 | os.umask |
| 5498 | |
| 5499 | mask: int |
| 5500 | / |
| 5501 | |
| 5502 | Set the current numeric umask and return the previous umask. |
| 5503 | [clinic start generated code]*/ |
| 5504 | |
| 5505 | PyDoc_STRVAR(os_umask__doc__, |
| 5506 | "umask($module, mask, /)\n" |
| 5507 | "--\n" |
| 5508 | "\n" |
| 5509 | "Set the current numeric umask and return the previous umask."); |
| 5510 | |
| 5511 | #define OS_UMASK_METHODDEF \ |
| 5512 | {"umask", (PyCFunction)os_umask, METH_VARARGS, os_umask__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5513 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5514 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5515 | os_umask_impl(PyModuleDef *module, int mask); |
| 5516 | |
| 5517 | static PyObject * |
| 5518 | os_umask(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5519 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5520 | PyObject *return_value = NULL; |
| 5521 | int mask; |
| 5522 | |
| 5523 | if (!PyArg_ParseTuple(args, |
| 5524 | "i:umask", |
| 5525 | &mask)) |
| 5526 | goto exit; |
| 5527 | return_value = os_umask_impl(module, mask); |
| 5528 | |
| 5529 | exit: |
| 5530 | return return_value; |
| 5531 | } |
| 5532 | |
| 5533 | static PyObject * |
| 5534 | os_umask_impl(PyModuleDef *module, int mask) |
| 5535 | /*[clinic end generated code: output=90048b39d2d4a961 input=ab6bfd9b24d8a7e8]*/ |
| 5536 | { |
| 5537 | int i = (int)umask(mask); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5538 | if (i < 0) |
| 5539 | return posix_error(); |
| 5540 | return PyLong_FromLong((long)i); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5541 | } |
| 5542 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5543 | #ifdef MS_WINDOWS |
| 5544 | |
| 5545 | /* override the default DeleteFileW behavior so that directory |
| 5546 | symlinks can be removed with this function, the same as with |
| 5547 | Unix symlinks */ |
| 5548 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) |
| 5549 | { |
| 5550 | WIN32_FILE_ATTRIBUTE_DATA info; |
| 5551 | WIN32_FIND_DATAW find_data; |
| 5552 | HANDLE find_data_handle; |
| 5553 | int is_directory = 0; |
| 5554 | int is_link = 0; |
| 5555 | |
| 5556 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { |
| 5557 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 5558 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5559 | /* Get WIN32_FIND_DATA structure for the path to determine if |
| 5560 | it is a symlink */ |
| 5561 | if(is_directory && |
| 5562 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 5563 | find_data_handle = FindFirstFileW(lpFileName, &find_data); |
| 5564 | |
| 5565 | if(find_data_handle != INVALID_HANDLE_VALUE) { |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 5566 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
| 5567 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ |
| 5568 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || |
| 5569 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 5570 | FindClose(find_data_handle); |
| 5571 | } |
| 5572 | } |
| 5573 | } |
| 5574 | |
| 5575 | if (is_directory && is_link) |
| 5576 | return RemoveDirectoryW(lpFileName); |
| 5577 | |
| 5578 | return DeleteFileW(lpFileName); |
| 5579 | } |
| 5580 | #endif /* MS_WINDOWS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5581 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5582 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5583 | /*[clinic input] |
| 5584 | os.unlink |
| 5585 | |
| 5586 | path: path_t |
| 5587 | * |
| 5588 | dir_fd: dir_fd(requires='unlinkat')=None |
| 5589 | |
| 5590 | Remove a file (same as remove()). |
| 5591 | |
| 5592 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 5593 | and path should be relative; path will then be relative to that directory. |
| 5594 | dir_fd may not be implemented on your platform. |
| 5595 | If it is unavailable, using it will raise a NotImplementedError. |
| 5596 | |
| 5597 | [clinic start generated code]*/ |
| 5598 | |
| 5599 | PyDoc_STRVAR(os_unlink__doc__, |
| 5600 | "unlink($module, /, path, *, dir_fd=None)\n" |
| 5601 | "--\n" |
| 5602 | "\n" |
| 5603 | "Remove a file (same as remove()).\n" |
| 5604 | "\n" |
| 5605 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 5606 | " and path should be relative; path will then be relative to that directory.\n" |
| 5607 | "dir_fd may not be implemented on your platform.\n" |
| 5608 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 5609 | |
| 5610 | #define OS_UNLINK_METHODDEF \ |
| 5611 | {"unlink", (PyCFunction)os_unlink, METH_VARARGS|METH_KEYWORDS, os_unlink__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5612 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5613 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5614 | os_unlink_impl(PyModuleDef *module, path_t *path, int dir_fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5615 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5616 | static PyObject * |
| 5617 | os_unlink(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5618 | { |
| 5619 | PyObject *return_value = NULL; |
| 5620 | static char *_keywords[] = {"path", "dir_fd", NULL}; |
| 5621 | path_t path = PATH_T_INITIALIZE("unlink", "path", 0, 0); |
| 5622 | int dir_fd = DEFAULT_DIR_FD; |
| 5623 | |
| 5624 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5625 | "O&|$O&:unlink", _keywords, |
| 5626 | path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
| 5627 | goto exit; |
| 5628 | return_value = os_unlink_impl(module, &path, dir_fd); |
| 5629 | |
| 5630 | exit: |
| 5631 | /* Cleanup for path */ |
| 5632 | path_cleanup(&path); |
| 5633 | |
| 5634 | return return_value; |
| 5635 | } |
| 5636 | |
| 5637 | static PyObject * |
| 5638 | os_unlink_impl(PyModuleDef *module, path_t *path, int dir_fd) |
| 5639 | /*[clinic end generated code: output=59a6e66d67ff2e75 input=d7bcde2b1b2a2552]*/ |
| 5640 | { |
| 5641 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5642 | |
| 5643 | Py_BEGIN_ALLOW_THREADS |
| 5644 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5645 | if (path->wide) |
| 5646 | result = Py_DeleteFileW(path->wide); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 5647 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5648 | result = DeleteFileA(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5649 | result = !result; /* Windows, success=1, UNIX, success=0 */ |
| 5650 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5651 | #ifdef HAVE_UNLINKAT |
| 5652 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5653 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5654 | else |
| 5655 | #endif /* HAVE_UNLINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5656 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5657 | #endif |
| 5658 | Py_END_ALLOW_THREADS |
| 5659 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5660 | if (result) |
| 5661 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5662 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5663 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5664 | } |
| 5665 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5666 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5667 | /*[clinic input] |
| 5668 | os.remove = os.unlink |
| 5669 | |
| 5670 | Remove a file (same as unlink()). |
| 5671 | |
| 5672 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 5673 | and path should be relative; path will then be relative to that directory. |
| 5674 | dir_fd may not be implemented on your platform. |
| 5675 | If it is unavailable, using it will raise a NotImplementedError. |
| 5676 | [clinic start generated code]*/ |
| 5677 | |
| 5678 | PyDoc_STRVAR(os_remove__doc__, |
| 5679 | "remove($module, /, path, *, dir_fd=None)\n" |
| 5680 | "--\n" |
| 5681 | "\n" |
| 5682 | "Remove a file (same as unlink()).\n" |
| 5683 | "\n" |
| 5684 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 5685 | " and path should be relative; path will then be relative to that directory.\n" |
| 5686 | "dir_fd may not be implemented on your platform.\n" |
| 5687 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 5688 | |
| 5689 | #define OS_REMOVE_METHODDEF \ |
| 5690 | {"remove", (PyCFunction)os_remove, METH_VARARGS|METH_KEYWORDS, os_remove__doc__}, |
| 5691 | |
| 5692 | static PyObject * |
| 5693 | os_remove_impl(PyModuleDef *module, path_t *path, int dir_fd); |
| 5694 | |
| 5695 | static PyObject * |
| 5696 | os_remove(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 5697 | { |
| 5698 | PyObject *return_value = NULL; |
| 5699 | static char *_keywords[] = {"path", "dir_fd", NULL}; |
| 5700 | path_t path = PATH_T_INITIALIZE("remove", "path", 0, 0); |
| 5701 | int dir_fd = DEFAULT_DIR_FD; |
| 5702 | |
| 5703 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 5704 | "O&|$O&:remove", _keywords, |
| 5705 | path_converter, &path, UNLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
| 5706 | goto exit; |
| 5707 | return_value = os_remove_impl(module, &path, dir_fd); |
| 5708 | |
| 5709 | exit: |
| 5710 | /* Cleanup for path */ |
| 5711 | path_cleanup(&path); |
| 5712 | |
| 5713 | return return_value; |
| 5714 | } |
| 5715 | |
| 5716 | static PyObject * |
| 5717 | os_remove_impl(PyModuleDef *module, path_t *path, int dir_fd) |
| 5718 | /*[clinic end generated code: output=cb170cf1e195b8ed input=e05c5ab55cd30983]*/ |
| 5719 | { |
| 5720 | return os_unlink_impl(module, path, dir_fd); |
| 5721 | } |
| 5722 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5723 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 5724 | static PyStructSequence_Field uname_result_fields[] = { |
| 5725 | {"sysname", "operating system name"}, |
| 5726 | {"nodename", "name of machine on network (implementation-defined)"}, |
| 5727 | {"release", "operating system release"}, |
| 5728 | {"version", "operating system version"}, |
| 5729 | {"machine", "hardware identifier"}, |
| 5730 | {NULL} |
| 5731 | }; |
| 5732 | |
| 5733 | PyDoc_STRVAR(uname_result__doc__, |
| 5734 | "uname_result: Result from os.uname().\n\n\ |
| 5735 | This object may be accessed either as a tuple of\n\ |
| 5736 | (sysname, nodename, release, version, machine),\n\ |
| 5737 | or via the attributes sysname, nodename, release, version, and machine.\n\ |
| 5738 | \n\ |
| 5739 | See os.uname for more information."); |
| 5740 | |
| 5741 | static PyStructSequence_Desc uname_result_desc = { |
| 5742 | "uname_result", /* name */ |
| 5743 | uname_result__doc__, /* doc */ |
| 5744 | uname_result_fields, |
| 5745 | 5 |
| 5746 | }; |
| 5747 | |
| 5748 | static PyTypeObject UnameResultType; |
| 5749 | |
| 5750 | |
| 5751 | #ifdef HAVE_UNAME |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5752 | /*[clinic input] |
| 5753 | os.uname |
| 5754 | |
| 5755 | Return an object identifying the current operating system. |
| 5756 | |
| 5757 | The object behaves like a named tuple with the following fields: |
| 5758 | (sysname, nodename, release, version, machine) |
| 5759 | |
| 5760 | [clinic start generated code]*/ |
| 5761 | |
| 5762 | PyDoc_STRVAR(os_uname__doc__, |
| 5763 | "uname($module, /)\n" |
| 5764 | "--\n" |
| 5765 | "\n" |
| 5766 | "Return an object identifying the current operating system.\n" |
| 5767 | "\n" |
| 5768 | "The object behaves like a named tuple with the following fields:\n" |
| 5769 | " (sysname, nodename, release, version, machine)"); |
| 5770 | |
| 5771 | #define OS_UNAME_METHODDEF \ |
| 5772 | {"uname", (PyCFunction)os_uname, METH_NOARGS, os_uname__doc__}, |
| 5773 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 5774 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5775 | os_uname_impl(PyModuleDef *module); |
| 5776 | |
| 5777 | static PyObject * |
| 5778 | os_uname(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 5779 | { |
| 5780 | return os_uname_impl(module); |
| 5781 | } |
| 5782 | |
| 5783 | static PyObject * |
| 5784 | os_uname_impl(PyModuleDef *module) |
| 5785 | /*[clinic end generated code: output=459a86521ff5041c input=e68bd246db3043ed]*/ |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 5786 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5787 | struct utsname u; |
| 5788 | int res; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 5789 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 5790 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5791 | Py_BEGIN_ALLOW_THREADS |
| 5792 | res = uname(&u); |
| 5793 | Py_END_ALLOW_THREADS |
| 5794 | if (res < 0) |
| 5795 | return posix_error(); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 5796 | |
| 5797 | value = PyStructSequence_New(&UnameResultType); |
| 5798 | if (value == NULL) |
| 5799 | return NULL; |
| 5800 | |
| 5801 | #define SET(i, field) \ |
| 5802 | { \ |
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 5803 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 5804 | if (!o) { \ |
| 5805 | Py_DECREF(value); \ |
| 5806 | return NULL; \ |
| 5807 | } \ |
| 5808 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 5809 | } \ |
| 5810 | |
| 5811 | SET(0, u.sysname); |
| 5812 | SET(1, u.nodename); |
| 5813 | SET(2, u.release); |
| 5814 | SET(3, u.version); |
| 5815 | SET(4, u.machine); |
| 5816 | |
| 5817 | #undef SET |
| 5818 | |
| 5819 | return value; |
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 5820 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5821 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 5822 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 5823 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5824 | |
| 5825 | typedef struct { |
| 5826 | int now; |
| 5827 | time_t atime_s; |
| 5828 | long atime_ns; |
| 5829 | time_t mtime_s; |
| 5830 | long mtime_ns; |
| 5831 | } utime_t; |
| 5832 | |
| 5833 | /* |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5834 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5835 | * they also intentionally leak the declaration of a pointer named "time" |
| 5836 | */ |
| 5837 | #define UTIME_TO_TIMESPEC \ |
| 5838 | struct timespec ts[2]; \ |
| 5839 | struct timespec *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5840 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5841 | time = NULL; \ |
| 5842 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5843 | ts[0].tv_sec = ut->atime_s; \ |
| 5844 | ts[0].tv_nsec = ut->atime_ns; \ |
| 5845 | ts[1].tv_sec = ut->mtime_s; \ |
| 5846 | ts[1].tv_nsec = ut->mtime_ns; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5847 | time = ts; \ |
| 5848 | } \ |
| 5849 | |
| 5850 | #define UTIME_TO_TIMEVAL \ |
| 5851 | struct timeval tv[2]; \ |
| 5852 | struct timeval *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5853 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5854 | time = NULL; \ |
| 5855 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5856 | tv[0].tv_sec = ut->atime_s; \ |
| 5857 | tv[0].tv_usec = ut->atime_ns / 1000; \ |
| 5858 | tv[1].tv_sec = ut->mtime_s; \ |
| 5859 | tv[1].tv_usec = ut->mtime_ns / 1000; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5860 | time = tv; \ |
| 5861 | } \ |
| 5862 | |
| 5863 | #define UTIME_TO_UTIMBUF \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5864 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5865 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5866 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5867 | time = NULL; \ |
| 5868 | else { \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5869 | u.actime = ut->atime_s; \ |
| 5870 | u.modtime = ut->mtime_s; \ |
| 5871 | time = &u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5872 | } |
| 5873 | |
| 5874 | #define UTIME_TO_TIME_T \ |
| 5875 | time_t timet[2]; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5876 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5877 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5878 | time = NULL; \ |
| 5879 | else { \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5880 | timet[0] = ut->atime_s; \ |
| 5881 | timet[1] = ut->mtime_s; \ |
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 5882 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5883 | } \ |
| 5884 | |
| 5885 | |
| 5886 | #define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)) |
| 5887 | |
| 5888 | #if UTIME_HAVE_DIR_FD |
| 5889 | |
| 5890 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5891 | 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] | 5892 | { |
| 5893 | #ifdef HAVE_UTIMENSAT |
| 5894 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; |
| 5895 | UTIME_TO_TIMESPEC; |
| 5896 | return utimensat(dir_fd, path, time, flags); |
| 5897 | #elif defined(HAVE_FUTIMESAT) |
| 5898 | UTIME_TO_TIMEVAL; |
| 5899 | /* |
| 5900 | * follow_symlinks will never be false here; |
| 5901 | * we only allow !follow_symlinks and dir_fd together |
| 5902 | * if we have utimensat() |
| 5903 | */ |
| 5904 | assert(follow_symlinks); |
| 5905 | return futimesat(dir_fd, path, time); |
| 5906 | #endif |
| 5907 | } |
| 5908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5909 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
| 5910 | #else |
| 5911 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5912 | #endif |
| 5913 | |
| 5914 | #define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)) |
| 5915 | |
| 5916 | #if UTIME_HAVE_FD |
| 5917 | |
| 5918 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5919 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5920 | { |
| 5921 | #ifdef HAVE_FUTIMENS |
| 5922 | UTIME_TO_TIMESPEC; |
| 5923 | return futimens(fd, time); |
| 5924 | #else |
| 5925 | UTIME_TO_TIMEVAL; |
| 5926 | return futimes(fd, time); |
| 5927 | #endif |
| 5928 | } |
| 5929 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5930 | #define PATH_UTIME_HAVE_FD 1 |
| 5931 | #else |
| 5932 | #define PATH_UTIME_HAVE_FD 0 |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5933 | #endif |
| 5934 | |
| 5935 | |
| 5936 | #define UTIME_HAVE_NOFOLLOW_SYMLINKS \ |
| 5937 | (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)) |
| 5938 | |
| 5939 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 5940 | |
| 5941 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5942 | utime_nofollow_symlinks(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5943 | { |
| 5944 | #ifdef HAVE_UTIMENSAT |
| 5945 | UTIME_TO_TIMESPEC; |
| 5946 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); |
| 5947 | #else |
| 5948 | UTIME_TO_TIMEVAL; |
| 5949 | return lutimes(path, time); |
| 5950 | #endif |
| 5951 | } |
| 5952 | |
| 5953 | #endif |
| 5954 | |
| 5955 | #ifndef MS_WINDOWS |
| 5956 | |
| 5957 | static int |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 5958 | utime_default(utime_t *ut, char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5959 | { |
| 5960 | #ifdef HAVE_UTIMENSAT |
| 5961 | UTIME_TO_TIMESPEC; |
| 5962 | return utimensat(DEFAULT_DIR_FD, path, time, 0); |
| 5963 | #elif defined(HAVE_UTIMES) |
| 5964 | UTIME_TO_TIMEVAL; |
| 5965 | return utimes(path, time); |
| 5966 | #elif defined(HAVE_UTIME_H) |
| 5967 | UTIME_TO_UTIMBUF; |
| 5968 | return utime(path, time); |
| 5969 | #else |
| 5970 | UTIME_TO_TIME_T; |
| 5971 | return utime(path, time); |
| 5972 | #endif |
| 5973 | } |
| 5974 | |
| 5975 | #endif |
| 5976 | |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5977 | static int |
| 5978 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) |
| 5979 | { |
| 5980 | int result = 0; |
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 5981 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5982 | divmod = PyNumber_Divmod(py_long, billion); |
| 5983 | if (!divmod) |
| 5984 | goto exit; |
| 5985 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
| 5986 | if ((*s == -1) && PyErr_Occurred()) |
| 5987 | goto exit; |
| 5988 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); |
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 5989 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 5990 | goto exit; |
| 5991 | |
| 5992 | result = 1; |
| 5993 | exit: |
| 5994 | Py_XDECREF(divmod); |
| 5995 | return result; |
| 5996 | } |
| 5997 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5998 | |
| 5999 | /*[clinic input] |
| 6000 | os.utime |
| 6001 | |
| 6002 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') |
| 6003 | times: object = NULL |
| 6004 | * |
| 6005 | ns: object = NULL |
| 6006 | dir_fd: dir_fd(requires='futimensat') = None |
| 6007 | follow_symlinks: bool=True |
| 6008 | |
| 6009 | # "utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\ |
| 6010 | |
| 6011 | Set the access and modified time of path. |
| 6012 | |
| 6013 | path may always be specified as a string. |
| 6014 | On some platforms, path may also be specified as an open file descriptor. |
| 6015 | If this functionality is unavailable, using it raises an exception. |
| 6016 | |
| 6017 | If times is not None, it must be a tuple (atime, mtime); |
| 6018 | atime and mtime should be expressed as float seconds since the epoch. |
| 6019 | If ns is not None, it must be a tuple (atime_ns, mtime_ns); |
| 6020 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 6021 | since the epoch. |
| 6022 | If both times and ns are None, utime uses the current time. |
| 6023 | Specifying tuples for both times and ns is an error. |
| 6024 | |
| 6025 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 6026 | and path should be relative; path will then be relative to that directory. |
| 6027 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 6028 | link, utime will modify the symbolic link itself instead of the file the |
| 6029 | link points to. |
| 6030 | It is an error to use dir_fd or follow_symlinks when specifying path |
| 6031 | as an open file descriptor. |
| 6032 | dir_fd and follow_symlinks may not be available on your platform. |
| 6033 | If they are unavailable, using them will raise a NotImplementedError. |
| 6034 | |
| 6035 | [clinic start generated code]*/ |
| 6036 | |
| 6037 | PyDoc_STRVAR(os_utime__doc__, |
| 6038 | "utime($module, /, path, times=None, *, ns=None, dir_fd=None,\n" |
| 6039 | " follow_symlinks=True)\n" |
| 6040 | "--\n" |
| 6041 | "\n" |
| 6042 | "Set the access and modified time of path.\n" |
| 6043 | "\n" |
| 6044 | "path may always be specified as a string.\n" |
| 6045 | "On some platforms, path may also be specified as an open file descriptor.\n" |
| 6046 | " If this functionality is unavailable, using it raises an exception.\n" |
| 6047 | "\n" |
| 6048 | "If times is not None, it must be a tuple (atime, mtime);\n" |
| 6049 | " atime and mtime should be expressed as float seconds since the epoch.\n" |
| 6050 | "If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n" |
| 6051 | " atime_ns and mtime_ns should be expressed as integer nanoseconds\n" |
| 6052 | " since the epoch.\n" |
| 6053 | "If both times and ns are None, utime uses the current time.\n" |
| 6054 | "Specifying tuples for both times and ns is an error.\n" |
| 6055 | "\n" |
| 6056 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 6057 | " and path should be relative; path will then be relative to that directory.\n" |
| 6058 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 6059 | " link, utime will modify the symbolic link itself instead of the file the\n" |
| 6060 | " link points to.\n" |
| 6061 | "It is an error to use dir_fd or follow_symlinks when specifying path\n" |
| 6062 | " as an open file descriptor.\n" |
| 6063 | "dir_fd and follow_symlinks may not be available on your platform.\n" |
| 6064 | " If they are unavailable, using them will raise a NotImplementedError."); |
| 6065 | |
| 6066 | #define OS_UTIME_METHODDEF \ |
| 6067 | {"utime", (PyCFunction)os_utime, METH_VARARGS|METH_KEYWORDS, os_utime__doc__}, |
| 6068 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6069 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6070 | os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, PyObject *ns, int dir_fd, int follow_symlinks); |
| 6071 | |
| 6072 | static PyObject * |
| 6073 | os_utime(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6074 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6075 | PyObject *return_value = NULL; |
| 6076 | static char *_keywords[] = {"path", "times", "ns", "dir_fd", "follow_symlinks", NULL}; |
| 6077 | path_t path = PATH_T_INITIALIZE("utime", "path", 0, PATH_UTIME_HAVE_FD); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6078 | PyObject *times = NULL; |
| 6079 | PyObject *ns = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6080 | int dir_fd = DEFAULT_DIR_FD; |
| 6081 | int follow_symlinks = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6082 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6083 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 6084 | "O&|O$OO&p:utime", _keywords, |
| 6085 | path_converter, &path, ×, &ns, FUTIMENSAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks)) |
| 6086 | goto exit; |
| 6087 | return_value = os_utime_impl(module, &path, times, ns, dir_fd, follow_symlinks); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6089 | exit: |
| 6090 | /* Cleanup for path */ |
| 6091 | path_cleanup(&path); |
| 6092 | |
| 6093 | return return_value; |
| 6094 | } |
| 6095 | |
| 6096 | static PyObject * |
| 6097 | os_utime_impl(PyModuleDef *module, path_t *path, PyObject *times, PyObject *ns, int dir_fd, int follow_symlinks) |
| 6098 | /*[clinic end generated code: output=891489c35cc68c5d input=1f18c17d5941aa82]*/ |
| 6099 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6100 | #ifdef MS_WINDOWS |
| 6101 | HANDLE hFile; |
| 6102 | FILETIME atime, mtime; |
| 6103 | #else |
| 6104 | int result; |
| 6105 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6106 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6107 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6108 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6109 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 6110 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6111 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6112 | if (times && (times != Py_None) && ns) { |
| 6113 | PyErr_SetString(PyExc_ValueError, |
| 6114 | "utime: you may specify either 'times'" |
| 6115 | " or 'ns' but not both"); |
| 6116 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | if (times && (times != Py_None)) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 6120 | time_t a_sec, m_sec; |
| 6121 | long a_nsec, m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6122 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6123 | PyErr_SetString(PyExc_TypeError, |
| 6124 | "utime: 'times' must be either" |
| 6125 | " a tuple of two ints or None"); |
| 6126 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6127 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6128 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 6129 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 6130 | &a_sec, &a_nsec, _PyTime_ROUND_DOWN) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 6131 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | 3c1b379 | 2014-02-17 00:02:43 +0100 | [diff] [blame] | 6132 | &m_sec, &m_nsec, _PyTime_ROUND_DOWN) == -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6133 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 6134 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 6135 | utime.atime_s = a_sec; |
| 6136 | utime.atime_ns = a_nsec; |
| 6137 | utime.mtime_s = m_sec; |
| 6138 | utime.mtime_ns = m_nsec; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6139 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6140 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6141 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6142 | PyErr_SetString(PyExc_TypeError, |
| 6143 | "utime: 'ns' must be a tuple of two ints"); |
| 6144 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6145 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6146 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 6147 | 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] | 6148 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 6149 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6150 | &utime.mtime_s, &utime.mtime_ns)) { |
| 6151 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 6152 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6153 | } |
| 6154 | else { |
| 6155 | /* times and ns are both None/unspecified. use "now". */ |
| 6156 | utime.now = 1; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6157 | } |
| 6158 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6159 | #if !UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 6160 | if (follow_symlinks_specified("utime", follow_symlinks)) |
| 6161 | goto exit; |
| 6162 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 6163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6164 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
| 6165 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || |
| 6166 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6167 | goto exit; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6168 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6169 | #if !defined(HAVE_UTIMENSAT) |
| 6170 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 6171 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6172 | "utime: cannot use dir_fd and follow_symlinks " |
| 6173 | "together on this platform"); |
| 6174 | goto exit; |
| 6175 | } |
| 6176 | #endif |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6177 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6178 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6179 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6180 | if (path->wide) |
| 6181 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6182 | NULL, OPEN_EXISTING, |
| 6183 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6184 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6185 | hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6186 | NULL, OPEN_EXISTING, |
| 6187 | FILE_FLAG_BACKUP_SEMANTICS, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6188 | Py_END_ALLOW_THREADS |
| 6189 | if (hFile == INVALID_HANDLE_VALUE) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6190 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6191 | goto exit; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 6192 | } |
| 6193 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6194 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 6195 | GetSystemTimeAsFileTime(&mtime); |
| 6196 | atime = mtime; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6197 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6198 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 6199 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
| 6200 | _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] | 6201 | } |
| 6202 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { |
| 6203 | /* Avoid putting the file name into the error here, |
| 6204 | as that may confuse the user into believing that |
| 6205 | something is wrong with the file, when it also |
| 6206 | could be the time stamp that gives a problem. */ |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 6207 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6208 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6209 | } |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6210 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6211 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 6212 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6213 | #if UTIME_HAVE_NOFOLLOW_SYMLINKS |
| 6214 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6215 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6216 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 6217 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6218 | |
| 6219 | #if UTIME_HAVE_DIR_FD |
| 6220 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6221 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6222 | else |
| 6223 | #endif |
| 6224 | |
| 6225 | #if UTIME_HAVE_FD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6226 | if (path->fd != -1) |
| 6227 | result = utime_fd(&utime, path->fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6228 | else |
| 6229 | #endif |
| 6230 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6231 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6232 | |
| 6233 | Py_END_ALLOW_THREADS |
| 6234 | |
| 6235 | if (result < 0) { |
| 6236 | /* see previous comment about not putting filename in error here */ |
| 6237 | return_value = posix_error(); |
| 6238 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6239 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 6240 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 6241 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6242 | |
| 6243 | Py_INCREF(Py_None); |
| 6244 | return_value = Py_None; |
| 6245 | |
| 6246 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6247 | #ifdef MS_WINDOWS |
| 6248 | if (hFile != INVALID_HANDLE_VALUE) |
| 6249 | CloseHandle(hFile); |
| 6250 | #endif |
| 6251 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6252 | } |
| 6253 | |
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 6254 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6255 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6256 | |
| 6257 | /*[clinic input] |
| 6258 | os._exit |
| 6259 | |
| 6260 | status: int |
| 6261 | |
| 6262 | Exit to the system with specified status, without normal exit processing. |
| 6263 | [clinic start generated code]*/ |
| 6264 | |
| 6265 | PyDoc_STRVAR(os__exit__doc__, |
| 6266 | "_exit($module, /, status)\n" |
| 6267 | "--\n" |
| 6268 | "\n" |
| 6269 | "Exit to the system with specified status, without normal exit processing."); |
| 6270 | |
| 6271 | #define OS__EXIT_METHODDEF \ |
| 6272 | {"_exit", (PyCFunction)os__exit, METH_VARARGS|METH_KEYWORDS, os__exit__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6273 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6274 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6275 | os__exit_impl(PyModuleDef *module, int status); |
| 6276 | |
| 6277 | static PyObject * |
| 6278 | os__exit(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6279 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6280 | PyObject *return_value = NULL; |
| 6281 | static char *_keywords[] = {"status", NULL}; |
| 6282 | int status; |
| 6283 | |
| 6284 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 6285 | "i:_exit", _keywords, |
| 6286 | &status)) |
| 6287 | goto exit; |
| 6288 | return_value = os__exit_impl(module, status); |
| 6289 | |
| 6290 | exit: |
| 6291 | return return_value; |
| 6292 | } |
| 6293 | |
| 6294 | static PyObject * |
| 6295 | os__exit_impl(PyModuleDef *module, int status) |
| 6296 | /*[clinic end generated code: output=4f9858c4cc2dcb89 input=5e6d57556b0c4a62]*/ |
| 6297 | { |
| 6298 | _exit(status); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6299 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6302 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) |
| 6303 | static void |
Martin v. Löwis | 725507b | 2006-03-07 12:08:51 +0000 | [diff] [blame] | 6304 | free_string_array(char **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6305 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6306 | Py_ssize_t i; |
| 6307 | for (i = 0; i < count; i++) |
| 6308 | PyMem_Free(array[i]); |
| 6309 | PyMem_DEL(array); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6310 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6311 | |
Antoine Pitrou | 69f7114 | 2009-05-24 21:25:49 +0000 | [diff] [blame] | 6312 | static |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6313 | int fsconvert_strdup(PyObject *o, char**out) |
| 6314 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6315 | PyObject *bytes; |
| 6316 | Py_ssize_t size; |
| 6317 | if (!PyUnicode_FSConverter(o, &bytes)) |
| 6318 | return 0; |
| 6319 | size = PyBytes_GET_SIZE(bytes); |
| 6320 | *out = PyMem_Malloc(size+1); |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 6321 | if (!*out) { |
| 6322 | PyErr_NoMemory(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6323 | return 0; |
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 6324 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6325 | memcpy(*out, PyBytes_AsString(bytes), size+1); |
| 6326 | Py_DECREF(bytes); |
| 6327 | return 1; |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 6328 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6329 | #endif |
| 6330 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6331 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6332 | static char** |
| 6333 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
| 6334 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6335 | char **envlist; |
| 6336 | Py_ssize_t i, pos, envc; |
| 6337 | PyObject *keys=NULL, *vals=NULL; |
| 6338 | PyObject *key, *val, *key2, *val2; |
| 6339 | char *p, *k, *v; |
| 6340 | size_t len; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6341 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6342 | i = PyMapping_Size(env); |
| 6343 | if (i < 0) |
| 6344 | return NULL; |
| 6345 | envlist = PyMem_NEW(char *, i + 1); |
| 6346 | if (envlist == NULL) { |
| 6347 | PyErr_NoMemory(); |
| 6348 | return NULL; |
| 6349 | } |
| 6350 | envc = 0; |
| 6351 | keys = PyMapping_Keys(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 6352 | if (!keys) |
| 6353 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6354 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 6355 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6356 | goto error; |
| 6357 | if (!PyList_Check(keys) || !PyList_Check(vals)) { |
| 6358 | PyErr_Format(PyExc_TypeError, |
| 6359 | "env.keys() or env.values() is not a list"); |
| 6360 | goto error; |
| 6361 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6362 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6363 | for (pos = 0; pos < i; pos++) { |
| 6364 | key = PyList_GetItem(keys, pos); |
| 6365 | val = PyList_GetItem(vals, pos); |
| 6366 | if (!key || !val) |
| 6367 | goto error; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6368 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6369 | if (PyUnicode_FSConverter(key, &key2) == 0) |
| 6370 | goto error; |
| 6371 | if (PyUnicode_FSConverter(val, &val2) == 0) { |
| 6372 | Py_DECREF(key2); |
| 6373 | goto error; |
| 6374 | } |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6375 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6376 | k = PyBytes_AsString(key2); |
| 6377 | v = PyBytes_AsString(val2); |
| 6378 | len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6379 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6380 | p = PyMem_NEW(char, len); |
| 6381 | if (p == NULL) { |
| 6382 | PyErr_NoMemory(); |
| 6383 | Py_DECREF(key2); |
| 6384 | Py_DECREF(val2); |
| 6385 | goto error; |
| 6386 | } |
| 6387 | PyOS_snprintf(p, len, "%s=%s", k, v); |
| 6388 | envlist[envc++] = p; |
| 6389 | Py_DECREF(key2); |
| 6390 | Py_DECREF(val2); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6391 | } |
| 6392 | Py_DECREF(vals); |
| 6393 | Py_DECREF(keys); |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6394 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6395 | envlist[envc] = 0; |
| 6396 | *envc_ptr = envc; |
| 6397 | return envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6398 | |
| 6399 | error: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6400 | Py_XDECREF(keys); |
| 6401 | Py_XDECREF(vals); |
| 6402 | while (--envc >= 0) |
| 6403 | PyMem_DEL(envlist[envc]); |
| 6404 | PyMem_DEL(envlist); |
| 6405 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 6406 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6407 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6408 | static char** |
| 6409 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
| 6410 | { |
| 6411 | int i; |
| 6412 | char **argvlist = PyMem_NEW(char *, *argc+1); |
| 6413 | if (argvlist == NULL) { |
| 6414 | PyErr_NoMemory(); |
| 6415 | return NULL; |
| 6416 | } |
| 6417 | for (i = 0; i < *argc; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6418 | PyObject* item = PySequence_ITEM(argv, i); |
| 6419 | if (item == NULL) |
| 6420 | goto fail; |
| 6421 | if (!fsconvert_strdup(item, &argvlist[i])) { |
| 6422 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6423 | goto fail; |
| 6424 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6425 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6426 | } |
| 6427 | argvlist[*argc] = NULL; |
| 6428 | return argvlist; |
| 6429 | fail: |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 6430 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6431 | free_string_array(argvlist, *argc); |
| 6432 | return NULL; |
| 6433 | } |
| 6434 | #endif |
| 6435 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6436 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6437 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6438 | /*[clinic input] |
| 6439 | os.execv |
| 6440 | |
| 6441 | path: FSConverter |
| 6442 | Path of executable file. |
| 6443 | argv: object |
| 6444 | Tuple or list of strings. |
| 6445 | / |
| 6446 | |
| 6447 | Execute an executable path with arguments, replacing current process. |
| 6448 | [clinic start generated code]*/ |
| 6449 | |
| 6450 | PyDoc_STRVAR(os_execv__doc__, |
| 6451 | "execv($module, path, argv, /)\n" |
| 6452 | "--\n" |
| 6453 | "\n" |
| 6454 | "Execute an executable path with arguments, replacing current process.\n" |
| 6455 | "\n" |
| 6456 | " path\n" |
| 6457 | " Path of executable file.\n" |
| 6458 | " argv\n" |
| 6459 | " Tuple or list of strings."); |
| 6460 | |
| 6461 | #define OS_EXECV_METHODDEF \ |
| 6462 | {"execv", (PyCFunction)os_execv, METH_VARARGS, os_execv__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6463 | |
| 6464 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6465 | os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv); |
| 6466 | |
| 6467 | static PyObject * |
| 6468 | os_execv(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6469 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6470 | PyObject *return_value = NULL; |
| 6471 | PyObject *path = NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6472 | PyObject *argv; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6473 | |
| 6474 | if (!PyArg_ParseTuple(args, |
| 6475 | "O&O:execv", |
| 6476 | PyUnicode_FSConverter, &path, &argv)) |
| 6477 | goto exit; |
| 6478 | return_value = os_execv_impl(module, path, argv); |
| 6479 | |
| 6480 | exit: |
| 6481 | /* Cleanup for path */ |
| 6482 | Py_XDECREF(path); |
| 6483 | |
| 6484 | return return_value; |
| 6485 | } |
| 6486 | |
| 6487 | static PyObject * |
| 6488 | os_execv_impl(PyModuleDef *module, PyObject *path, PyObject *argv) |
| 6489 | /*[clinic end generated code: output=b0f5f2caa6097edc input=96041559925e5229]*/ |
| 6490 | { |
| 6491 | char *path_char; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6492 | char **argvlist; |
| 6493 | Py_ssize_t argc; |
| 6494 | |
| 6495 | /* execv has two arguments: (path, argv), where |
| 6496 | argv is a list or tuple of strings. */ |
| 6497 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6498 | path_char = PyBytes_AsString(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6499 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
| 6500 | PyErr_SetString(PyExc_TypeError, |
| 6501 | "execv() arg 2 must be a tuple or list"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6502 | return NULL; |
| 6503 | } |
| 6504 | argc = PySequence_Size(argv); |
| 6505 | if (argc < 1) { |
| 6506 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6507 | return NULL; |
| 6508 | } |
| 6509 | |
| 6510 | argvlist = parse_arglist(argv, &argc); |
| 6511 | if (argvlist == NULL) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6512 | return NULL; |
| 6513 | } |
| 6514 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6515 | execv(path_char, argvlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6516 | |
| 6517 | /* If we get here it's definitely an error */ |
| 6518 | |
| 6519 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6520 | return posix_error(); |
| 6521 | } |
| 6522 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6523 | |
| 6524 | /*[clinic input] |
| 6525 | os.execve |
| 6526 | |
| 6527 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') |
| 6528 | Path of executable file. |
| 6529 | argv: object |
| 6530 | Tuple or list of strings. |
| 6531 | env: object |
| 6532 | Dictionary of strings mapping to strings. |
| 6533 | |
| 6534 | Execute an executable path with arguments, replacing current process. |
| 6535 | [clinic start generated code]*/ |
| 6536 | |
| 6537 | PyDoc_STRVAR(os_execve__doc__, |
| 6538 | "execve($module, /, path, argv, env)\n" |
| 6539 | "--\n" |
| 6540 | "\n" |
| 6541 | "Execute an executable path with arguments, replacing current process.\n" |
| 6542 | "\n" |
| 6543 | " path\n" |
| 6544 | " Path of executable file.\n" |
| 6545 | " argv\n" |
| 6546 | " Tuple or list of strings.\n" |
| 6547 | " env\n" |
| 6548 | " Dictionary of strings mapping to strings."); |
| 6549 | |
| 6550 | #define OS_EXECVE_METHODDEF \ |
| 6551 | {"execve", (PyCFunction)os_execve, METH_VARARGS|METH_KEYWORDS, os_execve__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6552 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6553 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6554 | os_execve_impl(PyModuleDef *module, path_t *path, PyObject *argv, PyObject *env); |
| 6555 | |
| 6556 | static PyObject * |
| 6557 | os_execve(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 6558 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6559 | PyObject *return_value = NULL; |
| 6560 | static char *_keywords[] = {"path", "argv", "env", NULL}; |
| 6561 | path_t path = PATH_T_INITIALIZE("execve", "path", 0, PATH_HAVE_FEXECVE); |
| 6562 | PyObject *argv; |
| 6563 | PyObject *env; |
| 6564 | |
| 6565 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 6566 | "O&OO:execve", _keywords, |
| 6567 | path_converter, &path, &argv, &env)) |
| 6568 | goto exit; |
| 6569 | return_value = os_execve_impl(module, &path, argv, env); |
| 6570 | |
| 6571 | exit: |
| 6572 | /* Cleanup for path */ |
| 6573 | path_cleanup(&path); |
| 6574 | |
| 6575 | return return_value; |
| 6576 | } |
| 6577 | |
| 6578 | static PyObject * |
| 6579 | os_execve_impl(PyModuleDef *module, path_t *path, PyObject *argv, PyObject *env) |
| 6580 | /*[clinic end generated code: output=fb283760f5d15ab7 input=626804fa092606d9]*/ |
| 6581 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6582 | char **argvlist = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6583 | char **envlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6584 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 6585 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6586 | /* execve has three arguments: (path, argv, env), where |
| 6587 | argv is a list or tuple of strings and env is a dictionary |
| 6588 | like posix.environ. */ |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 6589 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6590 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6591 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6592 | "execve: argv must be a tuple or list"); |
| 6593 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6594 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6595 | argc = PySequence_Size(argv); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6596 | if (!PyMapping_Check(env)) { |
| 6597 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6598 | "execve: environment must be a mapping object"); |
| 6599 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6600 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 6601 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6602 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6603 | if (argvlist == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6604 | goto fail; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6605 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 6606 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6607 | envlist = parse_envlist(env, &envc); |
| 6608 | if (envlist == NULL) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6609 | goto fail; |
| 6610 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6611 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6612 | if (path->fd > -1) |
| 6613 | fexecve(path->fd, argvlist, envlist); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6614 | else |
| 6615 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6616 | execve(path->narrow, argvlist, envlist); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6617 | |
| 6618 | /* If we get here it's definitely an error */ |
| 6619 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6620 | path_error(path); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6621 | |
| 6622 | while (--envc >= 0) |
| 6623 | PyMem_DEL(envlist[envc]); |
| 6624 | PyMem_DEL(envlist); |
| 6625 | fail: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6626 | if (argvlist) |
| 6627 | free_string_array(argvlist, argc); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6628 | return NULL; |
| 6629 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 6630 | #endif /* HAVE_EXECV */ |
| 6631 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6632 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6633 | #ifdef HAVE_SPAWNV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6634 | /*[clinic input] |
| 6635 | os.spawnv |
| 6636 | |
| 6637 | mode: int |
| 6638 | Mode of process creation. |
| 6639 | path: FSConverter |
| 6640 | Path of executable file. |
| 6641 | argv: object |
| 6642 | Tuple or list of strings. |
| 6643 | / |
| 6644 | |
| 6645 | Execute the program specified by path in a new process. |
| 6646 | [clinic start generated code]*/ |
| 6647 | |
| 6648 | PyDoc_STRVAR(os_spawnv__doc__, |
| 6649 | "spawnv($module, mode, path, argv, /)\n" |
| 6650 | "--\n" |
| 6651 | "\n" |
| 6652 | "Execute the program specified by path in a new process.\n" |
| 6653 | "\n" |
| 6654 | " mode\n" |
| 6655 | " Mode of process creation.\n" |
| 6656 | " path\n" |
| 6657 | " Path of executable file.\n" |
| 6658 | " argv\n" |
| 6659 | " Tuple or list of strings."); |
| 6660 | |
| 6661 | #define OS_SPAWNV_METHODDEF \ |
| 6662 | {"spawnv", (PyCFunction)os_spawnv, METH_VARARGS, os_spawnv__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6663 | |
| 6664 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6665 | os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv); |
| 6666 | |
| 6667 | static PyObject * |
| 6668 | os_spawnv(PyModuleDef *module, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6669 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6670 | PyObject *return_value = NULL; |
| 6671 | int mode; |
| 6672 | PyObject *path = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6673 | PyObject *argv; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6674 | |
| 6675 | if (!PyArg_ParseTuple(args, |
| 6676 | "iO&O:spawnv", |
| 6677 | &mode, PyUnicode_FSConverter, &path, &argv)) |
| 6678 | goto exit; |
| 6679 | return_value = os_spawnv_impl(module, mode, path, argv); |
| 6680 | |
| 6681 | exit: |
| 6682 | /* Cleanup for path */ |
| 6683 | Py_XDECREF(path); |
| 6684 | |
| 6685 | return return_value; |
| 6686 | } |
| 6687 | |
| 6688 | static PyObject * |
| 6689 | os_spawnv_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv) |
| 6690 | /*[clinic end generated code: output=dfee6be062e780e3 input=042c91dfc1e6debc]*/ |
| 6691 | { |
| 6692 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6693 | char **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6694 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6695 | Py_ssize_t argc; |
| 6696 | Py_intptr_t spawnval; |
| 6697 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6698 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6699 | /* spawnv has three arguments: (mode, path, argv), where |
| 6700 | argv is a list or tuple of strings. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6701 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6702 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6703 | if (PyList_Check(argv)) { |
| 6704 | argc = PyList_Size(argv); |
| 6705 | getitem = PyList_GetItem; |
| 6706 | } |
| 6707 | else if (PyTuple_Check(argv)) { |
| 6708 | argc = PyTuple_Size(argv); |
| 6709 | getitem = PyTuple_GetItem; |
| 6710 | } |
| 6711 | else { |
| 6712 | PyErr_SetString(PyExc_TypeError, |
| 6713 | "spawnv() arg 2 must be a tuple or list"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6714 | return NULL; |
| 6715 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6716 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6717 | argvlist = PyMem_NEW(char *, argc+1); |
| 6718 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6719 | return PyErr_NoMemory(); |
| 6720 | } |
| 6721 | for (i = 0; i < argc; i++) { |
| 6722 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6723 | &argvlist[i])) { |
| 6724 | free_string_array(argvlist, i); |
| 6725 | PyErr_SetString( |
| 6726 | PyExc_TypeError, |
| 6727 | "spawnv() arg 2 must contain only strings"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6728 | return NULL; |
| 6729 | } |
| 6730 | } |
| 6731 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6732 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6733 | if (mode == _OLD_P_OVERLAY) |
| 6734 | mode = _P_OVERLAY; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6735 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6736 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6737 | spawnval = _spawnv(mode, path_char, argvlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6738 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6739 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6740 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6741 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6742 | if (spawnval == -1) |
| 6743 | return posix_error(); |
| 6744 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6745 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6746 | } |
| 6747 | |
| 6748 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6749 | /*[clinic input] |
| 6750 | os.spawnve |
| 6751 | |
| 6752 | mode: int |
| 6753 | Mode of process creation. |
| 6754 | path: FSConverter |
| 6755 | Path of executable file. |
| 6756 | argv: object |
| 6757 | Tuple or list of strings. |
| 6758 | env: object |
| 6759 | Dictionary of strings mapping to strings. |
| 6760 | / |
| 6761 | |
| 6762 | Execute the program specified by path in a new process. |
| 6763 | [clinic start generated code]*/ |
| 6764 | |
| 6765 | PyDoc_STRVAR(os_spawnve__doc__, |
| 6766 | "spawnve($module, mode, path, argv, env, /)\n" |
| 6767 | "--\n" |
| 6768 | "\n" |
| 6769 | "Execute the program specified by path in a new process.\n" |
| 6770 | "\n" |
| 6771 | " mode\n" |
| 6772 | " Mode of process creation.\n" |
| 6773 | " path\n" |
| 6774 | " Path of executable file.\n" |
| 6775 | " argv\n" |
| 6776 | " Tuple or list of strings.\n" |
| 6777 | " env\n" |
| 6778 | " Dictionary of strings mapping to strings."); |
| 6779 | |
| 6780 | #define OS_SPAWNVE_METHODDEF \ |
| 6781 | {"spawnve", (PyCFunction)os_spawnve, METH_VARARGS, os_spawnve__doc__}, |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6782 | |
| 6783 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6784 | os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv, PyObject *env); |
| 6785 | |
| 6786 | static PyObject * |
| 6787 | os_spawnve(PyModuleDef *module, PyObject *args) |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6788 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6789 | PyObject *return_value = NULL; |
| 6790 | int mode; |
| 6791 | PyObject *path = NULL; |
| 6792 | PyObject *argv; |
| 6793 | PyObject *env; |
| 6794 | |
| 6795 | if (!PyArg_ParseTuple(args, |
| 6796 | "iO&OO:spawnve", |
| 6797 | &mode, PyUnicode_FSConverter, &path, &argv, &env)) |
| 6798 | goto exit; |
| 6799 | return_value = os_spawnve_impl(module, mode, path, argv, env); |
| 6800 | |
| 6801 | exit: |
| 6802 | /* Cleanup for path */ |
| 6803 | Py_XDECREF(path); |
| 6804 | |
| 6805 | return return_value; |
| 6806 | } |
| 6807 | |
| 6808 | static PyObject * |
| 6809 | os_spawnve_impl(PyModuleDef *module, int mode, PyObject *path, PyObject *argv, PyObject *env) |
| 6810 | /*[clinic end generated code: output=6f7df38473f63c7c input=02362fd937963f8f]*/ |
| 6811 | { |
| 6812 | char *path_char; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6813 | char **argvlist; |
| 6814 | char **envlist; |
| 6815 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 6816 | Py_ssize_t argc, i, envc; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6817 | Py_intptr_t spawnval; |
| 6818 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
| 6819 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6820 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6821 | /* spawnve has four arguments: (mode, path, argv, env), where |
| 6822 | argv is a list or tuple of strings and env is a dictionary |
| 6823 | like posix.environ. */ |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6824 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6825 | path_char = PyBytes_AsString(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6826 | if (PyList_Check(argv)) { |
| 6827 | argc = PyList_Size(argv); |
| 6828 | getitem = PyList_GetItem; |
| 6829 | } |
| 6830 | else if (PyTuple_Check(argv)) { |
| 6831 | argc = PyTuple_Size(argv); |
| 6832 | getitem = PyTuple_GetItem; |
| 6833 | } |
| 6834 | else { |
| 6835 | PyErr_SetString(PyExc_TypeError, |
| 6836 | "spawnve() arg 2 must be a tuple or list"); |
| 6837 | goto fail_0; |
| 6838 | } |
| 6839 | if (!PyMapping_Check(env)) { |
| 6840 | PyErr_SetString(PyExc_TypeError, |
| 6841 | "spawnve() arg 3 must be a mapping object"); |
| 6842 | goto fail_0; |
| 6843 | } |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6844 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6845 | argvlist = PyMem_NEW(char *, argc+1); |
| 6846 | if (argvlist == NULL) { |
| 6847 | PyErr_NoMemory(); |
| 6848 | goto fail_0; |
| 6849 | } |
| 6850 | for (i = 0; i < argc; i++) { |
| 6851 | if (!fsconvert_strdup((*getitem)(argv, i), |
| 6852 | &argvlist[i])) |
| 6853 | { |
| 6854 | lastarg = i; |
| 6855 | goto fail_1; |
| 6856 | } |
| 6857 | } |
| 6858 | lastarg = argc; |
| 6859 | argvlist[argc] = NULL; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6860 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6861 | envlist = parse_envlist(env, &envc); |
| 6862 | if (envlist == NULL) |
| 6863 | goto fail_1; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6864 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6865 | if (mode == _OLD_P_OVERLAY) |
| 6866 | mode = _P_OVERLAY; |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6867 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6868 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6869 | spawnval = _spawnve(mode, path_char, argvlist, envlist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6870 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6871 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6872 | if (spawnval == -1) |
| 6873 | (void) posix_error(); |
| 6874 | else |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6875 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6876 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6877 | while (--envc >= 0) |
| 6878 | PyMem_DEL(envlist[envc]); |
| 6879 | PyMem_DEL(envlist); |
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6880 | fail_1: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6881 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6882 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6883 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6884 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6885 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6886 | #endif /* HAVE_SPAWNV */ |
| 6887 | |
| 6888 | |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6889 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6890 | /*[clinic input] |
| 6891 | os.fork1 |
| 6892 | |
| 6893 | Fork a child process with a single multiplexed (i.e., not bound) thread. |
| 6894 | |
| 6895 | Return 0 to child process and PID of child to parent process. |
| 6896 | [clinic start generated code]*/ |
| 6897 | |
| 6898 | PyDoc_STRVAR(os_fork1__doc__, |
| 6899 | "fork1($module, /)\n" |
| 6900 | "--\n" |
| 6901 | "\n" |
| 6902 | "Fork a child process with a single multiplexed (i.e., not bound) thread.\n" |
| 6903 | "\n" |
| 6904 | "Return 0 to child process and PID of child to parent process."); |
| 6905 | |
| 6906 | #define OS_FORK1_METHODDEF \ |
| 6907 | {"fork1", (PyCFunction)os_fork1, METH_NOARGS, os_fork1__doc__}, |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6908 | |
| 6909 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6910 | os_fork1_impl(PyModuleDef *module); |
| 6911 | |
| 6912 | static PyObject * |
| 6913 | os_fork1(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 6914 | { |
| 6915 | return os_fork1_impl(module); |
| 6916 | } |
| 6917 | |
| 6918 | static PyObject * |
| 6919 | os_fork1_impl(PyModuleDef *module) |
| 6920 | /*[clinic end generated code: output=fa04088d6bc02efa input=12db02167893926e]*/ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6921 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6922 | pid_t pid; |
| 6923 | int result = 0; |
| 6924 | _PyImport_AcquireLock(); |
| 6925 | pid = fork1(); |
| 6926 | if (pid == 0) { |
| 6927 | /* child: this clobbers and resets the import lock. */ |
| 6928 | PyOS_AfterFork(); |
| 6929 | } else { |
| 6930 | /* parent: release the import lock. */ |
| 6931 | result = _PyImport_ReleaseLock(); |
| 6932 | } |
| 6933 | if (pid == -1) |
| 6934 | return posix_error(); |
| 6935 | if (result < 0) { |
| 6936 | /* Don't clobber the OSError if the fork failed. */ |
| 6937 | PyErr_SetString(PyExc_RuntimeError, |
| 6938 | "not holding the import lock"); |
| 6939 | return NULL; |
| 6940 | } |
| 6941 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6942 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6943 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6944 | |
| 6945 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6946 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6947 | /*[clinic input] |
| 6948 | os.fork |
| 6949 | |
| 6950 | Fork a child process. |
| 6951 | |
| 6952 | Return 0 to child process and PID of child to parent process. |
| 6953 | [clinic start generated code]*/ |
| 6954 | |
| 6955 | PyDoc_STRVAR(os_fork__doc__, |
| 6956 | "fork($module, /)\n" |
| 6957 | "--\n" |
| 6958 | "\n" |
| 6959 | "Fork a child process.\n" |
| 6960 | "\n" |
| 6961 | "Return 0 to child process and PID of child to parent process."); |
| 6962 | |
| 6963 | #define OS_FORK_METHODDEF \ |
| 6964 | {"fork", (PyCFunction)os_fork, METH_NOARGS, os_fork__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6965 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 6966 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6967 | os_fork_impl(PyModuleDef *module); |
| 6968 | |
| 6969 | static PyObject * |
| 6970 | os_fork(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 6971 | { |
| 6972 | return os_fork_impl(module); |
| 6973 | } |
| 6974 | |
| 6975 | static PyObject * |
| 6976 | os_fork_impl(PyModuleDef *module) |
| 6977 | /*[clinic end generated code: output=b3c8e6bdc11eedc6 input=13c956413110eeaa]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6978 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6979 | pid_t pid; |
| 6980 | int result = 0; |
| 6981 | _PyImport_AcquireLock(); |
| 6982 | pid = fork(); |
| 6983 | if (pid == 0) { |
| 6984 | /* child: this clobbers and resets the import lock. */ |
| 6985 | PyOS_AfterFork(); |
| 6986 | } else { |
| 6987 | /* parent: release the import lock. */ |
| 6988 | result = _PyImport_ReleaseLock(); |
| 6989 | } |
| 6990 | if (pid == -1) |
| 6991 | return posix_error(); |
| 6992 | if (result < 0) { |
| 6993 | /* Don't clobber the OSError if the fork failed. */ |
| 6994 | PyErr_SetString(PyExc_RuntimeError, |
| 6995 | "not holding the import lock"); |
| 6996 | return NULL; |
| 6997 | } |
| 6998 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6999 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7000 | #endif /* HAVE_FORK */ |
| 7001 | |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7002 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7003 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 7004 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7005 | /*[clinic input] |
| 7006 | os.sched_get_priority_max |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 7007 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7008 | policy: int |
| 7009 | |
| 7010 | Get the maximum scheduling priority for policy. |
| 7011 | [clinic start generated code]*/ |
| 7012 | |
| 7013 | PyDoc_STRVAR(os_sched_get_priority_max__doc__, |
| 7014 | "sched_get_priority_max($module, /, policy)\n" |
| 7015 | "--\n" |
| 7016 | "\n" |
| 7017 | "Get the maximum scheduling priority for policy."); |
| 7018 | |
| 7019 | #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF \ |
| 7020 | {"sched_get_priority_max", (PyCFunction)os_sched_get_priority_max, METH_VARARGS|METH_KEYWORDS, os_sched_get_priority_max__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7021 | |
| 7022 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7023 | os_sched_get_priority_max_impl(PyModuleDef *module, int policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7024 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7025 | static PyObject * |
| 7026 | os_sched_get_priority_max(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 7027 | { |
| 7028 | PyObject *return_value = NULL; |
| 7029 | static char *_keywords[] = {"policy", NULL}; |
| 7030 | int policy; |
| 7031 | |
| 7032 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7033 | "i:sched_get_priority_max", _keywords, |
| 7034 | &policy)) |
| 7035 | goto exit; |
| 7036 | return_value = os_sched_get_priority_max_impl(module, policy); |
| 7037 | |
| 7038 | exit: |
| 7039 | return return_value; |
| 7040 | } |
| 7041 | |
| 7042 | static PyObject * |
| 7043 | os_sched_get_priority_max_impl(PyModuleDef *module, int policy) |
| 7044 | /*[clinic end generated code: output=a580a52f25238c1f input=2097b7998eca6874]*/ |
| 7045 | { |
| 7046 | int max; |
| 7047 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7048 | max = sched_get_priority_max(policy); |
| 7049 | if (max < 0) |
| 7050 | return posix_error(); |
| 7051 | return PyLong_FromLong(max); |
| 7052 | } |
| 7053 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7054 | |
| 7055 | /*[clinic input] |
| 7056 | os.sched_get_priority_min |
| 7057 | |
| 7058 | policy: int |
| 7059 | |
| 7060 | Get the minimum scheduling priority for policy. |
| 7061 | [clinic start generated code]*/ |
| 7062 | |
| 7063 | PyDoc_STRVAR(os_sched_get_priority_min__doc__, |
| 7064 | "sched_get_priority_min($module, /, policy)\n" |
| 7065 | "--\n" |
| 7066 | "\n" |
| 7067 | "Get the minimum scheduling priority for policy."); |
| 7068 | |
| 7069 | #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF \ |
| 7070 | {"sched_get_priority_min", (PyCFunction)os_sched_get_priority_min, METH_VARARGS|METH_KEYWORDS, os_sched_get_priority_min__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7071 | |
| 7072 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7073 | os_sched_get_priority_min_impl(PyModuleDef *module, int policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7074 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7075 | static PyObject * |
| 7076 | os_sched_get_priority_min(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 7077 | { |
| 7078 | PyObject *return_value = NULL; |
| 7079 | static char *_keywords[] = {"policy", NULL}; |
| 7080 | int policy; |
| 7081 | |
| 7082 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7083 | "i:sched_get_priority_min", _keywords, |
| 7084 | &policy)) |
| 7085 | goto exit; |
| 7086 | return_value = os_sched_get_priority_min_impl(module, policy); |
| 7087 | |
| 7088 | exit: |
| 7089 | return return_value; |
| 7090 | } |
| 7091 | |
| 7092 | static PyObject * |
| 7093 | os_sched_get_priority_min_impl(PyModuleDef *module, int policy) |
| 7094 | /*[clinic end generated code: output=bad8ba10e7d0e977 input=21bc8fa0d70983bf]*/ |
| 7095 | { |
| 7096 | int min = sched_get_priority_min(policy); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7097 | if (min < 0) |
| 7098 | return posix_error(); |
| 7099 | return PyLong_FromLong(min); |
| 7100 | } |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 7101 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
| 7102 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7104 | #ifdef HAVE_SCHED_SETSCHEDULER |
| 7105 | /*[clinic input] |
| 7106 | os.sched_getscheduler |
| 7107 | pid: pid_t |
| 7108 | / |
| 7109 | |
| 7110 | Get the scheduling policy for the process identifiedy by pid. |
| 7111 | |
| 7112 | Passing 0 for pid returns the scheduling policy for the calling process. |
| 7113 | [clinic start generated code]*/ |
| 7114 | |
| 7115 | PyDoc_STRVAR(os_sched_getscheduler__doc__, |
| 7116 | "sched_getscheduler($module, pid, /)\n" |
| 7117 | "--\n" |
| 7118 | "\n" |
| 7119 | "Get the scheduling policy for the process identifiedy by pid.\n" |
| 7120 | "\n" |
| 7121 | "Passing 0 for pid returns the scheduling policy for the calling process."); |
| 7122 | |
| 7123 | #define OS_SCHED_GETSCHEDULER_METHODDEF \ |
| 7124 | {"sched_getscheduler", (PyCFunction)os_sched_getscheduler, METH_VARARGS, os_sched_getscheduler__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7125 | |
| 7126 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7127 | os_sched_getscheduler_impl(PyModuleDef *module, pid_t pid); |
| 7128 | |
| 7129 | static PyObject * |
| 7130 | os_sched_getscheduler(PyModuleDef *module, PyObject *args) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7131 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7132 | PyObject *return_value = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7133 | pid_t pid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7134 | |
| 7135 | if (!PyArg_ParseTuple(args, |
| 7136 | "" _Py_PARSE_PID ":sched_getscheduler", |
| 7137 | &pid)) |
| 7138 | goto exit; |
| 7139 | return_value = os_sched_getscheduler_impl(module, pid); |
| 7140 | |
| 7141 | exit: |
| 7142 | return return_value; |
| 7143 | } |
| 7144 | |
| 7145 | static PyObject * |
| 7146 | os_sched_getscheduler_impl(PyModuleDef *module, pid_t pid) |
| 7147 | /*[clinic end generated code: output=e0d6244207b1d828 input=5f14cfd1f189e1a0]*/ |
| 7148 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7149 | int policy; |
| 7150 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7151 | policy = sched_getscheduler(pid); |
| 7152 | if (policy < 0) |
| 7153 | return posix_error(); |
| 7154 | return PyLong_FromLong(policy); |
| 7155 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7156 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7157 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7158 | |
| 7159 | #if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7160 | /*[clinic input] |
| 7161 | class os.sched_param "PyObject *" "&SchedParamType" |
| 7162 | |
| 7163 | @classmethod |
| 7164 | os.sched_param.__new__ |
| 7165 | |
| 7166 | sched_priority: object |
| 7167 | A scheduling parameter. |
| 7168 | |
| 7169 | Current has only one field: sched_priority"); |
| 7170 | [clinic start generated code]*/ |
| 7171 | |
| 7172 | PyDoc_STRVAR(os_sched_param__doc__, |
| 7173 | "sched_param(sched_priority)\n" |
| 7174 | "--\n" |
| 7175 | "\n" |
| 7176 | "Current has only one field: sched_priority\");\n" |
| 7177 | "\n" |
| 7178 | " sched_priority\n" |
| 7179 | " A scheduling parameter."); |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7180 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7181 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7182 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7183 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7184 | static PyObject * |
| 7185 | os_sched_param(PyTypeObject *type, PyObject *args, PyObject *kwargs) |
| 7186 | { |
| 7187 | PyObject *return_value = NULL; |
| 7188 | static char *_keywords[] = {"sched_priority", NULL}; |
| 7189 | PyObject *sched_priority; |
| 7190 | |
| 7191 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 7192 | "O:sched_param", _keywords, |
| 7193 | &sched_priority)) |
| 7194 | goto exit; |
| 7195 | return_value = os_sched_param_impl(type, sched_priority); |
| 7196 | |
| 7197 | exit: |
| 7198 | return return_value; |
| 7199 | } |
| 7200 | |
| 7201 | static PyObject * |
| 7202 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) |
| 7203 | /*[clinic end generated code: output=d3791e345f7fe573 input=73a4c22f7071fc62]*/ |
| 7204 | { |
| 7205 | PyObject *res; |
| 7206 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7207 | res = PyStructSequence_New(type); |
| 7208 | if (!res) |
| 7209 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7210 | Py_INCREF(sched_priority); |
| 7211 | PyStructSequence_SET_ITEM(res, 0, sched_priority); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7212 | return res; |
| 7213 | } |
| 7214 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7215 | |
| 7216 | static PyStructSequence_Field sched_param_fields[] = { |
| 7217 | {"sched_priority", "the scheduling priority"}, |
| 7218 | {0} |
| 7219 | }; |
| 7220 | |
| 7221 | static PyStructSequence_Desc sched_param_desc = { |
| 7222 | "sched_param", /* name */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7223 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7224 | sched_param_fields, |
| 7225 | 1 |
| 7226 | }; |
| 7227 | |
| 7228 | static int |
| 7229 | convert_sched_param(PyObject *param, struct sched_param *res) |
| 7230 | { |
| 7231 | long priority; |
| 7232 | |
| 7233 | if (Py_TYPE(param) != &SchedParamType) { |
| 7234 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
| 7235 | return 0; |
| 7236 | } |
| 7237 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); |
| 7238 | if (priority == -1 && PyErr_Occurred()) |
| 7239 | return 0; |
| 7240 | if (priority > INT_MAX || priority < INT_MIN) { |
| 7241 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); |
| 7242 | return 0; |
| 7243 | } |
| 7244 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); |
| 7245 | return 1; |
| 7246 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7247 | #endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7248 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7249 | |
| 7250 | #ifdef HAVE_SCHED_SETSCHEDULER |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7251 | /*[clinic input] |
| 7252 | os.sched_setscheduler |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7253 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7254 | pid: pid_t |
| 7255 | policy: int |
| 7256 | param: sched_param |
| 7257 | / |
| 7258 | |
| 7259 | Set the scheduling policy for the process identified by pid. |
| 7260 | |
| 7261 | If pid is 0, the calling process is changed. |
| 7262 | param is an instance of sched_param. |
| 7263 | [clinic start generated code]*/ |
| 7264 | |
| 7265 | PyDoc_STRVAR(os_sched_setscheduler__doc__, |
| 7266 | "sched_setscheduler($module, pid, policy, param, /)\n" |
| 7267 | "--\n" |
| 7268 | "\n" |
| 7269 | "Set the scheduling policy for the process identified by pid.\n" |
| 7270 | "\n" |
| 7271 | "If pid is 0, the calling process is changed.\n" |
| 7272 | "param is an instance of sched_param."); |
| 7273 | |
| 7274 | #define OS_SCHED_SETSCHEDULER_METHODDEF \ |
| 7275 | {"sched_setscheduler", (PyCFunction)os_sched_setscheduler, METH_VARARGS, os_sched_setscheduler__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7276 | |
| 7277 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7278 | os_sched_setscheduler_impl(PyModuleDef *module, pid_t pid, int policy, struct sched_param *param); |
| 7279 | |
| 7280 | static PyObject * |
| 7281 | os_sched_setscheduler(PyModuleDef *module, PyObject *args) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7282 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7283 | PyObject *return_value = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7284 | pid_t pid; |
| 7285 | int policy; |
| 7286 | struct sched_param param; |
| 7287 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7288 | if (!PyArg_ParseTuple(args, |
| 7289 | "" _Py_PARSE_PID "iO&:sched_setscheduler", |
| 7290 | &pid, &policy, convert_sched_param, ¶m)) |
| 7291 | goto exit; |
| 7292 | return_value = os_sched_setscheduler_impl(module, pid, policy, ¶m); |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 7293 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7294 | exit: |
| 7295 | return return_value; |
| 7296 | } |
| 7297 | |
| 7298 | static PyObject * |
| 7299 | os_sched_setscheduler_impl(PyModuleDef *module, pid_t pid, int policy, struct sched_param *param) |
| 7300 | /*[clinic end generated code: output=36abdb73f81c224f input=c581f9469a5327dd]*/ |
| 7301 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 7302 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 7303 | ** sched_setscheduler() returns 0 in Linux, but the previous |
| 7304 | ** scheduling policy under Solaris/Illumos, and others. |
| 7305 | ** On error, -1 is returned in all Operating Systems. |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 7306 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7307 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7308 | return posix_error(); |
| 7309 | Py_RETURN_NONE; |
| 7310 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7311 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7312 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7313 | |
| 7314 | #ifdef HAVE_SCHED_SETPARAM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7315 | /*[clinic input] |
| 7316 | os.sched_getparam |
| 7317 | pid: pid_t |
| 7318 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7319 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7320 | Returns scheduling parameters for the process identified by pid. |
| 7321 | |
| 7322 | If pid is 0, returns parameters for the calling process. |
| 7323 | Return value is an instance of sched_param. |
| 7324 | [clinic start generated code]*/ |
| 7325 | |
| 7326 | PyDoc_STRVAR(os_sched_getparam__doc__, |
| 7327 | "sched_getparam($module, pid, /)\n" |
| 7328 | "--\n" |
| 7329 | "\n" |
| 7330 | "Returns scheduling parameters for the process identified by pid.\n" |
| 7331 | "\n" |
| 7332 | "If pid is 0, returns parameters for the calling process.\n" |
| 7333 | "Return value is an instance of sched_param."); |
| 7334 | |
| 7335 | #define OS_SCHED_GETPARAM_METHODDEF \ |
| 7336 | {"sched_getparam", (PyCFunction)os_sched_getparam, METH_VARARGS, os_sched_getparam__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7337 | |
| 7338 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7339 | os_sched_getparam_impl(PyModuleDef *module, pid_t pid); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7340 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7341 | static PyObject * |
| 7342 | os_sched_getparam(PyModuleDef *module, PyObject *args) |
| 7343 | { |
| 7344 | PyObject *return_value = NULL; |
| 7345 | pid_t pid; |
| 7346 | |
| 7347 | if (!PyArg_ParseTuple(args, |
| 7348 | "" _Py_PARSE_PID ":sched_getparam", |
| 7349 | &pid)) |
| 7350 | goto exit; |
| 7351 | return_value = os_sched_getparam_impl(module, pid); |
| 7352 | |
| 7353 | exit: |
| 7354 | return return_value; |
| 7355 | } |
| 7356 | |
| 7357 | static PyObject * |
| 7358 | os_sched_getparam_impl(PyModuleDef *module, pid_t pid) |
| 7359 | /*[clinic end generated code: output=b33acc8db004a8c9 input=18a1ef9c2efae296]*/ |
| 7360 | { |
| 7361 | struct sched_param param; |
| 7362 | PyObject *result; |
| 7363 | PyObject *priority; |
| 7364 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7365 | if (sched_getparam(pid, ¶m)) |
| 7366 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7367 | result = PyStructSequence_New(&SchedParamType); |
| 7368 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7369 | return NULL; |
| 7370 | priority = PyLong_FromLong(param.sched_priority); |
| 7371 | if (!priority) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7372 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7373 | return NULL; |
| 7374 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7375 | PyStructSequence_SET_ITEM(result, 0, priority); |
| 7376 | return result; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7377 | } |
| 7378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7379 | |
| 7380 | /*[clinic input] |
| 7381 | os.sched_setparam |
| 7382 | pid: pid_t |
| 7383 | param: sched_param |
| 7384 | / |
| 7385 | |
| 7386 | Set scheduling parameters for the process identified by pid. |
| 7387 | |
| 7388 | If pid is 0, sets parameters for the calling process. |
| 7389 | param should be an instance of sched_param. |
| 7390 | [clinic start generated code]*/ |
| 7391 | |
| 7392 | PyDoc_STRVAR(os_sched_setparam__doc__, |
| 7393 | "sched_setparam($module, pid, param, /)\n" |
| 7394 | "--\n" |
| 7395 | "\n" |
| 7396 | "Set scheduling parameters for the process identified by pid.\n" |
| 7397 | "\n" |
| 7398 | "If pid is 0, sets parameters for the calling process.\n" |
| 7399 | "param should be an instance of sched_param."); |
| 7400 | |
| 7401 | #define OS_SCHED_SETPARAM_METHODDEF \ |
| 7402 | {"sched_setparam", (PyCFunction)os_sched_setparam, METH_VARARGS, os_sched_setparam__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7403 | |
| 7404 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7405 | os_sched_setparam_impl(PyModuleDef *module, pid_t pid, struct sched_param *param); |
| 7406 | |
| 7407 | static PyObject * |
| 7408 | os_sched_setparam(PyModuleDef *module, PyObject *args) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7409 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7410 | PyObject *return_value = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7411 | pid_t pid; |
| 7412 | struct sched_param param; |
| 7413 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7414 | if (!PyArg_ParseTuple(args, |
| 7415 | "" _Py_PARSE_PID "O&:sched_setparam", |
| 7416 | &pid, convert_sched_param, ¶m)) |
| 7417 | goto exit; |
| 7418 | return_value = os_sched_setparam_impl(module, pid, ¶m); |
| 7419 | |
| 7420 | exit: |
| 7421 | return return_value; |
| 7422 | } |
| 7423 | |
| 7424 | static PyObject * |
| 7425 | os_sched_setparam_impl(PyModuleDef *module, pid_t pid, struct sched_param *param) |
| 7426 | /*[clinic end generated code: output=488bdf5bcbe0d4e8 input=6b8d6dfcecdc21bd]*/ |
| 7427 | { |
| 7428 | if (sched_setparam(pid, param)) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7429 | return posix_error(); |
| 7430 | Py_RETURN_NONE; |
| 7431 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7432 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7433 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7434 | |
| 7435 | #ifdef HAVE_SCHED_RR_GET_INTERVAL |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7436 | /*[clinic input] |
| 7437 | os.sched_rr_get_interval -> double |
| 7438 | pid: pid_t |
| 7439 | / |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7440 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7441 | Return the round-robin quantum for the process identified by pid, in seconds. |
| 7442 | |
| 7443 | Value returned is a float. |
| 7444 | [clinic start generated code]*/ |
| 7445 | |
| 7446 | PyDoc_STRVAR(os_sched_rr_get_interval__doc__, |
| 7447 | "sched_rr_get_interval($module, pid, /)\n" |
| 7448 | "--\n" |
| 7449 | "\n" |
| 7450 | "Return the round-robin quantum for the process identified by pid, in seconds.\n" |
| 7451 | "\n" |
| 7452 | "Value returned is a float."); |
| 7453 | |
| 7454 | #define OS_SCHED_RR_GET_INTERVAL_METHODDEF \ |
| 7455 | {"sched_rr_get_interval", (PyCFunction)os_sched_rr_get_interval, METH_VARARGS, os_sched_rr_get_interval__doc__}, |
| 7456 | |
| 7457 | static double |
| 7458 | os_sched_rr_get_interval_impl(PyModuleDef *module, pid_t pid); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7459 | |
| 7460 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7461 | os_sched_rr_get_interval(PyModuleDef *module, PyObject *args) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7462 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7463 | PyObject *return_value = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7464 | pid_t pid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7465 | double _return_value; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7466 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7467 | if (!PyArg_ParseTuple(args, |
| 7468 | "" _Py_PARSE_PID ":sched_rr_get_interval", |
| 7469 | &pid)) |
| 7470 | goto exit; |
| 7471 | _return_value = os_sched_rr_get_interval_impl(module, pid); |
| 7472 | if ((_return_value == -1.0) && PyErr_Occurred()) |
| 7473 | goto exit; |
| 7474 | return_value = PyFloat_FromDouble(_return_value); |
| 7475 | |
| 7476 | exit: |
| 7477 | return return_value; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7478 | } |
| 7479 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7480 | static double |
| 7481 | os_sched_rr_get_interval_impl(PyModuleDef *module, pid_t pid) |
| 7482 | /*[clinic end generated code: output=5b3b8d1f27fb2c0a input=2a973da15cca6fae]*/ |
| 7483 | { |
| 7484 | struct timespec interval; |
| 7485 | if (sched_rr_get_interval(pid, &interval)) { |
| 7486 | posix_error(); |
| 7487 | return -1.0; |
| 7488 | } |
| 7489 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
| 7490 | } |
| 7491 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 7492 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7493 | |
| 7494 | /*[clinic input] |
| 7495 | os.sched_yield |
| 7496 | |
| 7497 | Voluntarily relinquish the CPU. |
| 7498 | [clinic start generated code]*/ |
| 7499 | |
| 7500 | PyDoc_STRVAR(os_sched_yield__doc__, |
| 7501 | "sched_yield($module, /)\n" |
| 7502 | "--\n" |
| 7503 | "\n" |
| 7504 | "Voluntarily relinquish the CPU."); |
| 7505 | |
| 7506 | #define OS_SCHED_YIELD_METHODDEF \ |
| 7507 | {"sched_yield", (PyCFunction)os_sched_yield, METH_NOARGS, os_sched_yield__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7508 | |
| 7509 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7510 | os_sched_yield_impl(PyModuleDef *module); |
| 7511 | |
| 7512 | static PyObject * |
| 7513 | os_sched_yield(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 7514 | { |
| 7515 | return os_sched_yield_impl(module); |
| 7516 | } |
| 7517 | |
| 7518 | static PyObject * |
| 7519 | os_sched_yield_impl(PyModuleDef *module) |
| 7520 | /*[clinic end generated code: output=9d2e5f29f1370324 input=e54d6f98189391d4]*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7521 | { |
| 7522 | if (sched_yield()) |
| 7523 | return posix_error(); |
| 7524 | Py_RETURN_NONE; |
| 7525 | } |
| 7526 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 7527 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7528 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
| 7529 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7530 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7531 | /*[clinic input] |
| 7532 | os.sched_setaffinity |
| 7533 | pid: pid_t |
| 7534 | mask : object |
| 7535 | / |
| 7536 | |
| 7537 | Set the CPU affinity of the process identified by pid to mask. |
| 7538 | |
| 7539 | mask should be an iterable of integers identifying CPUs. |
| 7540 | [clinic start generated code]*/ |
| 7541 | |
| 7542 | PyDoc_STRVAR(os_sched_setaffinity__doc__, |
| 7543 | "sched_setaffinity($module, pid, mask, /)\n" |
| 7544 | "--\n" |
| 7545 | "\n" |
| 7546 | "Set the CPU affinity of the process identified by pid to mask.\n" |
| 7547 | "\n" |
| 7548 | "mask should be an iterable of integers identifying CPUs."); |
| 7549 | |
| 7550 | #define OS_SCHED_SETAFFINITY_METHODDEF \ |
| 7551 | {"sched_setaffinity", (PyCFunction)os_sched_setaffinity, METH_VARARGS, os_sched_setaffinity__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7552 | |
| 7553 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7554 | os_sched_setaffinity_impl(PyModuleDef *module, pid_t pid, PyObject *mask); |
| 7555 | |
| 7556 | static PyObject * |
| 7557 | os_sched_setaffinity(PyModuleDef *module, PyObject *args) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7558 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7559 | PyObject *return_value = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7560 | pid_t pid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7561 | PyObject *mask; |
| 7562 | |
| 7563 | if (!PyArg_ParseTuple(args, |
| 7564 | "" _Py_PARSE_PID "O:sched_setaffinity", |
| 7565 | &pid, &mask)) |
| 7566 | goto exit; |
| 7567 | return_value = os_sched_setaffinity_impl(module, pid, mask); |
| 7568 | |
| 7569 | exit: |
| 7570 | return return_value; |
| 7571 | } |
| 7572 | |
| 7573 | static PyObject * |
| 7574 | os_sched_setaffinity_impl(PyModuleDef *module, pid_t pid, PyObject *mask) |
| 7575 | /*[clinic end generated code: output=5199929738130196 input=a0791a597c7085ba]*/ |
| 7576 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7577 | int ncpus; |
| 7578 | size_t setsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7579 | cpu_set_t *cpu_set = NULL; |
| 7580 | PyObject *iterator = NULL, *item; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7581 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7582 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7583 | if (iterator == NULL) |
| 7584 | return NULL; |
| 7585 | |
| 7586 | ncpus = NCPUS_START; |
| 7587 | setsize = CPU_ALLOC_SIZE(ncpus); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7588 | cpu_set = CPU_ALLOC(ncpus); |
| 7589 | if (cpu_set == NULL) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7590 | PyErr_NoMemory(); |
| 7591 | goto error; |
| 7592 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7593 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7594 | |
| 7595 | while ((item = PyIter_Next(iterator))) { |
| 7596 | long cpu; |
| 7597 | if (!PyLong_Check(item)) { |
| 7598 | PyErr_Format(PyExc_TypeError, |
| 7599 | "expected an iterator of ints, " |
| 7600 | "but iterator yielded %R", |
| 7601 | Py_TYPE(item)); |
| 7602 | Py_DECREF(item); |
| 7603 | goto error; |
| 7604 | } |
| 7605 | cpu = PyLong_AsLong(item); |
| 7606 | Py_DECREF(item); |
| 7607 | if (cpu < 0) { |
| 7608 | if (!PyErr_Occurred()) |
| 7609 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); |
| 7610 | goto error; |
| 7611 | } |
| 7612 | if (cpu > INT_MAX - 1) { |
| 7613 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); |
| 7614 | goto error; |
| 7615 | } |
| 7616 | if (cpu >= ncpus) { |
| 7617 | /* Grow CPU mask to fit the CPU number */ |
| 7618 | int newncpus = ncpus; |
| 7619 | cpu_set_t *newmask; |
| 7620 | size_t newsetsize; |
| 7621 | while (newncpus <= cpu) { |
| 7622 | if (newncpus > INT_MAX / 2) |
| 7623 | newncpus = cpu + 1; |
| 7624 | else |
| 7625 | newncpus = newncpus * 2; |
| 7626 | } |
| 7627 | newmask = CPU_ALLOC(newncpus); |
| 7628 | if (newmask == NULL) { |
| 7629 | PyErr_NoMemory(); |
| 7630 | goto error; |
| 7631 | } |
| 7632 | newsetsize = CPU_ALLOC_SIZE(newncpus); |
| 7633 | CPU_ZERO_S(newsetsize, newmask); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7634 | memcpy(newmask, cpu_set, setsize); |
| 7635 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7636 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7637 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7638 | ncpus = newncpus; |
| 7639 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7640 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7641 | } |
| 7642 | Py_CLEAR(iterator); |
| 7643 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7644 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7645 | posix_error(); |
| 7646 | goto error; |
| 7647 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7648 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7649 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7650 | |
| 7651 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7652 | if (cpu_set) |
| 7653 | CPU_FREE(cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7654 | Py_XDECREF(iterator); |
| 7655 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7656 | } |
| 7657 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7658 | |
| 7659 | /*[clinic input] |
| 7660 | os.sched_getaffinity |
| 7661 | pid: pid_t |
| 7662 | / |
| 7663 | |
| 7664 | Return the affinity of the process identified by pid. |
| 7665 | |
| 7666 | The affinity is returned as a set of CPU identifiers. |
| 7667 | [clinic start generated code]*/ |
| 7668 | |
| 7669 | PyDoc_STRVAR(os_sched_getaffinity__doc__, |
| 7670 | "sched_getaffinity($module, pid, /)\n" |
| 7671 | "--\n" |
| 7672 | "\n" |
| 7673 | "Return the affinity of the process identified by pid.\n" |
| 7674 | "\n" |
| 7675 | "The affinity is returned as a set of CPU identifiers."); |
| 7676 | |
| 7677 | #define OS_SCHED_GETAFFINITY_METHODDEF \ |
| 7678 | {"sched_getaffinity", (PyCFunction)os_sched_getaffinity, METH_VARARGS, os_sched_getaffinity__doc__}, |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7679 | |
| 7680 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7681 | os_sched_getaffinity_impl(PyModuleDef *module, pid_t pid); |
| 7682 | |
| 7683 | static PyObject * |
| 7684 | os_sched_getaffinity(PyModuleDef *module, PyObject *args) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7685 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7686 | PyObject *return_value = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7687 | pid_t pid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7688 | |
| 7689 | if (!PyArg_ParseTuple(args, |
| 7690 | "" _Py_PARSE_PID ":sched_getaffinity", |
| 7691 | &pid)) |
| 7692 | goto exit; |
| 7693 | return_value = os_sched_getaffinity_impl(module, pid); |
| 7694 | |
| 7695 | exit: |
| 7696 | return return_value; |
| 7697 | } |
| 7698 | |
| 7699 | static PyObject * |
| 7700 | os_sched_getaffinity_impl(PyModuleDef *module, pid_t pid) |
| 7701 | /*[clinic end generated code: output=7b273b0fca9830f0 input=eaf161936874b8a1]*/ |
| 7702 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7703 | int cpu, ncpus, count; |
| 7704 | size_t setsize; |
| 7705 | cpu_set_t *mask = NULL; |
| 7706 | PyObject *res = NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7707 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7708 | ncpus = NCPUS_START; |
| 7709 | while (1) { |
| 7710 | setsize = CPU_ALLOC_SIZE(ncpus); |
| 7711 | mask = CPU_ALLOC(ncpus); |
| 7712 | if (mask == NULL) |
| 7713 | return PyErr_NoMemory(); |
| 7714 | if (sched_getaffinity(pid, setsize, mask) == 0) |
| 7715 | break; |
| 7716 | CPU_FREE(mask); |
| 7717 | if (errno != EINVAL) |
| 7718 | return posix_error(); |
| 7719 | if (ncpus > INT_MAX / 2) { |
| 7720 | PyErr_SetString(PyExc_OverflowError, "could not allocate " |
| 7721 | "a large enough CPU set"); |
| 7722 | return NULL; |
| 7723 | } |
| 7724 | ncpus = ncpus * 2; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7725 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 7726 | |
| 7727 | res = PySet_New(NULL); |
| 7728 | if (res == NULL) |
| 7729 | goto error; |
| 7730 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { |
| 7731 | if (CPU_ISSET_S(cpu, setsize, mask)) { |
| 7732 | PyObject *cpu_num = PyLong_FromLong(cpu); |
| 7733 | --count; |
| 7734 | if (cpu_num == NULL) |
| 7735 | goto error; |
| 7736 | if (PySet_Add(res, cpu_num)) { |
| 7737 | Py_DECREF(cpu_num); |
| 7738 | goto error; |
| 7739 | } |
| 7740 | Py_DECREF(cpu_num); |
| 7741 | } |
| 7742 | } |
| 7743 | CPU_FREE(mask); |
| 7744 | return res; |
| 7745 | |
| 7746 | error: |
| 7747 | if (mask) |
| 7748 | CPU_FREE(mask); |
| 7749 | Py_XDECREF(res); |
| 7750 | return NULL; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7751 | } |
| 7752 | |
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 7753 | #endif /* HAVE_SCHED_SETAFFINITY */ |
| 7754 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 7755 | #endif /* HAVE_SCHED_H */ |
| 7756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7757 | #ifndef OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 7758 | #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 7759 | #endif /* OS_SCHED_GET_PRIORITY_MAX_METHODDEF */ |
| 7760 | |
| 7761 | #ifndef OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 7762 | #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 7763 | #endif /* OS_SCHED_GET_PRIORITY_MIN_METHODDEF */ |
| 7764 | |
| 7765 | #ifndef OS_SCHED_GETSCHEDULER_METHODDEF |
| 7766 | #define OS_SCHED_GETSCHEDULER_METHODDEF |
| 7767 | #endif /* OS_SCHED_GETSCHEDULER_METHODDEF */ |
| 7768 | |
| 7769 | #ifndef OS_SCHED_SETSCHEDULER_METHODDEF |
| 7770 | #define OS_SCHED_SETSCHEDULER_METHODDEF |
| 7771 | #endif /* OS_SCHED_SETSCHEDULER_METHODDEF */ |
| 7772 | |
| 7773 | #ifndef OS_SCHED_GETPARAM_METHODDEF |
| 7774 | #define OS_SCHED_GETPARAM_METHODDEF |
| 7775 | #endif /* OS_SCHED_GETPARAM_METHODDEF */ |
| 7776 | |
| 7777 | #ifndef OS_SCHED_SETPARAM_METHODDEF |
| 7778 | #define OS_SCHED_SETPARAM_METHODDEF |
| 7779 | #endif /* OS_SCHED_SETPARAM_METHODDEF */ |
| 7780 | |
| 7781 | #ifndef OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 7782 | #define OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 7783 | #endif /* OS_SCHED_RR_GET_INTERVAL_METHODDEF */ |
| 7784 | |
| 7785 | #ifndef OS_SCHED_YIELD_METHODDEF |
| 7786 | #define OS_SCHED_YIELD_METHODDEF |
| 7787 | #endif /* OS_SCHED_YIELD_METHODDEF */ |
| 7788 | |
| 7789 | #ifndef OS_SCHED_SETAFFINITY_METHODDEF |
| 7790 | #define OS_SCHED_SETAFFINITY_METHODDEF |
| 7791 | #endif /* OS_SCHED_SETAFFINITY_METHODDEF */ |
| 7792 | |
| 7793 | #ifndef OS_SCHED_GETAFFINITY_METHODDEF |
| 7794 | #define OS_SCHED_GETAFFINITY_METHODDEF |
| 7795 | #endif /* OS_SCHED_GETAFFINITY_METHODDEF */ |
| 7796 | |
| 7797 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7798 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 7799 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
| 7800 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7801 | #define DEV_PTY_FILE "/dev/ptc" |
| 7802 | #define HAVE_DEV_PTMX |
| 7803 | #else |
| 7804 | #define DEV_PTY_FILE "/dev/ptmx" |
| 7805 | #endif |
| 7806 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7807 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7808 | #ifdef HAVE_PTY_H |
| 7809 | #include <pty.h> |
| 7810 | #else |
| 7811 | #ifdef HAVE_LIBUTIL_H |
| 7812 | #include <libutil.h> |
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 7813 | #else |
| 7814 | #ifdef HAVE_UTIL_H |
| 7815 | #include <util.h> |
| 7816 | #endif /* HAVE_UTIL_H */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7817 | #endif /* HAVE_LIBUTIL_H */ |
| 7818 | #endif /* HAVE_PTY_H */ |
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 7819 | #ifdef HAVE_STROPTS_H |
| 7820 | #include <stropts.h> |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7821 | #endif |
| 7822 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7823 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7824 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7825 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7826 | /*[clinic input] |
| 7827 | os.openpty |
| 7828 | |
| 7829 | Open a pseudo-terminal. |
| 7830 | |
| 7831 | Return a tuple of (master_fd, slave_fd) containing open file descriptors |
| 7832 | for both the master and slave ends. |
| 7833 | [clinic start generated code]*/ |
| 7834 | |
| 7835 | PyDoc_STRVAR(os_openpty__doc__, |
| 7836 | "openpty($module, /)\n" |
| 7837 | "--\n" |
| 7838 | "\n" |
| 7839 | "Open a pseudo-terminal.\n" |
| 7840 | "\n" |
| 7841 | "Return a tuple of (master_fd, slave_fd) containing open file descriptors\n" |
| 7842 | "for both the master and slave ends."); |
| 7843 | |
| 7844 | #define OS_OPENPTY_METHODDEF \ |
| 7845 | {"openpty", (PyCFunction)os_openpty, METH_NOARGS, os_openpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7846 | |
| 7847 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7848 | os_openpty_impl(PyModuleDef *module); |
| 7849 | |
| 7850 | static PyObject * |
| 7851 | os_openpty(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 7852 | { |
| 7853 | return os_openpty_impl(module); |
| 7854 | } |
| 7855 | |
| 7856 | static PyObject * |
| 7857 | os_openpty_impl(PyModuleDef *module) |
| 7858 | /*[clinic end generated code: output=b12d3c1735468464 input=f3d99fd99e762907]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7859 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7860 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7861 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7862 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7863 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7864 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7865 | PyOS_sighandler_t sig_saved; |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7866 | #ifdef sun |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7867 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7868 | #endif |
| 7869 | #endif |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7870 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7871 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7872 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7873 | goto posix_error; |
| 7874 | |
| 7875 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 7876 | goto error; |
| 7877 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) |
| 7878 | goto error; |
| 7879 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7880 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7881 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
| 7882 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7883 | goto posix_error; |
| 7884 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 7885 | goto error; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7886 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7887 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7888 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 7889 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7890 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7891 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 7892 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7893 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7894 | goto posix_error; |
| 7895 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7896 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7897 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7898 | /* change permission of slave */ |
| 7899 | if (grantpt(master_fd) < 0) { |
| 7900 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7901 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7902 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7903 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7904 | /* unlock slave */ |
| 7905 | if (unlockpt(master_fd) < 0) { |
| 7906 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7907 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7908 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7909 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7910 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7911 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7912 | slave_name = ptsname(master_fd); /* get name of slave */ |
| 7913 | if (slave_name == NULL) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7914 | goto posix_error; |
| 7915 | |
| 7916 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 7917 | if (slave_fd == -1) |
| 7918 | goto error; |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 7919 | |
| 7920 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) |
| 7921 | goto posix_error; |
| 7922 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 7923 | #if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7924 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
| 7925 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 7926 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7927 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 7928 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7929 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 7930 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7931 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7932 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 7933 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 7934 | posix_error: |
| 7935 | posix_error(); |
| 7936 | error: |
| 7937 | if (master_fd != -1) |
| 7938 | close(master_fd); |
| 7939 | if (slave_fd != -1) |
| 7940 | close(slave_fd); |
| 7941 | return NULL; |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7942 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 7943 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7944 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7945 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7946 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7947 | /*[clinic input] |
| 7948 | os.forkpty |
| 7949 | |
| 7950 | Fork a new process with a new pseudo-terminal as controlling tty. |
| 7951 | |
| 7952 | Returns a tuple of (pid, master_fd). |
| 7953 | Like fork(), return pid of 0 to the child process, |
| 7954 | and pid of child to the parent process. |
| 7955 | To both, return fd of newly opened pseudo-terminal. |
| 7956 | [clinic start generated code]*/ |
| 7957 | |
| 7958 | PyDoc_STRVAR(os_forkpty__doc__, |
| 7959 | "forkpty($module, /)\n" |
| 7960 | "--\n" |
| 7961 | "\n" |
| 7962 | "Fork a new process with a new pseudo-terminal as controlling tty.\n" |
| 7963 | "\n" |
| 7964 | "Returns a tuple of (pid, master_fd).\n" |
| 7965 | "Like fork(), return pid of 0 to the child process,\n" |
| 7966 | "and pid of child to the parent process.\n" |
| 7967 | "To both, return fd of newly opened pseudo-terminal."); |
| 7968 | |
| 7969 | #define OS_FORKPTY_METHODDEF \ |
| 7970 | {"forkpty", (PyCFunction)os_forkpty, METH_NOARGS, os_forkpty__doc__}, |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7971 | |
| 7972 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7973 | os_forkpty_impl(PyModuleDef *module); |
| 7974 | |
| 7975 | static PyObject * |
| 7976 | os_forkpty(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 7977 | { |
| 7978 | return os_forkpty_impl(module); |
| 7979 | } |
| 7980 | |
| 7981 | static PyObject * |
| 7982 | os_forkpty_impl(PyModuleDef *module) |
| 7983 | /*[clinic end generated code: output=d4f82958d2ed5cad input=f1f7f4bae3966010]*/ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 7984 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7985 | int master_fd = -1, result = 0; |
| 7986 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7987 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7988 | _PyImport_AcquireLock(); |
| 7989 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
| 7990 | if (pid == 0) { |
| 7991 | /* child: this clobbers and resets the import lock. */ |
| 7992 | PyOS_AfterFork(); |
| 7993 | } else { |
| 7994 | /* parent: release the import lock. */ |
| 7995 | result = _PyImport_ReleaseLock(); |
| 7996 | } |
| 7997 | if (pid == -1) |
| 7998 | return posix_error(); |
| 7999 | if (result < 0) { |
| 8000 | /* Don't clobber the OSError if the fork failed. */ |
| 8001 | PyErr_SetString(PyExc_RuntimeError, |
| 8002 | "not holding the import lock"); |
| 8003 | return NULL; |
| 8004 | } |
| 8005 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 8006 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8007 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8008 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8009 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8010 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8011 | /*[clinic input] |
| 8012 | os.getegid |
| 8013 | |
| 8014 | Return the current process's effective group id. |
| 8015 | [clinic start generated code]*/ |
| 8016 | |
| 8017 | PyDoc_STRVAR(os_getegid__doc__, |
| 8018 | "getegid($module, /)\n" |
| 8019 | "--\n" |
| 8020 | "\n" |
| 8021 | "Return the current process\'s effective group id."); |
| 8022 | |
| 8023 | #define OS_GETEGID_METHODDEF \ |
| 8024 | {"getegid", (PyCFunction)os_getegid, METH_NOARGS, os_getegid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8025 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8026 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8027 | os_getegid_impl(PyModuleDef *module); |
| 8028 | |
| 8029 | static PyObject * |
| 8030 | os_getegid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8031 | { |
| 8032 | return os_getegid_impl(module); |
| 8033 | } |
| 8034 | |
| 8035 | static PyObject * |
| 8036 | os_getegid_impl(PyModuleDef *module) |
| 8037 | /*[clinic end generated code: output=fd12c346fa41cccb input=1596f79ad1107d5d]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8038 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8039 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8040 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8041 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8042 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8043 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8044 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8045 | /*[clinic input] |
| 8046 | os.geteuid |
| 8047 | |
| 8048 | Return the current process's effective user id. |
| 8049 | [clinic start generated code]*/ |
| 8050 | |
| 8051 | PyDoc_STRVAR(os_geteuid__doc__, |
| 8052 | "geteuid($module, /)\n" |
| 8053 | "--\n" |
| 8054 | "\n" |
| 8055 | "Return the current process\'s effective user id."); |
| 8056 | |
| 8057 | #define OS_GETEUID_METHODDEF \ |
| 8058 | {"geteuid", (PyCFunction)os_geteuid, METH_NOARGS, os_geteuid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8059 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8060 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8061 | os_geteuid_impl(PyModuleDef *module); |
| 8062 | |
| 8063 | static PyObject * |
| 8064 | os_geteuid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8065 | { |
| 8066 | return os_geteuid_impl(module); |
| 8067 | } |
| 8068 | |
| 8069 | static PyObject * |
| 8070 | os_geteuid_impl(PyModuleDef *module) |
| 8071 | /*[clinic end generated code: output=03d98e07f4bc03d4 input=4644c662d3bd9f19]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8072 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8073 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8074 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8075 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8076 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8077 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8078 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8079 | /*[clinic input] |
| 8080 | os.getgid |
| 8081 | |
| 8082 | Return the current process's group id. |
| 8083 | [clinic start generated code]*/ |
| 8084 | |
| 8085 | PyDoc_STRVAR(os_getgid__doc__, |
| 8086 | "getgid($module, /)\n" |
| 8087 | "--\n" |
| 8088 | "\n" |
| 8089 | "Return the current process\'s group id."); |
| 8090 | |
| 8091 | #define OS_GETGID_METHODDEF \ |
| 8092 | {"getgid", (PyCFunction)os_getgid, METH_NOARGS, os_getgid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8093 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8094 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8095 | os_getgid_impl(PyModuleDef *module); |
| 8096 | |
| 8097 | static PyObject * |
| 8098 | os_getgid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8099 | { |
| 8100 | return os_getgid_impl(module); |
| 8101 | } |
| 8102 | |
| 8103 | static PyObject * |
| 8104 | os_getgid_impl(PyModuleDef *module) |
| 8105 | /*[clinic end generated code: output=07b0356121b8098d input=58796344cd87c0f6]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8106 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8107 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8108 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8109 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8110 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8111 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8112 | /*[clinic input] |
| 8113 | os.getpid |
| 8114 | |
| 8115 | Return the current process id. |
| 8116 | [clinic start generated code]*/ |
| 8117 | |
| 8118 | PyDoc_STRVAR(os_getpid__doc__, |
| 8119 | "getpid($module, /)\n" |
| 8120 | "--\n" |
| 8121 | "\n" |
| 8122 | "Return the current process id."); |
| 8123 | |
| 8124 | #define OS_GETPID_METHODDEF \ |
| 8125 | {"getpid", (PyCFunction)os_getpid, METH_NOARGS, os_getpid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8126 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8127 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8128 | os_getpid_impl(PyModuleDef *module); |
| 8129 | |
| 8130 | static PyObject * |
| 8131 | os_getpid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8132 | { |
| 8133 | return os_getpid_impl(module); |
| 8134 | } |
| 8135 | |
| 8136 | static PyObject * |
| 8137 | os_getpid_impl(PyModuleDef *module) |
| 8138 | /*[clinic end generated code: output=d63a01a3cebc573d input=5a9a00f0ab68aa00]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8139 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8140 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8141 | } |
| 8142 | |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8143 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8144 | |
| 8145 | /* AC 3.5: funny apple logic below */ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8146 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
| 8147 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ |
| 8148 | Returns a list of groups to which a user belongs.\n\n\ |
| 8149 | user: username to lookup\n\ |
| 8150 | group: base group id of the user"); |
| 8151 | |
| 8152 | static PyObject * |
| 8153 | posix_getgrouplist(PyObject *self, PyObject *args) |
| 8154 | { |
| 8155 | #ifdef NGROUPS_MAX |
| 8156 | #define MAX_GROUPS NGROUPS_MAX |
| 8157 | #else |
| 8158 | /* defined to be 16 on Solaris7, so this should be a small number */ |
| 8159 | #define MAX_GROUPS 64 |
| 8160 | #endif |
| 8161 | |
| 8162 | const char *user; |
| 8163 | int i, ngroups; |
| 8164 | PyObject *list; |
| 8165 | #ifdef __APPLE__ |
| 8166 | int *groups, basegid; |
| 8167 | #else |
| 8168 | gid_t *groups, basegid; |
| 8169 | #endif |
| 8170 | ngroups = MAX_GROUPS; |
| 8171 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8172 | #ifdef __APPLE__ |
| 8173 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8174 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8175 | #else |
| 8176 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, |
| 8177 | _Py_Gid_Converter, &basegid)) |
| 8178 | return NULL; |
| 8179 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8180 | |
| 8181 | #ifdef __APPLE__ |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 8182 | groups = PyMem_New(int, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8183 | #else |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 8184 | groups = PyMem_New(gid_t, ngroups); |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8185 | #endif |
| 8186 | if (groups == NULL) |
| 8187 | return PyErr_NoMemory(); |
| 8188 | |
| 8189 | if (getgrouplist(user, basegid, groups, &ngroups) == -1) { |
| 8190 | PyMem_Del(groups); |
| 8191 | return posix_error(); |
| 8192 | } |
| 8193 | |
| 8194 | list = PyList_New(ngroups); |
| 8195 | if (list == NULL) { |
| 8196 | PyMem_Del(groups); |
| 8197 | return NULL; |
| 8198 | } |
| 8199 | |
| 8200 | for (i = 0; i < ngroups; i++) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8201 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8202 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8203 | #else |
| 8204 | PyObject *o = _PyLong_FromGid(groups[i]); |
| 8205 | #endif |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 8206 | if (o == NULL) { |
| 8207 | Py_DECREF(list); |
| 8208 | PyMem_Del(groups); |
| 8209 | return NULL; |
| 8210 | } |
| 8211 | PyList_SET_ITEM(list, i, o); |
| 8212 | } |
| 8213 | |
| 8214 | PyMem_Del(groups); |
| 8215 | |
| 8216 | return list; |
| 8217 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8218 | #endif /* HAVE_GETGROUPLIST */ |
| 8219 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8220 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8221 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8222 | /*[clinic input] |
| 8223 | os.getgroups |
| 8224 | |
| 8225 | Return list of supplemental group IDs for the process. |
| 8226 | [clinic start generated code]*/ |
| 8227 | |
| 8228 | PyDoc_STRVAR(os_getgroups__doc__, |
| 8229 | "getgroups($module, /)\n" |
| 8230 | "--\n" |
| 8231 | "\n" |
| 8232 | "Return list of supplemental group IDs for the process."); |
| 8233 | |
| 8234 | #define OS_GETGROUPS_METHODDEF \ |
| 8235 | {"getgroups", (PyCFunction)os_getgroups, METH_NOARGS, os_getgroups__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8236 | |
| 8237 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8238 | os_getgroups_impl(PyModuleDef *module); |
| 8239 | |
| 8240 | static PyObject * |
| 8241 | os_getgroups(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8242 | { |
| 8243 | return os_getgroups_impl(module); |
| 8244 | } |
| 8245 | |
| 8246 | static PyObject * |
| 8247 | os_getgroups_impl(PyModuleDef *module) |
| 8248 | /*[clinic end generated code: output=d9a3559b2e6f4ab8 input=d3f109412e6a155c]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8249 | { |
| 8250 | PyObject *result = NULL; |
| 8251 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8252 | #ifdef NGROUPS_MAX |
| 8253 | #define MAX_GROUPS NGROUPS_MAX |
| 8254 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8255 | /* 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] | 8256 | #define MAX_GROUPS 64 |
| 8257 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8258 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 8259 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8260 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 8261 | * This is a helper variable to store the intermediate result when |
| 8262 | * that happens. |
| 8263 | * |
| 8264 | * To keep the code readable the OSX behaviour is unconditional, |
| 8265 | * according to the POSIX spec this should be safe on all unix-y |
| 8266 | * systems. |
| 8267 | */ |
| 8268 | gid_t* alt_grouplist = grouplist; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8269 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8270 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 8271 | #ifdef __APPLE__ |
| 8272 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if |
| 8273 | * there are more groups than can fit in grouplist. Therefore, on OS X |
| 8274 | * always first call getgroups with length 0 to get the actual number |
| 8275 | * of groups. |
| 8276 | */ |
| 8277 | n = getgroups(0, NULL); |
| 8278 | if (n < 0) { |
| 8279 | return posix_error(); |
| 8280 | } else if (n <= MAX_GROUPS) { |
| 8281 | /* groups will fit in existing array */ |
| 8282 | alt_grouplist = grouplist; |
| 8283 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 8284 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 8285 | if (alt_grouplist == NULL) { |
| 8286 | errno = EINVAL; |
| 8287 | return posix_error(); |
| 8288 | } |
| 8289 | } |
| 8290 | |
| 8291 | n = getgroups(n, alt_grouplist); |
| 8292 | if (n == -1) { |
| 8293 | if (alt_grouplist != grouplist) { |
| 8294 | PyMem_Free(alt_grouplist); |
| 8295 | } |
| 8296 | return posix_error(); |
| 8297 | } |
| 8298 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8299 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 8300 | if (n < 0) { |
| 8301 | if (errno == EINVAL) { |
| 8302 | n = getgroups(0, NULL); |
| 8303 | if (n == -1) { |
| 8304 | return posix_error(); |
| 8305 | } |
| 8306 | if (n == 0) { |
| 8307 | /* Avoid malloc(0) */ |
| 8308 | alt_grouplist = grouplist; |
| 8309 | } else { |
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 8310 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 8311 | if (alt_grouplist == NULL) { |
| 8312 | errno = EINVAL; |
| 8313 | return posix_error(); |
| 8314 | } |
| 8315 | n = getgroups(n, alt_grouplist); |
| 8316 | if (n == -1) { |
| 8317 | PyMem_Free(alt_grouplist); |
| 8318 | return posix_error(); |
| 8319 | } |
| 8320 | } |
| 8321 | } else { |
| 8322 | return posix_error(); |
| 8323 | } |
| 8324 | } |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 8325 | #endif |
| 8326 | |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 8327 | result = PyList_New(n); |
| 8328 | if (result != NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8329 | int i; |
| 8330 | for (i = 0; i < n; ++i) { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8331 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8332 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 8333 | Py_DECREF(result); |
| 8334 | result = NULL; |
| 8335 | break; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8336 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8337 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8338 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
| 8341 | if (alt_grouplist != grouplist) { |
| 8342 | PyMem_Free(alt_grouplist); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8343 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 8344 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8345 | return result; |
| 8346 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8347 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 8348 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8349 | #ifdef HAVE_INITGROUPS |
| 8350 | PyDoc_STRVAR(posix_initgroups__doc__, |
| 8351 | "initgroups(username, gid) -> None\n\n\ |
| 8352 | Call the system initgroups() to initialize the group access list with all of\n\ |
| 8353 | the groups of which the specified username is a member, plus the specified\n\ |
| 8354 | group id."); |
| 8355 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8356 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8357 | static PyObject * |
| 8358 | posix_initgroups(PyObject *self, PyObject *args) |
| 8359 | { |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 8360 | PyObject *oname; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8361 | char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 8362 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8363 | #ifdef __APPLE__ |
| 8364 | int gid; |
| 8365 | #else |
| 8366 | gid_t gid; |
| 8367 | #endif |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8368 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8369 | #ifdef __APPLE__ |
| 8370 | if (!PyArg_ParseTuple(args, "O&i:initgroups", |
| 8371 | PyUnicode_FSConverter, &oname, |
| 8372 | &gid)) |
| 8373 | #else |
| 8374 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", |
| 8375 | PyUnicode_FSConverter, &oname, |
| 8376 | _Py_Gid_Converter, &gid)) |
| 8377 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8378 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 8379 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8380 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8381 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 8382 | Py_DECREF(oname); |
| 8383 | if (res == -1) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8384 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8385 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8386 | Py_INCREF(Py_None); |
| 8387 | return Py_None; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8388 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8389 | #endif /* HAVE_INITGROUPS */ |
| 8390 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 8391 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8392 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8393 | /*[clinic input] |
| 8394 | os.getpgid |
| 8395 | |
| 8396 | pid: pid_t |
| 8397 | |
| 8398 | Call the system call getpgid(), and return the result. |
| 8399 | [clinic start generated code]*/ |
| 8400 | |
| 8401 | PyDoc_STRVAR(os_getpgid__doc__, |
| 8402 | "getpgid($module, /, pid)\n" |
| 8403 | "--\n" |
| 8404 | "\n" |
| 8405 | "Call the system call getpgid(), and return the result."); |
| 8406 | |
| 8407 | #define OS_GETPGID_METHODDEF \ |
| 8408 | {"getpgid", (PyCFunction)os_getpgid, METH_VARARGS|METH_KEYWORDS, os_getpgid__doc__}, |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8409 | |
| 8410 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8411 | os_getpgid_impl(PyModuleDef *module, pid_t pid); |
| 8412 | |
| 8413 | static PyObject * |
| 8414 | os_getpgid(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8415 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8416 | PyObject *return_value = NULL; |
| 8417 | static char *_keywords[] = {"pid", NULL}; |
| 8418 | pid_t pid; |
| 8419 | |
| 8420 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 8421 | "" _Py_PARSE_PID ":getpgid", _keywords, |
| 8422 | &pid)) |
| 8423 | goto exit; |
| 8424 | return_value = os_getpgid_impl(module, pid); |
| 8425 | |
| 8426 | exit: |
| 8427 | return return_value; |
| 8428 | } |
| 8429 | |
| 8430 | static PyObject * |
| 8431 | os_getpgid_impl(PyModuleDef *module, pid_t pid) |
| 8432 | /*[clinic end generated code: output=3db4ed686179160d input=39d710ae3baaf1c7]*/ |
| 8433 | { |
| 8434 | pid_t pgid = getpgid(pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8435 | if (pgid < 0) |
| 8436 | return posix_error(); |
| 8437 | return PyLong_FromPid(pgid); |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 8438 | } |
| 8439 | #endif /* HAVE_GETPGID */ |
| 8440 | |
| 8441 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8442 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8443 | /*[clinic input] |
| 8444 | os.getpgrp |
| 8445 | |
| 8446 | Return the current process group id. |
| 8447 | [clinic start generated code]*/ |
| 8448 | |
| 8449 | PyDoc_STRVAR(os_getpgrp__doc__, |
| 8450 | "getpgrp($module, /)\n" |
| 8451 | "--\n" |
| 8452 | "\n" |
| 8453 | "Return the current process group id."); |
| 8454 | |
| 8455 | #define OS_GETPGRP_METHODDEF \ |
| 8456 | {"getpgrp", (PyCFunction)os_getpgrp, METH_NOARGS, os_getpgrp__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8457 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8458 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8459 | os_getpgrp_impl(PyModuleDef *module); |
| 8460 | |
| 8461 | static PyObject * |
| 8462 | os_getpgrp(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8463 | { |
| 8464 | return os_getpgrp_impl(module); |
| 8465 | } |
| 8466 | |
| 8467 | static PyObject * |
| 8468 | os_getpgrp_impl(PyModuleDef *module) |
| 8469 | /*[clinic end generated code: output=3b0d3663ea054277 input=6846fb2bb9a3705e]*/ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 8470 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8471 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8472 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8473 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8474 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8475 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 8476 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8477 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 8478 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8479 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8480 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8481 | /*[clinic input] |
| 8482 | os.setpgrp |
| 8483 | |
| 8484 | Make the current process the leader of its process group. |
| 8485 | [clinic start generated code]*/ |
| 8486 | |
| 8487 | PyDoc_STRVAR(os_setpgrp__doc__, |
| 8488 | "setpgrp($module, /)\n" |
| 8489 | "--\n" |
| 8490 | "\n" |
| 8491 | "Make the current process the leader of its process group."); |
| 8492 | |
| 8493 | #define OS_SETPGRP_METHODDEF \ |
| 8494 | {"setpgrp", (PyCFunction)os_setpgrp, METH_NOARGS, os_setpgrp__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8495 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8496 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8497 | os_setpgrp_impl(PyModuleDef *module); |
| 8498 | |
| 8499 | static PyObject * |
| 8500 | os_setpgrp(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8501 | { |
| 8502 | return os_setpgrp_impl(module); |
| 8503 | } |
| 8504 | |
| 8505 | static PyObject * |
| 8506 | os_setpgrp_impl(PyModuleDef *module) |
| 8507 | /*[clinic end generated code: output=8fbb0ee29ef6fb2d input=1f0619fcb5731e7e]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8508 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 8509 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8510 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 8511 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8512 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 8513 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8514 | return posix_error(); |
| 8515 | Py_INCREF(Py_None); |
| 8516 | return Py_None; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8517 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8518 | #endif /* HAVE_SETPGRP */ |
| 8519 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8520 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 8521 | |
| 8522 | #ifdef MS_WINDOWS |
| 8523 | #include <tlhelp32.h> |
| 8524 | |
| 8525 | static PyObject* |
| 8526 | win32_getppid() |
| 8527 | { |
| 8528 | HANDLE snapshot; |
| 8529 | pid_t mypid; |
| 8530 | PyObject* result = NULL; |
| 8531 | BOOL have_record; |
| 8532 | PROCESSENTRY32 pe; |
| 8533 | |
| 8534 | mypid = getpid(); /* This function never fails */ |
| 8535 | |
| 8536 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); |
| 8537 | if (snapshot == INVALID_HANDLE_VALUE) |
| 8538 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 8539 | |
| 8540 | pe.dwSize = sizeof(pe); |
| 8541 | have_record = Process32First(snapshot, &pe); |
| 8542 | while (have_record) { |
| 8543 | if (mypid == (pid_t)pe.th32ProcessID) { |
| 8544 | /* We could cache the ulong value in a static variable. */ |
| 8545 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); |
| 8546 | break; |
| 8547 | } |
| 8548 | |
| 8549 | have_record = Process32Next(snapshot, &pe); |
| 8550 | } |
| 8551 | |
| 8552 | /* If our loop exits and our pid was not found (result will be NULL) |
| 8553 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an |
| 8554 | * error anyway, so let's raise it. */ |
| 8555 | if (!result) |
| 8556 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 8557 | |
| 8558 | CloseHandle(snapshot); |
| 8559 | |
| 8560 | return result; |
| 8561 | } |
| 8562 | #endif /*MS_WINDOWS*/ |
| 8563 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8564 | |
| 8565 | /*[clinic input] |
| 8566 | os.getppid |
| 8567 | |
| 8568 | Return the parent's process id. |
| 8569 | |
| 8570 | If the parent process has already exited, Windows machines will still |
| 8571 | return its id; others systems will return the id of the 'init' process (1). |
| 8572 | [clinic start generated code]*/ |
| 8573 | |
| 8574 | PyDoc_STRVAR(os_getppid__doc__, |
| 8575 | "getppid($module, /)\n" |
| 8576 | "--\n" |
| 8577 | "\n" |
| 8578 | "Return the parent\'s process id.\n" |
| 8579 | "\n" |
| 8580 | "If the parent process has already exited, Windows machines will still\n" |
| 8581 | "return its id; others systems will return the id of the \'init\' process (1)."); |
| 8582 | |
| 8583 | #define OS_GETPPID_METHODDEF \ |
| 8584 | {"getppid", (PyCFunction)os_getppid, METH_NOARGS, os_getppid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8585 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8586 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8587 | os_getppid_impl(PyModuleDef *module); |
| 8588 | |
| 8589 | static PyObject * |
| 8590 | os_getppid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8591 | { |
| 8592 | return os_getppid_impl(module); |
| 8593 | } |
| 8594 | |
| 8595 | static PyObject * |
| 8596 | os_getppid_impl(PyModuleDef *module) |
| 8597 | /*[clinic end generated code: output=9ff3b387781edf3a input=e637cb87539c030e]*/ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8598 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 8599 | #ifdef MS_WINDOWS |
| 8600 | return win32_getppid(); |
| 8601 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8602 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8603 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 8604 | } |
| 8605 | #endif /* HAVE_GETPPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 8606 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8607 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8608 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8609 | /*[clinic input] |
| 8610 | os.getlogin |
| 8611 | |
| 8612 | Return the actual login name. |
| 8613 | [clinic start generated code]*/ |
| 8614 | |
| 8615 | PyDoc_STRVAR(os_getlogin__doc__, |
| 8616 | "getlogin($module, /)\n" |
| 8617 | "--\n" |
| 8618 | "\n" |
| 8619 | "Return the actual login name."); |
| 8620 | |
| 8621 | #define OS_GETLOGIN_METHODDEF \ |
| 8622 | {"getlogin", (PyCFunction)os_getlogin, METH_NOARGS, os_getlogin__doc__}, |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8623 | |
| 8624 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8625 | os_getlogin_impl(PyModuleDef *module); |
| 8626 | |
| 8627 | static PyObject * |
| 8628 | os_getlogin(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8629 | { |
| 8630 | return os_getlogin_impl(module); |
| 8631 | } |
| 8632 | |
| 8633 | static PyObject * |
| 8634 | os_getlogin_impl(PyModuleDef *module) |
| 8635 | /*[clinic end generated code: output=ab6211dab104cbb2 input=2a21ab1e917163df]*/ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8636 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8637 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8638 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 8639 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 8640 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 8641 | |
| 8642 | if (GetUserNameW(user_name, &num_chars)) { |
| 8643 | /* num_chars is the number of unicode chars plus null terminator */ |
| 8644 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8645 | } |
| 8646 | else |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 8647 | result = PyErr_SetFromWindowsErr(GetLastError()); |
| 8648 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8649 | char *name; |
| 8650 | int old_errno = errno; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8651 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8652 | errno = 0; |
| 8653 | name = getlogin(); |
| 8654 | if (name == NULL) { |
| 8655 | if (errno) |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 8656 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8657 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 8658 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8659 | } |
| 8660 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 8661 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8662 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 8663 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8664 | return result; |
| 8665 | } |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 8666 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 8667 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8668 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 8669 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8670 | /*[clinic input] |
| 8671 | os.getuid |
| 8672 | |
| 8673 | Return the current process's user id. |
| 8674 | [clinic start generated code]*/ |
| 8675 | |
| 8676 | PyDoc_STRVAR(os_getuid__doc__, |
| 8677 | "getuid($module, /)\n" |
| 8678 | "--\n" |
| 8679 | "\n" |
| 8680 | "Return the current process\'s user id."); |
| 8681 | |
| 8682 | #define OS_GETUID_METHODDEF \ |
| 8683 | {"getuid", (PyCFunction)os_getuid, METH_NOARGS, os_getuid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8684 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8685 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8686 | os_getuid_impl(PyModuleDef *module); |
| 8687 | |
| 8688 | static PyObject * |
| 8689 | os_getuid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 8690 | { |
| 8691 | return os_getuid_impl(module); |
| 8692 | } |
| 8693 | |
| 8694 | static PyObject * |
| 8695 | os_getuid_impl(PyModuleDef *module) |
| 8696 | /*[clinic end generated code: output=77e0dcf2e37d1e89 input=b53c8b35f110a516]*/ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8697 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 8698 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8699 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8700 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 8701 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8702 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8703 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8704 | #define HAVE_KILL |
| 8705 | #endif /* MS_WINDOWS */ |
| 8706 | |
| 8707 | #ifdef HAVE_KILL |
| 8708 | /*[clinic input] |
| 8709 | os.kill |
| 8710 | |
| 8711 | pid: pid_t |
| 8712 | signal: Py_ssize_t |
| 8713 | / |
| 8714 | |
| 8715 | Kill a process with a signal. |
| 8716 | [clinic start generated code]*/ |
| 8717 | |
| 8718 | PyDoc_STRVAR(os_kill__doc__, |
| 8719 | "kill($module, pid, signal, /)\n" |
| 8720 | "--\n" |
| 8721 | "\n" |
| 8722 | "Kill a process with a signal."); |
| 8723 | |
| 8724 | #define OS_KILL_METHODDEF \ |
| 8725 | {"kill", (PyCFunction)os_kill, METH_VARARGS, os_kill__doc__}, |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8726 | |
| 8727 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8728 | os_kill_impl(PyModuleDef *module, pid_t pid, Py_ssize_t signal); |
| 8729 | |
| 8730 | static PyObject * |
| 8731 | os_kill(PyModuleDef *module, PyObject *args) |
| 8732 | { |
| 8733 | PyObject *return_value = NULL; |
| 8734 | pid_t pid; |
| 8735 | Py_ssize_t signal; |
| 8736 | |
| 8737 | if (!PyArg_ParseTuple(args, |
| 8738 | "" _Py_PARSE_PID "n:kill", |
| 8739 | &pid, &signal)) |
| 8740 | goto exit; |
| 8741 | return_value = os_kill_impl(module, pid, signal); |
| 8742 | |
| 8743 | exit: |
| 8744 | return return_value; |
| 8745 | } |
| 8746 | |
| 8747 | static PyObject * |
| 8748 | os_kill_impl(PyModuleDef *module, pid_t pid, Py_ssize_t signal) |
| 8749 | /*[clinic end generated code: output=2f5c77920ed575e6 input=61a36b86ca275ab9]*/ |
| 8750 | #ifndef MS_WINDOWS |
| 8751 | { |
| 8752 | if (kill(pid, (int)signal) == -1) |
| 8753 | return posix_error(); |
| 8754 | Py_RETURN_NONE; |
| 8755 | } |
| 8756 | #else /* !MS_WINDOWS */ |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8757 | { |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 8758 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8759 | DWORD sig = (DWORD)signal; |
| 8760 | DWORD err; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8761 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8762 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8763 | /* Console processes which share a common console can be sent CTRL+C or |
| 8764 | CTRL+BREAK events, provided they handle said events. */ |
| 8765 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 8766 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8767 | err = GetLastError(); |
| 8768 | PyErr_SetFromWindowsErr(err); |
| 8769 | } |
| 8770 | else |
| 8771 | Py_RETURN_NONE; |
| 8772 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8773 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8774 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
| 8775 | attempt to open and terminate the process. */ |
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 8776 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8777 | if (handle == NULL) { |
| 8778 | err = GetLastError(); |
| 8779 | return PyErr_SetFromWindowsErr(err); |
| 8780 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8781 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8782 | if (TerminateProcess(handle, sig) == 0) { |
| 8783 | err = GetLastError(); |
| 8784 | result = PyErr_SetFromWindowsErr(err); |
| 8785 | } else { |
| 8786 | Py_INCREF(Py_None); |
| 8787 | result = Py_None; |
| 8788 | } |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8789 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | CloseHandle(handle); |
| 8791 | return result; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8792 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8793 | #endif /* !MS_WINDOWS */ |
| 8794 | #endif /* HAVE_KILL */ |
| 8795 | |
| 8796 | |
| 8797 | #ifdef HAVE_KILLPG |
| 8798 | /*[clinic input] |
| 8799 | os.killpg |
| 8800 | |
| 8801 | pgid: pid_t |
| 8802 | signal: int |
| 8803 | / |
| 8804 | |
| 8805 | Kill a process group with a signal. |
| 8806 | [clinic start generated code]*/ |
| 8807 | |
| 8808 | PyDoc_STRVAR(os_killpg__doc__, |
| 8809 | "killpg($module, pgid, signal, /)\n" |
| 8810 | "--\n" |
| 8811 | "\n" |
| 8812 | "Kill a process group with a signal."); |
| 8813 | |
| 8814 | #define OS_KILLPG_METHODDEF \ |
| 8815 | {"killpg", (PyCFunction)os_killpg, METH_VARARGS, os_killpg__doc__}, |
| 8816 | |
| 8817 | static PyObject * |
| 8818 | os_killpg_impl(PyModuleDef *module, pid_t pgid, int signal); |
| 8819 | |
| 8820 | static PyObject * |
| 8821 | os_killpg(PyModuleDef *module, PyObject *args) |
| 8822 | { |
| 8823 | PyObject *return_value = NULL; |
| 8824 | pid_t pgid; |
| 8825 | int signal; |
| 8826 | |
| 8827 | if (!PyArg_ParseTuple(args, |
| 8828 | "" _Py_PARSE_PID "i:killpg", |
| 8829 | &pgid, &signal)) |
| 8830 | goto exit; |
| 8831 | return_value = os_killpg_impl(module, pgid, signal); |
| 8832 | |
| 8833 | exit: |
| 8834 | return return_value; |
| 8835 | } |
| 8836 | |
| 8837 | static PyObject * |
| 8838 | os_killpg_impl(PyModuleDef *module, pid_t pgid, int signal) |
| 8839 | /*[clinic end generated code: output=0e05215d1c007e01 input=38b5449eb8faec19]*/ |
| 8840 | { |
| 8841 | /* XXX some man pages make the `pgid` parameter an int, others |
| 8842 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should |
| 8843 | take the same type. Moreover, pid_t is always at least as wide as |
| 8844 | int (else compilation of this module fails), which is safe. */ |
| 8845 | if (killpg(pgid, signal) == -1) |
| 8846 | return posix_error(); |
| 8847 | Py_RETURN_NONE; |
| 8848 | } |
| 8849 | #endif /* HAVE_KILLPG */ |
| 8850 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 8851 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8852 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8853 | #ifdef HAVE_SYS_LOCK_H |
| 8854 | #include <sys/lock.h> |
| 8855 | #endif |
| 8856 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8857 | /*[clinic input] |
| 8858 | os.plock |
| 8859 | op: int |
| 8860 | / |
| 8861 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8862 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8863 | [clinic start generated code]*/ |
| 8864 | |
| 8865 | PyDoc_STRVAR(os_plock__doc__, |
| 8866 | "plock($module, op, /)\n" |
| 8867 | "--\n" |
| 8868 | "\n" |
| 8869 | "Lock program segments into memory.\");"); |
| 8870 | |
| 8871 | #define OS_PLOCK_METHODDEF \ |
| 8872 | {"plock", (PyCFunction)os_plock, METH_VARARGS, os_plock__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8873 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8874 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8875 | os_plock_impl(PyModuleDef *module, int op); |
| 8876 | |
| 8877 | static PyObject * |
| 8878 | os_plock(PyModuleDef *module, PyObject *args) |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8879 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8880 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8881 | int op; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8882 | |
| 8883 | if (!PyArg_ParseTuple(args, |
| 8884 | "i:plock", |
| 8885 | &op)) |
| 8886 | goto exit; |
| 8887 | return_value = os_plock_impl(module, op); |
| 8888 | |
| 8889 | exit: |
| 8890 | return return_value; |
| 8891 | } |
| 8892 | |
| 8893 | static PyObject * |
| 8894 | os_plock_impl(PyModuleDef *module, int op) |
| 8895 | /*[clinic end generated code: output=2744fe4b6e5f4dbc input=e6e5e348e1525f60]*/ |
| 8896 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8897 | if (plock(op) == -1) |
| 8898 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8899 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8900 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8901 | #endif /* HAVE_PLOCK */ |
| 8902 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 8903 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8904 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8905 | /*[clinic input] |
| 8906 | os.setuid |
| 8907 | |
| 8908 | uid: uid_t |
| 8909 | / |
| 8910 | |
| 8911 | Set the current process's user id. |
| 8912 | [clinic start generated code]*/ |
| 8913 | |
| 8914 | PyDoc_STRVAR(os_setuid__doc__, |
| 8915 | "setuid($module, uid, /)\n" |
| 8916 | "--\n" |
| 8917 | "\n" |
| 8918 | "Set the current process\'s user id."); |
| 8919 | |
| 8920 | #define OS_SETUID_METHODDEF \ |
| 8921 | {"setuid", (PyCFunction)os_setuid, METH_VARARGS, os_setuid__doc__}, |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8922 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8923 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8924 | os_setuid_impl(PyModuleDef *module, uid_t uid); |
| 8925 | |
| 8926 | static PyObject * |
| 8927 | os_setuid(PyModuleDef *module, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 8928 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8929 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8930 | uid_t uid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8931 | |
| 8932 | if (!PyArg_ParseTuple(args, |
| 8933 | "O&:setuid", |
| 8934 | _Py_Uid_Converter, &uid)) |
| 8935 | goto exit; |
| 8936 | return_value = os_setuid_impl(module, uid); |
| 8937 | |
| 8938 | exit: |
| 8939 | return return_value; |
| 8940 | } |
| 8941 | |
| 8942 | static PyObject * |
| 8943 | os_setuid_impl(PyModuleDef *module, uid_t uid) |
| 8944 | /*[clinic end generated code: output=aea344bc22ccf400 input=c921a3285aa22256]*/ |
| 8945 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8946 | if (setuid(uid) < 0) |
| 8947 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8948 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 8949 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8950 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 8951 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8952 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8953 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8954 | /*[clinic input] |
| 8955 | os.seteuid |
| 8956 | |
| 8957 | euid: uid_t |
| 8958 | / |
| 8959 | |
| 8960 | Set the current process's effective user id. |
| 8961 | [clinic start generated code]*/ |
| 8962 | |
| 8963 | PyDoc_STRVAR(os_seteuid__doc__, |
| 8964 | "seteuid($module, euid, /)\n" |
| 8965 | "--\n" |
| 8966 | "\n" |
| 8967 | "Set the current process\'s effective user id."); |
| 8968 | |
| 8969 | #define OS_SETEUID_METHODDEF \ |
| 8970 | {"seteuid", (PyCFunction)os_seteuid, METH_VARARGS, os_seteuid__doc__}, |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 8971 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8972 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8973 | os_seteuid_impl(PyModuleDef *module, uid_t euid); |
| 8974 | |
| 8975 | static PyObject * |
| 8976 | os_seteuid(PyModuleDef *module, PyObject *args) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8977 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8978 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8979 | uid_t euid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8980 | |
| 8981 | if (!PyArg_ParseTuple(args, |
| 8982 | "O&:seteuid", |
| 8983 | _Py_Uid_Converter, &euid)) |
| 8984 | goto exit; |
| 8985 | return_value = os_seteuid_impl(module, euid); |
| 8986 | |
| 8987 | exit: |
| 8988 | return return_value; |
| 8989 | } |
| 8990 | |
| 8991 | static PyObject * |
| 8992 | os_seteuid_impl(PyModuleDef *module, uid_t euid) |
| 8993 | /*[clinic end generated code: output=6e824cce4f3b8a5d input=ba93d927e4781aa9]*/ |
| 8994 | { |
| 8995 | if (seteuid(euid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8996 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8997 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 8998 | } |
| 8999 | #endif /* HAVE_SETEUID */ |
| 9000 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9001 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9002 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9003 | /*[clinic input] |
| 9004 | os.setegid |
| 9005 | |
| 9006 | egid: gid_t |
| 9007 | / |
| 9008 | |
| 9009 | Set the current process's effective group id. |
| 9010 | [clinic start generated code]*/ |
| 9011 | |
| 9012 | PyDoc_STRVAR(os_setegid__doc__, |
| 9013 | "setegid($module, egid, /)\n" |
| 9014 | "--\n" |
| 9015 | "\n" |
| 9016 | "Set the current process\'s effective group id."); |
| 9017 | |
| 9018 | #define OS_SETEGID_METHODDEF \ |
| 9019 | {"setegid", (PyCFunction)os_setegid, METH_VARARGS, os_setegid__doc__}, |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9020 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9021 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9022 | os_setegid_impl(PyModuleDef *module, gid_t egid); |
| 9023 | |
| 9024 | static PyObject * |
| 9025 | os_setegid(PyModuleDef *module, PyObject *args) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9026 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9027 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9028 | gid_t egid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9029 | |
| 9030 | if (!PyArg_ParseTuple(args, |
| 9031 | "O&:setegid", |
| 9032 | _Py_Gid_Converter, &egid)) |
| 9033 | goto exit; |
| 9034 | return_value = os_setegid_impl(module, egid); |
| 9035 | |
| 9036 | exit: |
| 9037 | return return_value; |
| 9038 | } |
| 9039 | |
| 9040 | static PyObject * |
| 9041 | os_setegid_impl(PyModuleDef *module, gid_t egid) |
| 9042 | /*[clinic end generated code: output=80a32263a4d56a9c input=4080526d0ccd6ce3]*/ |
| 9043 | { |
| 9044 | if (setegid(egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9045 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9046 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9047 | } |
| 9048 | #endif /* HAVE_SETEGID */ |
| 9049 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9050 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9051 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9052 | /*[clinic input] |
| 9053 | os.setreuid |
| 9054 | |
| 9055 | ruid: uid_t |
| 9056 | euid: uid_t |
| 9057 | / |
| 9058 | |
| 9059 | Set the current process's real and effective user ids. |
| 9060 | [clinic start generated code]*/ |
| 9061 | |
| 9062 | PyDoc_STRVAR(os_setreuid__doc__, |
| 9063 | "setreuid($module, ruid, euid, /)\n" |
| 9064 | "--\n" |
| 9065 | "\n" |
| 9066 | "Set the current process\'s real and effective user ids."); |
| 9067 | |
| 9068 | #define OS_SETREUID_METHODDEF \ |
| 9069 | {"setreuid", (PyCFunction)os_setreuid, METH_VARARGS, os_setreuid__doc__}, |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9070 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9071 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9072 | os_setreuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid); |
| 9073 | |
| 9074 | static PyObject * |
| 9075 | os_setreuid(PyModuleDef *module, PyObject *args) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9076 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9077 | PyObject *return_value = NULL; |
| 9078 | uid_t ruid; |
| 9079 | uid_t euid; |
| 9080 | |
| 9081 | if (!PyArg_ParseTuple(args, |
| 9082 | "O&O&:setreuid", |
| 9083 | _Py_Uid_Converter, &ruid, _Py_Uid_Converter, &euid)) |
| 9084 | goto exit; |
| 9085 | return_value = os_setreuid_impl(module, ruid, euid); |
| 9086 | |
| 9087 | exit: |
| 9088 | return return_value; |
| 9089 | } |
| 9090 | |
| 9091 | static PyObject * |
| 9092 | os_setreuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid) |
| 9093 | /*[clinic end generated code: output=d7f226f943dad739 input=0ca8978de663880c]*/ |
| 9094 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9095 | if (setreuid(ruid, euid) < 0) { |
| 9096 | return posix_error(); |
| 9097 | } else { |
| 9098 | Py_INCREF(Py_None); |
| 9099 | return Py_None; |
| 9100 | } |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9101 | } |
| 9102 | #endif /* HAVE_SETREUID */ |
| 9103 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9104 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9105 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9106 | /*[clinic input] |
| 9107 | os.setregid |
| 9108 | |
| 9109 | rgid: gid_t |
| 9110 | egid: gid_t |
| 9111 | / |
| 9112 | |
| 9113 | Set the current process's real and effective group ids. |
| 9114 | [clinic start generated code]*/ |
| 9115 | |
| 9116 | PyDoc_STRVAR(os_setregid__doc__, |
| 9117 | "setregid($module, rgid, egid, /)\n" |
| 9118 | "--\n" |
| 9119 | "\n" |
| 9120 | "Set the current process\'s real and effective group ids."); |
| 9121 | |
| 9122 | #define OS_SETREGID_METHODDEF \ |
| 9123 | {"setregid", (PyCFunction)os_setregid, METH_VARARGS, os_setregid__doc__}, |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 9124 | |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9125 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9126 | os_setregid_impl(PyModuleDef *module, gid_t rgid, gid_t egid); |
| 9127 | |
| 9128 | static PyObject * |
| 9129 | os_setregid(PyModuleDef *module, PyObject *args) |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9130 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9131 | PyObject *return_value = NULL; |
| 9132 | gid_t rgid; |
| 9133 | gid_t egid; |
| 9134 | |
| 9135 | if (!PyArg_ParseTuple(args, |
| 9136 | "O&O&:setregid", |
| 9137 | _Py_Gid_Converter, &rgid, _Py_Gid_Converter, &egid)) |
| 9138 | goto exit; |
| 9139 | return_value = os_setregid_impl(module, rgid, egid); |
| 9140 | |
| 9141 | exit: |
| 9142 | return return_value; |
| 9143 | } |
| 9144 | |
| 9145 | static PyObject * |
| 9146 | os_setregid_impl(PyModuleDef *module, gid_t rgid, gid_t egid) |
| 9147 | /*[clinic end generated code: output=a82d9ab70f8e6562 input=c59499f72846db78]*/ |
| 9148 | { |
| 9149 | if (setregid(rgid, egid) < 0) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9150 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9151 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2 | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 9152 | } |
| 9153 | #endif /* HAVE_SETREGID */ |
| 9154 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9155 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9156 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9157 | /*[clinic input] |
| 9158 | os.setgid |
| 9159 | gid: gid_t |
| 9160 | / |
| 9161 | |
| 9162 | Set the current process's group id. |
| 9163 | [clinic start generated code]*/ |
| 9164 | |
| 9165 | PyDoc_STRVAR(os_setgid__doc__, |
| 9166 | "setgid($module, gid, /)\n" |
| 9167 | "--\n" |
| 9168 | "\n" |
| 9169 | "Set the current process\'s group id."); |
| 9170 | |
| 9171 | #define OS_SETGID_METHODDEF \ |
| 9172 | {"setgid", (PyCFunction)os_setgid, METH_VARARGS, os_setgid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9173 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 9174 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9175 | os_setgid_impl(PyModuleDef *module, gid_t gid); |
| 9176 | |
| 9177 | static PyObject * |
| 9178 | os_setgid(PyModuleDef *module, PyObject *args) |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 9179 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9180 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9181 | gid_t gid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9182 | |
| 9183 | if (!PyArg_ParseTuple(args, |
| 9184 | "O&:setgid", |
| 9185 | _Py_Gid_Converter, &gid)) |
| 9186 | goto exit; |
| 9187 | return_value = os_setgid_impl(module, gid); |
| 9188 | |
| 9189 | exit: |
| 9190 | return return_value; |
| 9191 | } |
| 9192 | |
| 9193 | static PyObject * |
| 9194 | os_setgid_impl(PyModuleDef *module, gid_t gid) |
| 9195 | /*[clinic end generated code: output=08287886db435f23 input=27d30c4059045dc6]*/ |
| 9196 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9197 | if (setgid(gid) < 0) |
| 9198 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9199 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 9200 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 9201 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 9202 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9203 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9204 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9205 | /*[clinic input] |
| 9206 | os.setgroups |
| 9207 | |
| 9208 | groups: object |
| 9209 | / |
| 9210 | |
| 9211 | Set the groups of the current process to list. |
| 9212 | [clinic start generated code]*/ |
| 9213 | |
| 9214 | PyDoc_STRVAR(os_setgroups__doc__, |
| 9215 | "setgroups($module, groups, /)\n" |
| 9216 | "--\n" |
| 9217 | "\n" |
| 9218 | "Set the groups of the current process to list."); |
| 9219 | |
| 9220 | #define OS_SETGROUPS_METHODDEF \ |
| 9221 | {"setgroups", (PyCFunction)os_setgroups, METH_O, os_setgroups__doc__}, |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9222 | |
| 9223 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9224 | os_setgroups(PyModuleDef *module, PyObject *groups) |
| 9225 | /*[clinic end generated code: output=0b8de65d5b3cda94 input=fa742ca3daf85a7e]*/ |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9226 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9227 | int i, len; |
| 9228 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9229 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9230 | if (!PySequence_Check(groups)) { |
| 9231 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); |
| 9232 | return NULL; |
| 9233 | } |
| 9234 | len = PySequence_Size(groups); |
| 9235 | if (len > MAX_GROUPS) { |
| 9236 | PyErr_SetString(PyExc_ValueError, "too many groups"); |
| 9237 | return NULL; |
| 9238 | } |
| 9239 | for(i = 0; i < len; i++) { |
| 9240 | PyObject *elem; |
| 9241 | elem = PySequence_GetItem(groups, i); |
| 9242 | if (!elem) |
| 9243 | return NULL; |
| 9244 | if (!PyLong_Check(elem)) { |
| 9245 | PyErr_SetString(PyExc_TypeError, |
| 9246 | "groups must be integers"); |
| 9247 | Py_DECREF(elem); |
| 9248 | return NULL; |
| 9249 | } else { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9250 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9251 | Py_DECREF(elem); |
| 9252 | return NULL; |
| 9253 | } |
| 9254 | } |
| 9255 | Py_DECREF(elem); |
| 9256 | } |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9257 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9258 | if (setgroups(len, grouplist) < 0) |
| 9259 | return posix_error(); |
| 9260 | Py_INCREF(Py_None); |
| 9261 | return Py_None; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 9262 | } |
| 9263 | #endif /* HAVE_SETGROUPS */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9264 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9265 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
| 9266 | static PyObject * |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9267 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9268 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9269 | PyObject *result; |
| 9270 | static PyObject *struct_rusage; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 9271 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9272 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9273 | if (pid == -1) |
| 9274 | return posix_error(); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9275 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9276 | if (struct_rusage == NULL) { |
| 9277 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); |
| 9278 | if (m == NULL) |
| 9279 | return NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 9280 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9281 | Py_DECREF(m); |
| 9282 | if (struct_rusage == NULL) |
| 9283 | return NULL; |
| 9284 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9285 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9286 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
| 9287 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); |
| 9288 | if (!result) |
| 9289 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9290 | |
| 9291 | #ifndef doubletime |
| 9292 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) |
| 9293 | #endif |
| 9294 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9295 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9296 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9297 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9298 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9299 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9300 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
| 9301 | SET_INT(result, 2, ru->ru_maxrss); |
| 9302 | SET_INT(result, 3, ru->ru_ixrss); |
| 9303 | SET_INT(result, 4, ru->ru_idrss); |
| 9304 | SET_INT(result, 5, ru->ru_isrss); |
| 9305 | SET_INT(result, 6, ru->ru_minflt); |
| 9306 | SET_INT(result, 7, ru->ru_majflt); |
| 9307 | SET_INT(result, 8, ru->ru_nswap); |
| 9308 | SET_INT(result, 9, ru->ru_inblock); |
| 9309 | SET_INT(result, 10, ru->ru_oublock); |
| 9310 | SET_INT(result, 11, ru->ru_msgsnd); |
| 9311 | SET_INT(result, 12, ru->ru_msgrcv); |
| 9312 | SET_INT(result, 13, ru->ru_nsignals); |
| 9313 | SET_INT(result, 14, ru->ru_nvcsw); |
| 9314 | SET_INT(result, 15, ru->ru_nivcsw); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9315 | #undef SET_INT |
| 9316 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9317 | if (PyErr_Occurred()) { |
| 9318 | Py_DECREF(result); |
| 9319 | return NULL; |
| 9320 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9321 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9322 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9323 | } |
| 9324 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ |
| 9325 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9326 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9327 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9328 | /*[clinic input] |
| 9329 | os.wait3 |
| 9330 | |
| 9331 | options: int |
| 9332 | Wait for completion of a child process. |
| 9333 | |
| 9334 | Returns a tuple of information about the child process: |
| 9335 | (pid, status, rusage) |
| 9336 | [clinic start generated code]*/ |
| 9337 | |
| 9338 | PyDoc_STRVAR(os_wait3__doc__, |
| 9339 | "wait3($module, /, options)\n" |
| 9340 | "--\n" |
| 9341 | "\n" |
| 9342 | "Wait for completion of a child process.\n" |
| 9343 | "\n" |
| 9344 | "Returns a tuple of information about the child process:\n" |
| 9345 | " (pid, status, rusage)"); |
| 9346 | |
| 9347 | #define OS_WAIT3_METHODDEF \ |
| 9348 | {"wait3", (PyCFunction)os_wait3, METH_VARARGS|METH_KEYWORDS, os_wait3__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9349 | |
| 9350 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9351 | os_wait3_impl(PyModuleDef *module, int options); |
| 9352 | |
| 9353 | static PyObject * |
| 9354 | os_wait3(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 9355 | { |
| 9356 | PyObject *return_value = NULL; |
| 9357 | static char *_keywords[] = {"options", NULL}; |
| 9358 | int options; |
| 9359 | |
| 9360 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 9361 | "i:wait3", _keywords, |
| 9362 | &options)) |
| 9363 | goto exit; |
| 9364 | return_value = os_wait3_impl(module, options); |
| 9365 | |
| 9366 | exit: |
| 9367 | return return_value; |
| 9368 | } |
| 9369 | |
| 9370 | static PyObject * |
| 9371 | os_wait3_impl(PyModuleDef *module, int options) |
| 9372 | /*[clinic end generated code: output=1f2a63b6a93cbb57 input=8ac4c56956b61710]*/ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9373 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9374 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9375 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9376 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9377 | WAIT_TYPE status; |
| 9378 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9379 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9380 | do { |
| 9381 | Py_BEGIN_ALLOW_THREADS |
| 9382 | pid = wait3(&status, options, &ru); |
| 9383 | Py_END_ALLOW_THREADS |
| 9384 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9385 | if (pid < 0) |
| 9386 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9387 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9388 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9389 | } |
| 9390 | #endif /* HAVE_WAIT3 */ |
| 9391 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9392 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9393 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9394 | /*[clinic input] |
| 9395 | |
| 9396 | os.wait4 |
| 9397 | |
| 9398 | pid: pid_t |
| 9399 | options: int |
| 9400 | |
| 9401 | Wait for completion of a specific child process. |
| 9402 | |
| 9403 | Returns a tuple of information about the child process: |
| 9404 | (pid, status, rusage) |
| 9405 | [clinic start generated code]*/ |
| 9406 | |
| 9407 | PyDoc_STRVAR(os_wait4__doc__, |
| 9408 | "wait4($module, /, pid, options)\n" |
| 9409 | "--\n" |
| 9410 | "\n" |
| 9411 | "Wait for completion of a specific child process.\n" |
| 9412 | "\n" |
| 9413 | "Returns a tuple of information about the child process:\n" |
| 9414 | " (pid, status, rusage)"); |
| 9415 | |
| 9416 | #define OS_WAIT4_METHODDEF \ |
| 9417 | {"wait4", (PyCFunction)os_wait4, METH_VARARGS|METH_KEYWORDS, os_wait4__doc__}, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9418 | |
| 9419 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9420 | os_wait4_impl(PyModuleDef *module, pid_t pid, int options); |
| 9421 | |
| 9422 | static PyObject * |
| 9423 | os_wait4(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9424 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9425 | PyObject *return_value = NULL; |
| 9426 | static char *_keywords[] = {"pid", "options", NULL}; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9427 | pid_t pid; |
| 9428 | int options; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9429 | |
| 9430 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 9431 | "" _Py_PARSE_PID "i:wait4", _keywords, |
| 9432 | &pid, &options)) |
| 9433 | goto exit; |
| 9434 | return_value = os_wait4_impl(module, pid, options); |
| 9435 | |
| 9436 | exit: |
| 9437 | return return_value; |
| 9438 | } |
| 9439 | |
| 9440 | static PyObject * |
| 9441 | os_wait4_impl(PyModuleDef *module, pid_t pid, int options) |
| 9442 | /*[clinic end generated code: output=20dfb05289d37dc6 input=d11deed0750600ba]*/ |
| 9443 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9444 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9445 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9446 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9447 | WAIT_TYPE status; |
| 9448 | WAIT_STATUS_INT(status) = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9449 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9450 | do { |
| 9451 | Py_BEGIN_ALLOW_THREADS |
| 9452 | res = wait4(pid, &status, options, &ru); |
| 9453 | Py_END_ALLOW_THREADS |
| 9454 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9455 | if (res < 0) |
| 9456 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9457 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9458 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9459 | } |
| 9460 | #endif /* HAVE_WAIT4 */ |
| 9461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9462 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9463 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9464 | /*[clinic input] |
| 9465 | os.waitid |
| 9466 | |
| 9467 | idtype: idtype_t |
| 9468 | Must be one of be P_PID, P_PGID or P_ALL. |
| 9469 | id: id_t |
| 9470 | The id to wait on. |
| 9471 | options: int |
| 9472 | Constructed from the ORing of one or more of WEXITED, WSTOPPED |
| 9473 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. |
| 9474 | / |
| 9475 | |
| 9476 | Returns the result of waiting for a process or processes. |
| 9477 | |
| 9478 | Returns either waitid_result or None if WNOHANG is specified and there are |
| 9479 | no children in a waitable state. |
| 9480 | [clinic start generated code]*/ |
| 9481 | |
| 9482 | PyDoc_STRVAR(os_waitid__doc__, |
| 9483 | "waitid($module, idtype, id, options, /)\n" |
| 9484 | "--\n" |
| 9485 | "\n" |
| 9486 | "Returns the result of waiting for a process or processes.\n" |
| 9487 | "\n" |
| 9488 | " idtype\n" |
| 9489 | " Must be one of be P_PID, P_PGID or P_ALL.\n" |
| 9490 | " id\n" |
| 9491 | " The id to wait on.\n" |
| 9492 | " options\n" |
| 9493 | " Constructed from the ORing of one or more of WEXITED, WSTOPPED\n" |
| 9494 | " or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n" |
| 9495 | "\n" |
| 9496 | "Returns either waitid_result or None if WNOHANG is specified and there are\n" |
| 9497 | "no children in a waitable state."); |
| 9498 | |
| 9499 | #define OS_WAITID_METHODDEF \ |
| 9500 | {"waitid", (PyCFunction)os_waitid, METH_VARARGS, os_waitid__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9501 | |
| 9502 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9503 | os_waitid_impl(PyModuleDef *module, idtype_t idtype, id_t id, int options); |
| 9504 | |
| 9505 | static PyObject * |
| 9506 | os_waitid(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9507 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9508 | PyObject *return_value = NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9509 | idtype_t idtype; |
| 9510 | id_t id; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9511 | int options; |
| 9512 | |
| 9513 | if (!PyArg_ParseTuple(args, |
| 9514 | "i" _Py_PARSE_PID "i:waitid", |
| 9515 | &idtype, &id, &options)) |
| 9516 | goto exit; |
| 9517 | return_value = os_waitid_impl(module, idtype, id, options); |
| 9518 | |
| 9519 | exit: |
| 9520 | return return_value; |
| 9521 | } |
| 9522 | |
| 9523 | static PyObject * |
| 9524 | os_waitid_impl(PyModuleDef *module, idtype_t idtype, id_t id, int options) |
| 9525 | /*[clinic end generated code: output=fb44bf97f01021b2 input=d8e7f76e052b7920]*/ |
| 9526 | { |
| 9527 | PyObject *result; |
| 9528 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9529 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9530 | siginfo_t si; |
| 9531 | si.si_pid = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9532 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9533 | do { |
| 9534 | Py_BEGIN_ALLOW_THREADS |
| 9535 | res = waitid(idtype, id, &si, options); |
| 9536 | Py_END_ALLOW_THREADS |
| 9537 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9538 | if (res < 0) |
| 9539 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9540 | |
| 9541 | if (si.si_pid == 0) |
| 9542 | Py_RETURN_NONE; |
| 9543 | |
| 9544 | result = PyStructSequence_New(&WaitidResultType); |
| 9545 | if (!result) |
| 9546 | return NULL; |
| 9547 | |
| 9548 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 9549 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9550 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
| 9551 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); |
| 9552 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); |
| 9553 | if (PyErr_Occurred()) { |
| 9554 | Py_DECREF(result); |
| 9555 | return NULL; |
| 9556 | } |
| 9557 | |
| 9558 | return result; |
| 9559 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9560 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9561 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9562 | |
| 9563 | #if defined(HAVE_WAITPID) |
| 9564 | /*[clinic input] |
| 9565 | os.waitpid |
| 9566 | pid: pid_t |
| 9567 | options: int |
| 9568 | / |
| 9569 | |
| 9570 | Wait for completion of a given child process. |
| 9571 | |
| 9572 | Returns a tuple of information regarding the child process: |
| 9573 | (pid, status) |
| 9574 | |
| 9575 | The options argument is ignored on Windows. |
| 9576 | [clinic start generated code]*/ |
| 9577 | |
| 9578 | PyDoc_STRVAR(os_waitpid__doc__, |
| 9579 | "waitpid($module, pid, options, /)\n" |
| 9580 | "--\n" |
| 9581 | "\n" |
| 9582 | "Wait for completion of a given child process.\n" |
| 9583 | "\n" |
| 9584 | "Returns a tuple of information regarding the child process:\n" |
| 9585 | " (pid, status)\n" |
| 9586 | "\n" |
| 9587 | "The options argument is ignored on Windows."); |
| 9588 | |
| 9589 | #define OS_WAITPID_METHODDEF \ |
| 9590 | {"waitpid", (PyCFunction)os_waitpid, METH_VARARGS, os_waitpid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9591 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 9592 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9593 | os_waitpid_impl(PyModuleDef *module, pid_t pid, int options); |
| 9594 | |
| 9595 | static PyObject * |
| 9596 | os_waitpid(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 9597 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9598 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9599 | pid_t pid; |
| 9600 | int options; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9601 | |
| 9602 | if (!PyArg_ParseTuple(args, |
| 9603 | "" _Py_PARSE_PID "i:waitpid", |
| 9604 | &pid, &options)) |
| 9605 | goto exit; |
| 9606 | return_value = os_waitpid_impl(module, pid, options); |
| 9607 | |
| 9608 | exit: |
| 9609 | return return_value; |
| 9610 | } |
| 9611 | |
| 9612 | static PyObject * |
| 9613 | os_waitpid_impl(PyModuleDef *module, pid_t pid, int options) |
| 9614 | /*[clinic end generated code: output=095a6b00af70b7ac input=0bf1666b8758fda3]*/ |
| 9615 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9616 | pid_t res; |
| 9617 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9618 | WAIT_TYPE status; |
| 9619 | WAIT_STATUS_INT(status) = 0; |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 9620 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9621 | do { |
| 9622 | Py_BEGIN_ALLOW_THREADS |
| 9623 | res = waitpid(pid, &status, options); |
| 9624 | Py_END_ALLOW_THREADS |
| 9625 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9626 | if (res < 0) |
| 9627 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9628 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9629 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 9630 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 9631 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 9632 | /* 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] | 9633 | /*[clinic input] |
| 9634 | os.waitpid |
| 9635 | pid: Py_intptr_t |
| 9636 | options: int |
| 9637 | / |
| 9638 | |
| 9639 | Wait for completion of a given process. |
| 9640 | |
| 9641 | Returns a tuple of information regarding the process: |
| 9642 | (pid, status << 8) |
| 9643 | |
| 9644 | The options argument is ignored on Windows. |
| 9645 | [clinic start generated code]*/ |
| 9646 | |
| 9647 | PyDoc_STRVAR(os_waitpid__doc__, |
| 9648 | "waitpid($module, pid, options, /)\n" |
| 9649 | "--\n" |
| 9650 | "\n" |
| 9651 | "Wait for completion of a given process.\n" |
| 9652 | "\n" |
| 9653 | "Returns a tuple of information regarding the process:\n" |
| 9654 | " (pid, status << 8)\n" |
| 9655 | "\n" |
| 9656 | "The options argument is ignored on Windows."); |
| 9657 | |
| 9658 | #define OS_WAITPID_METHODDEF \ |
| 9659 | {"waitpid", (PyCFunction)os_waitpid, METH_VARARGS, os_waitpid__doc__}, |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 9660 | |
| 9661 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9662 | os_waitpid_impl(PyModuleDef *module, Py_intptr_t pid, int options); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 9663 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9664 | static PyObject * |
| 9665 | os_waitpid(PyModuleDef *module, PyObject *args) |
| 9666 | { |
| 9667 | PyObject *return_value = NULL; |
| 9668 | Py_intptr_t pid; |
| 9669 | int options; |
| 9670 | |
| 9671 | if (!PyArg_ParseTuple(args, |
| 9672 | "" _Py_PARSE_INTPTR "i:waitpid", |
| 9673 | &pid, &options)) |
| 9674 | goto exit; |
| 9675 | return_value = os_waitpid_impl(module, pid, options); |
| 9676 | |
| 9677 | exit: |
| 9678 | return return_value; |
| 9679 | } |
| 9680 | |
| 9681 | static PyObject * |
| 9682 | os_waitpid_impl(PyModuleDef *module, Py_intptr_t pid, int options) |
| 9683 | /*[clinic end generated code: output=c20b95b15ad44a3a input=444c8f51cca5b862]*/ |
| 9684 | { |
| 9685 | int status; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9686 | Py_intptr_t res; |
| 9687 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9688 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9689 | do { |
| 9690 | Py_BEGIN_ALLOW_THREADS |
| 9691 | res = _cwait(&status, pid, options); |
| 9692 | Py_END_ALLOW_THREADS |
| 9693 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9694 | if (res != 0) |
| 9695 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9696 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9697 | /* 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] | 9698 | return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 9699 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9700 | #endif |
| 9701 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9702 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 9703 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9704 | /*[clinic input] |
| 9705 | os.wait |
| 9706 | |
| 9707 | Wait for completion of a child process. |
| 9708 | |
| 9709 | Returns a tuple of information about the child process: |
| 9710 | (pid, status) |
| 9711 | [clinic start generated code]*/ |
| 9712 | |
| 9713 | PyDoc_STRVAR(os_wait__doc__, |
| 9714 | "wait($module, /)\n" |
| 9715 | "--\n" |
| 9716 | "\n" |
| 9717 | "Wait for completion of a child process.\n" |
| 9718 | "\n" |
| 9719 | "Returns a tuple of information about the child process:\n" |
| 9720 | " (pid, status)"); |
| 9721 | |
| 9722 | #define OS_WAIT_METHODDEF \ |
| 9723 | {"wait", (PyCFunction)os_wait, METH_NOARGS, os_wait__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9724 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 9725 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9726 | os_wait_impl(PyModuleDef *module); |
| 9727 | |
| 9728 | static PyObject * |
| 9729 | os_wait(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 9730 | { |
| 9731 | return os_wait_impl(module); |
| 9732 | } |
| 9733 | |
| 9734 | static PyObject * |
| 9735 | os_wait_impl(PyModuleDef *module) |
| 9736 | /*[clinic end generated code: output=2a83a9d164e7e6a8 input=03b0182d4a4700ce]*/ |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 9737 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9738 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9739 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9740 | WAIT_TYPE status; |
| 9741 | WAIT_STATUS_INT(status) = 0; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 9742 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9743 | do { |
| 9744 | Py_BEGIN_ALLOW_THREADS |
| 9745 | pid = wait(&status); |
| 9746 | Py_END_ALLOW_THREADS |
| 9747 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 9748 | if (pid < 0) |
| 9749 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 9750 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9751 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 9752 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9753 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 9754 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9755 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9756 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
| 9757 | PyDoc_STRVAR(readlink__doc__, |
| 9758 | "readlink(path, *, dir_fd=None) -> path\n\n\ |
| 9759 | Return a string representing the path to which the symbolic link points.\n\ |
| 9760 | \n\ |
| 9761 | If dir_fd is not None, it should be a file descriptor open to a directory,\n\ |
| 9762 | and path should be relative; path will then be relative to that directory.\n\ |
| 9763 | dir_fd may not be implemented on your platform.\n\ |
| 9764 | If it is unavailable, using it will raise a NotImplementedError."); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 9765 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9766 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9767 | #ifdef HAVE_READLINK |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9768 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9769 | /* AC 3.5: merge win32 and not together */ |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 9770 | static PyObject * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9771 | posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9772 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9773 | path_t path; |
| 9774 | int dir_fd = DEFAULT_DIR_FD; |
| 9775 | char buffer[MAXPATHLEN]; |
| 9776 | ssize_t length; |
| 9777 | PyObject *return_value = NULL; |
| 9778 | static char *keywords[] = {"path", "dir_fd", NULL}; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9779 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9780 | memset(&path, 0, sizeof(path)); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9781 | path.function_name = "readlink"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9782 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords, |
| 9783 | path_converter, &path, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9784 | READLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9785 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9786 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9787 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9788 | #ifdef HAVE_READLINKAT |
| 9789 | if (dir_fd != DEFAULT_DIR_FD) |
| 9790 | length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer)); |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 9791 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9792 | #endif |
| 9793 | length = readlink(path.narrow, buffer, sizeof(buffer)); |
| 9794 | Py_END_ALLOW_THREADS |
| 9795 | |
| 9796 | if (length < 0) { |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 9797 | return_value = path_error(&path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9798 | goto exit; |
| 9799 | } |
| 9800 | |
| 9801 | if (PyUnicode_Check(path.object)) |
| 9802 | return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length); |
| 9803 | else |
| 9804 | return_value = PyBytes_FromStringAndSize(buffer, length); |
| 9805 | exit: |
| 9806 | path_cleanup(&path); |
| 9807 | return return_value; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9808 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9809 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 9810 | #endif /* HAVE_READLINK */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9811 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9812 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
| 9813 | |
| 9814 | static PyObject * |
| 9815 | win_readlink(PyObject *self, PyObject *args, PyObject *kwargs) |
| 9816 | { |
| 9817 | wchar_t *path; |
| 9818 | DWORD n_bytes_returned; |
| 9819 | DWORD io_result; |
| 9820 | PyObject *po, *result; |
| 9821 | int dir_fd; |
| 9822 | HANDLE reparse_point_handle; |
| 9823 | |
| 9824 | char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
| 9825 | REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer; |
| 9826 | wchar_t *print_name; |
| 9827 | |
| 9828 | static char *keywords[] = {"path", "dir_fd", NULL}; |
| 9829 | |
| 9830 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords, |
| 9831 | &po, |
| 9832 | dir_fd_unavailable, &dir_fd |
| 9833 | )) |
| 9834 | return NULL; |
| 9835 | |
| 9836 | path = PyUnicode_AsUnicode(po); |
| 9837 | if (path == NULL) |
| 9838 | return NULL; |
| 9839 | |
| 9840 | /* First get a handle to the reparse point */ |
| 9841 | Py_BEGIN_ALLOW_THREADS |
| 9842 | reparse_point_handle = CreateFileW( |
| 9843 | path, |
| 9844 | 0, |
| 9845 | 0, |
| 9846 | 0, |
| 9847 | OPEN_EXISTING, |
| 9848 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, |
| 9849 | 0); |
| 9850 | Py_END_ALLOW_THREADS |
| 9851 | |
| 9852 | if (reparse_point_handle==INVALID_HANDLE_VALUE) |
| 9853 | return win32_error_object("readlink", po); |
| 9854 | |
| 9855 | Py_BEGIN_ALLOW_THREADS |
| 9856 | /* New call DeviceIoControl to read the reparse point */ |
| 9857 | io_result = DeviceIoControl( |
| 9858 | reparse_point_handle, |
| 9859 | FSCTL_GET_REPARSE_POINT, |
| 9860 | 0, 0, /* in buffer */ |
| 9861 | target_buffer, sizeof(target_buffer), |
| 9862 | &n_bytes_returned, |
| 9863 | 0 /* we're not using OVERLAPPED_IO */ |
| 9864 | ); |
| 9865 | CloseHandle(reparse_point_handle); |
| 9866 | Py_END_ALLOW_THREADS |
| 9867 | |
| 9868 | if (io_result==0) |
| 9869 | return win32_error_object("readlink", po); |
| 9870 | |
| 9871 | if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK) |
| 9872 | { |
| 9873 | PyErr_SetString(PyExc_ValueError, |
| 9874 | "not a symbolic link"); |
| 9875 | return NULL; |
| 9876 | } |
| 9877 | print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer + |
| 9878 | rdb->SymbolicLinkReparseBuffer.PrintNameOffset; |
| 9879 | |
| 9880 | result = PyUnicode_FromWideChar(print_name, |
| 9881 | rdb->SymbolicLinkReparseBuffer.PrintNameLength/2); |
| 9882 | return result; |
| 9883 | } |
| 9884 | |
| 9885 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
| 9886 | |
| 9887 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9888 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9889 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9890 | |
| 9891 | #if defined(MS_WINDOWS) |
| 9892 | |
| 9893 | /* Grab CreateSymbolicLinkW dynamically from kernel32 */ |
| 9894 | static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL; |
| 9895 | static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9896 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9897 | static int |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9898 | check_CreateSymbolicLink(void) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 9899 | { |
| 9900 | HINSTANCE hKernel32; |
| 9901 | /* only recheck */ |
| 9902 | if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA) |
| 9903 | return 1; |
| 9904 | hKernel32 = GetModuleHandleW(L"KERNEL32"); |
| 9905 | *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32, |
| 9906 | "CreateSymbolicLinkW"); |
| 9907 | *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32, |
| 9908 | "CreateSymbolicLinkA"); |
| 9909 | return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA); |
| 9910 | } |
| 9911 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9912 | /* Remove the last portion of the path */ |
| 9913 | static void |
| 9914 | _dirnameW(WCHAR *path) |
| 9915 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9916 | WCHAR *ptr; |
| 9917 | |
| 9918 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9919 | for(ptr = path + wcslen(path); ptr != path; ptr--) { |
Victor Stinner | 072318b | 2013-06-05 02:07:46 +0200 | [diff] [blame] | 9920 | if (*ptr == L'\\' || *ptr == L'/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9921 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9922 | } |
| 9923 | *ptr = 0; |
| 9924 | } |
| 9925 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9926 | /* Remove the last portion of the path */ |
| 9927 | static void |
| 9928 | _dirnameA(char *path) |
| 9929 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9930 | char *ptr; |
| 9931 | |
| 9932 | /* walk the path from the end until a backslash is encountered */ |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9933 | for(ptr = path + strlen(path); ptr != path; ptr--) { |
| 9934 | if (*ptr == '\\' || *ptr == '/') |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9935 | break; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9936 | } |
| 9937 | *ptr = 0; |
| 9938 | } |
| 9939 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9940 | /* Is this path absolute? */ |
| 9941 | static int |
| 9942 | _is_absW(const WCHAR *path) |
| 9943 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9944 | return path[0] == L'\\' || path[0] == L'/' || path[1] == L':'; |
| 9945 | |
| 9946 | } |
| 9947 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9948 | /* Is this path absolute? */ |
| 9949 | static int |
| 9950 | _is_absA(const char *path) |
| 9951 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9952 | return path[0] == '\\' || path[0] == '/' || path[1] == ':'; |
| 9953 | |
| 9954 | } |
| 9955 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9956 | /* join root and rest with a backslash */ |
| 9957 | static void |
| 9958 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
| 9959 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 9960 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9961 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9962 | if (_is_absW(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9963 | wcscpy(dest_path, rest); |
| 9964 | return; |
| 9965 | } |
| 9966 | |
| 9967 | root_len = wcslen(root); |
| 9968 | |
| 9969 | wcscpy(dest_path, root); |
| 9970 | if(root_len) { |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9971 | dest_path[root_len] = L'\\'; |
| 9972 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9973 | } |
| 9974 | wcscpy(dest_path+root_len, rest); |
| 9975 | } |
| 9976 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9977 | /* join root and rest with a backslash */ |
| 9978 | static void |
| 9979 | _joinA(char *dest_path, const char *root, const char *rest) |
| 9980 | { |
Victor Stinner | e7e7eba | 2013-06-05 00:35:54 +0200 | [diff] [blame] | 9981 | size_t root_len; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9982 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9983 | if (_is_absA(rest)) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9984 | strcpy(dest_path, rest); |
| 9985 | return; |
| 9986 | } |
| 9987 | |
| 9988 | root_len = strlen(root); |
| 9989 | |
| 9990 | strcpy(dest_path, root); |
| 9991 | if(root_len) { |
| 9992 | dest_path[root_len] = '\\'; |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9993 | root_len++; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 9994 | } |
| 9995 | strcpy(dest_path+root_len, rest); |
| 9996 | } |
| 9997 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 9998 | /* Return True if the path at src relative to dest is a directory */ |
| 9999 | static int |
| 10000 | _check_dirW(WCHAR *src, WCHAR *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10001 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10002 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 10003 | WCHAR dest_parent[MAX_PATH]; |
| 10004 | WCHAR src_resolved[MAX_PATH] = L""; |
| 10005 | |
| 10006 | /* dest_parent = os.path.dirname(dest) */ |
| 10007 | wcscpy(dest_parent, dest); |
| 10008 | _dirnameW(dest_parent); |
| 10009 | /* src_resolved = os.path.join(dest_parent, src) */ |
| 10010 | _joinW(src_resolved, dest_parent, src); |
| 10011 | return ( |
| 10012 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) |
| 10013 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 10014 | ); |
| 10015 | } |
| 10016 | |
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 10017 | /* Return True if the path at src relative to dest is a directory */ |
| 10018 | static int |
| 10019 | _check_dirA(char *src, char *dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10020 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10021 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
| 10022 | char dest_parent[MAX_PATH]; |
| 10023 | char src_resolved[MAX_PATH] = ""; |
| 10024 | |
| 10025 | /* dest_parent = os.path.dirname(dest) */ |
| 10026 | strcpy(dest_parent, dest); |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 10027 | _dirnameA(dest_parent); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10028 | /* src_resolved = os.path.join(dest_parent, src) */ |
Victor Stinner | 5a43676 | 2013-06-05 00:37:12 +0200 | [diff] [blame] | 10029 | _joinA(src_resolved, dest_parent, src); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10030 | return ( |
| 10031 | GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info) |
| 10032 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY |
| 10033 | ); |
| 10034 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10035 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10037 | |
| 10038 | /*[clinic input] |
| 10039 | os.symlink |
| 10040 | src: path_t |
| 10041 | dst: path_t |
| 10042 | target_is_directory: bool = False |
| 10043 | * |
| 10044 | dir_fd: dir_fd(requires='symlinkat')=None |
| 10045 | |
| 10046 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ |
| 10047 | |
| 10048 | Create a symbolic link pointing to src named dst. |
| 10049 | |
| 10050 | target_is_directory is required on Windows if the target is to be |
| 10051 | interpreted as a directory. (On Windows, symlink requires |
| 10052 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 10053 | target_is_directory is ignored on non-Windows platforms. |
| 10054 | |
| 10055 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10056 | and path should be relative; path will then be relative to that directory. |
| 10057 | dir_fd may not be implemented on your platform. |
| 10058 | If it is unavailable, using it will raise a NotImplementedError. |
| 10059 | |
| 10060 | [clinic start generated code]*/ |
| 10061 | |
| 10062 | PyDoc_STRVAR(os_symlink__doc__, |
| 10063 | "symlink($module, /, src, dst, target_is_directory=False, *, dir_fd=None)\n" |
| 10064 | "--\n" |
| 10065 | "\n" |
| 10066 | "Create a symbolic link pointing to src named dst.\n" |
| 10067 | "\n" |
| 10068 | "target_is_directory is required on Windows if the target is to be\n" |
| 10069 | " interpreted as a directory. (On Windows, symlink requires\n" |
| 10070 | " Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n" |
| 10071 | " target_is_directory is ignored on non-Windows platforms.\n" |
| 10072 | "\n" |
| 10073 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 10074 | " and path should be relative; path will then be relative to that directory.\n" |
| 10075 | "dir_fd may not be implemented on your platform.\n" |
| 10076 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 10077 | |
| 10078 | #define OS_SYMLINK_METHODDEF \ |
| 10079 | {"symlink", (PyCFunction)os_symlink, METH_VARARGS|METH_KEYWORDS, os_symlink__doc__}, |
| 10080 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 10081 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10082 | os_symlink_impl(PyModuleDef *module, path_t *src, path_t *dst, int target_is_directory, int dir_fd); |
| 10083 | |
| 10084 | static PyObject * |
| 10085 | os_symlink(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 10086 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10087 | PyObject *return_value = NULL; |
| 10088 | static char *_keywords[] = {"src", "dst", "target_is_directory", "dir_fd", NULL}; |
| 10089 | path_t src = PATH_T_INITIALIZE("symlink", "src", 0, 0); |
| 10090 | path_t dst = PATH_T_INITIALIZE("symlink", "dst", 0, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10091 | int target_is_directory = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10092 | int dir_fd = DEFAULT_DIR_FD; |
| 10093 | |
| 10094 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 10095 | "O&O&|p$O&:symlink", _keywords, |
| 10096 | path_converter, &src, path_converter, &dst, &target_is_directory, SYMLINKAT_DIR_FD_CONVERTER, &dir_fd)) |
| 10097 | goto exit; |
| 10098 | return_value = os_symlink_impl(module, &src, &dst, target_is_directory, dir_fd); |
| 10099 | |
| 10100 | exit: |
| 10101 | /* Cleanup for src */ |
| 10102 | path_cleanup(&src); |
| 10103 | /* Cleanup for dst */ |
| 10104 | path_cleanup(&dst); |
| 10105 | |
| 10106 | return return_value; |
| 10107 | } |
| 10108 | |
| 10109 | static PyObject * |
| 10110 | os_symlink_impl(PyModuleDef *module, path_t *src, path_t *dst, int target_is_directory, int dir_fd) |
| 10111 | /*[clinic end generated code: output=1a31e6d88aafe9b6 input=e820ec4472547bc3]*/ |
| 10112 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10113 | #ifdef MS_WINDOWS |
| 10114 | DWORD result; |
| 10115 | #else |
| 10116 | int result; |
| 10117 | #endif |
| 10118 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10119 | #ifdef MS_WINDOWS |
| 10120 | if (!check_CreateSymbolicLink()) { |
| 10121 | PyErr_SetString(PyExc_NotImplementedError, |
| 10122 | "CreateSymbolicLink functions not found"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10123 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 10124 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10125 | if (!win32_can_symlink) { |
| 10126 | PyErr_SetString(PyExc_OSError, "symbolic link privilege not held"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10127 | return NULL; |
Petri Lehtinen | 5445a8c | 2012-10-23 16:12:14 +0300 | [diff] [blame] | 10128 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10129 | #endif |
| 10130 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10131 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10132 | PyErr_SetString(PyExc_ValueError, |
| 10133 | "symlink: src and dst must be the same type"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10134 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10135 | } |
| 10136 | |
| 10137 | #ifdef MS_WINDOWS |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10138 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10139 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10140 | if (dst->wide) { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10141 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10142 | target_is_directory |= _check_dirW(src->wide, dst->wide); |
| 10143 | result = Py_CreateSymbolicLinkW(dst->wide, src->wide, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10144 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10145 | } |
| 10146 | else { |
| 10147 | /* if src is a directory, ensure target_is_directory==1 */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10148 | target_is_directory |= _check_dirA(src->narrow, dst->narrow); |
| 10149 | result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10150 | target_is_directory); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 10151 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10152 | Py_END_ALLOW_THREADS |
| 10153 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10154 | if (!result) |
| 10155 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10156 | |
| 10157 | #else |
| 10158 | |
| 10159 | Py_BEGIN_ALLOW_THREADS |
| 10160 | #if HAVE_SYMLINKAT |
| 10161 | if (dir_fd != DEFAULT_DIR_FD) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10162 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10163 | else |
| 10164 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10165 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10166 | Py_END_ALLOW_THREADS |
| 10167 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10168 | if (result) |
| 10169 | return path_error2(src, dst); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10170 | #endif |
| 10171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10172 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 10173 | } |
| 10174 | #endif /* HAVE_SYMLINK */ |
| 10175 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10176 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 10177 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 10178 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 10179 | static PyStructSequence_Field times_result_fields[] = { |
| 10180 | {"user", "user time"}, |
| 10181 | {"system", "system time"}, |
| 10182 | {"children_user", "user time of children"}, |
| 10183 | {"children_system", "system time of children"}, |
| 10184 | {"elapsed", "elapsed time since an arbitrary point in the past"}, |
| 10185 | {NULL} |
| 10186 | }; |
| 10187 | |
| 10188 | PyDoc_STRVAR(times_result__doc__, |
| 10189 | "times_result: Result from os.times().\n\n\ |
| 10190 | This object may be accessed either as a tuple of\n\ |
| 10191 | (user, system, children_user, children_system, elapsed),\n\ |
| 10192 | or via the attributes user, system, children_user, children_system,\n\ |
| 10193 | and elapsed.\n\ |
| 10194 | \n\ |
| 10195 | See os.times for more information."); |
| 10196 | |
| 10197 | static PyStructSequence_Desc times_result_desc = { |
| 10198 | "times_result", /* name */ |
| 10199 | times_result__doc__, /* doc */ |
| 10200 | times_result_fields, |
| 10201 | 5 |
| 10202 | }; |
| 10203 | |
| 10204 | static PyTypeObject TimesResultType; |
| 10205 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 10206 | #ifdef MS_WINDOWS |
| 10207 | #define HAVE_TIMES /* mandatory, for the method table */ |
| 10208 | #endif |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 10209 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 10210 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 10211 | |
| 10212 | static PyObject * |
| 10213 | build_times_result(double user, double system, |
| 10214 | double children_user, double children_system, |
| 10215 | double elapsed) |
| 10216 | { |
| 10217 | PyObject *value = PyStructSequence_New(&TimesResultType); |
| 10218 | if (value == NULL) |
| 10219 | return NULL; |
| 10220 | |
| 10221 | #define SET(i, field) \ |
| 10222 | { \ |
| 10223 | PyObject *o = PyFloat_FromDouble(field); \ |
| 10224 | if (!o) { \ |
| 10225 | Py_DECREF(value); \ |
| 10226 | return NULL; \ |
| 10227 | } \ |
| 10228 | PyStructSequence_SET_ITEM(value, i, o); \ |
| 10229 | } \ |
| 10230 | |
| 10231 | SET(0, user); |
| 10232 | SET(1, system); |
| 10233 | SET(2, children_user); |
| 10234 | SET(3, children_system); |
| 10235 | SET(4, elapsed); |
| 10236 | |
| 10237 | #undef SET |
| 10238 | |
| 10239 | return value; |
| 10240 | } |
| 10241 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 10242 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10243 | #ifndef MS_WINDOWS |
| 10244 | #define NEED_TICKS_PER_SECOND |
| 10245 | static long ticks_per_second = -1; |
| 10246 | #endif /* MS_WINDOWS */ |
| 10247 | |
| 10248 | /*[clinic input] |
| 10249 | os.times |
| 10250 | |
| 10251 | Return a collection containing process timing information. |
| 10252 | |
| 10253 | The object returned behaves like a named tuple with these fields: |
| 10254 | (utime, stime, cutime, cstime, elapsed_time) |
| 10255 | All fields are floating point numbers. |
| 10256 | [clinic start generated code]*/ |
| 10257 | |
| 10258 | PyDoc_STRVAR(os_times__doc__, |
| 10259 | "times($module, /)\n" |
| 10260 | "--\n" |
| 10261 | "\n" |
| 10262 | "Return a collection containing process timing information.\n" |
| 10263 | "\n" |
| 10264 | "The object returned behaves like a named tuple with these fields:\n" |
| 10265 | " (utime, stime, cutime, cstime, elapsed_time)\n" |
| 10266 | "All fields are floating point numbers."); |
| 10267 | |
| 10268 | #define OS_TIMES_METHODDEF \ |
| 10269 | {"times", (PyCFunction)os_times, METH_NOARGS, os_times__doc__}, |
| 10270 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10271 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10272 | os_times_impl(PyModuleDef *module); |
| 10273 | |
| 10274 | static PyObject * |
| 10275 | os_times(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 10276 | { |
| 10277 | return os_times_impl(module); |
| 10278 | } |
| 10279 | |
| 10280 | static PyObject * |
| 10281 | os_times_impl(PyModuleDef *module) |
| 10282 | /*[clinic end generated code: output=b86896d031a9b768 input=2bf9df3d6ab2e48b]*/ |
| 10283 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 10284 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10285 | FILETIME create, exit, kernel, user; |
| 10286 | HANDLE hProc; |
| 10287 | hProc = GetCurrentProcess(); |
| 10288 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); |
| 10289 | /* The fields of a FILETIME structure are the hi and lo part |
| 10290 | of a 64-bit value expressed in 100 nanosecond units. |
| 10291 | 1e7 is one second in such units; 1e-7 the inverse. |
| 10292 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. |
| 10293 | */ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 10294 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10295 | (double)(user.dwHighDateTime*429.4967296 + |
| 10296 | user.dwLowDateTime*1e-7), |
| 10297 | (double)(kernel.dwHighDateTime*429.4967296 + |
| 10298 | kernel.dwLowDateTime*1e-7), |
| 10299 | (double)0, |
| 10300 | (double)0, |
| 10301 | (double)0); |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 10302 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10303 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 10304 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10305 | |
| 10306 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 10307 | struct tms t; |
| 10308 | clock_t c; |
| 10309 | errno = 0; |
| 10310 | c = times(&t); |
| 10311 | if (c == (clock_t) -1) |
| 10312 | return posix_error(); |
| 10313 | return build_times_result( |
| 10314 | (double)t.tms_utime / ticks_per_second, |
| 10315 | (double)t.tms_stime / ticks_per_second, |
| 10316 | (double)t.tms_cutime / ticks_per_second, |
| 10317 | (double)t.tms_cstime / ticks_per_second, |
| 10318 | (double)c / ticks_per_second); |
| 10319 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10320 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 10321 | #endif /* HAVE_TIMES */ |
| 10322 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10323 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10324 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10325 | /*[clinic input] |
| 10326 | os.getsid |
| 10327 | |
| 10328 | pid: pid_t |
| 10329 | / |
| 10330 | |
| 10331 | Call the system call getsid(pid) and return the result. |
| 10332 | [clinic start generated code]*/ |
| 10333 | |
| 10334 | PyDoc_STRVAR(os_getsid__doc__, |
| 10335 | "getsid($module, pid, /)\n" |
| 10336 | "--\n" |
| 10337 | "\n" |
| 10338 | "Call the system call getsid(pid) and return the result."); |
| 10339 | |
| 10340 | #define OS_GETSID_METHODDEF \ |
| 10341 | {"getsid", (PyCFunction)os_getsid, METH_VARARGS, os_getsid__doc__}, |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10342 | |
| 10343 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10344 | os_getsid_impl(PyModuleDef *module, pid_t pid); |
| 10345 | |
| 10346 | static PyObject * |
| 10347 | os_getsid(PyModuleDef *module, PyObject *args) |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10348 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10349 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10350 | pid_t pid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10351 | |
| 10352 | if (!PyArg_ParseTuple(args, |
| 10353 | "" _Py_PARSE_PID ":getsid", |
| 10354 | &pid)) |
| 10355 | goto exit; |
| 10356 | return_value = os_getsid_impl(module, pid); |
| 10357 | |
| 10358 | exit: |
| 10359 | return return_value; |
| 10360 | } |
| 10361 | |
| 10362 | static PyObject * |
| 10363 | os_getsid_impl(PyModuleDef *module, pid_t pid) |
| 10364 | /*[clinic end generated code: output=ea8390f395f4e0e1 input=eeb2b923a30ce04e]*/ |
| 10365 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10366 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10367 | sid = getsid(pid); |
| 10368 | if (sid < 0) |
| 10369 | return posix_error(); |
| 10370 | return PyLong_FromLong((long)sid); |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 10371 | } |
| 10372 | #endif /* HAVE_GETSID */ |
| 10373 | |
| 10374 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10375 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10376 | /*[clinic input] |
| 10377 | os.setsid |
| 10378 | |
| 10379 | Call the system call setsid(). |
| 10380 | [clinic start generated code]*/ |
| 10381 | |
| 10382 | PyDoc_STRVAR(os_setsid__doc__, |
| 10383 | "setsid($module, /)\n" |
| 10384 | "--\n" |
| 10385 | "\n" |
| 10386 | "Call the system call setsid()."); |
| 10387 | |
| 10388 | #define OS_SETSID_METHODDEF \ |
| 10389 | {"setsid", (PyCFunction)os_setsid, METH_NOARGS, os_setsid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10390 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10391 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10392 | os_setsid_impl(PyModuleDef *module); |
| 10393 | |
| 10394 | static PyObject * |
| 10395 | os_setsid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 10396 | { |
| 10397 | return os_setsid_impl(module); |
| 10398 | } |
| 10399 | |
| 10400 | static PyObject * |
| 10401 | os_setsid_impl(PyModuleDef *module) |
| 10402 | /*[clinic end generated code: output=2a9a1435d8d764d5 input=5fff45858e2f0776]*/ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 10403 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10404 | if (setsid() < 0) |
| 10405 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10406 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 10407 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10408 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 10409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10410 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10411 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10412 | /*[clinic input] |
| 10413 | os.setpgid |
| 10414 | |
| 10415 | pid: pid_t |
| 10416 | pgrp: pid_t |
| 10417 | / |
| 10418 | |
| 10419 | Call the system call setpgid(pid, pgrp). |
| 10420 | [clinic start generated code]*/ |
| 10421 | |
| 10422 | PyDoc_STRVAR(os_setpgid__doc__, |
| 10423 | "setpgid($module, pid, pgrp, /)\n" |
| 10424 | "--\n" |
| 10425 | "\n" |
| 10426 | "Call the system call setpgid(pid, pgrp)."); |
| 10427 | |
| 10428 | #define OS_SETPGID_METHODDEF \ |
| 10429 | {"setpgid", (PyCFunction)os_setpgid, METH_VARARGS, os_setpgid__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10430 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10431 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10432 | os_setpgid_impl(PyModuleDef *module, pid_t pid, pid_t pgrp); |
| 10433 | |
| 10434 | static PyObject * |
| 10435 | os_setpgid(PyModuleDef *module, PyObject *args) |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 10436 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10437 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10438 | pid_t pid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10439 | pid_t pgrp; |
| 10440 | |
| 10441 | if (!PyArg_ParseTuple(args, |
| 10442 | "" _Py_PARSE_PID "" _Py_PARSE_PID ":setpgid", |
| 10443 | &pid, &pgrp)) |
| 10444 | goto exit; |
| 10445 | return_value = os_setpgid_impl(module, pid, pgrp); |
| 10446 | |
| 10447 | exit: |
| 10448 | return return_value; |
| 10449 | } |
| 10450 | |
| 10451 | static PyObject * |
| 10452 | os_setpgid_impl(PyModuleDef *module, pid_t pid, pid_t pgrp) |
| 10453 | /*[clinic end generated code: output=7ad79b725f890e1f input=fceb395eca572e1a]*/ |
| 10454 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10455 | if (setpgid(pid, pgrp) < 0) |
| 10456 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10457 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 10458 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10459 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 10460 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10461 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10462 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10463 | /*[clinic input] |
| 10464 | os.tcgetpgrp |
| 10465 | |
| 10466 | fd: int |
| 10467 | / |
| 10468 | |
| 10469 | Return the process group associated with the terminal specified by fd. |
| 10470 | [clinic start generated code]*/ |
| 10471 | |
| 10472 | PyDoc_STRVAR(os_tcgetpgrp__doc__, |
| 10473 | "tcgetpgrp($module, fd, /)\n" |
| 10474 | "--\n" |
| 10475 | "\n" |
| 10476 | "Return the process group associated with the terminal specified by fd."); |
| 10477 | |
| 10478 | #define OS_TCGETPGRP_METHODDEF \ |
| 10479 | {"tcgetpgrp", (PyCFunction)os_tcgetpgrp, METH_VARARGS, os_tcgetpgrp__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10480 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10481 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10482 | os_tcgetpgrp_impl(PyModuleDef *module, int fd); |
| 10483 | |
| 10484 | static PyObject * |
| 10485 | os_tcgetpgrp(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 10486 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10487 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10488 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10489 | |
| 10490 | if (!PyArg_ParseTuple(args, |
| 10491 | "i:tcgetpgrp", |
| 10492 | &fd)) |
| 10493 | goto exit; |
| 10494 | return_value = os_tcgetpgrp_impl(module, fd); |
| 10495 | |
| 10496 | exit: |
| 10497 | return return_value; |
| 10498 | } |
| 10499 | |
| 10500 | static PyObject * |
| 10501 | os_tcgetpgrp_impl(PyModuleDef *module, int fd) |
| 10502 | /*[clinic end generated code: output=abcf52ed4c8d22cb input=7f6c18eac10ada86]*/ |
| 10503 | { |
| 10504 | pid_t pgid = tcgetpgrp(fd); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10505 | if (pgid < 0) |
| 10506 | return posix_error(); |
| 10507 | return PyLong_FromPid(pgid); |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 10508 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10509 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 10510 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10511 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10512 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10513 | /*[clinic input] |
| 10514 | os.tcsetpgrp |
| 10515 | |
| 10516 | fd: int |
| 10517 | pgid: pid_t |
| 10518 | / |
| 10519 | |
| 10520 | Set the process group associated with the terminal specified by fd. |
| 10521 | [clinic start generated code]*/ |
| 10522 | |
| 10523 | PyDoc_STRVAR(os_tcsetpgrp__doc__, |
| 10524 | "tcsetpgrp($module, fd, pgid, /)\n" |
| 10525 | "--\n" |
| 10526 | "\n" |
| 10527 | "Set the process group associated with the terminal specified by fd."); |
| 10528 | |
| 10529 | #define OS_TCSETPGRP_METHODDEF \ |
| 10530 | {"tcsetpgrp", (PyCFunction)os_tcsetpgrp, METH_VARARGS, os_tcsetpgrp__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10531 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10532 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10533 | os_tcsetpgrp_impl(PyModuleDef *module, int fd, pid_t pgid); |
| 10534 | |
| 10535 | static PyObject * |
| 10536 | os_tcsetpgrp(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 10537 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10538 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10539 | int fd; |
| 10540 | pid_t pgid; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10541 | |
| 10542 | if (!PyArg_ParseTuple(args, |
| 10543 | "i" _Py_PARSE_PID ":tcsetpgrp", |
| 10544 | &fd, &pgid)) |
| 10545 | goto exit; |
| 10546 | return_value = os_tcsetpgrp_impl(module, fd, pgid); |
| 10547 | |
| 10548 | exit: |
| 10549 | return return_value; |
| 10550 | } |
| 10551 | |
| 10552 | static PyObject * |
| 10553 | os_tcsetpgrp_impl(PyModuleDef *module, int fd, pid_t pgid) |
| 10554 | /*[clinic end generated code: output=76f9bb8fd00f20f5 input=5bdc997c6a619020]*/ |
| 10555 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10556 | if (tcsetpgrp(fd, pgid) < 0) |
| 10557 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10558 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 10559 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 10560 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 10561 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10562 | /* Functions acting on file descriptors */ |
| 10563 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10564 | #ifdef O_CLOEXEC |
| 10565 | extern int _Py_open_cloexec_works; |
| 10566 | #endif |
| 10567 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10568 | |
| 10569 | /*[clinic input] |
| 10570 | os.open -> int |
| 10571 | path: path_t |
| 10572 | flags: int |
| 10573 | mode: int = 0o777 |
| 10574 | * |
| 10575 | dir_fd: dir_fd(requires='openat') = None |
| 10576 | |
| 10577 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ |
| 10578 | |
| 10579 | Open a file for low level IO. Returns a file descriptor (integer). |
| 10580 | |
| 10581 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 10582 | and path should be relative; path will then be relative to that directory. |
| 10583 | dir_fd may not be implemented on your platform. |
| 10584 | If it is unavailable, using it will raise a NotImplementedError. |
| 10585 | [clinic start generated code]*/ |
| 10586 | |
| 10587 | PyDoc_STRVAR(os_open__doc__, |
| 10588 | "open($module, /, path, flags, mode=511, *, dir_fd=None)\n" |
| 10589 | "--\n" |
| 10590 | "\n" |
| 10591 | "Open a file for low level IO. Returns a file descriptor (integer).\n" |
| 10592 | "\n" |
| 10593 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 10594 | " and path should be relative; path will then be relative to that directory.\n" |
| 10595 | "dir_fd may not be implemented on your platform.\n" |
| 10596 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 10597 | |
| 10598 | #define OS_OPEN_METHODDEF \ |
| 10599 | {"open", (PyCFunction)os_open, METH_VARARGS|METH_KEYWORDS, os_open__doc__}, |
| 10600 | |
| 10601 | static int |
| 10602 | os_open_impl(PyModuleDef *module, path_t *path, int flags, int mode, int dir_fd); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10603 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10604 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10605 | os_open(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10606 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10607 | PyObject *return_value = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10608 | static char *_keywords[] = {"path", "flags", "mode", "dir_fd", NULL}; |
| 10609 | path_t path = PATH_T_INITIALIZE("open", "path", 0, 0); |
| 10610 | int flags; |
| 10611 | int mode = 511; |
| 10612 | int dir_fd = DEFAULT_DIR_FD; |
| 10613 | int _return_value; |
| 10614 | |
| 10615 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 10616 | "O&i|i$O&:open", _keywords, |
| 10617 | path_converter, &path, &flags, &mode, OPENAT_DIR_FD_CONVERTER, &dir_fd)) |
| 10618 | goto exit; |
| 10619 | _return_value = os_open_impl(module, &path, flags, mode, dir_fd); |
| 10620 | if ((_return_value == -1) && PyErr_Occurred()) |
| 10621 | goto exit; |
| 10622 | return_value = PyLong_FromLong((long)_return_value); |
| 10623 | |
| 10624 | exit: |
| 10625 | /* Cleanup for path */ |
| 10626 | path_cleanup(&path); |
| 10627 | |
| 10628 | return return_value; |
| 10629 | } |
| 10630 | |
| 10631 | static int |
| 10632 | os_open_impl(PyModuleDef *module, path_t *path, int flags, int mode, int dir_fd) |
| 10633 | /*[clinic end generated code: output=05b68fc4ed5e29c9 input=ad8623b29acd2934]*/ |
| 10634 | { |
| 10635 | int fd; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10636 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10637 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10638 | #ifdef O_CLOEXEC |
| 10639 | int *atomic_flag_works = &_Py_open_cloexec_works; |
| 10640 | #elif !defined(MS_WINDOWS) |
| 10641 | int *atomic_flag_works = NULL; |
| 10642 | #endif |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 10643 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10644 | #ifdef MS_WINDOWS |
| 10645 | flags |= O_NOINHERIT; |
| 10646 | #elif defined(O_CLOEXEC) |
| 10647 | flags |= O_CLOEXEC; |
| 10648 | #endif |
| 10649 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10650 | do { |
| 10651 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10652 | #ifdef MS_WINDOWS |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10653 | if (path->wide) |
| 10654 | fd = _wopen(path->wide, flags, mode); |
| 10655 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10656 | #endif |
| 10657 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10658 | if (dir_fd != DEFAULT_DIR_FD) |
| 10659 | fd = openat(dir_fd, path->narrow, flags, mode); |
| 10660 | else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10661 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10662 | fd = open(path->narrow, flags, mode); |
| 10663 | Py_END_ALLOW_THREADS |
| 10664 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10665 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10666 | if (fd == -1) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10667 | if (!async_err) |
| 10668 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10669 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10670 | } |
| 10671 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10672 | #ifndef MS_WINDOWS |
| 10673 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { |
| 10674 | close(fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10675 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10676 | } |
| 10677 | #endif |
| 10678 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10679 | return fd; |
| 10680 | } |
| 10681 | |
| 10682 | |
| 10683 | /*[clinic input] |
| 10684 | os.close |
| 10685 | |
| 10686 | fd: int |
| 10687 | |
| 10688 | Close a file descriptor. |
| 10689 | [clinic start generated code]*/ |
| 10690 | |
| 10691 | PyDoc_STRVAR(os_close__doc__, |
| 10692 | "close($module, /, fd)\n" |
| 10693 | "--\n" |
| 10694 | "\n" |
| 10695 | "Close a file descriptor."); |
| 10696 | |
| 10697 | #define OS_CLOSE_METHODDEF \ |
| 10698 | {"close", (PyCFunction)os_close, METH_VARARGS|METH_KEYWORDS, os_close__doc__}, |
| 10699 | |
| 10700 | static PyObject * |
| 10701 | os_close_impl(PyModuleDef *module, int fd); |
| 10702 | |
| 10703 | static PyObject * |
| 10704 | os_close(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 10705 | { |
| 10706 | PyObject *return_value = NULL; |
| 10707 | static char *_keywords[] = {"fd", NULL}; |
| 10708 | int fd; |
| 10709 | |
| 10710 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 10711 | "i:close", _keywords, |
| 10712 | &fd)) |
| 10713 | goto exit; |
| 10714 | return_value = os_close_impl(module, fd); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10715 | |
| 10716 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10717 | return return_value; |
| 10718 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10719 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10720 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10721 | os_close_impl(PyModuleDef *module, int fd) |
| 10722 | /*[clinic end generated code: output=927004e29ad55808 input=2bc42451ca5c3223]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10723 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10724 | int res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10725 | if (!_PyVerify_fd(fd)) |
| 10726 | return posix_error(); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10727 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
| 10728 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html |
| 10729 | * for more details. |
| 10730 | */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10731 | Py_BEGIN_ALLOW_THREADS |
| 10732 | res = close(fd); |
| 10733 | Py_END_ALLOW_THREADS |
| 10734 | if (res < 0) |
| 10735 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10736 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10737 | } |
| 10738 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10739 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10740 | /*[clinic input] |
| 10741 | os.closerange |
| 10742 | |
| 10743 | fd_low: int |
| 10744 | fd_high: int |
| 10745 | / |
| 10746 | |
| 10747 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. |
| 10748 | [clinic start generated code]*/ |
| 10749 | |
| 10750 | PyDoc_STRVAR(os_closerange__doc__, |
| 10751 | "closerange($module, fd_low, fd_high, /)\n" |
| 10752 | "--\n" |
| 10753 | "\n" |
| 10754 | "Closes all file descriptors in [fd_low, fd_high), ignoring errors."); |
| 10755 | |
| 10756 | #define OS_CLOSERANGE_METHODDEF \ |
| 10757 | {"closerange", (PyCFunction)os_closerange, METH_VARARGS, os_closerange__doc__}, |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 10758 | |
| 10759 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10760 | os_closerange_impl(PyModuleDef *module, int fd_low, int fd_high); |
| 10761 | |
| 10762 | static PyObject * |
| 10763 | os_closerange(PyModuleDef *module, PyObject *args) |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 10764 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10765 | PyObject *return_value = NULL; |
| 10766 | int fd_low; |
| 10767 | int fd_high; |
| 10768 | |
| 10769 | if (!PyArg_ParseTuple(args, |
| 10770 | "ii:closerange", |
| 10771 | &fd_low, &fd_high)) |
| 10772 | goto exit; |
| 10773 | return_value = os_closerange_impl(module, fd_low, fd_high); |
| 10774 | |
| 10775 | exit: |
| 10776 | return return_value; |
| 10777 | } |
| 10778 | |
| 10779 | static PyObject * |
| 10780 | os_closerange_impl(PyModuleDef *module, int fd_low, int fd_high) |
| 10781 | /*[clinic end generated code: output=0a929ece386811c3 input=5855a3d053ebd4ec]*/ |
| 10782 | { |
| 10783 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10784 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10785 | for (i = fd_low; i < fd_high; i++) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10786 | if (_PyVerify_fd(i)) |
| 10787 | close(i); |
| 10788 | Py_END_ALLOW_THREADS |
| 10789 | Py_RETURN_NONE; |
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 10790 | } |
| 10791 | |
| 10792 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10793 | /*[clinic input] |
| 10794 | os.dup -> int |
| 10795 | |
| 10796 | fd: int |
| 10797 | / |
| 10798 | |
| 10799 | Return a duplicate of a file descriptor. |
| 10800 | [clinic start generated code]*/ |
| 10801 | |
| 10802 | PyDoc_STRVAR(os_dup__doc__, |
| 10803 | "dup($module, fd, /)\n" |
| 10804 | "--\n" |
| 10805 | "\n" |
| 10806 | "Return a duplicate of a file descriptor."); |
| 10807 | |
| 10808 | #define OS_DUP_METHODDEF \ |
| 10809 | {"dup", (PyCFunction)os_dup, METH_VARARGS, os_dup__doc__}, |
| 10810 | |
| 10811 | static int |
| 10812 | os_dup_impl(PyModuleDef *module, int fd); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10813 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10814 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10815 | os_dup(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10816 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10817 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10818 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10819 | int _return_value; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10820 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10821 | if (!PyArg_ParseTuple(args, |
| 10822 | "i:dup", |
| 10823 | &fd)) |
| 10824 | goto exit; |
| 10825 | _return_value = os_dup_impl(module, fd); |
| 10826 | if ((_return_value == -1) && PyErr_Occurred()) |
| 10827 | goto exit; |
| 10828 | return_value = PyLong_FromLong((long)_return_value); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 10829 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10830 | exit: |
| 10831 | return return_value; |
| 10832 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10834 | static int |
| 10835 | os_dup_impl(PyModuleDef *module, int fd) |
| 10836 | /*[clinic end generated code: output=75943e057b25e1bd input=6f10f7ea97f7852a]*/ |
| 10837 | { |
| 10838 | return _Py_dup(fd); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10839 | } |
| 10840 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10841 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10842 | /*[clinic input] |
| 10843 | os.dup2 |
| 10844 | fd: int |
| 10845 | fd2: int |
| 10846 | inheritable: bool=True |
| 10847 | |
| 10848 | Duplicate file descriptor. |
| 10849 | [clinic start generated code]*/ |
| 10850 | |
| 10851 | PyDoc_STRVAR(os_dup2__doc__, |
| 10852 | "dup2($module, /, fd, fd2, inheritable=True)\n" |
| 10853 | "--\n" |
| 10854 | "\n" |
| 10855 | "Duplicate file descriptor."); |
| 10856 | |
| 10857 | #define OS_DUP2_METHODDEF \ |
| 10858 | {"dup2", (PyCFunction)os_dup2, METH_VARARGS|METH_KEYWORDS, os_dup2__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10859 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 10860 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10861 | os_dup2_impl(PyModuleDef *module, int fd, int fd2, int inheritable); |
| 10862 | |
| 10863 | static PyObject * |
| 10864 | os_dup2(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10865 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10866 | PyObject *return_value = NULL; |
| 10867 | static char *_keywords[] = {"fd", "fd2", "inheritable", NULL}; |
| 10868 | int fd; |
| 10869 | int fd2; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10870 | int inheritable = 1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10871 | |
| 10872 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 10873 | "ii|p:dup2", _keywords, |
| 10874 | &fd, &fd2, &inheritable)) |
| 10875 | goto exit; |
| 10876 | return_value = os_dup2_impl(module, fd, fd2, inheritable); |
| 10877 | |
| 10878 | exit: |
| 10879 | return return_value; |
| 10880 | } |
| 10881 | |
| 10882 | static PyObject * |
| 10883 | os_dup2_impl(PyModuleDef *module, int fd, int fd2, int inheritable) |
| 10884 | /*[clinic end generated code: output=531e482dd11a99a0 input=76e96f511be0352f]*/ |
| 10885 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10886 | int res; |
| 10887 | #if defined(HAVE_DUP3) && \ |
| 10888 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) |
| 10889 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ |
| 10890 | int dup3_works = -1; |
| 10891 | #endif |
| 10892 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10893 | if (!_PyVerify_fd_dup2(fd, fd2)) |
| 10894 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10895 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10896 | /* dup2() can fail with EINTR if the target FD is already open, because it |
| 10897 | * then has to be closed. See os_close_impl() for why we don't handle EINTR |
| 10898 | * upon close(), and therefore below. |
| 10899 | */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10900 | #ifdef MS_WINDOWS |
| 10901 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10902 | res = dup2(fd, fd2); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10903 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10904 | if (res < 0) |
| 10905 | return posix_error(); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 10906 | |
| 10907 | /* Character files like console cannot be make non-inheritable */ |
| 10908 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 10909 | close(fd2); |
| 10910 | return NULL; |
| 10911 | } |
| 10912 | |
| 10913 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) |
| 10914 | Py_BEGIN_ALLOW_THREADS |
| 10915 | if (!inheritable) |
| 10916 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); |
| 10917 | else |
| 10918 | res = dup2(fd, fd2); |
| 10919 | Py_END_ALLOW_THREADS |
| 10920 | if (res < 0) |
| 10921 | return posix_error(); |
| 10922 | |
| 10923 | #else |
| 10924 | |
| 10925 | #ifdef HAVE_DUP3 |
| 10926 | if (!inheritable && dup3_works != 0) { |
| 10927 | Py_BEGIN_ALLOW_THREADS |
| 10928 | res = dup3(fd, fd2, O_CLOEXEC); |
| 10929 | Py_END_ALLOW_THREADS |
| 10930 | if (res < 0) { |
| 10931 | if (dup3_works == -1) |
| 10932 | dup3_works = (errno != ENOSYS); |
| 10933 | if (dup3_works) |
| 10934 | return posix_error(); |
| 10935 | } |
| 10936 | } |
| 10937 | |
| 10938 | if (inheritable || dup3_works == 0) |
| 10939 | { |
| 10940 | #endif |
| 10941 | Py_BEGIN_ALLOW_THREADS |
| 10942 | res = dup2(fd, fd2); |
| 10943 | Py_END_ALLOW_THREADS |
| 10944 | if (res < 0) |
| 10945 | return posix_error(); |
| 10946 | |
| 10947 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { |
| 10948 | close(fd2); |
| 10949 | return NULL; |
| 10950 | } |
| 10951 | #ifdef HAVE_DUP3 |
| 10952 | } |
| 10953 | #endif |
| 10954 | |
| 10955 | #endif |
| 10956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10957 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 10958 | } |
| 10959 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10960 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10961 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10962 | /*[clinic input] |
| 10963 | os.lockf |
| 10964 | |
| 10965 | fd: int |
| 10966 | An open file descriptor. |
| 10967 | command: int |
| 10968 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. |
| 10969 | length: Py_off_t |
| 10970 | The number of bytes to lock, starting at the current position. |
| 10971 | / |
| 10972 | |
| 10973 | Apply, test or remove a POSIX lock on an open file descriptor. |
| 10974 | |
| 10975 | [clinic start generated code]*/ |
| 10976 | |
| 10977 | PyDoc_STRVAR(os_lockf__doc__, |
| 10978 | "lockf($module, fd, command, length, /)\n" |
| 10979 | "--\n" |
| 10980 | "\n" |
| 10981 | "Apply, test or remove a POSIX lock on an open file descriptor.\n" |
| 10982 | "\n" |
| 10983 | " fd\n" |
| 10984 | " An open file descriptor.\n" |
| 10985 | " command\n" |
| 10986 | " One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.\n" |
| 10987 | " length\n" |
| 10988 | " The number of bytes to lock, starting at the current position."); |
| 10989 | |
| 10990 | #define OS_LOCKF_METHODDEF \ |
| 10991 | {"lockf", (PyCFunction)os_lockf, METH_VARARGS, os_lockf__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10992 | |
| 10993 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10994 | os_lockf_impl(PyModuleDef *module, int fd, int command, Py_off_t length); |
| 10995 | |
| 10996 | static PyObject * |
| 10997 | os_lockf(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10998 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10999 | PyObject *return_value = NULL; |
| 11000 | int fd; |
| 11001 | int command; |
| 11002 | Py_off_t length; |
| 11003 | |
| 11004 | if (!PyArg_ParseTuple(args, |
| 11005 | "iiO&:lockf", |
| 11006 | &fd, &command, Py_off_t_converter, &length)) |
| 11007 | goto exit; |
| 11008 | return_value = os_lockf_impl(module, fd, command, length); |
| 11009 | |
| 11010 | exit: |
| 11011 | return return_value; |
| 11012 | } |
| 11013 | |
| 11014 | static PyObject * |
| 11015 | os_lockf_impl(PyModuleDef *module, int fd, int command, Py_off_t length) |
| 11016 | /*[clinic end generated code: output=1b28346ac7335c0f input=65da41d2106e9b79]*/ |
| 11017 | { |
| 11018 | int res; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11019 | |
| 11020 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11021 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11022 | Py_END_ALLOW_THREADS |
| 11023 | |
| 11024 | if (res < 0) |
| 11025 | return posix_error(); |
| 11026 | |
| 11027 | Py_RETURN_NONE; |
| 11028 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11029 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11030 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11031 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11032 | /*[clinic input] |
| 11033 | os.lseek -> Py_off_t |
| 11034 | |
| 11035 | fd: int |
| 11036 | position: Py_off_t |
| 11037 | how: int |
| 11038 | / |
| 11039 | |
| 11040 | Set the position of a file descriptor. Return the new position. |
| 11041 | |
| 11042 | Return the new cursor position in number of bytes |
| 11043 | relative to the beginning of the file. |
| 11044 | [clinic start generated code]*/ |
| 11045 | |
| 11046 | PyDoc_STRVAR(os_lseek__doc__, |
| 11047 | "lseek($module, fd, position, how, /)\n" |
| 11048 | "--\n" |
| 11049 | "\n" |
| 11050 | "Set the position of a file descriptor. Return the new position.\n" |
| 11051 | "\n" |
| 11052 | "Return the new cursor position in number of bytes\n" |
| 11053 | "relative to the beginning of the file."); |
| 11054 | |
| 11055 | #define OS_LSEEK_METHODDEF \ |
| 11056 | {"lseek", (PyCFunction)os_lseek, METH_VARARGS, os_lseek__doc__}, |
| 11057 | |
| 11058 | static Py_off_t |
| 11059 | os_lseek_impl(PyModuleDef *module, int fd, Py_off_t position, int how); |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11060 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 11061 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11062 | os_lseek(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11063 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11064 | PyObject *return_value = NULL; |
| 11065 | int fd; |
| 11066 | Py_off_t position; |
| 11067 | int how; |
| 11068 | Py_off_t _return_value; |
| 11069 | |
| 11070 | if (!PyArg_ParseTuple(args, |
| 11071 | "iO&i:lseek", |
| 11072 | &fd, Py_off_t_converter, &position, &how)) |
| 11073 | goto exit; |
| 11074 | _return_value = os_lseek_impl(module, fd, position, how); |
| 11075 | if ((_return_value == -1) && PyErr_Occurred()) |
| 11076 | goto exit; |
| 11077 | return_value = PyLong_FromPy_off_t(_return_value); |
| 11078 | |
| 11079 | exit: |
| 11080 | return return_value; |
| 11081 | } |
| 11082 | |
| 11083 | static Py_off_t |
| 11084 | os_lseek_impl(PyModuleDef *module, int fd, Py_off_t position, int how) |
| 11085 | /*[clinic end generated code: output=88cfc146f55667af input=902654ad3f96a6d3]*/ |
| 11086 | { |
| 11087 | Py_off_t result; |
| 11088 | |
| 11089 | if (!_PyVerify_fd(fd)) { |
| 11090 | posix_error(); |
| 11091 | return -1; |
| 11092 | } |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11093 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
| 11095 | switch (how) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11096 | case 0: how = SEEK_SET; break; |
| 11097 | case 1: how = SEEK_CUR; break; |
| 11098 | case 2: how = SEEK_END; break; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11099 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 11100 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11101 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11102 | if (PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11103 | return -1; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11105 | if (!_PyVerify_fd(fd)) { |
| 11106 | posix_error(); |
| 11107 | return -1; |
| 11108 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 11110 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11111 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 11112 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11113 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 11114 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11116 | if (result < 0) |
| 11117 | posix_error(); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 11118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11119 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11120 | } |
| 11121 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11122 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11123 | /*[clinic input] |
| 11124 | os.read |
| 11125 | fd: int |
| 11126 | length: Py_ssize_t |
| 11127 | / |
| 11128 | |
| 11129 | Read from a file descriptor. Returns a bytes object. |
| 11130 | [clinic start generated code]*/ |
| 11131 | |
| 11132 | PyDoc_STRVAR(os_read__doc__, |
| 11133 | "read($module, fd, length, /)\n" |
| 11134 | "--\n" |
| 11135 | "\n" |
| 11136 | "Read from a file descriptor. Returns a bytes object."); |
| 11137 | |
| 11138 | #define OS_READ_METHODDEF \ |
| 11139 | {"read", (PyCFunction)os_read, METH_VARARGS, os_read__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11140 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 11141 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11142 | os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length); |
| 11143 | |
| 11144 | static PyObject * |
| 11145 | os_read(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11146 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11147 | PyObject *return_value = NULL; |
Victor Stinner | b28ed92 | 2014-07-11 17:04:41 +0200 | [diff] [blame] | 11148 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11149 | Py_ssize_t length; |
| 11150 | |
| 11151 | if (!PyArg_ParseTuple(args, |
| 11152 | "in:read", |
| 11153 | &fd, &length)) |
| 11154 | goto exit; |
| 11155 | return_value = os_read_impl(module, fd, length); |
| 11156 | |
| 11157 | exit: |
| 11158 | return return_value; |
| 11159 | } |
| 11160 | |
| 11161 | static PyObject * |
| 11162 | os_read_impl(PyModuleDef *module, int fd, Py_ssize_t length) |
| 11163 | /*[clinic end generated code: output=1f3bc27260a24968 input=1df2eaa27c0bf1d3]*/ |
| 11164 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11165 | Py_ssize_t n; |
| 11166 | PyObject *buffer; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11167 | |
| 11168 | if (length < 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11169 | errno = EINVAL; |
| 11170 | return posix_error(); |
| 11171 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11172 | |
| 11173 | #ifdef MS_WINDOWS |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 11174 | /* On Windows, the count parameter of read() is an int */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11175 | if (length > INT_MAX) |
| 11176 | length = INT_MAX; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11177 | #endif |
| 11178 | |
| 11179 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11180 | if (buffer == NULL) |
| 11181 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11182 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 11183 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
| 11184 | if (n == -1) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11185 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 11186 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11187 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11188 | |
| 11189 | if (n != length) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11191 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11192 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11193 | } |
| 11194 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11195 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
| 11196 | || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV) |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11197 | static Py_ssize_t |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11198 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type) |
| 11199 | { |
| 11200 | int i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11201 | Py_ssize_t blen, total = 0; |
| 11202 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11203 | *iov = PyMem_New(struct iovec, cnt); |
| 11204 | if (*iov == NULL) { |
| 11205 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 11206 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11207 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11208 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11209 | *buf = PyMem_New(Py_buffer, cnt); |
| 11210 | if (*buf == NULL) { |
| 11211 | PyMem_Del(*iov); |
| 11212 | PyErr_NoMemory(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 11213 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11214 | } |
| 11215 | |
| 11216 | for (i = 0; i < cnt; i++) { |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 11217 | PyObject *item = PySequence_GetItem(seq, i); |
| 11218 | if (item == NULL) |
| 11219 | goto fail; |
| 11220 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { |
| 11221 | Py_DECREF(item); |
| 11222 | goto fail; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11223 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 11224 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11225 | (*iov)[i].iov_base = (*buf)[i].buf; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11226 | blen = (*buf)[i].len; |
| 11227 | (*iov)[i].iov_len = blen; |
| 11228 | total += blen; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11229 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11230 | return total; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 11231 | |
| 11232 | fail: |
| 11233 | PyMem_Del(*iov); |
| 11234 | for (j = 0; j < i; j++) { |
| 11235 | PyBuffer_Release(&(*buf)[j]); |
| 11236 | } |
| 11237 | PyMem_Del(*buf); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 11238 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11239 | } |
| 11240 | |
| 11241 | static void |
| 11242 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) |
| 11243 | { |
| 11244 | int i; |
| 11245 | PyMem_Del(iov); |
| 11246 | for (i = 0; i < cnt; i++) { |
| 11247 | PyBuffer_Release(&buf[i]); |
| 11248 | } |
| 11249 | PyMem_Del(buf); |
| 11250 | } |
| 11251 | #endif |
| 11252 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11253 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11254 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11255 | /*[clinic input] |
| 11256 | os.readv -> Py_ssize_t |
| 11257 | |
| 11258 | fd: int |
| 11259 | buffers: object |
| 11260 | / |
| 11261 | |
| 11262 | Read from a file descriptor fd into an iterable of buffers. |
| 11263 | |
| 11264 | The buffers should be mutable buffers accepting bytes. |
| 11265 | readv will transfer data into each buffer until it is full |
| 11266 | and then move on to the next buffer in the sequence to hold |
| 11267 | the rest of the data. |
| 11268 | |
| 11269 | readv returns the total number of bytes read, |
| 11270 | which may be less than the total capacity of all the buffers. |
| 11271 | [clinic start generated code]*/ |
| 11272 | |
| 11273 | PyDoc_STRVAR(os_readv__doc__, |
| 11274 | "readv($module, fd, buffers, /)\n" |
| 11275 | "--\n" |
| 11276 | "\n" |
| 11277 | "Read from a file descriptor fd into an iterable of buffers.\n" |
| 11278 | "\n" |
| 11279 | "The buffers should be mutable buffers accepting bytes.\n" |
| 11280 | "readv will transfer data into each buffer until it is full\n" |
| 11281 | "and then move on to the next buffer in the sequence to hold\n" |
| 11282 | "the rest of the data.\n" |
| 11283 | "\n" |
| 11284 | "readv returns the total number of bytes read,\n" |
| 11285 | "which may be less than the total capacity of all the buffers."); |
| 11286 | |
| 11287 | #define OS_READV_METHODDEF \ |
| 11288 | {"readv", (PyCFunction)os_readv, METH_VARARGS, os_readv__doc__}, |
| 11289 | |
| 11290 | static Py_ssize_t |
| 11291 | os_readv_impl(PyModuleDef *module, int fd, PyObject *buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11292 | |
| 11293 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11294 | os_readv(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11295 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11296 | PyObject *return_value = NULL; |
| 11297 | int fd; |
| 11298 | PyObject *buffers; |
| 11299 | Py_ssize_t _return_value; |
| 11300 | |
| 11301 | if (!PyArg_ParseTuple(args, |
| 11302 | "iO:readv", |
| 11303 | &fd, &buffers)) |
| 11304 | goto exit; |
| 11305 | _return_value = os_readv_impl(module, fd, buffers); |
| 11306 | if ((_return_value == -1) && PyErr_Occurred()) |
| 11307 | goto exit; |
| 11308 | return_value = PyLong_FromSsize_t(_return_value); |
| 11309 | |
| 11310 | exit: |
| 11311 | return return_value; |
| 11312 | } |
| 11313 | |
| 11314 | static Py_ssize_t |
| 11315 | os_readv_impl(PyModuleDef *module, int fd, PyObject *buffers) |
| 11316 | /*[clinic end generated code: output=72748b1c32a6e2a1 input=e679eb5dbfa0357d]*/ |
| 11317 | { |
| 11318 | int cnt; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11319 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11320 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11321 | struct iovec *iov; |
| 11322 | Py_buffer *buf; |
| 11323 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11324 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11325 | PyErr_SetString(PyExc_TypeError, |
| 11326 | "readv() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11327 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11328 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11329 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11330 | cnt = PySequence_Size(buffers); |
| 11331 | |
| 11332 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) |
| 11333 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11334 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11335 | do { |
| 11336 | Py_BEGIN_ALLOW_THREADS |
| 11337 | n = readv(fd, iov, cnt); |
| 11338 | Py_END_ALLOW_THREADS |
| 11339 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11340 | |
| 11341 | iov_cleanup(iov, buf, cnt); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11342 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11343 | if (!async_err) |
| 11344 | posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11345 | return -1; |
| 11346 | } |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 11347 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11348 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11349 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11350 | #endif /* HAVE_READV */ |
| 11351 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11352 | |
| 11353 | #ifdef HAVE_PREAD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11354 | /*[clinic input] |
| 11355 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. |
| 11356 | os.pread |
| 11357 | |
| 11358 | fd: int |
| 11359 | length: int |
| 11360 | offset: Py_off_t |
| 11361 | / |
| 11362 | |
| 11363 | Read a number of bytes from a file descriptor starting at a particular offset. |
| 11364 | |
| 11365 | Read length bytes from file descriptor fd, starting at offset bytes from |
| 11366 | the beginning of the file. The file offset remains unchanged. |
| 11367 | [clinic start generated code]*/ |
| 11368 | |
| 11369 | PyDoc_STRVAR(os_pread__doc__, |
| 11370 | "pread($module, fd, length, offset, /)\n" |
| 11371 | "--\n" |
| 11372 | "\n" |
| 11373 | "Read a number of bytes from a file descriptor starting at a particular offset.\n" |
| 11374 | "\n" |
| 11375 | "Read length bytes from file descriptor fd, starting at offset bytes from\n" |
| 11376 | "the beginning of the file. The file offset remains unchanged."); |
| 11377 | |
| 11378 | #define OS_PREAD_METHODDEF \ |
| 11379 | {"pread", (PyCFunction)os_pread, METH_VARARGS, os_pread__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11380 | |
| 11381 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11382 | os_pread_impl(PyModuleDef *module, int fd, int length, Py_off_t offset); |
| 11383 | |
| 11384 | static PyObject * |
| 11385 | os_pread(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11386 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11387 | PyObject *return_value = NULL; |
| 11388 | int fd; |
| 11389 | int length; |
| 11390 | Py_off_t offset; |
| 11391 | |
| 11392 | if (!PyArg_ParseTuple(args, |
| 11393 | "iiO&:pread", |
| 11394 | &fd, &length, Py_off_t_converter, &offset)) |
| 11395 | goto exit; |
| 11396 | return_value = os_pread_impl(module, fd, length, offset); |
| 11397 | |
| 11398 | exit: |
| 11399 | return return_value; |
| 11400 | } |
| 11401 | |
| 11402 | static PyObject * |
| 11403 | os_pread_impl(PyModuleDef *module, int fd, int length, Py_off_t offset) |
| 11404 | /*[clinic end generated code: output=7b62bf6c06e20ae8 input=084948dcbaa35d4c]*/ |
| 11405 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11406 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11407 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11408 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11409 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11410 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11411 | errno = EINVAL; |
| 11412 | return posix_error(); |
| 11413 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11414 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11415 | if (buffer == NULL) |
| 11416 | return NULL; |
| 11417 | if (!_PyVerify_fd(fd)) { |
| 11418 | Py_DECREF(buffer); |
| 11419 | return posix_error(); |
| 11420 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11421 | |
| 11422 | do { |
| 11423 | Py_BEGIN_ALLOW_THREADS |
| 11424 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
| 11425 | Py_END_ALLOW_THREADS |
| 11426 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
| 11427 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11428 | if (n < 0) { |
| 11429 | Py_DECREF(buffer); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11430 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11431 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11432 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11433 | _PyBytes_Resize(&buffer, n); |
| 11434 | return buffer; |
| 11435 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11436 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11437 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11438 | |
| 11439 | /*[clinic input] |
| 11440 | os.write -> Py_ssize_t |
| 11441 | |
| 11442 | fd: int |
| 11443 | data: Py_buffer |
| 11444 | / |
| 11445 | |
| 11446 | Write a bytes object to a file descriptor. |
| 11447 | [clinic start generated code]*/ |
| 11448 | |
| 11449 | PyDoc_STRVAR(os_write__doc__, |
| 11450 | "write($module, fd, data, /)\n" |
| 11451 | "--\n" |
| 11452 | "\n" |
| 11453 | "Write a bytes object to a file descriptor."); |
| 11454 | |
| 11455 | #define OS_WRITE_METHODDEF \ |
| 11456 | {"write", (PyCFunction)os_write, METH_VARARGS, os_write__doc__}, |
| 11457 | |
| 11458 | static Py_ssize_t |
| 11459 | os_write_impl(PyModuleDef *module, int fd, Py_buffer *data); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11460 | |
| 11461 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11462 | os_write(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11463 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11464 | PyObject *return_value = NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11465 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11466 | Py_buffer data = {NULL, NULL}; |
| 11467 | Py_ssize_t _return_value; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11468 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11469 | if (!PyArg_ParseTuple(args, |
| 11470 | "iy*:write", |
| 11471 | &fd, &data)) |
| 11472 | goto exit; |
| 11473 | _return_value = os_write_impl(module, fd, &data); |
| 11474 | if ((_return_value == -1) && PyErr_Occurred()) |
| 11475 | goto exit; |
| 11476 | return_value = PyLong_FromSsize_t(_return_value); |
| 11477 | |
| 11478 | exit: |
| 11479 | /* Cleanup for data */ |
| 11480 | if (data.obj) |
| 11481 | PyBuffer_Release(&data); |
| 11482 | |
| 11483 | return return_value; |
| 11484 | } |
| 11485 | |
| 11486 | static Py_ssize_t |
| 11487 | os_write_impl(PyModuleDef *module, int fd, Py_buffer *data) |
| 11488 | /*[clinic end generated code: output=aeb96acfdd4d5112 input=3207e28963234f3c]*/ |
| 11489 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 11490 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11491 | } |
| 11492 | |
| 11493 | #ifdef HAVE_SENDFILE |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11494 | PyDoc_STRVAR(posix_sendfile__doc__, |
| 11495 | "sendfile(out, in, offset, nbytes) -> byteswritten\n\ |
| 11496 | sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\ |
| 11497 | -> byteswritten\n\ |
| 11498 | Copy nbytes bytes from file descriptor in to file descriptor out."); |
| 11499 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11500 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11501 | static PyObject * |
| 11502 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) |
| 11503 | { |
| 11504 | int in, out; |
| 11505 | Py_ssize_t ret; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11506 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11507 | off_t offset; |
| 11508 | |
| 11509 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
| 11510 | #ifndef __APPLE__ |
| 11511 | Py_ssize_t len; |
| 11512 | #endif |
| 11513 | PyObject *headers = NULL, *trailers = NULL; |
| 11514 | Py_buffer *hbuf, *tbuf; |
| 11515 | off_t sbytes; |
| 11516 | struct sf_hdtr sf; |
| 11517 | int flags = 0; |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 11518 | static char *keywords[] = {"out", "in", |
| 11519 | "offset", "count", |
| 11520 | "headers", "trailers", "flags", NULL}; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11521 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 11522 | sf.headers = NULL; |
| 11523 | sf.trailers = NULL; |
| 11524 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11525 | #ifdef __APPLE__ |
| 11526 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11527 | 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] | 11528 | #else |
| 11529 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11530 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11531 | #endif |
| 11532 | &headers, &trailers, &flags)) |
| 11533 | return NULL; |
| 11534 | if (headers != NULL) { |
| 11535 | if (!PySequence_Check(headers)) { |
| 11536 | PyErr_SetString(PyExc_TypeError, |
| 11537 | "sendfile() headers must be a sequence or None"); |
| 11538 | return NULL; |
| 11539 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11540 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11541 | sf.hdr_cnt = PySequence_Size(headers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11542 | if (sf.hdr_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 11543 | (i = iov_setup(&(sf.headers), &hbuf, |
| 11544 | headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11545 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11546 | #ifdef __APPLE__ |
| 11547 | sbytes += i; |
| 11548 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11549 | } |
| 11550 | } |
| 11551 | if (trailers != NULL) { |
| 11552 | if (!PySequence_Check(trailers)) { |
| 11553 | PyErr_SetString(PyExc_TypeError, |
| 11554 | "sendfile() trailers must be a sequence or None"); |
| 11555 | return NULL; |
| 11556 | } else { |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11557 | Py_ssize_t i = 0; /* Avoid uninitialized warning */ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11558 | sf.trl_cnt = PySequence_Size(trailers); |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11559 | if (sf.trl_cnt > 0 && |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 11560 | (i = iov_setup(&(sf.trailers), &tbuf, |
| 11561 | trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11562 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 11563 | #ifdef __APPLE__ |
| 11564 | sbytes += i; |
| 11565 | #endif |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11566 | } |
| 11567 | } |
| 11568 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11569 | do { |
| 11570 | Py_BEGIN_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11571 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11572 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11573 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11574 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11575 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11576 | Py_END_ALLOW_THREADS |
| 11577 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11578 | |
| 11579 | if (sf.headers != NULL) |
| 11580 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); |
| 11581 | if (sf.trailers != NULL) |
| 11582 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); |
| 11583 | |
| 11584 | if (ret < 0) { |
| 11585 | if ((errno == EAGAIN) || (errno == EBUSY)) { |
| 11586 | if (sbytes != 0) { |
| 11587 | // some data has been sent |
| 11588 | goto done; |
| 11589 | } |
| 11590 | else { |
| 11591 | // no data has been sent; upper application is supposed |
| 11592 | // to retry on EAGAIN or EBUSY |
| 11593 | return posix_error(); |
| 11594 | } |
| 11595 | } |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11596 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11597 | } |
| 11598 | goto done; |
| 11599 | |
| 11600 | done: |
| 11601 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 11602 | return Py_BuildValue("l", sbytes); |
| 11603 | #else |
| 11604 | return Py_BuildValue("L", sbytes); |
| 11605 | #endif |
| 11606 | |
| 11607 | #else |
| 11608 | Py_ssize_t count; |
| 11609 | PyObject *offobj; |
| 11610 | static char *keywords[] = {"out", "in", |
| 11611 | "offset", "count", NULL}; |
| 11612 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", |
| 11613 | keywords, &out, &in, &offobj, &count)) |
| 11614 | return NULL; |
| 11615 | #ifdef linux |
| 11616 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11617 | do { |
| 11618 | Py_BEGIN_ALLOW_THREADS |
| 11619 | ret = sendfile(out, in, NULL, count); |
| 11620 | Py_END_ALLOW_THREADS |
| 11621 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11622 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11623 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 11624 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11625 | } |
| 11626 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11627 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 11628 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11629 | |
| 11630 | do { |
| 11631 | Py_BEGIN_ALLOW_THREADS |
| 11632 | ret = sendfile(out, in, &offset, count); |
| 11633 | Py_END_ALLOW_THREADS |
| 11634 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11635 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11636 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 11637 | return Py_BuildValue("n", ret); |
| 11638 | #endif |
| 11639 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11640 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11642 | |
| 11643 | /*[clinic input] |
| 11644 | os.fstat |
| 11645 | |
| 11646 | fd : int |
| 11647 | |
| 11648 | Perform a stat system call on the given file descriptor. |
| 11649 | |
| 11650 | Like stat(), but for an open file descriptor. |
| 11651 | Equivalent to os.stat(fd). |
| 11652 | [clinic start generated code]*/ |
| 11653 | |
| 11654 | PyDoc_STRVAR(os_fstat__doc__, |
| 11655 | "fstat($module, /, fd)\n" |
| 11656 | "--\n" |
| 11657 | "\n" |
| 11658 | "Perform a stat system call on the given file descriptor.\n" |
| 11659 | "\n" |
| 11660 | "Like stat(), but for an open file descriptor.\n" |
| 11661 | "Equivalent to os.stat(fd)."); |
| 11662 | |
| 11663 | #define OS_FSTAT_METHODDEF \ |
| 11664 | {"fstat", (PyCFunction)os_fstat, METH_VARARGS|METH_KEYWORDS, os_fstat__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11665 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 11666 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11667 | os_fstat_impl(PyModuleDef *module, int fd); |
| 11668 | |
| 11669 | static PyObject * |
| 11670 | os_fstat(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11671 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11672 | PyObject *return_value = NULL; |
| 11673 | static char *_keywords[] = {"fd", NULL}; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11674 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11675 | |
| 11676 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 11677 | "i:fstat", _keywords, |
| 11678 | &fd)) |
| 11679 | goto exit; |
| 11680 | return_value = os_fstat_impl(module, fd); |
| 11681 | |
| 11682 | exit: |
| 11683 | return return_value; |
| 11684 | } |
| 11685 | |
| 11686 | static PyObject * |
| 11687 | os_fstat_impl(PyModuleDef *module, int fd) |
| 11688 | /*[clinic end generated code: output=dae4a9678c7bd881 input=27e0e0ebbe5600c9]*/ |
| 11689 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11690 | STRUCT_STAT st; |
| 11691 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11692 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11693 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11694 | do { |
| 11695 | Py_BEGIN_ALLOW_THREADS |
| 11696 | res = FSTAT(fd, &st); |
| 11697 | Py_END_ALLOW_THREADS |
| 11698 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11699 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 11700 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 11701 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 11702 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11703 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 11704 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11705 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 11706 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 11707 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11708 | } |
| 11709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11710 | |
| 11711 | /*[clinic input] |
| 11712 | os.isatty -> bool |
| 11713 | fd: int |
| 11714 | / |
| 11715 | |
| 11716 | Return True if the fd is connected to a terminal. |
| 11717 | |
| 11718 | Return True if the file descriptor is an open file descriptor |
| 11719 | connected to the slave end of a terminal. |
| 11720 | [clinic start generated code]*/ |
| 11721 | |
| 11722 | PyDoc_STRVAR(os_isatty__doc__, |
| 11723 | "isatty($module, fd, /)\n" |
| 11724 | "--\n" |
| 11725 | "\n" |
| 11726 | "Return True if the fd is connected to a terminal.\n" |
| 11727 | "\n" |
| 11728 | "Return True if the file descriptor is an open file descriptor\n" |
| 11729 | "connected to the slave end of a terminal."); |
| 11730 | |
| 11731 | #define OS_ISATTY_METHODDEF \ |
| 11732 | {"isatty", (PyCFunction)os_isatty, METH_VARARGS, os_isatty__doc__}, |
| 11733 | |
| 11734 | static int |
| 11735 | os_isatty_impl(PyModuleDef *module, int fd); |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 11736 | |
| 11737 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11738 | os_isatty(PyModuleDef *module, PyObject *args) |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 11739 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11740 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11741 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11742 | int _return_value; |
| 11743 | |
| 11744 | if (!PyArg_ParseTuple(args, |
| 11745 | "i:isatty", |
| 11746 | &fd)) |
| 11747 | goto exit; |
| 11748 | _return_value = os_isatty_impl(module, fd); |
| 11749 | if ((_return_value == -1) && PyErr_Occurred()) |
| 11750 | goto exit; |
| 11751 | return_value = PyBool_FromLong((long)_return_value); |
| 11752 | |
| 11753 | exit: |
| 11754 | return return_value; |
Skip Montanaro | 1517d84 | 2000-07-19 14:34:14 +0000 | [diff] [blame] | 11755 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11756 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11757 | static int |
| 11758 | os_isatty_impl(PyModuleDef *module, int fd) |
| 11759 | /*[clinic end generated code: output=4bfadbfe22715097 input=08ce94aa1eaf7b5e]*/ |
| 11760 | { |
| 11761 | if (!_PyVerify_fd(fd)) |
| 11762 | return 0; |
| 11763 | return isatty(fd); |
| 11764 | } |
| 11765 | |
| 11766 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11767 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11768 | /*[clinic input] |
| 11769 | os.pipe |
| 11770 | |
| 11771 | Create a pipe. |
| 11772 | |
| 11773 | Returns a tuple of two file descriptors: |
| 11774 | (read_fd, write_fd) |
| 11775 | [clinic start generated code]*/ |
| 11776 | |
| 11777 | PyDoc_STRVAR(os_pipe__doc__, |
| 11778 | "pipe($module, /)\n" |
| 11779 | "--\n" |
| 11780 | "\n" |
| 11781 | "Create a pipe.\n" |
| 11782 | "\n" |
| 11783 | "Returns a tuple of two file descriptors:\n" |
| 11784 | " (read_fd, write_fd)"); |
| 11785 | |
| 11786 | #define OS_PIPE_METHODDEF \ |
| 11787 | {"pipe", (PyCFunction)os_pipe, METH_NOARGS, os_pipe__doc__}, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 11788 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 11789 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11790 | os_pipe_impl(PyModuleDef *module); |
| 11791 | |
| 11792 | static PyObject * |
| 11793 | os_pipe(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 11794 | { |
| 11795 | return os_pipe_impl(module); |
| 11796 | } |
| 11797 | |
| 11798 | static PyObject * |
| 11799 | os_pipe_impl(PyModuleDef *module) |
| 11800 | /*[clinic end generated code: output=0da2479f2266e774 input=02535e8c8fa6c4d4]*/ |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11801 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11802 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11803 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11804 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11805 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11806 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11807 | #else |
| 11808 | int res; |
| 11809 | #endif |
| 11810 | |
| 11811 | #ifdef MS_WINDOWS |
| 11812 | attr.nLength = sizeof(attr); |
| 11813 | attr.lpSecurityDescriptor = NULL; |
| 11814 | attr.bInheritHandle = FALSE; |
| 11815 | |
| 11816 | Py_BEGIN_ALLOW_THREADS |
| 11817 | ok = CreatePipe(&read, &write, &attr, 0); |
| 11818 | if (ok) { |
| 11819 | fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY); |
| 11820 | fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY); |
| 11821 | if (fds[0] == -1 || fds[1] == -1) { |
| 11822 | CloseHandle(read); |
| 11823 | CloseHandle(write); |
| 11824 | ok = 0; |
| 11825 | } |
| 11826 | } |
| 11827 | Py_END_ALLOW_THREADS |
| 11828 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11829 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 11830 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 11831 | #else |
| 11832 | |
| 11833 | #ifdef HAVE_PIPE2 |
| 11834 | Py_BEGIN_ALLOW_THREADS |
| 11835 | res = pipe2(fds, O_CLOEXEC); |
| 11836 | Py_END_ALLOW_THREADS |
| 11837 | |
| 11838 | if (res != 0 && errno == ENOSYS) |
| 11839 | { |
| 11840 | #endif |
| 11841 | Py_BEGIN_ALLOW_THREADS |
| 11842 | res = pipe(fds); |
| 11843 | Py_END_ALLOW_THREADS |
| 11844 | |
| 11845 | if (res == 0) { |
| 11846 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { |
| 11847 | close(fds[0]); |
| 11848 | close(fds[1]); |
| 11849 | return NULL; |
| 11850 | } |
| 11851 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { |
| 11852 | close(fds[0]); |
| 11853 | close(fds[1]); |
| 11854 | return NULL; |
| 11855 | } |
| 11856 | } |
| 11857 | #ifdef HAVE_PIPE2 |
| 11858 | } |
| 11859 | #endif |
| 11860 | |
| 11861 | if (res != 0) |
| 11862 | return PyErr_SetFromErrno(PyExc_OSError); |
| 11863 | #endif /* !MS_WINDOWS */ |
| 11864 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 11865 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 11866 | #endif /* HAVE_PIPE */ |
| 11867 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11868 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11869 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11870 | /*[clinic input] |
| 11871 | os.pipe2 |
| 11872 | |
| 11873 | flags: int |
| 11874 | / |
| 11875 | |
| 11876 | Create a pipe with flags set atomically. |
| 11877 | |
| 11878 | Returns a tuple of two file descriptors: |
| 11879 | (read_fd, write_fd) |
| 11880 | |
| 11881 | flags can be constructed by ORing together one or more of these values: |
| 11882 | O_NONBLOCK, O_CLOEXEC. |
| 11883 | [clinic start generated code]*/ |
| 11884 | |
| 11885 | PyDoc_STRVAR(os_pipe2__doc__, |
| 11886 | "pipe2($module, flags, /)\n" |
| 11887 | "--\n" |
| 11888 | "\n" |
| 11889 | "Create a pipe with flags set atomically.\n" |
| 11890 | "\n" |
| 11891 | "Returns a tuple of two file descriptors:\n" |
| 11892 | " (read_fd, write_fd)\n" |
| 11893 | "\n" |
| 11894 | "flags can be constructed by ORing together one or more of these values:\n" |
| 11895 | "O_NONBLOCK, O_CLOEXEC."); |
| 11896 | |
| 11897 | #define OS_PIPE2_METHODDEF \ |
| 11898 | {"pipe2", (PyCFunction)os_pipe2, METH_VARARGS, os_pipe2__doc__}, |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11899 | |
| 11900 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11901 | os_pipe2_impl(PyModuleDef *module, int flags); |
| 11902 | |
| 11903 | static PyObject * |
| 11904 | os_pipe2(PyModuleDef *module, PyObject *args) |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11905 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11906 | PyObject *return_value = NULL; |
Charles-François Natali | 368f34b | 2011-06-06 19:49:47 +0200 | [diff] [blame] | 11907 | int flags; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11908 | |
| 11909 | if (!PyArg_ParseTuple(args, |
| 11910 | "i:pipe2", |
| 11911 | &flags)) |
| 11912 | goto exit; |
| 11913 | return_value = os_pipe2_impl(module, flags); |
| 11914 | |
| 11915 | exit: |
| 11916 | return return_value; |
| 11917 | } |
| 11918 | |
| 11919 | static PyObject * |
| 11920 | os_pipe2_impl(PyModuleDef *module, int flags) |
| 11921 | /*[clinic end generated code: output=9e27c799ce19220b input=f261b6e7e63c6817]*/ |
| 11922 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11923 | int fds[2]; |
| 11924 | int res; |
| 11925 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 11926 | res = pipe2(fds, flags); |
| 11927 | if (res != 0) |
| 11928 | return posix_error(); |
| 11929 | return Py_BuildValue("(ii)", fds[0], fds[1]); |
| 11930 | } |
| 11931 | #endif /* HAVE_PIPE2 */ |
| 11932 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11933 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11934 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11935 | /*[clinic input] |
| 11936 | os.writev -> Py_ssize_t |
| 11937 | fd: int |
| 11938 | buffers: object |
| 11939 | / |
| 11940 | |
| 11941 | Iterate over buffers, and write the contents of each to a file descriptor. |
| 11942 | |
| 11943 | Returns the total number of bytes written. |
| 11944 | buffers must be a sequence of bytes-like objects. |
| 11945 | [clinic start generated code]*/ |
| 11946 | |
| 11947 | PyDoc_STRVAR(os_writev__doc__, |
| 11948 | "writev($module, fd, buffers, /)\n" |
| 11949 | "--\n" |
| 11950 | "\n" |
| 11951 | "Iterate over buffers, and write the contents of each to a file descriptor.\n" |
| 11952 | "\n" |
| 11953 | "Returns the total number of bytes written.\n" |
| 11954 | "buffers must be a sequence of bytes-like objects."); |
| 11955 | |
| 11956 | #define OS_WRITEV_METHODDEF \ |
| 11957 | {"writev", (PyCFunction)os_writev, METH_VARARGS, os_writev__doc__}, |
| 11958 | |
| 11959 | static Py_ssize_t |
| 11960 | os_writev_impl(PyModuleDef *module, int fd, PyObject *buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11961 | |
| 11962 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11963 | os_writev(PyModuleDef *module, PyObject *args) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11964 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11965 | PyObject *return_value = NULL; |
| 11966 | int fd; |
| 11967 | PyObject *buffers; |
| 11968 | Py_ssize_t _return_value; |
| 11969 | |
| 11970 | if (!PyArg_ParseTuple(args, |
| 11971 | "iO:writev", |
| 11972 | &fd, &buffers)) |
| 11973 | goto exit; |
| 11974 | _return_value = os_writev_impl(module, fd, buffers); |
| 11975 | if ((_return_value == -1) && PyErr_Occurred()) |
| 11976 | goto exit; |
| 11977 | return_value = PyLong_FromSsize_t(_return_value); |
| 11978 | |
| 11979 | exit: |
| 11980 | return return_value; |
| 11981 | } |
| 11982 | |
| 11983 | static Py_ssize_t |
| 11984 | os_writev_impl(PyModuleDef *module, int fd, PyObject *buffers) |
| 11985 | /*[clinic end generated code: output=591c662dccbe4951 input=5b8d17fe4189d2fe]*/ |
| 11986 | { |
| 11987 | int cnt; |
| 11988 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 11989 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11990 | struct iovec *iov; |
| 11991 | Py_buffer *buf; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11992 | |
| 11993 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11994 | PyErr_SetString(PyExc_TypeError, |
| 11995 | "writev() arg 2 must be a sequence"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11996 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11997 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11998 | cnt = PySequence_Size(buffers); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 11999 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12000 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
| 12001 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12002 | } |
| 12003 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12004 | do { |
| 12005 | Py_BEGIN_ALLOW_THREADS |
| 12006 | result = writev(fd, iov, cnt); |
| 12007 | Py_END_ALLOW_THREADS |
| 12008 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12009 | |
| 12010 | iov_cleanup(iov, buf, cnt); |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12011 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12012 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 12013 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 12014 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12015 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12016 | #endif /* HAVE_WRITEV */ |
| 12017 | |
| 12018 | |
| 12019 | #ifdef HAVE_PWRITE |
| 12020 | /*[clinic input] |
| 12021 | os.pwrite -> Py_ssize_t |
| 12022 | |
| 12023 | fd: int |
| 12024 | buffer: Py_buffer |
| 12025 | offset: Py_off_t |
| 12026 | / |
| 12027 | |
| 12028 | Write bytes to a file descriptor starting at a particular offset. |
| 12029 | |
| 12030 | Write buffer to fd, starting at offset bytes from the beginning of |
| 12031 | the file. Returns the number of bytes writte. Does not change the |
| 12032 | current file offset. |
| 12033 | [clinic start generated code]*/ |
| 12034 | |
| 12035 | PyDoc_STRVAR(os_pwrite__doc__, |
| 12036 | "pwrite($module, fd, buffer, offset, /)\n" |
| 12037 | "--\n" |
| 12038 | "\n" |
| 12039 | "Write bytes to a file descriptor starting at a particular offset.\n" |
| 12040 | "\n" |
| 12041 | "Write buffer to fd, starting at offset bytes from the beginning of\n" |
| 12042 | "the file. Returns the number of bytes writte. Does not change the\n" |
| 12043 | "current file offset."); |
| 12044 | |
| 12045 | #define OS_PWRITE_METHODDEF \ |
| 12046 | {"pwrite", (PyCFunction)os_pwrite, METH_VARARGS, os_pwrite__doc__}, |
| 12047 | |
| 12048 | static Py_ssize_t |
| 12049 | os_pwrite_impl(PyModuleDef *module, int fd, Py_buffer *buffer, Py_off_t offset); |
| 12050 | |
| 12051 | static PyObject * |
| 12052 | os_pwrite(PyModuleDef *module, PyObject *args) |
| 12053 | { |
| 12054 | PyObject *return_value = NULL; |
| 12055 | int fd; |
| 12056 | Py_buffer buffer = {NULL, NULL}; |
| 12057 | Py_off_t offset; |
| 12058 | Py_ssize_t _return_value; |
| 12059 | |
| 12060 | if (!PyArg_ParseTuple(args, |
| 12061 | "iy*O&:pwrite", |
| 12062 | &fd, &buffer, Py_off_t_converter, &offset)) |
| 12063 | goto exit; |
| 12064 | _return_value = os_pwrite_impl(module, fd, &buffer, offset); |
| 12065 | if ((_return_value == -1) && PyErr_Occurred()) |
| 12066 | goto exit; |
| 12067 | return_value = PyLong_FromSsize_t(_return_value); |
| 12068 | |
| 12069 | exit: |
| 12070 | /* Cleanup for buffer */ |
| 12071 | if (buffer.obj) |
| 12072 | PyBuffer_Release(&buffer); |
| 12073 | |
| 12074 | return return_value; |
| 12075 | } |
| 12076 | |
| 12077 | static Py_ssize_t |
| 12078 | os_pwrite_impl(PyModuleDef *module, int fd, Py_buffer *buffer, Py_off_t offset) |
| 12079 | /*[clinic end generated code: output=ec9cc5b2238e96a7 input=19903f1b3dd26377]*/ |
| 12080 | { |
| 12081 | Py_ssize_t size; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12082 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12083 | |
| 12084 | if (!_PyVerify_fd(fd)) { |
| 12085 | posix_error(); |
| 12086 | return -1; |
| 12087 | } |
| 12088 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12089 | do { |
| 12090 | Py_BEGIN_ALLOW_THREADS |
| 12091 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
| 12092 | Py_END_ALLOW_THREADS |
| 12093 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12094 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12095 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12096 | posix_error(); |
| 12097 | return size; |
| 12098 | } |
| 12099 | #endif /* HAVE_PWRITE */ |
| 12100 | |
| 12101 | |
| 12102 | #ifdef HAVE_MKFIFO |
| 12103 | /*[clinic input] |
| 12104 | os.mkfifo |
| 12105 | |
| 12106 | path: path_t |
| 12107 | mode: int=0o666 |
| 12108 | * |
| 12109 | dir_fd: dir_fd(requires='mkfifoat')=None |
| 12110 | |
| 12111 | Create a "fifo" (a POSIX named pipe). |
| 12112 | |
| 12113 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 12114 | and path should be relative; path will then be relative to that directory. |
| 12115 | dir_fd may not be implemented on your platform. |
| 12116 | If it is unavailable, using it will raise a NotImplementedError. |
| 12117 | [clinic start generated code]*/ |
| 12118 | |
| 12119 | PyDoc_STRVAR(os_mkfifo__doc__, |
| 12120 | "mkfifo($module, /, path, mode=438, *, dir_fd=None)\n" |
| 12121 | "--\n" |
| 12122 | "\n" |
| 12123 | "Create a \"fifo\" (a POSIX named pipe).\n" |
| 12124 | "\n" |
| 12125 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 12126 | " and path should be relative; path will then be relative to that directory.\n" |
| 12127 | "dir_fd may not be implemented on your platform.\n" |
| 12128 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 12129 | |
| 12130 | #define OS_MKFIFO_METHODDEF \ |
| 12131 | {"mkfifo", (PyCFunction)os_mkfifo, METH_VARARGS|METH_KEYWORDS, os_mkfifo__doc__}, |
| 12132 | |
| 12133 | static PyObject * |
| 12134 | os_mkfifo_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd); |
| 12135 | |
| 12136 | static PyObject * |
| 12137 | os_mkfifo(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 12138 | { |
| 12139 | PyObject *return_value = NULL; |
| 12140 | static char *_keywords[] = {"path", "mode", "dir_fd", NULL}; |
| 12141 | path_t path = PATH_T_INITIALIZE("mkfifo", "path", 0, 0); |
| 12142 | int mode = 438; |
| 12143 | int dir_fd = DEFAULT_DIR_FD; |
| 12144 | |
| 12145 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 12146 | "O&|i$O&:mkfifo", _keywords, |
| 12147 | path_converter, &path, &mode, MKFIFOAT_DIR_FD_CONVERTER, &dir_fd)) |
| 12148 | goto exit; |
| 12149 | return_value = os_mkfifo_impl(module, &path, mode, dir_fd); |
| 12150 | |
| 12151 | exit: |
| 12152 | /* Cleanup for path */ |
| 12153 | path_cleanup(&path); |
| 12154 | |
| 12155 | return return_value; |
| 12156 | } |
| 12157 | |
| 12158 | static PyObject * |
| 12159 | os_mkfifo_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd) |
| 12160 | /*[clinic end generated code: output=b3321927546893d0 input=73032e98a36e0e19]*/ |
| 12161 | { |
| 12162 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12163 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12164 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12165 | do { |
| 12166 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12167 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12168 | if (dir_fd != DEFAULT_DIR_FD) |
| 12169 | result = mkfifoat(dir_fd, path->narrow, mode); |
| 12170 | else |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12171 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12172 | result = mkfifo(path->narrow, mode); |
| 12173 | Py_END_ALLOW_THREADS |
| 12174 | } while (result != 0 && errno == EINTR && |
| 12175 | !(async_err = PyErr_CheckSignals())); |
| 12176 | if (result != 0) |
| 12177 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12178 | |
| 12179 | Py_RETURN_NONE; |
| 12180 | } |
| 12181 | #endif /* HAVE_MKFIFO */ |
| 12182 | |
| 12183 | |
| 12184 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
| 12185 | /*[clinic input] |
| 12186 | os.mknod |
| 12187 | |
| 12188 | path: path_t |
| 12189 | mode: int=0o600 |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12190 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12191 | * |
| 12192 | dir_fd: dir_fd(requires='mknodat')=None |
| 12193 | |
| 12194 | Create a node in the file system. |
| 12195 | |
| 12196 | Create a node in the file system (file, device special file or named pipe) |
| 12197 | at path. mode specifies both the permissions to use and the |
| 12198 | type of node to be created, being combined (bitwise OR) with one of |
| 12199 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, |
| 12200 | device defines the newly created device special file (probably using |
| 12201 | os.makedev()). Otherwise device is ignored. |
| 12202 | |
| 12203 | If dir_fd is not None, it should be a file descriptor open to a directory, |
| 12204 | and path should be relative; path will then be relative to that directory. |
| 12205 | dir_fd may not be implemented on your platform. |
| 12206 | If it is unavailable, using it will raise a NotImplementedError. |
| 12207 | [clinic start generated code]*/ |
| 12208 | |
| 12209 | PyDoc_STRVAR(os_mknod__doc__, |
| 12210 | "mknod($module, /, path, mode=384, device=0, *, dir_fd=None)\n" |
| 12211 | "--\n" |
| 12212 | "\n" |
| 12213 | "Create a node in the file system.\n" |
| 12214 | "\n" |
| 12215 | "Create a node in the file system (file, device special file or named pipe)\n" |
| 12216 | "at path. mode specifies both the permissions to use and the\n" |
| 12217 | "type of node to be created, being combined (bitwise OR) with one of\n" |
| 12218 | "S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,\n" |
| 12219 | "device defines the newly created device special file (probably using\n" |
| 12220 | "os.makedev()). Otherwise device is ignored.\n" |
| 12221 | "\n" |
| 12222 | "If dir_fd is not None, it should be a file descriptor open to a directory,\n" |
| 12223 | " and path should be relative; path will then be relative to that directory.\n" |
| 12224 | "dir_fd may not be implemented on your platform.\n" |
| 12225 | " If it is unavailable, using it will raise a NotImplementedError."); |
| 12226 | |
| 12227 | #define OS_MKNOD_METHODDEF \ |
| 12228 | {"mknod", (PyCFunction)os_mknod, METH_VARARGS|METH_KEYWORDS, os_mknod__doc__}, |
| 12229 | |
| 12230 | static PyObject * |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12231 | os_mknod_impl(PyModuleDef *module, path_t *path, int mode, dev_t device, int dir_fd); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12232 | |
| 12233 | static PyObject * |
| 12234 | os_mknod(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 12235 | { |
| 12236 | PyObject *return_value = NULL; |
| 12237 | static char *_keywords[] = {"path", "mode", "device", "dir_fd", NULL}; |
| 12238 | path_t path = PATH_T_INITIALIZE("mknod", "path", 0, 0); |
| 12239 | int mode = 384; |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12240 | dev_t device = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12241 | int dir_fd = DEFAULT_DIR_FD; |
| 12242 | |
| 12243 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12244 | "O&|iO&$O&:mknod", _keywords, |
| 12245 | path_converter, &path, &mode, _Py_Dev_Converter, &device, MKNODAT_DIR_FD_CONVERTER, &dir_fd)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12246 | goto exit; |
| 12247 | return_value = os_mknod_impl(module, &path, mode, device, dir_fd); |
| 12248 | |
| 12249 | exit: |
| 12250 | /* Cleanup for path */ |
| 12251 | path_cleanup(&path); |
| 12252 | |
| 12253 | return return_value; |
| 12254 | } |
| 12255 | |
| 12256 | static PyObject * |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12257 | os_mknod_impl(PyModuleDef *module, path_t *path, int mode, dev_t device, int dir_fd) |
| 12258 | /*[clinic end generated code: output=f71d54eaf9bb6f1a input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12259 | { |
| 12260 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12261 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12262 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12263 | do { |
| 12264 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12265 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12266 | if (dir_fd != DEFAULT_DIR_FD) |
| 12267 | result = mknodat(dir_fd, path->narrow, mode, device); |
| 12268 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12269 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12270 | result = mknod(path->narrow, mode, device); |
| 12271 | Py_END_ALLOW_THREADS |
| 12272 | } while (result != 0 && errno == EINTR && |
| 12273 | !(async_err = PyErr_CheckSignals())); |
| 12274 | if (result != 0) |
| 12275 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12276 | |
| 12277 | Py_RETURN_NONE; |
| 12278 | } |
| 12279 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ |
| 12280 | |
| 12281 | |
| 12282 | #ifdef HAVE_DEVICE_MACROS |
| 12283 | /*[clinic input] |
| 12284 | os.major -> unsigned_int |
| 12285 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12286 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12287 | / |
| 12288 | |
| 12289 | Extracts a device major number from a raw device number. |
| 12290 | [clinic start generated code]*/ |
| 12291 | |
| 12292 | PyDoc_STRVAR(os_major__doc__, |
| 12293 | "major($module, device, /)\n" |
| 12294 | "--\n" |
| 12295 | "\n" |
| 12296 | "Extracts a device major number from a raw device number."); |
| 12297 | |
| 12298 | #define OS_MAJOR_METHODDEF \ |
| 12299 | {"major", (PyCFunction)os_major, METH_VARARGS, os_major__doc__}, |
| 12300 | |
| 12301 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12302 | os_major_impl(PyModuleDef *module, dev_t device); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12303 | |
| 12304 | static PyObject * |
| 12305 | os_major(PyModuleDef *module, PyObject *args) |
| 12306 | { |
| 12307 | PyObject *return_value = NULL; |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12308 | dev_t device; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12309 | unsigned int _return_value; |
| 12310 | |
| 12311 | if (!PyArg_ParseTuple(args, |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12312 | "O&:major", |
| 12313 | _Py_Dev_Converter, &device)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12314 | goto exit; |
| 12315 | _return_value = os_major_impl(module, device); |
Larry Hastings | a73cb8a | 2014-08-05 19:55:21 +1000 | [diff] [blame] | 12316 | if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12317 | goto exit; |
| 12318 | return_value = PyLong_FromUnsignedLong((unsigned long)_return_value); |
| 12319 | |
| 12320 | exit: |
| 12321 | return return_value; |
| 12322 | } |
| 12323 | |
| 12324 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12325 | os_major_impl(PyModuleDef *module, dev_t device) |
| 12326 | /*[clinic end generated code: output=a2d06e908ebf95b5 input=1e16a4d30c4d4462]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12327 | { |
| 12328 | return major(device); |
| 12329 | } |
| 12330 | |
| 12331 | |
| 12332 | /*[clinic input] |
| 12333 | os.minor -> unsigned_int |
| 12334 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12335 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12336 | / |
| 12337 | |
| 12338 | Extracts a device minor number from a raw device number. |
| 12339 | [clinic start generated code]*/ |
| 12340 | |
| 12341 | PyDoc_STRVAR(os_minor__doc__, |
| 12342 | "minor($module, device, /)\n" |
| 12343 | "--\n" |
| 12344 | "\n" |
| 12345 | "Extracts a device minor number from a raw device number."); |
| 12346 | |
| 12347 | #define OS_MINOR_METHODDEF \ |
| 12348 | {"minor", (PyCFunction)os_minor, METH_VARARGS, os_minor__doc__}, |
| 12349 | |
| 12350 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12351 | os_minor_impl(PyModuleDef *module, dev_t device); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12352 | |
| 12353 | static PyObject * |
| 12354 | os_minor(PyModuleDef *module, PyObject *args) |
| 12355 | { |
| 12356 | PyObject *return_value = NULL; |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12357 | dev_t device; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12358 | unsigned int _return_value; |
| 12359 | |
| 12360 | if (!PyArg_ParseTuple(args, |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12361 | "O&:minor", |
| 12362 | _Py_Dev_Converter, &device)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12363 | goto exit; |
| 12364 | _return_value = os_minor_impl(module, device); |
Larry Hastings | a73cb8a | 2014-08-05 19:55:21 +1000 | [diff] [blame] | 12365 | if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12366 | goto exit; |
| 12367 | return_value = PyLong_FromUnsignedLong((unsigned long)_return_value); |
| 12368 | |
| 12369 | exit: |
| 12370 | return return_value; |
| 12371 | } |
| 12372 | |
| 12373 | static unsigned int |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12374 | os_minor_impl(PyModuleDef *module, dev_t device) |
| 12375 | /*[clinic end generated code: output=6332287ee3f006e2 input=0842c6d23f24c65e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12376 | { |
| 12377 | return minor(device); |
| 12378 | } |
| 12379 | |
| 12380 | |
| 12381 | /*[clinic input] |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12382 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12383 | |
| 12384 | major: int |
| 12385 | minor: int |
| 12386 | / |
| 12387 | |
| 12388 | Composes a raw device number from the major and minor device numbers. |
| 12389 | [clinic start generated code]*/ |
| 12390 | |
| 12391 | PyDoc_STRVAR(os_makedev__doc__, |
| 12392 | "makedev($module, major, minor, /)\n" |
| 12393 | "--\n" |
| 12394 | "\n" |
| 12395 | "Composes a raw device number from the major and minor device numbers."); |
| 12396 | |
| 12397 | #define OS_MAKEDEV_METHODDEF \ |
| 12398 | {"makedev", (PyCFunction)os_makedev, METH_VARARGS, os_makedev__doc__}, |
| 12399 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12400 | static dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12401 | os_makedev_impl(PyModuleDef *module, int major, int minor); |
| 12402 | |
| 12403 | static PyObject * |
| 12404 | os_makedev(PyModuleDef *module, PyObject *args) |
| 12405 | { |
| 12406 | PyObject *return_value = NULL; |
| 12407 | int major; |
| 12408 | int minor; |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12409 | dev_t _return_value; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12410 | |
| 12411 | if (!PyArg_ParseTuple(args, |
| 12412 | "ii:makedev", |
| 12413 | &major, &minor)) |
| 12414 | goto exit; |
| 12415 | _return_value = os_makedev_impl(module, major, minor); |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12416 | if ((_return_value == (dev_t)-1) && PyErr_Occurred()) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12417 | goto exit; |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12418 | return_value = _PyLong_FromDev(_return_value); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12419 | |
| 12420 | exit: |
| 12421 | return return_value; |
| 12422 | } |
| 12423 | |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12424 | static dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12425 | os_makedev_impl(PyModuleDef *module, int major, int minor) |
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 12426 | /*[clinic end generated code: output=38e9a9774c96511a input=4b9fd8fc73cbe48f]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12427 | { |
| 12428 | return makedev(major, minor); |
| 12429 | } |
| 12430 | #endif /* HAVE_DEVICE_MACROS */ |
| 12431 | |
| 12432 | |
| 12433 | #ifdef HAVE_FTRUNCATE |
| 12434 | /*[clinic input] |
| 12435 | os.ftruncate |
| 12436 | |
| 12437 | fd: int |
| 12438 | length: Py_off_t |
| 12439 | / |
| 12440 | |
| 12441 | Truncate a file, specified by file descriptor, to a specific length. |
| 12442 | [clinic start generated code]*/ |
| 12443 | |
| 12444 | PyDoc_STRVAR(os_ftruncate__doc__, |
| 12445 | "ftruncate($module, fd, length, /)\n" |
| 12446 | "--\n" |
| 12447 | "\n" |
| 12448 | "Truncate a file, specified by file descriptor, to a specific length."); |
| 12449 | |
| 12450 | #define OS_FTRUNCATE_METHODDEF \ |
| 12451 | {"ftruncate", (PyCFunction)os_ftruncate, METH_VARARGS, os_ftruncate__doc__}, |
| 12452 | |
| 12453 | static PyObject * |
| 12454 | os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length); |
| 12455 | |
| 12456 | static PyObject * |
| 12457 | os_ftruncate(PyModuleDef *module, PyObject *args) |
| 12458 | { |
| 12459 | PyObject *return_value = NULL; |
| 12460 | int fd; |
| 12461 | Py_off_t length; |
| 12462 | |
| 12463 | if (!PyArg_ParseTuple(args, |
| 12464 | "iO&:ftruncate", |
| 12465 | &fd, Py_off_t_converter, &length)) |
| 12466 | goto exit; |
| 12467 | return_value = os_ftruncate_impl(module, fd, length); |
| 12468 | |
| 12469 | exit: |
| 12470 | return return_value; |
| 12471 | } |
| 12472 | |
| 12473 | static PyObject * |
| 12474 | os_ftruncate_impl(PyModuleDef *module, int fd, Py_off_t length) |
| 12475 | /*[clinic end generated code: output=62326766cb9b76bf input=63b43641e52818f2]*/ |
| 12476 | { |
| 12477 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12478 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12479 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12480 | do { |
| 12481 | Py_BEGIN_ALLOW_THREADS |
| 12482 | result = ftruncate(fd, length); |
| 12483 | Py_END_ALLOW_THREADS |
| 12484 | } while (result != 0 && errno == EINTR && |
| 12485 | !(async_err = PyErr_CheckSignals())); |
| 12486 | if (result != 0) |
| 12487 | return (!async_err) ? posix_error() : NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12488 | Py_RETURN_NONE; |
| 12489 | } |
| 12490 | #endif /* HAVE_FTRUNCATE */ |
| 12491 | |
| 12492 | |
| 12493 | #ifdef HAVE_TRUNCATE |
| 12494 | /*[clinic input] |
| 12495 | os.truncate |
| 12496 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') |
| 12497 | length: Py_off_t |
| 12498 | |
| 12499 | Truncate a file, specified by path, to a specific length. |
| 12500 | |
| 12501 | On some platforms, path may also be specified as an open file descriptor. |
| 12502 | If this functionality is unavailable, using it raises an exception. |
| 12503 | [clinic start generated code]*/ |
| 12504 | |
| 12505 | PyDoc_STRVAR(os_truncate__doc__, |
| 12506 | "truncate($module, /, path, length)\n" |
| 12507 | "--\n" |
| 12508 | "\n" |
| 12509 | "Truncate a file, specified by path, to a specific length.\n" |
| 12510 | "\n" |
| 12511 | "On some platforms, path may also be specified as an open file descriptor.\n" |
| 12512 | " If this functionality is unavailable, using it raises an exception."); |
| 12513 | |
| 12514 | #define OS_TRUNCATE_METHODDEF \ |
| 12515 | {"truncate", (PyCFunction)os_truncate, METH_VARARGS|METH_KEYWORDS, os_truncate__doc__}, |
| 12516 | |
| 12517 | static PyObject * |
| 12518 | os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length); |
| 12519 | |
| 12520 | static PyObject * |
| 12521 | os_truncate(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 12522 | { |
| 12523 | PyObject *return_value = NULL; |
| 12524 | static char *_keywords[] = {"path", "length", NULL}; |
| 12525 | path_t path = PATH_T_INITIALIZE("truncate", "path", 0, PATH_HAVE_FTRUNCATE); |
| 12526 | Py_off_t length; |
| 12527 | |
| 12528 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 12529 | "O&O&:truncate", _keywords, |
| 12530 | path_converter, &path, Py_off_t_converter, &length)) |
| 12531 | goto exit; |
| 12532 | return_value = os_truncate_impl(module, &path, length); |
| 12533 | |
| 12534 | exit: |
| 12535 | /* Cleanup for path */ |
| 12536 | path_cleanup(&path); |
| 12537 | |
| 12538 | return return_value; |
| 12539 | } |
| 12540 | |
| 12541 | static PyObject * |
| 12542 | os_truncate_impl(PyModuleDef *module, path_t *path, Py_off_t length) |
| 12543 | /*[clinic end generated code: output=6bd76262d2e027c6 input=77229cf0b50a9b77]*/ |
| 12544 | { |
| 12545 | int result; |
| 12546 | |
| 12547 | Py_BEGIN_ALLOW_THREADS |
| 12548 | #ifdef HAVE_FTRUNCATE |
| 12549 | if (path->fd != -1) |
| 12550 | result = ftruncate(path->fd, length); |
| 12551 | else |
| 12552 | #endif |
| 12553 | result = truncate(path->narrow, length); |
| 12554 | Py_END_ALLOW_THREADS |
| 12555 | if (result < 0) |
| 12556 | return path_error(path); |
| 12557 | |
| 12558 | Py_RETURN_NONE; |
| 12559 | } |
| 12560 | #endif /* HAVE_TRUNCATE */ |
| 12561 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12562 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 12563 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
| 12564 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is |
| 12565 | defined, which is the case in Python on AIX. AIX bug report: |
| 12566 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ |
| 12567 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) |
| 12568 | # define POSIX_FADVISE_AIX_BUG |
| 12569 | #endif |
| 12570 | |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 12571 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 12572 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12573 | /*[clinic input] |
| 12574 | os.posix_fallocate |
| 12575 | |
| 12576 | fd: int |
| 12577 | offset: Py_off_t |
| 12578 | length: Py_off_t |
| 12579 | / |
| 12580 | |
| 12581 | Ensure a file has allocated at least a particular number of bytes on disk. |
| 12582 | |
| 12583 | Ensure that the file specified by fd encompasses a range of bytes |
| 12584 | starting at offset bytes from the beginning and continuing for length bytes. |
| 12585 | [clinic start generated code]*/ |
| 12586 | |
| 12587 | PyDoc_STRVAR(os_posix_fallocate__doc__, |
| 12588 | "posix_fallocate($module, fd, offset, length, /)\n" |
| 12589 | "--\n" |
| 12590 | "\n" |
| 12591 | "Ensure a file has allocated at least a particular number of bytes on disk.\n" |
| 12592 | "\n" |
| 12593 | "Ensure that the file specified by fd encompasses a range of bytes\n" |
| 12594 | "starting at offset bytes from the beginning and continuing for length bytes."); |
| 12595 | |
| 12596 | #define OS_POSIX_FALLOCATE_METHODDEF \ |
| 12597 | {"posix_fallocate", (PyCFunction)os_posix_fallocate, METH_VARARGS, os_posix_fallocate__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12598 | |
| 12599 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12600 | os_posix_fallocate_impl(PyModuleDef *module, int fd, Py_off_t offset, Py_off_t length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12601 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12602 | static PyObject * |
| 12603 | os_posix_fallocate(PyModuleDef *module, PyObject *args) |
| 12604 | { |
| 12605 | PyObject *return_value = NULL; |
| 12606 | int fd; |
| 12607 | Py_off_t offset; |
| 12608 | Py_off_t length; |
| 12609 | |
| 12610 | if (!PyArg_ParseTuple(args, |
| 12611 | "iO&O&:posix_fallocate", |
| 12612 | &fd, Py_off_t_converter, &offset, Py_off_t_converter, &length)) |
| 12613 | goto exit; |
| 12614 | return_value = os_posix_fallocate_impl(module, fd, offset, length); |
| 12615 | |
| 12616 | exit: |
| 12617 | return return_value; |
| 12618 | } |
| 12619 | |
| 12620 | static PyObject * |
| 12621 | os_posix_fallocate_impl(PyModuleDef *module, int fd, Py_off_t offset, Py_off_t length) |
| 12622 | /*[clinic end generated code: output=0cd702d2065c79db input=d7a2ef0ab2ca52fb]*/ |
| 12623 | { |
| 12624 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12625 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12626 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12627 | do { |
| 12628 | Py_BEGIN_ALLOW_THREADS |
| 12629 | result = posix_fallocate(fd, offset, length); |
| 12630 | Py_END_ALLOW_THREADS |
| 12631 | } while (result != 0 && errno == EINTR && |
| 12632 | !(async_err = PyErr_CheckSignals())); |
| 12633 | if (result != 0) |
| 12634 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12635 | Py_RETURN_NONE; |
| 12636 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 12637 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12638 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12639 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 12640 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12641 | /*[clinic input] |
| 12642 | os.posix_fadvise |
| 12643 | |
| 12644 | fd: int |
| 12645 | offset: Py_off_t |
| 12646 | length: Py_off_t |
| 12647 | advice: int |
| 12648 | / |
| 12649 | |
| 12650 | Announce an intention to access data in a specific pattern. |
| 12651 | |
| 12652 | Announce an intention to access data in a specific pattern, thus allowing |
| 12653 | the kernel to make optimizations. |
| 12654 | The advice applies to the region of the file specified by fd starting at |
| 12655 | offset and continuing for length bytes. |
| 12656 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, |
| 12657 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or |
| 12658 | POSIX_FADV_DONTNEED. |
| 12659 | [clinic start generated code]*/ |
| 12660 | |
| 12661 | PyDoc_STRVAR(os_posix_fadvise__doc__, |
| 12662 | "posix_fadvise($module, fd, offset, length, advice, /)\n" |
| 12663 | "--\n" |
| 12664 | "\n" |
| 12665 | "Announce an intention to access data in a specific pattern.\n" |
| 12666 | "\n" |
| 12667 | "Announce an intention to access data in a specific pattern, thus allowing\n" |
| 12668 | "the kernel to make optimizations.\n" |
| 12669 | "The advice applies to the region of the file specified by fd starting at\n" |
| 12670 | "offset and continuing for length bytes.\n" |
| 12671 | "advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n" |
| 12672 | "POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or\n" |
| 12673 | "POSIX_FADV_DONTNEED."); |
| 12674 | |
| 12675 | #define OS_POSIX_FADVISE_METHODDEF \ |
| 12676 | {"posix_fadvise", (PyCFunction)os_posix_fadvise, METH_VARARGS, os_posix_fadvise__doc__}, |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12677 | |
| 12678 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12679 | os_posix_fadvise_impl(PyModuleDef *module, int fd, Py_off_t offset, Py_off_t length, int advice); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12680 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12681 | static PyObject * |
| 12682 | os_posix_fadvise(PyModuleDef *module, PyObject *args) |
| 12683 | { |
| 12684 | PyObject *return_value = NULL; |
| 12685 | int fd; |
| 12686 | Py_off_t offset; |
| 12687 | Py_off_t length; |
| 12688 | int advice; |
| 12689 | |
| 12690 | if (!PyArg_ParseTuple(args, |
| 12691 | "iO&O&i:posix_fadvise", |
| 12692 | &fd, Py_off_t_converter, &offset, Py_off_t_converter, &length, &advice)) |
| 12693 | goto exit; |
| 12694 | return_value = os_posix_fadvise_impl(module, fd, offset, length, advice); |
| 12695 | |
| 12696 | exit: |
| 12697 | return return_value; |
| 12698 | } |
| 12699 | |
| 12700 | static PyObject * |
| 12701 | os_posix_fadvise_impl(PyModuleDef *module, int fd, Py_off_t offset, Py_off_t length, int advice) |
| 12702 | /*[clinic end generated code: output=dad93f32c04dd4f7 input=0fbe554edc2f04b5]*/ |
| 12703 | { |
| 12704 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12705 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12706 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 12707 | do { |
| 12708 | Py_BEGIN_ALLOW_THREADS |
| 12709 | result = posix_fadvise(fd, offset, length, advice); |
| 12710 | Py_END_ALLOW_THREADS |
| 12711 | } while (result != 0 && errno == EINTR && |
| 12712 | !(async_err = PyErr_CheckSignals())); |
| 12713 | if (result != 0) |
| 12714 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12715 | Py_RETURN_NONE; |
| 12716 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 12717 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 12718 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 12719 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 12720 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 12721 | /* Save putenv() parameters as values here, so we can collect them when they |
| 12722 | * get re-set with another call for the same key. */ |
| 12723 | static PyObject *posix_putenv_garbage; |
| 12724 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12725 | static void |
| 12726 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 12727 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12728 | /* Install the first arg and newstr in posix_putenv_garbage; |
| 12729 | * this will cause previous value to be collected. This has to |
| 12730 | * happen after the real putenv() call because the old value |
| 12731 | * was still accessible until then. */ |
| 12732 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) |
| 12733 | /* really not much we can do; just leak */ |
| 12734 | PyErr_Clear(); |
| 12735 | else |
| 12736 | Py_DECREF(value); |
| 12737 | } |
| 12738 | |
| 12739 | |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 12740 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12741 | /*[clinic input] |
| 12742 | os.putenv |
| 12743 | |
| 12744 | name: unicode |
| 12745 | value: unicode |
| 12746 | / |
| 12747 | |
| 12748 | Change or add an environment variable. |
| 12749 | [clinic start generated code]*/ |
| 12750 | |
| 12751 | PyDoc_STRVAR(os_putenv__doc__, |
| 12752 | "putenv($module, name, value, /)\n" |
| 12753 | "--\n" |
| 12754 | "\n" |
| 12755 | "Change or add an environment variable."); |
| 12756 | |
| 12757 | #define OS_PUTENV_METHODDEF \ |
| 12758 | {"putenv", (PyCFunction)os_putenv, METH_VARARGS, os_putenv__doc__}, |
| 12759 | |
| 12760 | static PyObject * |
| 12761 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value); |
| 12762 | |
| 12763 | static PyObject * |
| 12764 | os_putenv(PyModuleDef *module, PyObject *args) |
| 12765 | { |
| 12766 | PyObject *return_value = NULL; |
| 12767 | PyObject *name; |
| 12768 | PyObject *value; |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 12769 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12770 | if (!PyArg_ParseTuple(args, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12771 | "UU:putenv", |
| 12772 | &name, &value)) |
| 12773 | goto exit; |
| 12774 | return_value = os_putenv_impl(module, name, value); |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 12775 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12776 | exit: |
| 12777 | return return_value; |
| 12778 | } |
| 12779 | |
| 12780 | static PyObject * |
| 12781 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
| 12782 | /*[clinic end generated code: output=5ce9ef9b15606e7e input=ba586581c2e6105f]*/ |
| 12783 | { |
| 12784 | wchar_t *env; |
| 12785 | |
| 12786 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
| 12787 | if (unicode == NULL) { |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12788 | PyErr_NoMemory(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12789 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12790 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12791 | if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) { |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 12792 | PyErr_Format(PyExc_ValueError, |
| 12793 | "the environment variable is longer than %u characters", |
| 12794 | _MAX_ENV); |
| 12795 | goto error; |
| 12796 | } |
| 12797 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12798 | env = PyUnicode_AsUnicode(unicode); |
| 12799 | if (env == NULL) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 12800 | goto error; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12801 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12802 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12803 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12804 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 12805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12806 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12807 | Py_RETURN_NONE; |
| 12808 | |
| 12809 | error: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12810 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12811 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 12812 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12813 | #else /* MS_WINDOWS */ |
| 12814 | /*[clinic input] |
| 12815 | os.putenv |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 12816 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12817 | name: FSConverter |
| 12818 | value: FSConverter |
| 12819 | / |
| 12820 | |
| 12821 | Change or add an environment variable. |
| 12822 | [clinic start generated code]*/ |
| 12823 | |
| 12824 | PyDoc_STRVAR(os_putenv__doc__, |
| 12825 | "putenv($module, name, value, /)\n" |
| 12826 | "--\n" |
| 12827 | "\n" |
| 12828 | "Change or add an environment variable."); |
| 12829 | |
| 12830 | #define OS_PUTENV_METHODDEF \ |
| 12831 | {"putenv", (PyCFunction)os_putenv, METH_VARARGS, os_putenv__doc__}, |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 12832 | |
| 12833 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12834 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value); |
| 12835 | |
| 12836 | static PyObject * |
| 12837 | os_putenv(PyModuleDef *module, PyObject *args) |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 12838 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12839 | PyObject *return_value = NULL; |
| 12840 | PyObject *name = NULL; |
| 12841 | PyObject *value = NULL; |
| 12842 | |
| 12843 | if (!PyArg_ParseTuple(args, |
| 12844 | "O&O&:putenv", |
| 12845 | PyUnicode_FSConverter, &name, PyUnicode_FSConverter, &value)) |
| 12846 | goto exit; |
| 12847 | return_value = os_putenv_impl(module, name, value); |
| 12848 | |
| 12849 | exit: |
| 12850 | /* Cleanup for name */ |
| 12851 | Py_XDECREF(name); |
| 12852 | /* Cleanup for value */ |
| 12853 | Py_XDECREF(value); |
| 12854 | |
| 12855 | return return_value; |
| 12856 | } |
| 12857 | |
| 12858 | static PyObject * |
| 12859 | os_putenv_impl(PyModuleDef *module, PyObject *name, PyObject *value) |
| 12860 | /*[clinic end generated code: output=85ab223393dc7afd input=a97bc6152f688d31]*/ |
| 12861 | { |
| 12862 | PyObject *bytes = NULL; |
| 12863 | char *env; |
| 12864 | char *name_string = PyBytes_AsString(name); |
| 12865 | char *value_string = PyBytes_AsString(value); |
| 12866 | |
| 12867 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
| 12868 | if (bytes == NULL) { |
| 12869 | PyErr_NoMemory(); |
| 12870 | return NULL; |
| 12871 | } |
| 12872 | |
| 12873 | env = PyBytes_AS_STRING(bytes); |
| 12874 | if (putenv(env)) { |
| 12875 | Py_DECREF(bytes); |
| 12876 | return posix_error(); |
| 12877 | } |
| 12878 | |
| 12879 | posix_putenv_garbage_setitem(name, bytes); |
| 12880 | Py_RETURN_NONE; |
| 12881 | } |
| 12882 | #endif /* MS_WINDOWS */ |
| 12883 | #endif /* HAVE_PUTENV */ |
| 12884 | |
| 12885 | |
| 12886 | #ifdef HAVE_UNSETENV |
| 12887 | /*[clinic input] |
| 12888 | os.unsetenv |
| 12889 | name: FSConverter |
| 12890 | / |
| 12891 | |
| 12892 | Delete an environment variable. |
| 12893 | [clinic start generated code]*/ |
| 12894 | |
| 12895 | PyDoc_STRVAR(os_unsetenv__doc__, |
| 12896 | "unsetenv($module, name, /)\n" |
| 12897 | "--\n" |
| 12898 | "\n" |
| 12899 | "Delete an environment variable."); |
| 12900 | |
| 12901 | #define OS_UNSETENV_METHODDEF \ |
| 12902 | {"unsetenv", (PyCFunction)os_unsetenv, METH_VARARGS, os_unsetenv__doc__}, |
| 12903 | |
| 12904 | static PyObject * |
| 12905 | os_unsetenv_impl(PyModuleDef *module, PyObject *name); |
| 12906 | |
| 12907 | static PyObject * |
| 12908 | os_unsetenv(PyModuleDef *module, PyObject *args) |
| 12909 | { |
| 12910 | PyObject *return_value = NULL; |
| 12911 | PyObject *name = NULL; |
| 12912 | |
| 12913 | if (!PyArg_ParseTuple(args, |
| 12914 | "O&:unsetenv", |
| 12915 | PyUnicode_FSConverter, &name)) |
| 12916 | goto exit; |
| 12917 | return_value = os_unsetenv_impl(module, name); |
| 12918 | |
| 12919 | exit: |
| 12920 | /* Cleanup for name */ |
| 12921 | Py_XDECREF(name); |
| 12922 | |
| 12923 | return return_value; |
| 12924 | } |
| 12925 | |
| 12926 | static PyObject * |
| 12927 | os_unsetenv_impl(PyModuleDef *module, PyObject *name) |
| 12928 | /*[clinic end generated code: output=91318c995f9a0767 input=2bb5288a599c7107]*/ |
| 12929 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 12930 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 12931 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 12932 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12933 | |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 12934 | #ifdef HAVE_BROKEN_UNSETENV |
| 12935 | unsetenv(PyBytes_AS_STRING(name)); |
| 12936 | #else |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 12937 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12938 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 12939 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 12940 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 12941 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12942 | /* Remove the key from posix_putenv_garbage; |
| 12943 | * this will cause it to be collected. This has to |
| 12944 | * happen after the real unsetenv() call because the |
| 12945 | * old value was still accessible until then. |
| 12946 | */ |
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 12947 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12948 | /* really not much we can do; just leak */ |
| 12949 | PyErr_Clear(); |
| 12950 | } |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 12951 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 12952 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12953 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 12954 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12955 | |
| 12956 | /*[clinic input] |
| 12957 | os.strerror |
| 12958 | |
| 12959 | code: int |
| 12960 | / |
| 12961 | |
| 12962 | Translate an error code to a message string. |
| 12963 | [clinic start generated code]*/ |
| 12964 | |
| 12965 | PyDoc_STRVAR(os_strerror__doc__, |
| 12966 | "strerror($module, code, /)\n" |
| 12967 | "--\n" |
| 12968 | "\n" |
| 12969 | "Translate an error code to a message string."); |
| 12970 | |
| 12971 | #define OS_STRERROR_METHODDEF \ |
| 12972 | {"strerror", (PyCFunction)os_strerror, METH_VARARGS, os_strerror__doc__}, |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 12973 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 12974 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12975 | os_strerror_impl(PyModuleDef *module, int code); |
| 12976 | |
| 12977 | static PyObject * |
| 12978 | os_strerror(PyModuleDef *module, PyObject *args) |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 12979 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12980 | PyObject *return_value = NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12981 | int code; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12982 | |
| 12983 | if (!PyArg_ParseTuple(args, |
| 12984 | "i:strerror", |
| 12985 | &code)) |
| 12986 | goto exit; |
| 12987 | return_value = os_strerror_impl(module, code); |
| 12988 | |
| 12989 | exit: |
| 12990 | return return_value; |
| 12991 | } |
| 12992 | |
| 12993 | static PyObject * |
| 12994 | os_strerror_impl(PyModuleDef *module, int code) |
| 12995 | /*[clinic end generated code: output=8665c70bb2ca4720 input=75a8673d97915a91]*/ |
| 12996 | { |
| 12997 | char *message = strerror(code); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 12998 | if (message == NULL) { |
| 12999 | PyErr_SetString(PyExc_ValueError, |
| 13000 | "strerror() argument out of range"); |
| 13001 | return NULL; |
| 13002 | } |
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 13003 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 13004 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 13005 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 13006 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13007 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13008 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13009 | /*[clinic input] |
| 13010 | os.WCOREDUMP -> bool |
| 13011 | |
| 13012 | status: int |
| 13013 | / |
| 13014 | |
| 13015 | Return True if the process returning status was dumped to a core file. |
| 13016 | [clinic start generated code]*/ |
| 13017 | |
| 13018 | PyDoc_STRVAR(os_WCOREDUMP__doc__, |
| 13019 | "WCOREDUMP($module, status, /)\n" |
| 13020 | "--\n" |
| 13021 | "\n" |
| 13022 | "Return True if the process returning status was dumped to a core file."); |
| 13023 | |
| 13024 | #define OS_WCOREDUMP_METHODDEF \ |
| 13025 | {"WCOREDUMP", (PyCFunction)os_WCOREDUMP, METH_VARARGS, os_WCOREDUMP__doc__}, |
| 13026 | |
| 13027 | static int |
| 13028 | os_WCOREDUMP_impl(PyModuleDef *module, int status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13029 | |
| 13030 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13031 | os_WCOREDUMP(PyModuleDef *module, PyObject *args) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13032 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13033 | PyObject *return_value = NULL; |
| 13034 | int status; |
| 13035 | int _return_value; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13036 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13037 | if (!PyArg_ParseTuple(args, |
| 13038 | "i:WCOREDUMP", |
| 13039 | &status)) |
| 13040 | goto exit; |
| 13041 | _return_value = os_WCOREDUMP_impl(module, status); |
| 13042 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13043 | goto exit; |
| 13044 | return_value = PyBool_FromLong((long)_return_value); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13045 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13046 | exit: |
| 13047 | return return_value; |
| 13048 | } |
| 13049 | |
| 13050 | static int |
| 13051 | os_WCOREDUMP_impl(PyModuleDef *module, int status) |
| 13052 | /*[clinic end generated code: output=e04d55c09c299828 input=8b05e7ab38528d04]*/ |
| 13053 | { |
| 13054 | WAIT_TYPE wait_status; |
| 13055 | WAIT_STATUS_INT(wait_status) = status; |
| 13056 | return WCOREDUMP(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13057 | } |
| 13058 | #endif /* WCOREDUMP */ |
| 13059 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13060 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13061 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13062 | /*[clinic input] |
| 13063 | os.WIFCONTINUED -> bool |
| 13064 | |
| 13065 | status: int |
| 13066 | |
| 13067 | Return True if a particular process was continued from a job control stop. |
| 13068 | |
| 13069 | Return True if the process returning status was continued from a |
| 13070 | job control stop. |
| 13071 | [clinic start generated code]*/ |
| 13072 | |
| 13073 | PyDoc_STRVAR(os_WIFCONTINUED__doc__, |
| 13074 | "WIFCONTINUED($module, /, status)\n" |
| 13075 | "--\n" |
| 13076 | "\n" |
| 13077 | "Return True if a particular process was continued from a job control stop.\n" |
| 13078 | "\n" |
| 13079 | "Return True if the process returning status was continued from a\n" |
| 13080 | "job control stop."); |
| 13081 | |
| 13082 | #define OS_WIFCONTINUED_METHODDEF \ |
| 13083 | {"WIFCONTINUED", (PyCFunction)os_WIFCONTINUED, METH_VARARGS|METH_KEYWORDS, os_WIFCONTINUED__doc__}, |
| 13084 | |
| 13085 | static int |
| 13086 | os_WIFCONTINUED_impl(PyModuleDef *module, int status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13087 | |
| 13088 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13089 | os_WIFCONTINUED(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13090 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13091 | PyObject *return_value = NULL; |
| 13092 | static char *_keywords[] = {"status", NULL}; |
| 13093 | int status; |
| 13094 | int _return_value; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13095 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13096 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13097 | "i:WIFCONTINUED", _keywords, |
| 13098 | &status)) |
| 13099 | goto exit; |
| 13100 | _return_value = os_WIFCONTINUED_impl(module, status); |
| 13101 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13102 | goto exit; |
| 13103 | return_value = PyBool_FromLong((long)_return_value); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13104 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13105 | exit: |
| 13106 | return return_value; |
| 13107 | } |
| 13108 | |
| 13109 | static int |
| 13110 | os_WIFCONTINUED_impl(PyModuleDef *module, int status) |
| 13111 | /*[clinic end generated code: output=9c4e6105a4520ab5 input=e777e7d38eb25bd9]*/ |
| 13112 | { |
| 13113 | WAIT_TYPE wait_status; |
| 13114 | WAIT_STATUS_INT(wait_status) = status; |
| 13115 | return WIFCONTINUED(wait_status); |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13116 | } |
| 13117 | #endif /* WIFCONTINUED */ |
| 13118 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13119 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13120 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13121 | /*[clinic input] |
| 13122 | os.WIFSTOPPED -> bool |
| 13123 | |
| 13124 | status: int |
| 13125 | |
| 13126 | Return True if the process returning status was stopped. |
| 13127 | [clinic start generated code]*/ |
| 13128 | |
| 13129 | PyDoc_STRVAR(os_WIFSTOPPED__doc__, |
| 13130 | "WIFSTOPPED($module, /, status)\n" |
| 13131 | "--\n" |
| 13132 | "\n" |
| 13133 | "Return True if the process returning status was stopped."); |
| 13134 | |
| 13135 | #define OS_WIFSTOPPED_METHODDEF \ |
| 13136 | {"WIFSTOPPED", (PyCFunction)os_WIFSTOPPED, METH_VARARGS|METH_KEYWORDS, os_WIFSTOPPED__doc__}, |
| 13137 | |
| 13138 | static int |
| 13139 | os_WIFSTOPPED_impl(PyModuleDef *module, int status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13140 | |
| 13141 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13142 | os_WIFSTOPPED(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13143 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13144 | PyObject *return_value = NULL; |
| 13145 | static char *_keywords[] = {"status", NULL}; |
| 13146 | int status; |
| 13147 | int _return_value; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13148 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13149 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13150 | "i:WIFSTOPPED", _keywords, |
| 13151 | &status)) |
| 13152 | goto exit; |
| 13153 | _return_value = os_WIFSTOPPED_impl(module, status); |
| 13154 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13155 | goto exit; |
| 13156 | return_value = PyBool_FromLong((long)_return_value); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13157 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13158 | exit: |
| 13159 | return return_value; |
| 13160 | } |
| 13161 | |
| 13162 | static int |
| 13163 | os_WIFSTOPPED_impl(PyModuleDef *module, int status) |
| 13164 | /*[clinic end generated code: output=e0de2da8ec9593ff input=043cb7f1289ef904]*/ |
| 13165 | { |
| 13166 | WAIT_TYPE wait_status; |
| 13167 | WAIT_STATUS_INT(wait_status) = status; |
| 13168 | return WIFSTOPPED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13169 | } |
| 13170 | #endif /* WIFSTOPPED */ |
| 13171 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13172 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13173 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13174 | /*[clinic input] |
| 13175 | os.WIFSIGNALED -> bool |
| 13176 | |
| 13177 | status: int |
| 13178 | |
| 13179 | Return True if the process returning status was terminated by a signal. |
| 13180 | [clinic start generated code]*/ |
| 13181 | |
| 13182 | PyDoc_STRVAR(os_WIFSIGNALED__doc__, |
| 13183 | "WIFSIGNALED($module, /, status)\n" |
| 13184 | "--\n" |
| 13185 | "\n" |
| 13186 | "Return True if the process returning status was terminated by a signal."); |
| 13187 | |
| 13188 | #define OS_WIFSIGNALED_METHODDEF \ |
| 13189 | {"WIFSIGNALED", (PyCFunction)os_WIFSIGNALED, METH_VARARGS|METH_KEYWORDS, os_WIFSIGNALED__doc__}, |
| 13190 | |
| 13191 | static int |
| 13192 | os_WIFSIGNALED_impl(PyModuleDef *module, int status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13193 | |
| 13194 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13195 | os_WIFSIGNALED(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13196 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13197 | PyObject *return_value = NULL; |
| 13198 | static char *_keywords[] = {"status", NULL}; |
| 13199 | int status; |
| 13200 | int _return_value; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13202 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13203 | "i:WIFSIGNALED", _keywords, |
| 13204 | &status)) |
| 13205 | goto exit; |
| 13206 | _return_value = os_WIFSIGNALED_impl(module, status); |
| 13207 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13208 | goto exit; |
| 13209 | return_value = PyBool_FromLong((long)_return_value); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13210 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13211 | exit: |
| 13212 | return return_value; |
| 13213 | } |
| 13214 | |
| 13215 | static int |
| 13216 | os_WIFSIGNALED_impl(PyModuleDef *module, int status) |
| 13217 | /*[clinic end generated code: output=f14d106558f406be input=d55ba7cc9ce5dc43]*/ |
| 13218 | { |
| 13219 | WAIT_TYPE wait_status; |
| 13220 | WAIT_STATUS_INT(wait_status) = status; |
| 13221 | return WIFSIGNALED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13222 | } |
| 13223 | #endif /* WIFSIGNALED */ |
| 13224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13225 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13226 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13227 | /*[clinic input] |
| 13228 | os.WIFEXITED -> bool |
| 13229 | |
| 13230 | status: int |
| 13231 | |
| 13232 | Return True if the process returning status exited via the exit() system call. |
| 13233 | [clinic start generated code]*/ |
| 13234 | |
| 13235 | PyDoc_STRVAR(os_WIFEXITED__doc__, |
| 13236 | "WIFEXITED($module, /, status)\n" |
| 13237 | "--\n" |
| 13238 | "\n" |
| 13239 | "Return True if the process returning status exited via the exit() system call."); |
| 13240 | |
| 13241 | #define OS_WIFEXITED_METHODDEF \ |
| 13242 | {"WIFEXITED", (PyCFunction)os_WIFEXITED, METH_VARARGS|METH_KEYWORDS, os_WIFEXITED__doc__}, |
| 13243 | |
| 13244 | static int |
| 13245 | os_WIFEXITED_impl(PyModuleDef *module, int status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13246 | |
| 13247 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13248 | os_WIFEXITED(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13249 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13250 | PyObject *return_value = NULL; |
| 13251 | static char *_keywords[] = {"status", NULL}; |
| 13252 | int status; |
| 13253 | int _return_value; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13254 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13255 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13256 | "i:WIFEXITED", _keywords, |
| 13257 | &status)) |
| 13258 | goto exit; |
| 13259 | _return_value = os_WIFEXITED_impl(module, status); |
| 13260 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13261 | goto exit; |
| 13262 | return_value = PyBool_FromLong((long)_return_value); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13263 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13264 | exit: |
| 13265 | return return_value; |
| 13266 | } |
| 13267 | |
| 13268 | static int |
| 13269 | os_WIFEXITED_impl(PyModuleDef *module, int status) |
| 13270 | /*[clinic end generated code: output=2f76087d53721255 input=d63775a6791586c0]*/ |
| 13271 | { |
| 13272 | WAIT_TYPE wait_status; |
| 13273 | WAIT_STATUS_INT(wait_status) = status; |
| 13274 | return WIFEXITED(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13275 | } |
| 13276 | #endif /* WIFEXITED */ |
| 13277 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13278 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 13279 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13280 | /*[clinic input] |
| 13281 | os.WEXITSTATUS -> int |
| 13282 | |
| 13283 | status: int |
| 13284 | |
| 13285 | Return the process return code from status. |
| 13286 | [clinic start generated code]*/ |
| 13287 | |
| 13288 | PyDoc_STRVAR(os_WEXITSTATUS__doc__, |
| 13289 | "WEXITSTATUS($module, /, status)\n" |
| 13290 | "--\n" |
| 13291 | "\n" |
| 13292 | "Return the process return code from status."); |
| 13293 | |
| 13294 | #define OS_WEXITSTATUS_METHODDEF \ |
| 13295 | {"WEXITSTATUS", (PyCFunction)os_WEXITSTATUS, METH_VARARGS|METH_KEYWORDS, os_WEXITSTATUS__doc__}, |
| 13296 | |
| 13297 | static int |
| 13298 | os_WEXITSTATUS_impl(PyModuleDef *module, int status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13299 | |
| 13300 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13301 | os_WEXITSTATUS(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13302 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13303 | PyObject *return_value = NULL; |
| 13304 | static char *_keywords[] = {"status", NULL}; |
| 13305 | int status; |
| 13306 | int _return_value; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13307 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13308 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13309 | "i:WEXITSTATUS", _keywords, |
| 13310 | &status)) |
| 13311 | goto exit; |
| 13312 | _return_value = os_WEXITSTATUS_impl(module, status); |
| 13313 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13314 | goto exit; |
| 13315 | return_value = PyLong_FromLong((long)_return_value); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13317 | exit: |
| 13318 | return return_value; |
| 13319 | } |
| 13320 | |
| 13321 | static int |
| 13322 | os_WEXITSTATUS_impl(PyModuleDef *module, int status) |
| 13323 | /*[clinic end generated code: output=13b6c270e2a326b1 input=e1fb4944e377585b]*/ |
| 13324 | { |
| 13325 | WAIT_TYPE wait_status; |
| 13326 | WAIT_STATUS_INT(wait_status) = status; |
| 13327 | return WEXITSTATUS(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13328 | } |
| 13329 | #endif /* WEXITSTATUS */ |
| 13330 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13331 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13332 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13333 | /*[clinic input] |
| 13334 | os.WTERMSIG -> int |
| 13335 | |
| 13336 | status: int |
| 13337 | |
| 13338 | Return the signal that terminated the process that provided the status value. |
| 13339 | [clinic start generated code]*/ |
| 13340 | |
| 13341 | PyDoc_STRVAR(os_WTERMSIG__doc__, |
| 13342 | "WTERMSIG($module, /, status)\n" |
| 13343 | "--\n" |
| 13344 | "\n" |
| 13345 | "Return the signal that terminated the process that provided the status value."); |
| 13346 | |
| 13347 | #define OS_WTERMSIG_METHODDEF \ |
| 13348 | {"WTERMSIG", (PyCFunction)os_WTERMSIG, METH_VARARGS|METH_KEYWORDS, os_WTERMSIG__doc__}, |
| 13349 | |
| 13350 | static int |
| 13351 | os_WTERMSIG_impl(PyModuleDef *module, int status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13352 | |
| 13353 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13354 | os_WTERMSIG(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13355 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13356 | PyObject *return_value = NULL; |
| 13357 | static char *_keywords[] = {"status", NULL}; |
| 13358 | int status; |
| 13359 | int _return_value; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13361 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13362 | "i:WTERMSIG", _keywords, |
| 13363 | &status)) |
| 13364 | goto exit; |
| 13365 | _return_value = os_WTERMSIG_impl(module, status); |
| 13366 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13367 | goto exit; |
| 13368 | return_value = PyLong_FromLong((long)_return_value); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13369 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13370 | exit: |
| 13371 | return return_value; |
| 13372 | } |
| 13373 | |
| 13374 | static int |
| 13375 | os_WTERMSIG_impl(PyModuleDef *module, int status) |
| 13376 | /*[clinic end generated code: output=bf1fd4b002d0a9ed input=727fd7f84ec3f243]*/ |
| 13377 | { |
| 13378 | WAIT_TYPE wait_status; |
| 13379 | WAIT_STATUS_INT(wait_status) = status; |
| 13380 | return WTERMSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13381 | } |
| 13382 | #endif /* WTERMSIG */ |
| 13383 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13384 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13385 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13386 | /*[clinic input] |
| 13387 | os.WSTOPSIG -> int |
| 13388 | |
| 13389 | status: int |
| 13390 | |
| 13391 | Return the signal that stopped the process that provided the status value. |
| 13392 | [clinic start generated code]*/ |
| 13393 | |
| 13394 | PyDoc_STRVAR(os_WSTOPSIG__doc__, |
| 13395 | "WSTOPSIG($module, /, status)\n" |
| 13396 | "--\n" |
| 13397 | "\n" |
| 13398 | "Return the signal that stopped the process that provided the status value."); |
| 13399 | |
| 13400 | #define OS_WSTOPSIG_METHODDEF \ |
| 13401 | {"WSTOPSIG", (PyCFunction)os_WSTOPSIG, METH_VARARGS|METH_KEYWORDS, os_WSTOPSIG__doc__}, |
| 13402 | |
| 13403 | static int |
| 13404 | os_WSTOPSIG_impl(PyModuleDef *module, int status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13405 | |
| 13406 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13407 | os_WSTOPSIG(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13408 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13409 | PyObject *return_value = NULL; |
| 13410 | static char *_keywords[] = {"status", NULL}; |
| 13411 | int status; |
| 13412 | int _return_value; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13413 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13414 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13415 | "i:WSTOPSIG", _keywords, |
| 13416 | &status)) |
| 13417 | goto exit; |
| 13418 | _return_value = os_WSTOPSIG_impl(module, status); |
| 13419 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13420 | goto exit; |
| 13421 | return_value = PyLong_FromLong((long)_return_value); |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13422 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13423 | exit: |
| 13424 | return return_value; |
| 13425 | } |
| 13426 | |
| 13427 | static int |
| 13428 | os_WSTOPSIG_impl(PyModuleDef *module, int status) |
| 13429 | /*[clinic end generated code: output=92e1647d29ee0549 input=46ebf1d1b293c5c1]*/ |
| 13430 | { |
| 13431 | WAIT_TYPE wait_status; |
| 13432 | WAIT_STATUS_INT(wait_status) = status; |
| 13433 | return WSTOPSIG(wait_status); |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13434 | } |
| 13435 | #endif /* WSTOPSIG */ |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 13436 | #endif /* HAVE_SYS_WAIT_H */ |
| 13437 | |
| 13438 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13439 | #ifndef OS_WCOREDUMP_METHODDEF |
| 13440 | #define OS_WCOREDUMP_METHODDEF |
| 13441 | #endif /* OS_WCOREDUMP_METHODDEF */ |
| 13442 | |
| 13443 | #ifndef OS_WIFCONTINUED_METHODDEF |
| 13444 | #define OS_WIFCONTINUED_METHODDEF |
| 13445 | #endif /* OS_WIFCONTINUED_METHODDEF */ |
| 13446 | |
| 13447 | #ifndef OS_WIFSTOPPED_METHODDEF |
| 13448 | #define OS_WIFSTOPPED_METHODDEF |
| 13449 | #endif /* OS_WIFSTOPPED_METHODDEF */ |
| 13450 | |
| 13451 | #ifndef OS_WIFSIGNALED_METHODDEF |
| 13452 | #define OS_WIFSIGNALED_METHODDEF |
| 13453 | #endif /* OS_WIFSIGNALED_METHODDEF */ |
| 13454 | |
| 13455 | #ifndef OS_WIFEXITED_METHODDEF |
| 13456 | #define OS_WIFEXITED_METHODDEF |
| 13457 | #endif /* OS_WIFEXITED_METHODDEF */ |
| 13458 | |
| 13459 | #ifndef OS_WEXITSTATUS_METHODDEF |
| 13460 | #define OS_WEXITSTATUS_METHODDEF |
| 13461 | #endif /* OS_WEXITSTATUS_METHODDEF */ |
| 13462 | |
| 13463 | #ifndef OS_WTERMSIG_METHODDEF |
| 13464 | #define OS_WTERMSIG_METHODDEF |
| 13465 | #endif /* OS_WTERMSIG_METHODDEF */ |
| 13466 | |
| 13467 | #ifndef OS_WSTOPSIG_METHODDEF |
| 13468 | #define OS_WSTOPSIG_METHODDEF |
| 13469 | #endif /* OS_WSTOPSIG_METHODDEF */ |
| 13470 | |
| 13471 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13472 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 13473 | #ifdef _SCO_DS |
| 13474 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the |
| 13475 | needed definitions in sys/statvfs.h */ |
| 13476 | #define _SVID3 |
| 13477 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13478 | #include <sys/statvfs.h> |
| 13479 | |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13480 | static PyObject* |
| 13481 | _pystatvfs_fromstructstatvfs(struct statvfs st) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13482 | PyObject *v = PyStructSequence_New(&StatVFSResultType); |
| 13483 | if (v == NULL) |
| 13484 | return NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13485 | |
| 13486 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13487 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 13488 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 13489 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); |
| 13490 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); |
| 13491 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); |
| 13492 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); |
| 13493 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); |
| 13494 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); |
| 13495 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 13496 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13497 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13498 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
| 13499 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); |
| 13500 | PyStructSequence_SET_ITEM(v, 2, |
| 13501 | PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks)); |
| 13502 | PyStructSequence_SET_ITEM(v, 3, |
| 13503 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree)); |
| 13504 | PyStructSequence_SET_ITEM(v, 4, |
| 13505 | PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail)); |
| 13506 | PyStructSequence_SET_ITEM(v, 5, |
| 13507 | PyLong_FromLongLong((PY_LONG_LONG) st.f_files)); |
| 13508 | PyStructSequence_SET_ITEM(v, 6, |
| 13509 | PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree)); |
| 13510 | PyStructSequence_SET_ITEM(v, 7, |
| 13511 | PyLong_FromLongLong((PY_LONG_LONG) st.f_favail)); |
| 13512 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
| 13513 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13514 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 13515 | if (PyErr_Occurred()) { |
| 13516 | Py_DECREF(v); |
| 13517 | return NULL; |
| 13518 | } |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13519 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13520 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13521 | } |
| 13522 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13523 | |
| 13524 | /*[clinic input] |
| 13525 | os.fstatvfs |
| 13526 | fd: int |
| 13527 | / |
| 13528 | |
| 13529 | Perform an fstatvfs system call on the given fd. |
| 13530 | |
| 13531 | Equivalent to statvfs(fd). |
| 13532 | [clinic start generated code]*/ |
| 13533 | |
| 13534 | PyDoc_STRVAR(os_fstatvfs__doc__, |
| 13535 | "fstatvfs($module, fd, /)\n" |
| 13536 | "--\n" |
| 13537 | "\n" |
| 13538 | "Perform an fstatvfs system call on the given fd.\n" |
| 13539 | "\n" |
| 13540 | "Equivalent to statvfs(fd)."); |
| 13541 | |
| 13542 | #define OS_FSTATVFS_METHODDEF \ |
| 13543 | {"fstatvfs", (PyCFunction)os_fstatvfs, METH_VARARGS, os_fstatvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13544 | |
| 13545 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13546 | os_fstatvfs_impl(PyModuleDef *module, int fd); |
| 13547 | |
| 13548 | static PyObject * |
| 13549 | os_fstatvfs(PyModuleDef *module, PyObject *args) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13550 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13551 | PyObject *return_value = NULL; |
| 13552 | int fd; |
| 13553 | |
| 13554 | if (!PyArg_ParseTuple(args, |
| 13555 | "i:fstatvfs", |
| 13556 | &fd)) |
| 13557 | goto exit; |
| 13558 | return_value = os_fstatvfs_impl(module, fd); |
| 13559 | |
| 13560 | exit: |
| 13561 | return return_value; |
| 13562 | } |
| 13563 | |
| 13564 | static PyObject * |
| 13565 | os_fstatvfs_impl(PyModuleDef *module, int fd) |
| 13566 | /*[clinic end generated code: output=0e32bf07f946ec0d input=d8122243ac50975e]*/ |
| 13567 | { |
| 13568 | int result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 13569 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13570 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13571 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 13572 | do { |
| 13573 | Py_BEGIN_ALLOW_THREADS |
| 13574 | result = fstatvfs(fd, &st); |
| 13575 | Py_END_ALLOW_THREADS |
| 13576 | } while (result != 0 && errno == EINTR && |
| 13577 | !(async_err = PyErr_CheckSignals())); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13578 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 13579 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13580 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13581 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13582 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13583 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13584 | |
| 13585 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 13586 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13587 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13588 | /*[clinic input] |
| 13589 | os.statvfs |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13590 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13591 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
| 13592 | |
| 13593 | Perform a statvfs system call on the given path. |
| 13594 | |
| 13595 | path may always be specified as a string. |
| 13596 | On some platforms, path may also be specified as an open file descriptor. |
| 13597 | If this functionality is unavailable, using it raises an exception. |
| 13598 | [clinic start generated code]*/ |
| 13599 | |
| 13600 | PyDoc_STRVAR(os_statvfs__doc__, |
| 13601 | "statvfs($module, /, path)\n" |
| 13602 | "--\n" |
| 13603 | "\n" |
| 13604 | "Perform a statvfs system call on the given path.\n" |
| 13605 | "\n" |
| 13606 | "path may always be specified as a string.\n" |
| 13607 | "On some platforms, path may also be specified as an open file descriptor.\n" |
| 13608 | " If this functionality is unavailable, using it raises an exception."); |
| 13609 | |
| 13610 | #define OS_STATVFS_METHODDEF \ |
| 13611 | {"statvfs", (PyCFunction)os_statvfs, METH_VARARGS|METH_KEYWORDS, os_statvfs__doc__}, |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13612 | |
| 13613 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13614 | os_statvfs_impl(PyModuleDef *module, path_t *path); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 13615 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13616 | static PyObject * |
| 13617 | os_statvfs(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 13618 | { |
| 13619 | PyObject *return_value = NULL; |
| 13620 | static char *_keywords[] = {"path", NULL}; |
| 13621 | path_t path = PATH_T_INITIALIZE("statvfs", "path", 0, PATH_HAVE_FSTATVFS); |
| 13622 | |
| 13623 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13624 | "O&:statvfs", _keywords, |
| 13625 | path_converter, &path)) |
| 13626 | goto exit; |
| 13627 | return_value = os_statvfs_impl(module, &path); |
| 13628 | |
| 13629 | exit: |
| 13630 | /* Cleanup for path */ |
| 13631 | path_cleanup(&path); |
| 13632 | |
| 13633 | return return_value; |
| 13634 | } |
| 13635 | |
| 13636 | static PyObject * |
| 13637 | os_statvfs_impl(PyModuleDef *module, path_t *path) |
| 13638 | /*[clinic end generated code: output=00ff54983360b446 input=3f5c35791c669bd9]*/ |
| 13639 | { |
| 13640 | int result; |
| 13641 | struct statvfs st; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13642 | |
| 13643 | Py_BEGIN_ALLOW_THREADS |
| 13644 | #ifdef HAVE_FSTATVFS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13645 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13646 | #ifdef __APPLE__ |
| 13647 | /* handle weak-linking on Mac OS X 10.3 */ |
| 13648 | if (fstatvfs == NULL) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13649 | fd_specified("statvfs", path->fd); |
| 13650 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13651 | } |
| 13652 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13653 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13654 | } |
| 13655 | else |
| 13656 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13657 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13658 | Py_END_ALLOW_THREADS |
| 13659 | |
| 13660 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13661 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 13662 | } |
| 13663 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13664 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13665 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13666 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
| 13667 | |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13668 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 13669 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13670 | /*[clinic input] |
| 13671 | os._getdiskusage |
| 13672 | |
| 13673 | path: Py_UNICODE |
| 13674 | |
| 13675 | Return disk usage statistics about the given path as a (total, free) tuple. |
| 13676 | [clinic start generated code]*/ |
| 13677 | |
| 13678 | PyDoc_STRVAR(os__getdiskusage__doc__, |
| 13679 | "_getdiskusage($module, /, path)\n" |
| 13680 | "--\n" |
| 13681 | "\n" |
| 13682 | "Return disk usage statistics about the given path as a (total, free) tuple."); |
| 13683 | |
| 13684 | #define OS__GETDISKUSAGE_METHODDEF \ |
| 13685 | {"_getdiskusage", (PyCFunction)os__getdiskusage, METH_VARARGS|METH_KEYWORDS, os__getdiskusage__doc__}, |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 13686 | |
| 13687 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13688 | os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path); |
| 13689 | |
| 13690 | static PyObject * |
| 13691 | os__getdiskusage(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 13692 | { |
| 13693 | PyObject *return_value = NULL; |
| 13694 | static char *_keywords[] = {"path", NULL}; |
| 13695 | Py_UNICODE *path; |
| 13696 | |
| 13697 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13698 | "u:_getdiskusage", _keywords, |
| 13699 | &path)) |
| 13700 | goto exit; |
| 13701 | return_value = os__getdiskusage_impl(module, path); |
| 13702 | |
| 13703 | exit: |
| 13704 | return return_value; |
| 13705 | } |
| 13706 | |
| 13707 | static PyObject * |
| 13708 | os__getdiskusage_impl(PyModuleDef *module, Py_UNICODE *path) |
| 13709 | /*[clinic end generated code: output=054c972179b13708 input=6458133aed893c78]*/ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 13710 | { |
| 13711 | BOOL retval; |
| 13712 | ULARGE_INTEGER _, total, free; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 13713 | |
| 13714 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 6139c1b | 2011-11-09 22:14:14 +0100 | [diff] [blame] | 13715 | retval = GetDiskFreeSpaceExW(path, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 13716 | Py_END_ALLOW_THREADS |
| 13717 | if (retval == 0) |
| 13718 | return PyErr_SetFromWindowsErr(0); |
| 13719 | |
| 13720 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
| 13721 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13722 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 13723 | |
| 13724 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13725 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
| 13726 | * It maps strings representing configuration variable names to |
| 13727 | * integer values, allowing those functions to be called with the |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 13728 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 13729 | * rarely-used constants. There are three separate tables that use |
| 13730 | * these definitions. |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 13731 | * |
| 13732 | * This code is always included, even if none of the interfaces that |
| 13733 | * need it are included. The #if hackery needed to avoid it would be |
| 13734 | * sufficiently pervasive that it's not worth the loss of readability. |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13735 | */ |
| 13736 | struct constdef { |
| 13737 | char *name; |
| 13738 | long value; |
| 13739 | }; |
| 13740 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 13741 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 13742 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 13743 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 13744 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 13745 | if (PyLong_Check(arg)) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 13746 | *valuep = PyLong_AS_LONG(arg); |
| 13747 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 13748 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 13749 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 13750 | /* look up the value in the table using a binary search */ |
| 13751 | size_t lo = 0; |
| 13752 | size_t mid; |
| 13753 | size_t hi = tablesize; |
| 13754 | int cmp; |
| 13755 | const char *confname; |
| 13756 | if (!PyUnicode_Check(arg)) { |
| 13757 | PyErr_SetString(PyExc_TypeError, |
| 13758 | "configuration names must be strings or integers"); |
| 13759 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13760 | } |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 13761 | confname = _PyUnicode_AsString(arg); |
| 13762 | if (confname == NULL) |
| 13763 | return 0; |
| 13764 | while (lo < hi) { |
| 13765 | mid = (lo + hi) / 2; |
| 13766 | cmp = strcmp(confname, table[mid].name); |
| 13767 | if (cmp < 0) |
| 13768 | hi = mid; |
| 13769 | else if (cmp > 0) |
| 13770 | lo = mid + 1; |
| 13771 | else { |
| 13772 | *valuep = table[mid].value; |
| 13773 | return 1; |
| 13774 | } |
| 13775 | } |
| 13776 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); |
| 13777 | return 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13778 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 13779 | } |
| 13780 | |
| 13781 | |
| 13782 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
| 13783 | static struct constdef posix_constants_pathconf[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 13784 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13785 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 13786 | #endif |
| 13787 | #ifdef _PC_ABI_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13788 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 13789 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13790 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13791 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13792 | #endif |
| 13793 | #ifdef _PC_CHOWN_RESTRICTED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13794 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13795 | #endif |
| 13796 | #ifdef _PC_FILESIZEBITS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13797 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13798 | #endif |
| 13799 | #ifdef _PC_LAST |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13800 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13801 | #endif |
| 13802 | #ifdef _PC_LINK_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13803 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13804 | #endif |
| 13805 | #ifdef _PC_MAX_CANON |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13806 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13807 | #endif |
| 13808 | #ifdef _PC_MAX_INPUT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13809 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13810 | #endif |
| 13811 | #ifdef _PC_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13812 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13813 | #endif |
| 13814 | #ifdef _PC_NO_TRUNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13815 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13816 | #endif |
| 13817 | #ifdef _PC_PATH_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13818 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13819 | #endif |
| 13820 | #ifdef _PC_PIPE_BUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13821 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13822 | #endif |
| 13823 | #ifdef _PC_PRIO_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13824 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13825 | #endif |
| 13826 | #ifdef _PC_SOCK_MAXBUF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13827 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13828 | #endif |
| 13829 | #ifdef _PC_SYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13830 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13831 | #endif |
| 13832 | #ifdef _PC_VDISABLE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13833 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13834 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 13835 | #ifdef _PC_ACL_ENABLED |
| 13836 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, |
| 13837 | #endif |
| 13838 | #ifdef _PC_MIN_HOLE_SIZE |
| 13839 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, |
| 13840 | #endif |
| 13841 | #ifdef _PC_ALLOC_SIZE_MIN |
| 13842 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, |
| 13843 | #endif |
| 13844 | #ifdef _PC_REC_INCR_XFER_SIZE |
| 13845 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, |
| 13846 | #endif |
| 13847 | #ifdef _PC_REC_MAX_XFER_SIZE |
| 13848 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, |
| 13849 | #endif |
| 13850 | #ifdef _PC_REC_MIN_XFER_SIZE |
| 13851 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, |
| 13852 | #endif |
| 13853 | #ifdef _PC_REC_XFER_ALIGN |
| 13854 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, |
| 13855 | #endif |
| 13856 | #ifdef _PC_SYMLINK_MAX |
| 13857 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, |
| 13858 | #endif |
| 13859 | #ifdef _PC_XATTR_ENABLED |
| 13860 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, |
| 13861 | #endif |
| 13862 | #ifdef _PC_XATTR_EXISTS |
| 13863 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, |
| 13864 | #endif |
| 13865 | #ifdef _PC_TIMESTAMP_RESOLUTION |
| 13866 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, |
| 13867 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13868 | }; |
| 13869 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13870 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 13871 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13872 | { |
| 13873 | return conv_confname(arg, valuep, posix_constants_pathconf, |
| 13874 | sizeof(posix_constants_pathconf) |
| 13875 | / sizeof(struct constdef)); |
| 13876 | } |
| 13877 | #endif |
| 13878 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13879 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13880 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13881 | /*[clinic input] |
| 13882 | os.fpathconf -> long |
| 13883 | |
| 13884 | fd: int |
| 13885 | name: path_confname |
| 13886 | / |
| 13887 | |
| 13888 | Return the configuration limit name for the file descriptor fd. |
| 13889 | |
| 13890 | If there is no limit, return -1. |
| 13891 | [clinic start generated code]*/ |
| 13892 | |
| 13893 | PyDoc_STRVAR(os_fpathconf__doc__, |
| 13894 | "fpathconf($module, fd, name, /)\n" |
| 13895 | "--\n" |
| 13896 | "\n" |
| 13897 | "Return the configuration limit name for the file descriptor fd.\n" |
| 13898 | "\n" |
| 13899 | "If there is no limit, return -1."); |
| 13900 | |
| 13901 | #define OS_FPATHCONF_METHODDEF \ |
| 13902 | {"fpathconf", (PyCFunction)os_fpathconf, METH_VARARGS, os_fpathconf__doc__}, |
| 13903 | |
| 13904 | static long |
| 13905 | os_fpathconf_impl(PyModuleDef *module, int fd, int name); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13906 | |
| 13907 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13908 | os_fpathconf(PyModuleDef *module, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13909 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13910 | PyObject *return_value = NULL; |
| 13911 | int fd; |
| 13912 | int name; |
| 13913 | long _return_value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13914 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13915 | if (!PyArg_ParseTuple(args, |
| 13916 | "iO&:fpathconf", |
| 13917 | &fd, conv_path_confname, &name)) |
| 13918 | goto exit; |
| 13919 | _return_value = os_fpathconf_impl(module, fd, name); |
| 13920 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13921 | goto exit; |
| 13922 | return_value = PyLong_FromLong(_return_value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13923 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13924 | exit: |
| 13925 | return return_value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13926 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13927 | |
| 13928 | static long |
| 13929 | os_fpathconf_impl(PyModuleDef *module, int fd, int name) |
| 13930 | /*[clinic end generated code: output=3bf04b40e0523a8c input=5942a024d3777810]*/ |
| 13931 | { |
| 13932 | long limit; |
| 13933 | |
| 13934 | errno = 0; |
| 13935 | limit = fpathconf(fd, name); |
| 13936 | if (limit == -1 && errno != 0) |
| 13937 | posix_error(); |
| 13938 | |
| 13939 | return limit; |
| 13940 | } |
| 13941 | #endif /* HAVE_FPATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13942 | |
| 13943 | |
| 13944 | #ifdef HAVE_PATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13945 | /*[clinic input] |
| 13946 | os.pathconf -> long |
| 13947 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') |
| 13948 | name: path_confname |
| 13949 | |
| 13950 | Return the configuration limit name for the file or directory path. |
| 13951 | |
| 13952 | If there is no limit, return -1. |
| 13953 | On some platforms, path may also be specified as an open file descriptor. |
| 13954 | If this functionality is unavailable, using it raises an exception. |
| 13955 | [clinic start generated code]*/ |
| 13956 | |
| 13957 | PyDoc_STRVAR(os_pathconf__doc__, |
| 13958 | "pathconf($module, /, path, name)\n" |
| 13959 | "--\n" |
| 13960 | "\n" |
| 13961 | "Return the configuration limit name for the file or directory path.\n" |
| 13962 | "\n" |
| 13963 | "If there is no limit, return -1.\n" |
| 13964 | "On some platforms, path may also be specified as an open file descriptor.\n" |
| 13965 | " If this functionality is unavailable, using it raises an exception."); |
| 13966 | |
| 13967 | #define OS_PATHCONF_METHODDEF \ |
| 13968 | {"pathconf", (PyCFunction)os_pathconf, METH_VARARGS|METH_KEYWORDS, os_pathconf__doc__}, |
| 13969 | |
| 13970 | static long |
| 13971 | os_pathconf_impl(PyModuleDef *module, path_t *path, int name); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13972 | |
| 13973 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13974 | os_pathconf(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13975 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13976 | PyObject *return_value = NULL; |
| 13977 | static char *_keywords[] = {"path", "name", NULL}; |
| 13978 | path_t path = PATH_T_INITIALIZE("pathconf", "path", 0, PATH_HAVE_FPATHCONF); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13979 | int name; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13980 | long _return_value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13981 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13982 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 13983 | "O&O&:pathconf", _keywords, |
| 13984 | path_converter, &path, conv_path_confname, &name)) |
| 13985 | goto exit; |
| 13986 | _return_value = os_pathconf_impl(module, &path, name); |
| 13987 | if ((_return_value == -1) && PyErr_Occurred()) |
| 13988 | goto exit; |
| 13989 | return_value = PyLong_FromLong(_return_value); |
| 13990 | |
| 13991 | exit: |
| 13992 | /* Cleanup for path */ |
| 13993 | path_cleanup(&path); |
| 13994 | |
| 13995 | return return_value; |
| 13996 | } |
| 13997 | |
| 13998 | static long |
| 13999 | os_pathconf_impl(PyModuleDef *module, path_t *path, int name) |
| 14000 | /*[clinic end generated code: output=1a53e125b6cf63e4 input=bc3e2a985af27e5e]*/ |
| 14001 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14002 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14003 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14004 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14005 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14006 | if (path->fd != -1) |
| 14007 | limit = fpathconf(path->fd, name); |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14008 | else |
| 14009 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14010 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14011 | if (limit == -1 && errno != 0) { |
| 14012 | if (errno == EINVAL) |
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 14013 | /* could be a path or name problem */ |
| 14014 | posix_error(); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14015 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14016 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14017 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14018 | |
| 14019 | return limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14020 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14021 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14022 | |
| 14023 | #ifdef HAVE_CONFSTR |
| 14024 | static struct constdef posix_constants_confstr[] = { |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14025 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14026 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14027 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 14028 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14029 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 14030 | #endif |
| 14031 | #ifdef _CS_GNU_LIBPTHREAD_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14032 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 14033 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14034 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14035 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14036 | #endif |
| 14037 | #ifdef _CS_HW_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14038 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14039 | #endif |
| 14040 | #ifdef _CS_HW_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14041 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14042 | #endif |
| 14043 | #ifdef _CS_INITTAB_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14044 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14045 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14046 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14047 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14048 | #endif |
| 14049 | #ifdef _CS_LFS64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14050 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14051 | #endif |
| 14052 | #ifdef _CS_LFS64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14053 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14054 | #endif |
| 14055 | #ifdef _CS_LFS64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14056 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14057 | #endif |
| 14058 | #ifdef _CS_LFS_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14059 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14060 | #endif |
| 14061 | #ifdef _CS_LFS_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14062 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14063 | #endif |
| 14064 | #ifdef _CS_LFS_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14065 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14066 | #endif |
| 14067 | #ifdef _CS_LFS_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14068 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14069 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14070 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14071 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14072 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14073 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14074 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14075 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14076 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14077 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14078 | #endif |
| 14079 | #ifdef _CS_SRPC_DOMAIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14080 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14081 | #endif |
| 14082 | #ifdef _CS_SYSNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14083 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14084 | #endif |
| 14085 | #ifdef _CS_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14086 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14087 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14088 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14089 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14090 | #endif |
| 14091 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14092 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14093 | #endif |
| 14094 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14095 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14096 | #endif |
| 14097 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14098 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14099 | #endif |
| 14100 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14101 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14102 | #endif |
| 14103 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14104 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14105 | #endif |
| 14106 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14107 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14108 | #endif |
| 14109 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14110 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14111 | #endif |
| 14112 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14113 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14114 | #endif |
| 14115 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14116 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14117 | #endif |
| 14118 | #ifdef _CS_XBS5_LP64_OFF64_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14119 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14120 | #endif |
| 14121 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14122 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14123 | #endif |
| 14124 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14125 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14126 | #endif |
| 14127 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14128 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14129 | #endif |
| 14130 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14131 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14132 | #endif |
| 14133 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14134 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14135 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14136 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14137 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14138 | #endif |
| 14139 | #ifdef _MIPS_CS_BASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14140 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14141 | #endif |
| 14142 | #ifdef _MIPS_CS_HOSTID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14143 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14144 | #endif |
| 14145 | #ifdef _MIPS_CS_HW_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14146 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14147 | #endif |
| 14148 | #ifdef _MIPS_CS_NUM_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14149 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14150 | #endif |
| 14151 | #ifdef _MIPS_CS_OSREL_MAJ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14152 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14153 | #endif |
| 14154 | #ifdef _MIPS_CS_OSREL_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14155 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14156 | #endif |
| 14157 | #ifdef _MIPS_CS_OSREL_PATCH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14158 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14159 | #endif |
| 14160 | #ifdef _MIPS_CS_OS_NAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14161 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14162 | #endif |
| 14163 | #ifdef _MIPS_CS_OS_PROVIDER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14164 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14165 | #endif |
| 14166 | #ifdef _MIPS_CS_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14167 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14168 | #endif |
| 14169 | #ifdef _MIPS_CS_SERIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14170 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14171 | #endif |
| 14172 | #ifdef _MIPS_CS_VENDOR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14173 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14174 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14175 | }; |
| 14176 | |
| 14177 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 14178 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14179 | { |
| 14180 | return conv_confname(arg, valuep, posix_constants_confstr, |
| 14181 | sizeof(posix_constants_confstr) |
| 14182 | / sizeof(struct constdef)); |
| 14183 | } |
| 14184 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14185 | |
| 14186 | /*[clinic input] |
| 14187 | os.confstr |
| 14188 | |
| 14189 | name: confstr_confname |
| 14190 | / |
| 14191 | |
| 14192 | Return a string-valued system configuration variable. |
| 14193 | [clinic start generated code]*/ |
| 14194 | |
| 14195 | PyDoc_STRVAR(os_confstr__doc__, |
| 14196 | "confstr($module, name, /)\n" |
| 14197 | "--\n" |
| 14198 | "\n" |
| 14199 | "Return a string-valued system configuration variable."); |
| 14200 | |
| 14201 | #define OS_CONFSTR_METHODDEF \ |
| 14202 | {"confstr", (PyCFunction)os_confstr, METH_VARARGS, os_confstr__doc__}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14203 | |
| 14204 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14205 | os_confstr_impl(PyModuleDef *module, int name); |
| 14206 | |
| 14207 | static PyObject * |
| 14208 | os_confstr(PyModuleDef *module, PyObject *args) |
| 14209 | { |
| 14210 | PyObject *return_value = NULL; |
| 14211 | int name; |
| 14212 | |
| 14213 | if (!PyArg_ParseTuple(args, |
| 14214 | "O&:confstr", |
| 14215 | conv_confstr_confname, &name)) |
| 14216 | goto exit; |
| 14217 | return_value = os_confstr_impl(module, name); |
| 14218 | |
| 14219 | exit: |
| 14220 | return return_value; |
| 14221 | } |
| 14222 | |
| 14223 | static PyObject * |
| 14224 | os_confstr_impl(PyModuleDef *module, int name) |
| 14225 | /*[clinic end generated code: output=3f5e8aba9f8e3174 input=18fb4d0567242e65]*/ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14226 | { |
| 14227 | PyObject *result = NULL; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 14228 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 14229 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14230 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 14231 | errno = 0; |
| 14232 | len = confstr(name, buffer, sizeof(buffer)); |
| 14233 | if (len == 0) { |
| 14234 | if (errno) { |
| 14235 | posix_error(); |
| 14236 | return NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14237 | } |
| 14238 | else { |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 14239 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14240 | } |
| 14241 | } |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 14242 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 14243 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 14244 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 14245 | char *buf = PyMem_Malloc(len); |
| 14246 | if (buf == NULL) |
| 14247 | return PyErr_NoMemory(); |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 14248 | len2 = confstr(name, buf, len); |
| 14249 | assert(len == len2); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 14250 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1); |
| 14251 | PyMem_Free(buf); |
| 14252 | } |
| 14253 | else |
| 14254 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14255 | return result; |
| 14256 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14257 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14258 | |
| 14259 | |
| 14260 | #ifdef HAVE_SYSCONF |
| 14261 | static struct constdef posix_constants_sysconf[] = { |
| 14262 | #ifdef _SC_2_CHAR_TERM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14263 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14264 | #endif |
| 14265 | #ifdef _SC_2_C_BIND |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14266 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14267 | #endif |
| 14268 | #ifdef _SC_2_C_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14269 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14270 | #endif |
| 14271 | #ifdef _SC_2_C_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14272 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14273 | #endif |
| 14274 | #ifdef _SC_2_FORT_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14275 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14276 | #endif |
| 14277 | #ifdef _SC_2_FORT_RUN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14278 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14279 | #endif |
| 14280 | #ifdef _SC_2_LOCALEDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14281 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14282 | #endif |
| 14283 | #ifdef _SC_2_SW_DEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14284 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14285 | #endif |
| 14286 | #ifdef _SC_2_UPE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14287 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14288 | #endif |
| 14289 | #ifdef _SC_2_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14290 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14291 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14292 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14293 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14294 | #endif |
| 14295 | #ifdef _SC_ACL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14296 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14297 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14298 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14299 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14300 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14301 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14302 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14303 | #endif |
| 14304 | #ifdef _SC_AIO_PRIO_DELTA_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14305 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14306 | #endif |
| 14307 | #ifdef _SC_ARG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14308 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14309 | #endif |
| 14310 | #ifdef _SC_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14311 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14312 | #endif |
| 14313 | #ifdef _SC_ATEXIT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14314 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14315 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14316 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14317 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14318 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14319 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14320 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14321 | #endif |
| 14322 | #ifdef _SC_BC_BASE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14323 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14324 | #endif |
| 14325 | #ifdef _SC_BC_DIM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14326 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14327 | #endif |
| 14328 | #ifdef _SC_BC_SCALE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14329 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14330 | #endif |
| 14331 | #ifdef _SC_BC_STRING_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14332 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14333 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14334 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14335 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14336 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14337 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14338 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14339 | #endif |
| 14340 | #ifdef _SC_CHAR_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14341 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14342 | #endif |
| 14343 | #ifdef _SC_CHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14344 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14345 | #endif |
| 14346 | #ifdef _SC_CHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14347 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14348 | #endif |
| 14349 | #ifdef _SC_CHILD_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14350 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14351 | #endif |
| 14352 | #ifdef _SC_CLK_TCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14353 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14354 | #endif |
| 14355 | #ifdef _SC_COHER_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14356 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14357 | #endif |
| 14358 | #ifdef _SC_COLL_WEIGHTS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14359 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14360 | #endif |
| 14361 | #ifdef _SC_DCACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14362 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14363 | #endif |
| 14364 | #ifdef _SC_DCACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14365 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14366 | #endif |
| 14367 | #ifdef _SC_DCACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14368 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14369 | #endif |
| 14370 | #ifdef _SC_DCACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14371 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14372 | #endif |
| 14373 | #ifdef _SC_DCACHE_TBLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14374 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14375 | #endif |
| 14376 | #ifdef _SC_DELAYTIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14377 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14378 | #endif |
| 14379 | #ifdef _SC_EQUIV_CLASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14380 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14381 | #endif |
| 14382 | #ifdef _SC_EXPR_NEST_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14383 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14384 | #endif |
| 14385 | #ifdef _SC_FSYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14386 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14387 | #endif |
| 14388 | #ifdef _SC_GETGR_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14389 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14390 | #endif |
| 14391 | #ifdef _SC_GETPW_R_SIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14392 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14393 | #endif |
| 14394 | #ifdef _SC_ICACHE_ASSOC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14395 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14396 | #endif |
| 14397 | #ifdef _SC_ICACHE_BLKSZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14398 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14399 | #endif |
| 14400 | #ifdef _SC_ICACHE_LINESZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14401 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14402 | #endif |
| 14403 | #ifdef _SC_ICACHE_SZ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14404 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14405 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14406 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14407 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14408 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14409 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14410 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14411 | #endif |
| 14412 | #ifdef _SC_INT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14413 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14414 | #endif |
| 14415 | #ifdef _SC_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14416 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14417 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14418 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14419 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14420 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14421 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14422 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14423 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14424 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14425 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14426 | #endif |
| 14427 | #ifdef _SC_KERN_SIM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14428 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14429 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14430 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14431 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14432 | #endif |
| 14433 | #ifdef _SC_LOGIN_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14434 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14435 | #endif |
| 14436 | #ifdef _SC_LOGNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14437 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14438 | #endif |
| 14439 | #ifdef _SC_LONG_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14440 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14441 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14442 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14443 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14444 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14445 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14446 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14447 | #endif |
| 14448 | #ifdef _SC_MAXPID |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14449 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14450 | #endif |
| 14451 | #ifdef _SC_MB_LEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14452 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14453 | #endif |
| 14454 | #ifdef _SC_MEMLOCK |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14455 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14456 | #endif |
| 14457 | #ifdef _SC_MEMLOCK_RANGE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14458 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14459 | #endif |
| 14460 | #ifdef _SC_MEMORY_PROTECTION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14461 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14462 | #endif |
| 14463 | #ifdef _SC_MESSAGE_PASSING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14464 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14465 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14466 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14467 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14468 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14469 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14470 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14471 | #endif |
| 14472 | #ifdef _SC_MQ_PRIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14473 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14474 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14475 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14476 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14477 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14478 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14479 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14480 | #endif |
| 14481 | #ifdef _SC_NL_ARGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14482 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14483 | #endif |
| 14484 | #ifdef _SC_NL_LANGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14485 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14486 | #endif |
| 14487 | #ifdef _SC_NL_MSGMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14488 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14489 | #endif |
| 14490 | #ifdef _SC_NL_NMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14491 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14492 | #endif |
| 14493 | #ifdef _SC_NL_SETMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14494 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14495 | #endif |
| 14496 | #ifdef _SC_NL_TEXTMAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14497 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14498 | #endif |
| 14499 | #ifdef _SC_NPROCESSORS_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14500 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14501 | #endif |
| 14502 | #ifdef _SC_NPROCESSORS_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14503 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14504 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14505 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14506 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14507 | #endif |
| 14508 | #ifdef _SC_NPROC_ONLN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14509 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14510 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14511 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14512 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14513 | #endif |
| 14514 | #ifdef _SC_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14515 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14516 | #endif |
| 14517 | #ifdef _SC_PAGESIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14518 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14519 | #endif |
| 14520 | #ifdef _SC_PAGE_SIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14521 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14522 | #endif |
| 14523 | #ifdef _SC_PASS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14524 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14525 | #endif |
| 14526 | #ifdef _SC_PHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14527 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14528 | #endif |
| 14529 | #ifdef _SC_PII |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14530 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14531 | #endif |
| 14532 | #ifdef _SC_PII_INTERNET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14533 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14534 | #endif |
| 14535 | #ifdef _SC_PII_INTERNET_DGRAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14536 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14537 | #endif |
| 14538 | #ifdef _SC_PII_INTERNET_STREAM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14539 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14540 | #endif |
| 14541 | #ifdef _SC_PII_OSI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14542 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14543 | #endif |
| 14544 | #ifdef _SC_PII_OSI_CLTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14545 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14546 | #endif |
| 14547 | #ifdef _SC_PII_OSI_COTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14548 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14549 | #endif |
| 14550 | #ifdef _SC_PII_OSI_M |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14551 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14552 | #endif |
| 14553 | #ifdef _SC_PII_SOCKET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14554 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14555 | #endif |
| 14556 | #ifdef _SC_PII_XTI |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14557 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14558 | #endif |
| 14559 | #ifdef _SC_POLL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14560 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14561 | #endif |
| 14562 | #ifdef _SC_PRIORITIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14563 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14564 | #endif |
| 14565 | #ifdef _SC_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14566 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14567 | #endif |
| 14568 | #ifdef _SC_REALTIME_SIGNALS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14569 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14570 | #endif |
| 14571 | #ifdef _SC_RE_DUP_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14572 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14573 | #endif |
| 14574 | #ifdef _SC_RTSIG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14575 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14576 | #endif |
| 14577 | #ifdef _SC_SAVED_IDS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14578 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14579 | #endif |
| 14580 | #ifdef _SC_SCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14581 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14582 | #endif |
| 14583 | #ifdef _SC_SCHAR_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14584 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14585 | #endif |
| 14586 | #ifdef _SC_SELECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14587 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14588 | #endif |
| 14589 | #ifdef _SC_SEMAPHORES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14590 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14591 | #endif |
| 14592 | #ifdef _SC_SEM_NSEMS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14593 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14594 | #endif |
| 14595 | #ifdef _SC_SEM_VALUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14596 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14597 | #endif |
| 14598 | #ifdef _SC_SHARED_MEMORY_OBJECTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14599 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14600 | #endif |
| 14601 | #ifdef _SC_SHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14602 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14603 | #endif |
| 14604 | #ifdef _SC_SHRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14605 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14606 | #endif |
| 14607 | #ifdef _SC_SIGQUEUE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14608 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14609 | #endif |
| 14610 | #ifdef _SC_SIGRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14611 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14612 | #endif |
| 14613 | #ifdef _SC_SIGRT_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14614 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14615 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14616 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14617 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14618 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14619 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14620 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14621 | #endif |
| 14622 | #ifdef _SC_SSIZE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14623 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14624 | #endif |
| 14625 | #ifdef _SC_STACK_PROT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14626 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14627 | #endif |
| 14628 | #ifdef _SC_STREAM_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14629 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14630 | #endif |
| 14631 | #ifdef _SC_SYNCHRONIZED_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14632 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14633 | #endif |
| 14634 | #ifdef _SC_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14635 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14636 | #endif |
| 14637 | #ifdef _SC_THREAD_ATTR_STACKADDR |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14638 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14639 | #endif |
| 14640 | #ifdef _SC_THREAD_ATTR_STACKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14641 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14642 | #endif |
| 14643 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14644 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14645 | #endif |
| 14646 | #ifdef _SC_THREAD_KEYS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14647 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14648 | #endif |
| 14649 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14650 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14651 | #endif |
| 14652 | #ifdef _SC_THREAD_PRIO_INHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14653 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14654 | #endif |
| 14655 | #ifdef _SC_THREAD_PRIO_PROTECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14656 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14657 | #endif |
| 14658 | #ifdef _SC_THREAD_PROCESS_SHARED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14659 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14660 | #endif |
| 14661 | #ifdef _SC_THREAD_SAFE_FUNCTIONS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14662 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14663 | #endif |
| 14664 | #ifdef _SC_THREAD_STACK_MIN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14665 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14666 | #endif |
| 14667 | #ifdef _SC_THREAD_THREADS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14668 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14669 | #endif |
| 14670 | #ifdef _SC_TIMERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14671 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14672 | #endif |
| 14673 | #ifdef _SC_TIMER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14674 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14675 | #endif |
| 14676 | #ifdef _SC_TTY_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14677 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14678 | #endif |
| 14679 | #ifdef _SC_TZNAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14680 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14681 | #endif |
| 14682 | #ifdef _SC_T_IOV_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14683 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14684 | #endif |
| 14685 | #ifdef _SC_UCHAR_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14686 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14687 | #endif |
| 14688 | #ifdef _SC_UINT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14689 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14690 | #endif |
| 14691 | #ifdef _SC_UIO_MAXIOV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14692 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14693 | #endif |
| 14694 | #ifdef _SC_ULONG_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14695 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14696 | #endif |
| 14697 | #ifdef _SC_USHRT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14698 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14699 | #endif |
| 14700 | #ifdef _SC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14701 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14702 | #endif |
| 14703 | #ifdef _SC_WORD_BIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14704 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14705 | #endif |
| 14706 | #ifdef _SC_XBS5_ILP32_OFF32 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14707 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14708 | #endif |
| 14709 | #ifdef _SC_XBS5_ILP32_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14710 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14711 | #endif |
| 14712 | #ifdef _SC_XBS5_LP64_OFF64 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14713 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14714 | #endif |
| 14715 | #ifdef _SC_XBS5_LPBIG_OFFBIG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14716 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14717 | #endif |
| 14718 | #ifdef _SC_XOPEN_CRYPT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14719 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14720 | #endif |
| 14721 | #ifdef _SC_XOPEN_ENH_I18N |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14722 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14723 | #endif |
| 14724 | #ifdef _SC_XOPEN_LEGACY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14725 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14726 | #endif |
| 14727 | #ifdef _SC_XOPEN_REALTIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14728 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14729 | #endif |
| 14730 | #ifdef _SC_XOPEN_REALTIME_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14731 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14732 | #endif |
| 14733 | #ifdef _SC_XOPEN_SHM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14734 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14735 | #endif |
| 14736 | #ifdef _SC_XOPEN_UNIX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14737 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14738 | #endif |
| 14739 | #ifdef _SC_XOPEN_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14740 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14741 | #endif |
| 14742 | #ifdef _SC_XOPEN_XCU_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14743 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14744 | #endif |
| 14745 | #ifdef _SC_XOPEN_XPG2 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14746 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14747 | #endif |
| 14748 | #ifdef _SC_XOPEN_XPG3 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14749 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14750 | #endif |
| 14751 | #ifdef _SC_XOPEN_XPG4 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14752 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14753 | #endif |
| 14754 | }; |
| 14755 | |
| 14756 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 14757 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14758 | { |
| 14759 | return conv_confname(arg, valuep, posix_constants_sysconf, |
| 14760 | sizeof(posix_constants_sysconf) |
| 14761 | / sizeof(struct constdef)); |
| 14762 | } |
| 14763 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14764 | |
| 14765 | /*[clinic input] |
| 14766 | os.sysconf -> long |
| 14767 | name: sysconf_confname |
| 14768 | / |
| 14769 | |
| 14770 | Return an integer-valued system configuration variable. |
| 14771 | [clinic start generated code]*/ |
| 14772 | |
| 14773 | PyDoc_STRVAR(os_sysconf__doc__, |
| 14774 | "sysconf($module, name, /)\n" |
| 14775 | "--\n" |
| 14776 | "\n" |
| 14777 | "Return an integer-valued system configuration variable."); |
| 14778 | |
| 14779 | #define OS_SYSCONF_METHODDEF \ |
| 14780 | {"sysconf", (PyCFunction)os_sysconf, METH_VARARGS, os_sysconf__doc__}, |
| 14781 | |
| 14782 | static long |
| 14783 | os_sysconf_impl(PyModuleDef *module, int name); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14784 | |
| 14785 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14786 | os_sysconf(PyModuleDef *module, PyObject *args) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14787 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14788 | PyObject *return_value = NULL; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14789 | int name; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14790 | long _return_value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14791 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14792 | if (!PyArg_ParseTuple(args, |
| 14793 | "O&:sysconf", |
| 14794 | conv_sysconf_confname, &name)) |
| 14795 | goto exit; |
| 14796 | _return_value = os_sysconf_impl(module, name); |
| 14797 | if ((_return_value == -1) && PyErr_Occurred()) |
| 14798 | goto exit; |
| 14799 | return_value = PyLong_FromLong(_return_value); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14801 | exit: |
| 14802 | return return_value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14803 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14804 | |
| 14805 | static long |
| 14806 | os_sysconf_impl(PyModuleDef *module, int name) |
| 14807 | /*[clinic end generated code: output=7b06dfdc472431e4 input=279e3430a33f29e4]*/ |
| 14808 | { |
| 14809 | long value; |
| 14810 | |
| 14811 | errno = 0; |
| 14812 | value = sysconf(name); |
| 14813 | if (value == -1 && errno != 0) |
| 14814 | posix_error(); |
| 14815 | return value; |
| 14816 | } |
| 14817 | #endif /* HAVE_SYSCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14818 | |
| 14819 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14820 | /* 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] | 14821 | * 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] | 14822 | * the exported dictionaries that are used to publish information about the |
| 14823 | * names available on the host platform. |
| 14824 | * |
| 14825 | * Sorting the table at runtime ensures that the table is properly ordered |
| 14826 | * when used, even for platforms we're not able to test on. It also makes |
| 14827 | * it easier to add additional entries to the tables. |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14828 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14829 | |
| 14830 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 14831 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14832 | { |
| 14833 | const struct constdef *c1 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14834 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14835 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14836 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14837 | |
| 14838 | return strcmp(c1->name, c2->name); |
| 14839 | } |
| 14840 | |
| 14841 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 14842 | setup_confname_table(struct constdef *table, size_t tablesize, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14843 | char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14844 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14845 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 14846 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14847 | |
| 14848 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); |
| 14849 | d = PyDict_New(); |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 14850 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14851 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14852 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 14853 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14854 | PyObject *o = PyLong_FromLong(table[i].value); |
| 14855 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { |
| 14856 | Py_XDECREF(o); |
| 14857 | Py_DECREF(d); |
| 14858 | return -1; |
| 14859 | } |
| 14860 | Py_DECREF(o); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14861 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 14862 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14863 | } |
| 14864 | |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14865 | /* Return -1 on failure, 0 on success. */ |
| 14866 | static int |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 14867 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14868 | { |
| 14869 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14870 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14871 | sizeof(posix_constants_pathconf) |
| 14872 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 14873 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 14874 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14875 | #endif |
| 14876 | #ifdef HAVE_CONFSTR |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14877 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14878 | sizeof(posix_constants_confstr) |
| 14879 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 14880 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 14881 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14882 | #endif |
| 14883 | #ifdef HAVE_SYSCONF |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14884 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14885 | sizeof(posix_constants_sysconf) |
| 14886 | / sizeof(struct constdef), |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 14887 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 14888 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14889 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14890 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14891 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 14892 | |
| 14893 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14894 | /*[clinic input] |
| 14895 | os.abort |
| 14896 | |
| 14897 | Abort the interpreter immediately. |
| 14898 | |
| 14899 | This function 'dumps core' or otherwise fails in the hardest way possible |
| 14900 | on the hosting operating system. This function never returns. |
| 14901 | [clinic start generated code]*/ |
| 14902 | |
| 14903 | PyDoc_STRVAR(os_abort__doc__, |
| 14904 | "abort($module, /)\n" |
| 14905 | "--\n" |
| 14906 | "\n" |
| 14907 | "Abort the interpreter immediately.\n" |
| 14908 | "\n" |
| 14909 | "This function \'dumps core\' or otherwise fails in the hardest way possible\n" |
| 14910 | "on the hosting operating system. This function never returns."); |
| 14911 | |
| 14912 | #define OS_ABORT_METHODDEF \ |
| 14913 | {"abort", (PyCFunction)os_abort, METH_NOARGS, os_abort__doc__}, |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14914 | |
| 14915 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14916 | os_abort_impl(PyModuleDef *module); |
| 14917 | |
| 14918 | static PyObject * |
| 14919 | os_abort(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 14920 | { |
| 14921 | return os_abort_impl(module); |
| 14922 | } |
| 14923 | |
| 14924 | static PyObject * |
| 14925 | os_abort_impl(PyModuleDef *module) |
| 14926 | /*[clinic end generated code: output=cded2cc8c5453d3a input=cf2c7d98bc504047]*/ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14927 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 14928 | abort(); |
| 14929 | /*NOTREACHED*/ |
| 14930 | Py_FatalError("abort() called from Python code didn't abort!"); |
| 14931 | return NULL; |
| 14932 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14933 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 14934 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14935 | /* 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] | 14936 | PyDoc_STRVAR(win32_startfile__doc__, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 14937 | "startfile(filepath [, operation])\n\ |
| 14938 | \n\ |
| 14939 | Start a file with its associated application.\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 14940 | \n\ |
Georg Brandl | f4f4415 | 2006-02-18 22:29:33 +0000 | [diff] [blame] | 14941 | When \"operation\" is not specified or \"open\", this acts like\n\ |
| 14942 | double-clicking the file in Explorer, or giving the file name as an\n\ |
| 14943 | argument to the DOS \"start\" command: the file is opened with whatever\n\ |
| 14944 | application (if any) its extension is associated.\n\ |
| 14945 | When another \"operation\" is given, it specifies what should be done with\n\ |
| 14946 | the file. A typical operation is \"print\".\n\ |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 14947 | \n\ |
| 14948 | startfile returns as soon as the associated application is launched.\n\ |
| 14949 | There is no option to wait for the application to close, and no way\n\ |
| 14950 | to retrieve the application's exit status.\n\ |
| 14951 | \n\ |
| 14952 | The filepath is relative to the current directory. If you want to use\n\ |
| 14953 | 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] | 14954 | the underlying Win32 ShellExecute function doesn't work if it is."); |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 14955 | |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 14956 | /* Grab ShellExecute dynamically from shell32 */ |
| 14957 | static int has_ShellExecute = -1; |
| 14958 | static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR, |
| 14959 | LPCSTR, INT); |
| 14960 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
| 14961 | LPCWSTR, INT); |
| 14962 | static int |
| 14963 | check_ShellExecute() |
| 14964 | { |
| 14965 | HINSTANCE hShell32; |
| 14966 | |
| 14967 | /* only recheck */ |
| 14968 | if (-1 == has_ShellExecute) { |
| 14969 | Py_BEGIN_ALLOW_THREADS |
| 14970 | hShell32 = LoadLibraryW(L"SHELL32"); |
| 14971 | Py_END_ALLOW_THREADS |
| 14972 | if (hShell32) { |
| 14973 | *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32, |
| 14974 | "ShellExecuteA"); |
| 14975 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
| 14976 | "ShellExecuteW"); |
| 14977 | has_ShellExecute = Py_ShellExecuteA && |
| 14978 | Py_ShellExecuteW; |
| 14979 | } else { |
| 14980 | has_ShellExecute = 0; |
| 14981 | } |
| 14982 | } |
| 14983 | return has_ShellExecute; |
| 14984 | } |
| 14985 | |
| 14986 | |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 14987 | static PyObject * |
| 14988 | win32_startfile(PyObject *self, PyObject *args) |
| 14989 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14990 | PyObject *ofilepath; |
| 14991 | char *filepath; |
| 14992 | char *operation = NULL; |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 14993 | wchar_t *wpath, *woperation; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14994 | HINSTANCE rc; |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 14995 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 14996 | PyObject *unipath, *uoperation = NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 14997 | |
| 14998 | if(!check_ShellExecute()) { |
| 14999 | /* If the OS doesn't have ShellExecute, return a |
| 15000 | NotImplementedError. */ |
| 15001 | return PyErr_Format(PyExc_NotImplementedError, |
| 15002 | "startfile not available on this platform"); |
| 15003 | } |
| 15004 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15005 | if (!PyArg_ParseTuple(args, "U|s:startfile", |
| 15006 | &unipath, &operation)) { |
| 15007 | PyErr_Clear(); |
| 15008 | goto normal; |
| 15009 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 15010 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15011 | if (operation) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 15012 | uoperation = PyUnicode_DecodeASCII(operation, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15013 | strlen(operation), NULL); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 15014 | if (!uoperation) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15015 | PyErr_Clear(); |
| 15016 | operation = NULL; |
| 15017 | goto normal; |
| 15018 | } |
| 15019 | } |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 15020 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 15021 | wpath = PyUnicode_AsUnicode(unipath); |
| 15022 | if (wpath == NULL) |
| 15023 | goto normal; |
| 15024 | if (uoperation) { |
| 15025 | woperation = PyUnicode_AsUnicode(uoperation); |
| 15026 | if (woperation == NULL) |
| 15027 | goto normal; |
| 15028 | } |
| 15029 | else |
| 15030 | woperation = NULL; |
| 15031 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15032 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 15033 | rc = Py_ShellExecuteW((HWND)0, woperation, wpath, |
| 15034 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15035 | Py_END_ALLOW_THREADS |
| 15036 | |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 15037 | Py_XDECREF(uoperation); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15038 | if (rc <= (HINSTANCE)32) { |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 15039 | win32_error_object("startfile", unipath); |
| 15040 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15041 | } |
| 15042 | Py_INCREF(Py_None); |
| 15043 | return Py_None; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15044 | |
| 15045 | normal: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15046 | if (!PyArg_ParseTuple(args, "O&|s:startfile", |
| 15047 | PyUnicode_FSConverter, &ofilepath, |
| 15048 | &operation)) |
| 15049 | return NULL; |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 15050 | if (win32_warn_bytes_api()) { |
| 15051 | Py_DECREF(ofilepath); |
| 15052 | return NULL; |
| 15053 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15054 | filepath = PyBytes_AsString(ofilepath); |
| 15055 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 15056 | rc = Py_ShellExecuteA((HWND)0, operation, filepath, |
| 15057 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15058 | Py_END_ALLOW_THREADS |
| 15059 | if (rc <= (HINSTANCE)32) { |
| 15060 | PyObject *errval = win32_error("startfile", filepath); |
| 15061 | Py_DECREF(ofilepath); |
| 15062 | return errval; |
| 15063 | } |
| 15064 | Py_DECREF(ofilepath); |
| 15065 | Py_INCREF(Py_None); |
| 15066 | return Py_None; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 15067 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15068 | #endif /* MS_WINDOWS */ |
| 15069 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 15070 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15071 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15072 | /*[clinic input] |
| 15073 | os.getloadavg |
| 15074 | |
| 15075 | Return average recent system load information. |
| 15076 | |
| 15077 | Return the number of processes in the system run queue averaged over |
| 15078 | the last 1, 5, and 15 minutes as a tuple of three floats. |
| 15079 | Raises OSError if the load average was unobtainable. |
| 15080 | [clinic start generated code]*/ |
| 15081 | |
| 15082 | PyDoc_STRVAR(os_getloadavg__doc__, |
| 15083 | "getloadavg($module, /)\n" |
| 15084 | "--\n" |
| 15085 | "\n" |
| 15086 | "Return average recent system load information.\n" |
| 15087 | "\n" |
| 15088 | "Return the number of processes in the system run queue averaged over\n" |
| 15089 | "the last 1, 5, and 15 minutes as a tuple of three floats.\n" |
| 15090 | "Raises OSError if the load average was unobtainable."); |
| 15091 | |
| 15092 | #define OS_GETLOADAVG_METHODDEF \ |
| 15093 | {"getloadavg", (PyCFunction)os_getloadavg, METH_NOARGS, os_getloadavg__doc__}, |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15094 | |
| 15095 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15096 | os_getloadavg_impl(PyModuleDef *module); |
| 15097 | |
| 15098 | static PyObject * |
| 15099 | os_getloadavg(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 15100 | { |
| 15101 | return os_getloadavg_impl(module); |
| 15102 | } |
| 15103 | |
| 15104 | static PyObject * |
| 15105 | os_getloadavg_impl(PyModuleDef *module) |
| 15106 | /*[clinic end generated code: output=67593a92457d55af input=3d6d826b76d8a34e]*/ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15107 | { |
| 15108 | double loadavg[3]; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15109 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 15110 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
| 15111 | return NULL; |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15112 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 15113 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15114 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15115 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 15116 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15117 | |
| 15118 | /*[clinic input] |
| 15119 | os.device_encoding |
| 15120 | fd: int |
| 15121 | |
| 15122 | Return a string describing the encoding of a terminal's file descriptor. |
| 15123 | |
| 15124 | The file descriptor must be attached to a terminal. |
| 15125 | If the device is not a terminal, return None. |
| 15126 | [clinic start generated code]*/ |
| 15127 | |
| 15128 | PyDoc_STRVAR(os_device_encoding__doc__, |
| 15129 | "device_encoding($module, /, fd)\n" |
| 15130 | "--\n" |
| 15131 | "\n" |
| 15132 | "Return a string describing the encoding of a terminal\'s file descriptor.\n" |
| 15133 | "\n" |
| 15134 | "The file descriptor must be attached to a terminal.\n" |
| 15135 | "If the device is not a terminal, return None."); |
| 15136 | |
| 15137 | #define OS_DEVICE_ENCODING_METHODDEF \ |
| 15138 | {"device_encoding", (PyCFunction)os_device_encoding, METH_VARARGS|METH_KEYWORDS, os_device_encoding__doc__}, |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 15139 | |
| 15140 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15141 | os_device_encoding_impl(PyModuleDef *module, int fd); |
| 15142 | |
| 15143 | static PyObject * |
| 15144 | os_device_encoding(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 15145 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15146 | PyObject *return_value = NULL; |
| 15147 | static char *_keywords[] = {"fd", NULL}; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15148 | int fd; |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 15149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15150 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 15151 | "i:device_encoding", _keywords, |
| 15152 | &fd)) |
| 15153 | goto exit; |
| 15154 | return_value = os_device_encoding_impl(module, fd); |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 15155 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15156 | exit: |
| 15157 | return return_value; |
| 15158 | } |
| 15159 | |
| 15160 | static PyObject * |
| 15161 | os_device_encoding_impl(PyModuleDef *module, int fd) |
| 15162 | /*[clinic end generated code: output=e9f8274d42f5cce3 input=9e1d4a42b66df312]*/ |
| 15163 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 15164 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 15165 | } |
| 15166 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15167 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15168 | #ifdef HAVE_SETRESUID |
| 15169 | /*[clinic input] |
| 15170 | os.setresuid |
| 15171 | |
| 15172 | ruid: uid_t |
| 15173 | euid: uid_t |
| 15174 | suid: uid_t |
| 15175 | / |
| 15176 | |
| 15177 | Set the current process's real, effective, and saved user ids. |
| 15178 | [clinic start generated code]*/ |
| 15179 | |
| 15180 | PyDoc_STRVAR(os_setresuid__doc__, |
| 15181 | "setresuid($module, ruid, euid, suid, /)\n" |
| 15182 | "--\n" |
| 15183 | "\n" |
| 15184 | "Set the current process\'s real, effective, and saved user ids."); |
| 15185 | |
| 15186 | #define OS_SETRESUID_METHODDEF \ |
| 15187 | {"setresuid", (PyCFunction)os_setresuid, METH_VARARGS, os_setresuid__doc__}, |
| 15188 | |
| 15189 | static PyObject * |
| 15190 | os_setresuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid, uid_t suid); |
| 15191 | |
| 15192 | static PyObject * |
| 15193 | os_setresuid(PyModuleDef *module, PyObject *args) |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15194 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15195 | PyObject *return_value = NULL; |
| 15196 | uid_t ruid; |
| 15197 | uid_t euid; |
| 15198 | uid_t suid; |
| 15199 | |
| 15200 | if (!PyArg_ParseTuple(args, |
| 15201 | "O&O&O&:setresuid", |
| 15202 | _Py_Uid_Converter, &ruid, _Py_Uid_Converter, &euid, _Py_Uid_Converter, &suid)) |
| 15203 | goto exit; |
| 15204 | return_value = os_setresuid_impl(module, ruid, euid, suid); |
| 15205 | |
| 15206 | exit: |
| 15207 | return return_value; |
| 15208 | } |
| 15209 | |
| 15210 | static PyObject * |
| 15211 | os_setresuid_impl(PyModuleDef *module, uid_t ruid, uid_t euid, uid_t suid) |
| 15212 | /*[clinic end generated code: output=2e3457cfe7cd1f94 input=9e33cb79a82792f3]*/ |
| 15213 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15214 | if (setresuid(ruid, euid, suid) < 0) |
| 15215 | return posix_error(); |
| 15216 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15217 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15218 | #endif /* HAVE_SETRESUID */ |
| 15219 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15220 | |
| 15221 | #ifdef HAVE_SETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15222 | /*[clinic input] |
| 15223 | os.setresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15224 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15225 | rgid: gid_t |
| 15226 | egid: gid_t |
| 15227 | sgid: gid_t |
| 15228 | / |
| 15229 | |
| 15230 | Set the current process's real, effective, and saved group ids. |
| 15231 | [clinic start generated code]*/ |
| 15232 | |
| 15233 | PyDoc_STRVAR(os_setresgid__doc__, |
| 15234 | "setresgid($module, rgid, egid, sgid, /)\n" |
| 15235 | "--\n" |
| 15236 | "\n" |
| 15237 | "Set the current process\'s real, effective, and saved group ids."); |
| 15238 | |
| 15239 | #define OS_SETRESGID_METHODDEF \ |
| 15240 | {"setresgid", (PyCFunction)os_setresgid, METH_VARARGS, os_setresgid__doc__}, |
| 15241 | |
| 15242 | static PyObject * |
| 15243 | os_setresgid_impl(PyModuleDef *module, gid_t rgid, gid_t egid, gid_t sgid); |
| 15244 | |
| 15245 | static PyObject * |
| 15246 | os_setresgid(PyModuleDef *module, PyObject *args) |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15247 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15248 | PyObject *return_value = NULL; |
| 15249 | gid_t rgid; |
| 15250 | gid_t egid; |
| 15251 | gid_t sgid; |
| 15252 | |
| 15253 | if (!PyArg_ParseTuple(args, |
| 15254 | "O&O&O&:setresgid", |
| 15255 | _Py_Gid_Converter, &rgid, _Py_Gid_Converter, &egid, _Py_Gid_Converter, &sgid)) |
| 15256 | goto exit; |
| 15257 | return_value = os_setresgid_impl(module, rgid, egid, sgid); |
| 15258 | |
| 15259 | exit: |
| 15260 | return return_value; |
| 15261 | } |
| 15262 | |
| 15263 | static PyObject * |
| 15264 | os_setresgid_impl(PyModuleDef *module, gid_t rgid, gid_t egid, gid_t sgid) |
| 15265 | /*[clinic end generated code: output=8a7ee6c1f2482362 input=33e9e0785ef426b1]*/ |
| 15266 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15267 | if (setresgid(rgid, egid, sgid) < 0) |
| 15268 | return posix_error(); |
| 15269 | Py_RETURN_NONE; |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15270 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15271 | #endif /* HAVE_SETRESGID */ |
| 15272 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15273 | |
| 15274 | #ifdef HAVE_GETRESUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15275 | /*[clinic input] |
| 15276 | os.getresuid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15277 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15278 | Return a tuple of the current process's real, effective, and saved user ids. |
| 15279 | [clinic start generated code]*/ |
| 15280 | |
| 15281 | PyDoc_STRVAR(os_getresuid__doc__, |
| 15282 | "getresuid($module, /)\n" |
| 15283 | "--\n" |
| 15284 | "\n" |
| 15285 | "Return a tuple of the current process\'s real, effective, and saved user ids."); |
| 15286 | |
| 15287 | #define OS_GETRESUID_METHODDEF \ |
| 15288 | {"getresuid", (PyCFunction)os_getresuid, METH_NOARGS, os_getresuid__doc__}, |
| 15289 | |
| 15290 | static PyObject * |
| 15291 | os_getresuid_impl(PyModuleDef *module); |
| 15292 | |
| 15293 | static PyObject * |
| 15294 | os_getresuid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 15295 | { |
| 15296 | return os_getresuid_impl(module); |
| 15297 | } |
| 15298 | |
| 15299 | static PyObject * |
| 15300 | os_getresuid_impl(PyModuleDef *module) |
| 15301 | /*[clinic end generated code: output=d0786686a6ef1320 input=41ccfa8e1f6517ad]*/ |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15302 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15303 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15304 | if (getresuid(&ruid, &euid, &suid) < 0) |
| 15305 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 15306 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
| 15307 | _PyLong_FromUid(euid), |
| 15308 | _PyLong_FromUid(suid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15309 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15310 | #endif /* HAVE_GETRESUID */ |
| 15311 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15312 | |
| 15313 | #ifdef HAVE_GETRESGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15314 | /*[clinic input] |
| 15315 | os.getresgid |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15316 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15317 | Return a tuple of the current process's real, effective, and saved group ids. |
| 15318 | [clinic start generated code]*/ |
| 15319 | |
| 15320 | PyDoc_STRVAR(os_getresgid__doc__, |
| 15321 | "getresgid($module, /)\n" |
| 15322 | "--\n" |
| 15323 | "\n" |
| 15324 | "Return a tuple of the current process\'s real, effective, and saved group ids."); |
| 15325 | |
| 15326 | #define OS_GETRESGID_METHODDEF \ |
| 15327 | {"getresgid", (PyCFunction)os_getresgid, METH_NOARGS, os_getresgid__doc__}, |
| 15328 | |
| 15329 | static PyObject * |
| 15330 | os_getresgid_impl(PyModuleDef *module); |
| 15331 | |
| 15332 | static PyObject * |
| 15333 | os_getresgid(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15334 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15335 | return os_getresgid_impl(module); |
| 15336 | } |
| 15337 | |
| 15338 | static PyObject * |
| 15339 | os_getresgid_impl(PyModuleDef *module) |
| 15340 | /*[clinic end generated code: output=05249ac795fa759f input=517e68db9ca32df6]*/ |
| 15341 | { |
| 15342 | gid_t rgid, egid, sgid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 15343 | if (getresgid(&rgid, &egid, &sgid) < 0) |
| 15344 | return posix_error(); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 15345 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
| 15346 | _PyLong_FromGid(egid), |
| 15347 | _PyLong_FromGid(sgid)); |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15348 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15349 | #endif /* HAVE_GETRESGID */ |
| 15350 | |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 15351 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 15352 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15353 | /*[clinic input] |
| 15354 | os.getxattr |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15355 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15356 | path: path_t(allow_fd=True) |
| 15357 | attribute: path_t |
| 15358 | * |
| 15359 | follow_symlinks: bool = True |
| 15360 | |
| 15361 | Return the value of extended attribute attribute on path. |
| 15362 | |
| 15363 | path may be either a string or an open file descriptor. |
| 15364 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 15365 | link, getxattr will examine the symbolic link itself instead of the file |
| 15366 | the link points to. |
| 15367 | |
| 15368 | [clinic start generated code]*/ |
| 15369 | |
| 15370 | PyDoc_STRVAR(os_getxattr__doc__, |
| 15371 | "getxattr($module, /, path, attribute, *, follow_symlinks=True)\n" |
| 15372 | "--\n" |
| 15373 | "\n" |
| 15374 | "Return the value of extended attribute attribute on path.\n" |
| 15375 | "\n" |
| 15376 | "path may be either a string or an open file descriptor.\n" |
| 15377 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 15378 | " link, getxattr will examine the symbolic link itself instead of the file\n" |
| 15379 | " the link points to."); |
| 15380 | |
| 15381 | #define OS_GETXATTR_METHODDEF \ |
| 15382 | {"getxattr", (PyCFunction)os_getxattr, METH_VARARGS|METH_KEYWORDS, os_getxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15383 | |
| 15384 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15385 | os_getxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, int follow_symlinks); |
| 15386 | |
| 15387 | static PyObject * |
| 15388 | os_getxattr(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15389 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15390 | PyObject *return_value = NULL; |
| 15391 | static char *_keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
| 15392 | path_t path = PATH_T_INITIALIZE("getxattr", "path", 0, 1); |
| 15393 | path_t attribute = PATH_T_INITIALIZE("getxattr", "attribute", 0, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15394 | int follow_symlinks = 1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15395 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15396 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 15397 | "O&O&|$p:getxattr", _keywords, |
| 15398 | path_converter, &path, path_converter, &attribute, &follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15399 | goto exit; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15400 | return_value = os_getxattr_impl(module, &path, &attribute, follow_symlinks); |
| 15401 | |
| 15402 | exit: |
| 15403 | /* Cleanup for path */ |
| 15404 | path_cleanup(&path); |
| 15405 | /* Cleanup for attribute */ |
| 15406 | path_cleanup(&attribute); |
| 15407 | |
| 15408 | return return_value; |
| 15409 | } |
| 15410 | |
| 15411 | static PyObject * |
| 15412 | os_getxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, int follow_symlinks) |
| 15413 | /*[clinic end generated code: output=bbc9454fe2b9ea86 input=8c8ea3bab78d89c2]*/ |
| 15414 | { |
| 15415 | Py_ssize_t i; |
| 15416 | PyObject *buffer = NULL; |
| 15417 | |
| 15418 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) |
| 15419 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15420 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15421 | for (i = 0; ; i++) { |
| 15422 | void *ptr; |
| 15423 | ssize_t result; |
| 15424 | static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
| 15425 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 15426 | if (!buffer_size) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15427 | path_error(path); |
| 15428 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15429 | } |
| 15430 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); |
| 15431 | if (!buffer) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15432 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15433 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15434 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15435 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15436 | if (path->fd >= 0) |
| 15437 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15438 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15439 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15440 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15441 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15442 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15443 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15444 | if (result < 0) { |
| 15445 | Py_DECREF(buffer); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15446 | if (errno == ERANGE) |
| 15447 | continue; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15448 | path_error(path); |
| 15449 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15450 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15451 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15452 | if (result != buffer_size) { |
| 15453 | /* Can only shrink. */ |
| 15454 | _PyBytes_Resize(&buffer, result); |
| 15455 | } |
| 15456 | break; |
| 15457 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15458 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15459 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15460 | } |
| 15461 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15462 | |
| 15463 | /*[clinic input] |
| 15464 | os.setxattr |
| 15465 | |
| 15466 | path: path_t(allow_fd=True) |
| 15467 | attribute: path_t |
| 15468 | value: Py_buffer |
| 15469 | flags: int = 0 |
| 15470 | * |
| 15471 | follow_symlinks: bool = True |
| 15472 | |
| 15473 | Set extended attribute attribute on path to value. |
| 15474 | |
| 15475 | path may be either a string or an open file descriptor. |
| 15476 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 15477 | link, setxattr will modify the symbolic link itself instead of the file |
| 15478 | the link points to. |
| 15479 | |
| 15480 | [clinic start generated code]*/ |
| 15481 | |
| 15482 | PyDoc_STRVAR(os_setxattr__doc__, |
| 15483 | "setxattr($module, /, path, attribute, value, flags=0, *,\n" |
| 15484 | " follow_symlinks=True)\n" |
| 15485 | "--\n" |
| 15486 | "\n" |
| 15487 | "Set extended attribute attribute on path to value.\n" |
| 15488 | "\n" |
| 15489 | "path may be either a string or an open file descriptor.\n" |
| 15490 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 15491 | " link, setxattr will modify the symbolic link itself instead of the file\n" |
| 15492 | " the link points to."); |
| 15493 | |
| 15494 | #define OS_SETXATTR_METHODDEF \ |
| 15495 | {"setxattr", (PyCFunction)os_setxattr, METH_VARARGS|METH_KEYWORDS, os_setxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15496 | |
| 15497 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15498 | os_setxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, Py_buffer *value, int flags, int follow_symlinks); |
| 15499 | |
| 15500 | static PyObject * |
| 15501 | os_setxattr(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15502 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15503 | PyObject *return_value = NULL; |
| 15504 | static char *_keywords[] = {"path", "attribute", "value", "flags", "follow_symlinks", NULL}; |
| 15505 | path_t path = PATH_T_INITIALIZE("setxattr", "path", 0, 1); |
| 15506 | path_t attribute = PATH_T_INITIALIZE("setxattr", "attribute", 0, 0); |
| 15507 | Py_buffer value = {NULL, NULL}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15508 | int flags = 0; |
| 15509 | int follow_symlinks = 1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15510 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15511 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 15512 | "O&O&y*|i$p:setxattr", _keywords, |
| 15513 | path_converter, &path, path_converter, &attribute, &value, &flags, &follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15514 | goto exit; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15515 | return_value = os_setxattr_impl(module, &path, &attribute, &value, flags, follow_symlinks); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15516 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15517 | exit: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15518 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15519 | path_cleanup(&path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15520 | /* Cleanup for attribute */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15521 | path_cleanup(&attribute); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15522 | /* Cleanup for value */ |
| 15523 | if (value.obj) |
| 15524 | PyBuffer_Release(&value); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15525 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15526 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15527 | } |
| 15528 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15529 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15530 | os_setxattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, Py_buffer *value, int flags, int follow_symlinks) |
| 15531 | /*[clinic end generated code: output=2ff845d8e024b218 input=f0d26833992015c2]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15532 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15533 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15534 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15535 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15536 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15537 | |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15538 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15539 | if (path->fd > -1) |
| 15540 | result = fsetxattr(path->fd, attribute->narrow, |
| 15541 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15542 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15543 | result = setxattr(path->narrow, attribute->narrow, |
| 15544 | value->buf, value->len, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15545 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15546 | result = lsetxattr(path->narrow, attribute->narrow, |
| 15547 | value->buf, value->len, flags); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15548 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15549 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15550 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15551 | path_error(path); |
| 15552 | return NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15553 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15555 | Py_RETURN_NONE; |
| 15556 | } |
| 15557 | |
| 15558 | |
| 15559 | /*[clinic input] |
| 15560 | os.removexattr |
| 15561 | |
| 15562 | path: path_t(allow_fd=True) |
| 15563 | attribute: path_t |
| 15564 | * |
| 15565 | follow_symlinks: bool = True |
| 15566 | |
| 15567 | Remove extended attribute attribute on path. |
| 15568 | |
| 15569 | path may be either a string or an open file descriptor. |
| 15570 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 15571 | link, removexattr will modify the symbolic link itself instead of the file |
| 15572 | the link points to. |
| 15573 | |
| 15574 | [clinic start generated code]*/ |
| 15575 | |
| 15576 | PyDoc_STRVAR(os_removexattr__doc__, |
| 15577 | "removexattr($module, /, path, attribute, *, follow_symlinks=True)\n" |
| 15578 | "--\n" |
| 15579 | "\n" |
| 15580 | "Remove extended attribute attribute on path.\n" |
| 15581 | "\n" |
| 15582 | "path may be either a string or an open file descriptor.\n" |
| 15583 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 15584 | " link, removexattr will modify the symbolic link itself instead of the file\n" |
| 15585 | " the link points to."); |
| 15586 | |
| 15587 | #define OS_REMOVEXATTR_METHODDEF \ |
| 15588 | {"removexattr", (PyCFunction)os_removexattr, METH_VARARGS|METH_KEYWORDS, os_removexattr__doc__}, |
| 15589 | |
| 15590 | static PyObject * |
| 15591 | os_removexattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, int follow_symlinks); |
| 15592 | |
| 15593 | static PyObject * |
| 15594 | os_removexattr(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
| 15595 | { |
| 15596 | PyObject *return_value = NULL; |
| 15597 | static char *_keywords[] = {"path", "attribute", "follow_symlinks", NULL}; |
| 15598 | path_t path = PATH_T_INITIALIZE("removexattr", "path", 0, 1); |
| 15599 | path_t attribute = PATH_T_INITIALIZE("removexattr", "attribute", 0, 0); |
| 15600 | int follow_symlinks = 1; |
| 15601 | |
| 15602 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 15603 | "O&O&|$p:removexattr", _keywords, |
| 15604 | path_converter, &path, path_converter, &attribute, &follow_symlinks)) |
| 15605 | goto exit; |
| 15606 | return_value = os_removexattr_impl(module, &path, &attribute, follow_symlinks); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15607 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15608 | exit: |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15609 | /* Cleanup for path */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15610 | path_cleanup(&path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15611 | /* Cleanup for attribute */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15612 | path_cleanup(&attribute); |
| 15613 | |
| 15614 | return return_value; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15615 | } |
| 15616 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15617 | static PyObject * |
| 15618 | os_removexattr_impl(PyModuleDef *module, path_t *path, path_t *attribute, int follow_symlinks) |
| 15619 | /*[clinic end generated code: output=8dfc715bf607c4cf input=cdb54834161e3329]*/ |
| 15620 | { |
| 15621 | ssize_t result; |
| 15622 | |
| 15623 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) |
| 15624 | return NULL; |
| 15625 | |
| 15626 | Py_BEGIN_ALLOW_THREADS; |
| 15627 | if (path->fd > -1) |
| 15628 | result = fremovexattr(path->fd, attribute->narrow); |
| 15629 | else if (follow_symlinks) |
| 15630 | result = removexattr(path->narrow, attribute->narrow); |
| 15631 | else |
| 15632 | result = lremovexattr(path->narrow, attribute->narrow); |
| 15633 | Py_END_ALLOW_THREADS; |
| 15634 | |
| 15635 | if (result) { |
| 15636 | return path_error(path); |
| 15637 | } |
| 15638 | |
| 15639 | Py_RETURN_NONE; |
| 15640 | } |
| 15641 | |
| 15642 | |
| 15643 | /*[clinic input] |
| 15644 | os.listxattr |
| 15645 | |
| 15646 | path: path_t(allow_fd=True, nullable=True) = None |
| 15647 | * |
| 15648 | follow_symlinks: bool = True |
| 15649 | |
| 15650 | Return a list of extended attributes on path. |
| 15651 | |
| 15652 | path may be either None, a string, or an open file descriptor. |
| 15653 | if path is None, listxattr will examine the current directory. |
| 15654 | If follow_symlinks is False, and the last element of the path is a symbolic |
| 15655 | link, listxattr will examine the symbolic link itself instead of the file |
| 15656 | the link points to. |
| 15657 | [clinic start generated code]*/ |
| 15658 | |
| 15659 | PyDoc_STRVAR(os_listxattr__doc__, |
| 15660 | "listxattr($module, /, path=None, *, follow_symlinks=True)\n" |
| 15661 | "--\n" |
| 15662 | "\n" |
| 15663 | "Return a list of extended attributes on path.\n" |
| 15664 | "\n" |
| 15665 | "path may be either None, a string, or an open file descriptor.\n" |
| 15666 | "if path is None, listxattr will examine the current directory.\n" |
| 15667 | "If follow_symlinks is False, and the last element of the path is a symbolic\n" |
| 15668 | " link, listxattr will examine the symbolic link itself instead of the file\n" |
| 15669 | " the link points to."); |
| 15670 | |
| 15671 | #define OS_LISTXATTR_METHODDEF \ |
| 15672 | {"listxattr", (PyCFunction)os_listxattr, METH_VARARGS|METH_KEYWORDS, os_listxattr__doc__}, |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15673 | |
| 15674 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15675 | os_listxattr_impl(PyModuleDef *module, path_t *path, int follow_symlinks); |
| 15676 | |
| 15677 | static PyObject * |
| 15678 | os_listxattr(PyModuleDef *module, PyObject *args, PyObject *kwargs) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15679 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15680 | PyObject *return_value = NULL; |
| 15681 | static char *_keywords[] = {"path", "follow_symlinks", NULL}; |
| 15682 | path_t path = PATH_T_INITIALIZE("listxattr", "path", 1, 1); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15683 | int follow_symlinks = 1; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15684 | |
| 15685 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 15686 | "|O&$p:listxattr", _keywords, |
| 15687 | path_converter, &path, &follow_symlinks)) |
| 15688 | goto exit; |
| 15689 | return_value = os_listxattr_impl(module, &path, follow_symlinks); |
| 15690 | |
| 15691 | exit: |
| 15692 | /* Cleanup for path */ |
| 15693 | path_cleanup(&path); |
| 15694 | |
| 15695 | return return_value; |
| 15696 | } |
| 15697 | |
| 15698 | static PyObject * |
| 15699 | os_listxattr_impl(PyModuleDef *module, path_t *path, int follow_symlinks) |
| 15700 | /*[clinic end generated code: output=3104cafda1a3d887 input=08cca53ac0b07c13]*/ |
| 15701 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15702 | Py_ssize_t i; |
| 15703 | PyObject *result = NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15704 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15705 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15706 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15707 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15708 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15709 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15710 | name = path->narrow ? path->narrow : "."; |
| 15711 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15712 | for (i = 0; ; i++) { |
| 15713 | char *start, *trace, *end; |
| 15714 | ssize_t length; |
| 15715 | static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
| 15716 | Py_ssize_t buffer_size = buffer_sizes[i]; |
| 15717 | if (!buffer_size) { |
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 15718 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15719 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15720 | break; |
| 15721 | } |
| 15722 | buffer = PyMem_MALLOC(buffer_size); |
| 15723 | if (!buffer) { |
| 15724 | PyErr_NoMemory(); |
| 15725 | break; |
| 15726 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15727 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15728 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15729 | if (path->fd > -1) |
| 15730 | length = flistxattr(path->fd, buffer, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15731 | else if (follow_symlinks) |
| 15732 | length = listxattr(name, buffer, buffer_size); |
| 15733 | else |
| 15734 | length = llistxattr(name, buffer, buffer_size); |
| 15735 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15736 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15737 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 15738 | if (errno == ERANGE) { |
| 15739 | PyMem_FREE(buffer); |
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 15740 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15741 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 15742 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15743 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15744 | break; |
| 15745 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15746 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15747 | result = PyList_New(0); |
| 15748 | if (!result) { |
| 15749 | goto exit; |
| 15750 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15751 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15752 | end = buffer + length; |
| 15753 | for (trace = start = buffer; trace != end; trace++) { |
| 15754 | if (!*trace) { |
| 15755 | int error; |
| 15756 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, |
| 15757 | trace - start); |
| 15758 | if (!attribute) { |
| 15759 | Py_DECREF(result); |
| 15760 | result = NULL; |
| 15761 | goto exit; |
| 15762 | } |
| 15763 | error = PyList_Append(result, attribute); |
| 15764 | Py_DECREF(attribute); |
| 15765 | if (error) { |
| 15766 | Py_DECREF(result); |
| 15767 | result = NULL; |
| 15768 | goto exit; |
| 15769 | } |
| 15770 | start = trace + 1; |
| 15771 | } |
| 15772 | } |
| 15773 | break; |
| 15774 | } |
| 15775 | exit: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 15776 | if (buffer) |
| 15777 | PyMem_FREE(buffer); |
| 15778 | return result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15779 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 15780 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 15781 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 15782 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15783 | /*[clinic input] |
| 15784 | os.urandom |
| 15785 | |
| 15786 | size: Py_ssize_t |
| 15787 | / |
| 15788 | |
| 15789 | Return a bytes object containing random bytes suitable for cryptographic use. |
| 15790 | [clinic start generated code]*/ |
| 15791 | |
| 15792 | PyDoc_STRVAR(os_urandom__doc__, |
| 15793 | "urandom($module, size, /)\n" |
| 15794 | "--\n" |
| 15795 | "\n" |
| 15796 | "Return a bytes object containing random bytes suitable for cryptographic use."); |
| 15797 | |
| 15798 | #define OS_URANDOM_METHODDEF \ |
| 15799 | {"urandom", (PyCFunction)os_urandom, METH_VARARGS, os_urandom__doc__}, |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 15800 | |
| 15801 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15802 | os_urandom_impl(PyModuleDef *module, Py_ssize_t size); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 15803 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15804 | static PyObject * |
| 15805 | os_urandom(PyModuleDef *module, PyObject *args) |
| 15806 | { |
| 15807 | PyObject *return_value = NULL; |
| 15808 | Py_ssize_t size; |
| 15809 | |
| 15810 | if (!PyArg_ParseTuple(args, |
| 15811 | "n:urandom", |
| 15812 | &size)) |
| 15813 | goto exit; |
| 15814 | return_value = os_urandom_impl(module, size); |
| 15815 | |
| 15816 | exit: |
| 15817 | return return_value; |
| 15818 | } |
| 15819 | |
| 15820 | static PyObject * |
| 15821 | os_urandom_impl(PyModuleDef *module, Py_ssize_t size) |
| 15822 | /*[clinic end generated code: output=5dbff582cab94cb9 input=4067cdb1b6776c29]*/ |
| 15823 | { |
| 15824 | PyObject *bytes; |
| 15825 | int result; |
| 15826 | |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 15827 | if (size < 0) |
| 15828 | return PyErr_Format(PyExc_ValueError, |
| 15829 | "negative argument not allowed"); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15830 | bytes = PyBytes_FromStringAndSize(NULL, size); |
| 15831 | if (bytes == NULL) |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 15832 | return NULL; |
| 15833 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15834 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), |
| 15835 | PyBytes_GET_SIZE(bytes)); |
| 15836 | if (result == -1) { |
| 15837 | Py_DECREF(bytes); |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 15838 | return NULL; |
| 15839 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15840 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 15841 | } |
| 15842 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 15843 | /* Terminal size querying */ |
| 15844 | |
| 15845 | static PyTypeObject TerminalSizeType; |
| 15846 | |
| 15847 | PyDoc_STRVAR(TerminalSize_docstring, |
| 15848 | "A tuple of (columns, lines) for holding terminal window size"); |
| 15849 | |
| 15850 | static PyStructSequence_Field TerminalSize_fields[] = { |
| 15851 | {"columns", "width of the terminal window in characters"}, |
| 15852 | {"lines", "height of the terminal window in characters"}, |
| 15853 | {NULL, NULL} |
| 15854 | }; |
| 15855 | |
| 15856 | static PyStructSequence_Desc TerminalSize_desc = { |
| 15857 | "os.terminal_size", |
| 15858 | TerminalSize_docstring, |
| 15859 | TerminalSize_fields, |
| 15860 | 2, |
| 15861 | }; |
| 15862 | |
| 15863 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15864 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 15865 | PyDoc_STRVAR(termsize__doc__, |
| 15866 | "Return the size of the terminal window as (columns, lines).\n" \ |
| 15867 | "\n" \ |
| 15868 | "The optional argument fd (default standard output) specifies\n" \ |
| 15869 | "which file descriptor should be queried.\n" \ |
| 15870 | "\n" \ |
| 15871 | "If the file descriptor is not connected to a terminal, an OSError\n" \ |
| 15872 | "is thrown.\n" \ |
| 15873 | "\n" \ |
| 15874 | "This function will only be defined if an implementation is\n" \ |
| 15875 | "available for this system.\n" \ |
| 15876 | "\n" \ |
| 15877 | "shutil.get_terminal_size is the high-level function which should \n" \ |
| 15878 | "normally be used, os.get_terminal_size is the low-level implementation."); |
| 15879 | |
| 15880 | static PyObject* |
| 15881 | get_terminal_size(PyObject *self, PyObject *args) |
| 15882 | { |
| 15883 | int columns, lines; |
| 15884 | PyObject *termsize; |
| 15885 | |
| 15886 | int fd = fileno(stdout); |
| 15887 | /* Under some conditions stdout may not be connected and |
| 15888 | * fileno(stdout) may point to an invalid file descriptor. For example |
| 15889 | * GUI apps don't have valid standard streams by default. |
| 15890 | * |
| 15891 | * If this happens, and the optional fd argument is not present, |
| 15892 | * the ioctl below will fail returning EBADF. This is what we want. |
| 15893 | */ |
| 15894 | |
| 15895 | if (!PyArg_ParseTuple(args, "|i", &fd)) |
| 15896 | return NULL; |
| 15897 | |
| 15898 | #ifdef TERMSIZE_USE_IOCTL |
| 15899 | { |
| 15900 | struct winsize w; |
| 15901 | if (ioctl(fd, TIOCGWINSZ, &w)) |
| 15902 | return PyErr_SetFromErrno(PyExc_OSError); |
| 15903 | columns = w.ws_col; |
| 15904 | lines = w.ws_row; |
| 15905 | } |
| 15906 | #endif /* TERMSIZE_USE_IOCTL */ |
| 15907 | |
| 15908 | #ifdef TERMSIZE_USE_CONIO |
| 15909 | { |
| 15910 | DWORD nhandle; |
| 15911 | HANDLE handle; |
| 15912 | CONSOLE_SCREEN_BUFFER_INFO csbi; |
| 15913 | switch (fd) { |
| 15914 | case 0: nhandle = STD_INPUT_HANDLE; |
| 15915 | break; |
| 15916 | case 1: nhandle = STD_OUTPUT_HANDLE; |
| 15917 | break; |
| 15918 | case 2: nhandle = STD_ERROR_HANDLE; |
| 15919 | break; |
| 15920 | default: |
| 15921 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); |
| 15922 | } |
| 15923 | handle = GetStdHandle(nhandle); |
| 15924 | if (handle == NULL) |
| 15925 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); |
| 15926 | if (handle == INVALID_HANDLE_VALUE) |
| 15927 | return PyErr_SetFromWindowsErr(0); |
| 15928 | |
| 15929 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) |
| 15930 | return PyErr_SetFromWindowsErr(0); |
| 15931 | |
| 15932 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; |
| 15933 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; |
| 15934 | } |
| 15935 | #endif /* TERMSIZE_USE_CONIO */ |
| 15936 | |
| 15937 | termsize = PyStructSequence_New(&TerminalSizeType); |
| 15938 | if (termsize == NULL) |
| 15939 | return NULL; |
| 15940 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); |
| 15941 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); |
| 15942 | if (PyErr_Occurred()) { |
| 15943 | Py_DECREF(termsize); |
| 15944 | return NULL; |
| 15945 | } |
| 15946 | return termsize; |
| 15947 | } |
| 15948 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ |
| 15949 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15950 | |
| 15951 | /*[clinic input] |
| 15952 | os.cpu_count |
| 15953 | |
| 15954 | Return the number of CPUs in the system; return None if indeterminable. |
| 15955 | [clinic start generated code]*/ |
| 15956 | |
| 15957 | PyDoc_STRVAR(os_cpu_count__doc__, |
| 15958 | "cpu_count($module, /)\n" |
| 15959 | "--\n" |
| 15960 | "\n" |
| 15961 | "Return the number of CPUs in the system; return None if indeterminable."); |
| 15962 | |
| 15963 | #define OS_CPU_COUNT_METHODDEF \ |
| 15964 | {"cpu_count", (PyCFunction)os_cpu_count, METH_NOARGS, os_cpu_count__doc__}, |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 15965 | |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 15966 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 15967 | os_cpu_count_impl(PyModuleDef *module); |
| 15968 | |
| 15969 | static PyObject * |
| 15970 | os_cpu_count(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) |
| 15971 | { |
| 15972 | return os_cpu_count_impl(module); |
| 15973 | } |
| 15974 | |
| 15975 | static PyObject * |
| 15976 | os_cpu_count_impl(PyModuleDef *module) |
| 15977 | /*[clinic end generated code: output=92e2a4a729eb7740 input=d55e2f8f3823a628]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 15978 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 15979 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 15980 | #ifdef MS_WINDOWS |
| 15981 | SYSTEM_INFO sysinfo; |
| 15982 | GetSystemInfo(&sysinfo); |
| 15983 | ncpu = sysinfo.dwNumberOfProcessors; |
| 15984 | #elif defined(__hpux) |
| 15985 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); |
| 15986 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) |
| 15987 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 15988 | #elif defined(__DragonFly__) || \ |
| 15989 | defined(__OpenBSD__) || \ |
| 15990 | defined(__FreeBSD__) || \ |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 15991 | defined(__NetBSD__) || \ |
| 15992 | defined(__APPLE__) |
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 15993 | int mib[2]; |
| 15994 | size_t len = sizeof(ncpu); |
| 15995 | mib[0] = CTL_HW; |
| 15996 | mib[1] = HW_NCPU; |
| 15997 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) |
| 15998 | ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 15999 | #endif |
| 16000 | if (ncpu >= 1) |
| 16001 | return PyLong_FromLong(ncpu); |
| 16002 | else |
| 16003 | Py_RETURN_NONE; |
| 16004 | } |
| 16005 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16006 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16007 | /*[clinic input] |
| 16008 | os.get_inheritable -> bool |
| 16009 | |
| 16010 | fd: int |
| 16011 | / |
| 16012 | |
| 16013 | Get the close-on-exe flag of the specified file descriptor. |
| 16014 | [clinic start generated code]*/ |
| 16015 | |
| 16016 | PyDoc_STRVAR(os_get_inheritable__doc__, |
| 16017 | "get_inheritable($module, fd, /)\n" |
| 16018 | "--\n" |
| 16019 | "\n" |
| 16020 | "Get the close-on-exe flag of the specified file descriptor."); |
| 16021 | |
| 16022 | #define OS_GET_INHERITABLE_METHODDEF \ |
| 16023 | {"get_inheritable", (PyCFunction)os_get_inheritable, METH_VARARGS, os_get_inheritable__doc__}, |
| 16024 | |
| 16025 | static int |
| 16026 | os_get_inheritable_impl(PyModuleDef *module, int fd); |
| 16027 | |
| 16028 | static PyObject * |
| 16029 | os_get_inheritable(PyModuleDef *module, PyObject *args) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16030 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16031 | PyObject *return_value = NULL; |
| 16032 | int fd; |
| 16033 | int _return_value; |
| 16034 | |
| 16035 | if (!PyArg_ParseTuple(args, |
| 16036 | "i:get_inheritable", |
| 16037 | &fd)) |
| 16038 | goto exit; |
| 16039 | _return_value = os_get_inheritable_impl(module, fd); |
| 16040 | if ((_return_value == -1) && PyErr_Occurred()) |
| 16041 | goto exit; |
| 16042 | return_value = PyBool_FromLong((long)_return_value); |
| 16043 | |
| 16044 | exit: |
| 16045 | return return_value; |
| 16046 | } |
| 16047 | |
| 16048 | static int |
| 16049 | os_get_inheritable_impl(PyModuleDef *module, int fd) |
| 16050 | /*[clinic end generated code: output=261d1dd2b0dbdc35 input=89ac008dc9ab6b95]*/ |
| 16051 | { |
| 16052 | if (!_PyVerify_fd(fd)){ |
| 16053 | posix_error(); |
| 16054 | return -1; |
| 16055 | } |
| 16056 | |
| 16057 | return _Py_get_inheritable(fd); |
| 16058 | } |
| 16059 | |
| 16060 | |
| 16061 | /*[clinic input] |
| 16062 | os.set_inheritable |
| 16063 | fd: int |
| 16064 | inheritable: int |
| 16065 | / |
| 16066 | |
| 16067 | Set the inheritable flag of the specified file descriptor. |
| 16068 | [clinic start generated code]*/ |
| 16069 | |
| 16070 | PyDoc_STRVAR(os_set_inheritable__doc__, |
| 16071 | "set_inheritable($module, fd, inheritable, /)\n" |
| 16072 | "--\n" |
| 16073 | "\n" |
| 16074 | "Set the inheritable flag of the specified file descriptor."); |
| 16075 | |
| 16076 | #define OS_SET_INHERITABLE_METHODDEF \ |
| 16077 | {"set_inheritable", (PyCFunction)os_set_inheritable, METH_VARARGS, os_set_inheritable__doc__}, |
| 16078 | |
| 16079 | static PyObject * |
| 16080 | os_set_inheritable_impl(PyModuleDef *module, int fd, int inheritable); |
| 16081 | |
| 16082 | static PyObject * |
| 16083 | os_set_inheritable(PyModuleDef *module, PyObject *args) |
| 16084 | { |
| 16085 | PyObject *return_value = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16086 | int fd; |
| 16087 | int inheritable; |
| 16088 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16089 | if (!PyArg_ParseTuple(args, |
| 16090 | "ii:set_inheritable", |
| 16091 | &fd, &inheritable)) |
| 16092 | goto exit; |
| 16093 | return_value = os_set_inheritable_impl(module, fd, inheritable); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16094 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16095 | exit: |
| 16096 | return return_value; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16097 | } |
| 16098 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16099 | static PyObject * |
| 16100 | os_set_inheritable_impl(PyModuleDef *module, int fd, int inheritable) |
| 16101 | /*[clinic end generated code: output=64dfe5e15c906539 input=9ceaead87a1e2402]*/ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16102 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16103 | if (!_PyVerify_fd(fd)) |
| 16104 | return posix_error(); |
| 16105 | |
| 16106 | if (_Py_set_inheritable(fd, inheritable, NULL) < 0) |
| 16107 | return NULL; |
| 16108 | Py_RETURN_NONE; |
| 16109 | } |
| 16110 | |
| 16111 | |
| 16112 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16113 | /*[clinic input] |
| 16114 | os.get_handle_inheritable -> bool |
| 16115 | handle: Py_intptr_t |
| 16116 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16117 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16118 | Get the close-on-exe flag of the specified file descriptor. |
| 16119 | [clinic start generated code]*/ |
| 16120 | |
| 16121 | PyDoc_STRVAR(os_get_handle_inheritable__doc__, |
| 16122 | "get_handle_inheritable($module, handle, /)\n" |
| 16123 | "--\n" |
| 16124 | "\n" |
| 16125 | "Get the close-on-exe flag of the specified file descriptor."); |
| 16126 | |
| 16127 | #define OS_GET_HANDLE_INHERITABLE_METHODDEF \ |
| 16128 | {"get_handle_inheritable", (PyCFunction)os_get_handle_inheritable, METH_VARARGS, os_get_handle_inheritable__doc__}, |
| 16129 | |
| 16130 | static int |
| 16131 | os_get_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle); |
| 16132 | |
| 16133 | static PyObject * |
| 16134 | os_get_handle_inheritable(PyModuleDef *module, PyObject *args) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16135 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16136 | PyObject *return_value = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16137 | Py_intptr_t handle; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16138 | int _return_value; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16139 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16140 | if (!PyArg_ParseTuple(args, |
| 16141 | "" _Py_PARSE_INTPTR ":get_handle_inheritable", |
| 16142 | &handle)) |
| 16143 | goto exit; |
| 16144 | _return_value = os_get_handle_inheritable_impl(module, handle); |
| 16145 | if ((_return_value == -1) && PyErr_Occurred()) |
| 16146 | goto exit; |
| 16147 | return_value = PyBool_FromLong((long)_return_value); |
| 16148 | |
| 16149 | exit: |
| 16150 | return return_value; |
| 16151 | } |
| 16152 | |
| 16153 | static int |
| 16154 | os_get_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle) |
| 16155 | /*[clinic end generated code: output=d5bf9d86900bf457 input=5f7759443aae3dc5]*/ |
| 16156 | { |
| 16157 | DWORD flags; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16158 | |
| 16159 | if (!GetHandleInformation((HANDLE)handle, &flags)) { |
| 16160 | PyErr_SetFromWindowsErr(0); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16161 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16162 | } |
| 16163 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16164 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16165 | } |
| 16166 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16167 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16168 | /*[clinic input] |
| 16169 | os.set_handle_inheritable |
| 16170 | handle: Py_intptr_t |
| 16171 | inheritable: bool |
| 16172 | / |
| 16173 | |
| 16174 | Set the inheritable flag of the specified handle. |
| 16175 | [clinic start generated code]*/ |
| 16176 | |
| 16177 | PyDoc_STRVAR(os_set_handle_inheritable__doc__, |
| 16178 | "set_handle_inheritable($module, handle, inheritable, /)\n" |
| 16179 | "--\n" |
| 16180 | "\n" |
| 16181 | "Set the inheritable flag of the specified handle."); |
| 16182 | |
| 16183 | #define OS_SET_HANDLE_INHERITABLE_METHODDEF \ |
| 16184 | {"set_handle_inheritable", (PyCFunction)os_set_handle_inheritable, METH_VARARGS, os_set_handle_inheritable__doc__}, |
| 16185 | |
| 16186 | static PyObject * |
| 16187 | os_set_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle, int inheritable); |
| 16188 | |
| 16189 | static PyObject * |
| 16190 | os_set_handle_inheritable(PyModuleDef *module, PyObject *args) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16191 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16192 | PyObject *return_value = NULL; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16193 | Py_intptr_t handle; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16194 | int inheritable; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16196 | if (!PyArg_ParseTuple(args, |
| 16197 | "" _Py_PARSE_INTPTR "p:set_handle_inheritable", |
| 16198 | &handle, &inheritable)) |
| 16199 | goto exit; |
| 16200 | return_value = os_set_handle_inheritable_impl(module, handle, inheritable); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16201 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16202 | exit: |
| 16203 | return return_value; |
| 16204 | } |
| 16205 | |
| 16206 | static PyObject * |
| 16207 | os_set_handle_inheritable_impl(PyModuleDef *module, Py_intptr_t handle, int inheritable) |
| 16208 | /*[clinic end generated code: output=ee5fcc6d9f0d4f8b input=e64b2b2730469def]*/ |
| 16209 | { |
| 16210 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 16211 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
| 16212 | PyErr_SetFromWindowsErr(0); |
| 16213 | return NULL; |
| 16214 | } |
| 16215 | Py_RETURN_NONE; |
| 16216 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 16217 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 16218 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 16219 | #ifndef MS_WINDOWS |
| 16220 | PyDoc_STRVAR(get_blocking__doc__, |
| 16221 | "get_blocking(fd) -> bool\n" \ |
| 16222 | "\n" \ |
| 16223 | "Get the blocking mode of the file descriptor:\n" \ |
| 16224 | "False if the O_NONBLOCK flag is set, True if the flag is cleared."); |
| 16225 | |
| 16226 | static PyObject* |
| 16227 | posix_get_blocking(PyObject *self, PyObject *args) |
| 16228 | { |
| 16229 | int fd; |
| 16230 | int blocking; |
| 16231 | |
| 16232 | if (!PyArg_ParseTuple(args, "i:get_blocking", &fd)) |
| 16233 | return NULL; |
| 16234 | |
| 16235 | if (!_PyVerify_fd(fd)) |
| 16236 | return posix_error(); |
| 16237 | |
| 16238 | blocking = _Py_get_blocking(fd); |
| 16239 | if (blocking < 0) |
| 16240 | return NULL; |
| 16241 | return PyBool_FromLong(blocking); |
| 16242 | } |
| 16243 | |
| 16244 | PyDoc_STRVAR(set_blocking__doc__, |
| 16245 | "set_blocking(fd, blocking)\n" \ |
| 16246 | "\n" \ |
| 16247 | "Set the blocking mode of the specified file descriptor.\n" \ |
| 16248 | "Set the O_NONBLOCK flag if blocking is False,\n" \ |
| 16249 | "clear the O_NONBLOCK flag otherwise."); |
| 16250 | |
| 16251 | static PyObject* |
| 16252 | posix_set_blocking(PyObject *self, PyObject *args) |
| 16253 | { |
| 16254 | int fd, blocking; |
| 16255 | |
| 16256 | if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking)) |
| 16257 | return NULL; |
| 16258 | |
| 16259 | if (!_PyVerify_fd(fd)) |
| 16260 | return posix_error(); |
| 16261 | |
| 16262 | if (_Py_set_blocking(fd, blocking) < 0) |
| 16263 | return NULL; |
| 16264 | Py_RETURN_NONE; |
| 16265 | } |
| 16266 | #endif /* !MS_WINDOWS */ |
| 16267 | |
| 16268 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16269 | PyDoc_STRVAR(posix_scandir__doc__, |
| 16270 | "scandir(path='.') -> iterator of DirEntry objects for given path"); |
| 16271 | |
| 16272 | static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL}; |
| 16273 | |
| 16274 | typedef struct { |
| 16275 | PyObject_HEAD |
| 16276 | PyObject *name; |
| 16277 | PyObject *path; |
| 16278 | PyObject *stat; |
| 16279 | PyObject *lstat; |
| 16280 | #ifdef MS_WINDOWS |
| 16281 | struct _Py_stat_struct win32_lstat; |
| 16282 | __int64 win32_file_index; |
| 16283 | int got_file_index; |
| 16284 | #else /* POSIX */ |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16285 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16286 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16287 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16288 | ino_t d_ino; |
| 16289 | #endif |
| 16290 | } DirEntry; |
| 16291 | |
| 16292 | static void |
| 16293 | DirEntry_dealloc(DirEntry *entry) |
| 16294 | { |
| 16295 | Py_XDECREF(entry->name); |
| 16296 | Py_XDECREF(entry->path); |
| 16297 | Py_XDECREF(entry->stat); |
| 16298 | Py_XDECREF(entry->lstat); |
| 16299 | Py_TYPE(entry)->tp_free((PyObject *)entry); |
| 16300 | } |
| 16301 | |
| 16302 | /* Forward reference */ |
| 16303 | static int |
| 16304 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); |
| 16305 | |
| 16306 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 16307 | static int |
| 16308 | DirEntry_is_symlink(DirEntry *self) |
| 16309 | { |
| 16310 | #ifdef MS_WINDOWS |
| 16311 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16312 | #elif defined(HAVE_DIRENT_D_TYPE) |
| 16313 | /* POSIX */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16314 | if (self->d_type != DT_UNKNOWN) |
| 16315 | return self->d_type == DT_LNK; |
| 16316 | else |
| 16317 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16318 | #else |
| 16319 | /* POSIX without d_type */ |
| 16320 | return DirEntry_test_mode(self, 0, S_IFLNK); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16321 | #endif |
| 16322 | } |
| 16323 | |
| 16324 | static PyObject * |
| 16325 | DirEntry_py_is_symlink(DirEntry *self) |
| 16326 | { |
| 16327 | int result; |
| 16328 | |
| 16329 | result = DirEntry_is_symlink(self); |
| 16330 | if (result == -1) |
| 16331 | return NULL; |
| 16332 | return PyBool_FromLong(result); |
| 16333 | } |
| 16334 | |
| 16335 | static PyObject * |
| 16336 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
| 16337 | { |
| 16338 | int result; |
| 16339 | struct _Py_stat_struct st; |
| 16340 | |
| 16341 | #ifdef MS_WINDOWS |
| 16342 | wchar_t *path; |
| 16343 | |
| 16344 | path = PyUnicode_AsUnicode(self->path); |
| 16345 | if (!path) |
| 16346 | return NULL; |
| 16347 | |
| 16348 | if (follow_symlinks) |
| 16349 | result = win32_stat_w(path, &st); |
| 16350 | else |
| 16351 | result = win32_lstat_w(path, &st); |
| 16352 | |
| 16353 | if (result != 0) { |
| 16354 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 16355 | 0, self->path); |
| 16356 | } |
| 16357 | #else /* POSIX */ |
| 16358 | PyObject *bytes; |
| 16359 | char *path; |
| 16360 | |
| 16361 | if (!PyUnicode_FSConverter(self->path, &bytes)) |
| 16362 | return NULL; |
| 16363 | path = PyBytes_AS_STRING(bytes); |
| 16364 | |
| 16365 | if (follow_symlinks) |
| 16366 | result = STAT(path, &st); |
| 16367 | else |
| 16368 | result = LSTAT(path, &st); |
| 16369 | Py_DECREF(bytes); |
| 16370 | |
| 16371 | if (result != 0) |
| 16372 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path); |
| 16373 | #endif |
| 16374 | |
| 16375 | return _pystat_fromstructstat(&st); |
| 16376 | } |
| 16377 | |
| 16378 | static PyObject * |
| 16379 | DirEntry_get_lstat(DirEntry *self) |
| 16380 | { |
| 16381 | if (!self->lstat) { |
| 16382 | #ifdef MS_WINDOWS |
| 16383 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); |
| 16384 | #else /* POSIX */ |
| 16385 | self->lstat = DirEntry_fetch_stat(self, 0); |
| 16386 | #endif |
| 16387 | } |
| 16388 | Py_XINCREF(self->lstat); |
| 16389 | return self->lstat; |
| 16390 | } |
| 16391 | |
| 16392 | static PyObject * |
| 16393 | DirEntry_get_stat(DirEntry *self, int follow_symlinks) |
| 16394 | { |
| 16395 | if (!follow_symlinks) |
| 16396 | return DirEntry_get_lstat(self); |
| 16397 | |
| 16398 | if (!self->stat) { |
| 16399 | int result = DirEntry_is_symlink(self); |
| 16400 | if (result == -1) |
| 16401 | return NULL; |
| 16402 | else if (result) |
| 16403 | self->stat = DirEntry_fetch_stat(self, 1); |
| 16404 | else |
| 16405 | self->stat = DirEntry_get_lstat(self); |
| 16406 | } |
| 16407 | |
| 16408 | Py_XINCREF(self->stat); |
| 16409 | return self->stat; |
| 16410 | } |
| 16411 | |
| 16412 | static PyObject * |
| 16413 | DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 16414 | { |
| 16415 | int follow_symlinks = 1; |
| 16416 | |
| 16417 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat", |
| 16418 | follow_symlinks_keywords, &follow_symlinks)) |
| 16419 | return NULL; |
| 16420 | |
| 16421 | return DirEntry_get_stat(self, follow_symlinks); |
| 16422 | } |
| 16423 | |
| 16424 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
| 16425 | static int |
| 16426 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 16427 | { |
| 16428 | PyObject *stat = NULL; |
| 16429 | PyObject *st_mode = NULL; |
| 16430 | long mode; |
| 16431 | int result; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16432 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16433 | int is_symlink; |
| 16434 | int need_stat; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16435 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16436 | #ifdef MS_WINDOWS |
| 16437 | unsigned long dir_bits; |
| 16438 | #endif |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16439 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16440 | |
| 16441 | #ifdef MS_WINDOWS |
| 16442 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; |
| 16443 | need_stat = follow_symlinks && is_symlink; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16444 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16445 | is_symlink = self->d_type == DT_LNK; |
| 16446 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); |
| 16447 | #endif |
| 16448 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16449 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16450 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16451 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16452 | stat = DirEntry_get_stat(self, follow_symlinks); |
| 16453 | if (!stat) { |
| 16454 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { |
| 16455 | /* If file doesn't exist (anymore), then return False |
| 16456 | (i.e., say it's not a file/directory) */ |
| 16457 | PyErr_Clear(); |
| 16458 | return 0; |
| 16459 | } |
| 16460 | goto error; |
| 16461 | } |
| 16462 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); |
| 16463 | if (!st_mode) |
| 16464 | goto error; |
| 16465 | |
| 16466 | mode = PyLong_AsLong(st_mode); |
| 16467 | if (mode == -1 && PyErr_Occurred()) |
| 16468 | goto error; |
| 16469 | Py_CLEAR(st_mode); |
| 16470 | Py_CLEAR(stat); |
| 16471 | result = (mode & S_IFMT) == mode_bits; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16472 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16473 | } |
| 16474 | else if (is_symlink) { |
| 16475 | assert(mode_bits != S_IFLNK); |
| 16476 | result = 0; |
| 16477 | } |
| 16478 | else { |
| 16479 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); |
| 16480 | #ifdef MS_WINDOWS |
| 16481 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; |
| 16482 | if (mode_bits == S_IFDIR) |
| 16483 | result = dir_bits != 0; |
| 16484 | else |
| 16485 | result = dir_bits == 0; |
| 16486 | #else /* POSIX */ |
| 16487 | if (mode_bits == S_IFDIR) |
| 16488 | result = self->d_type == DT_DIR; |
| 16489 | else |
| 16490 | result = self->d_type == DT_REG; |
| 16491 | #endif |
| 16492 | } |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16493 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16494 | |
| 16495 | return result; |
| 16496 | |
| 16497 | error: |
| 16498 | Py_XDECREF(st_mode); |
| 16499 | Py_XDECREF(stat); |
| 16500 | return -1; |
| 16501 | } |
| 16502 | |
| 16503 | static PyObject * |
| 16504 | DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) |
| 16505 | { |
| 16506 | int result; |
| 16507 | |
| 16508 | result = DirEntry_test_mode(self, follow_symlinks, mode_bits); |
| 16509 | if (result == -1) |
| 16510 | return NULL; |
| 16511 | return PyBool_FromLong(result); |
| 16512 | } |
| 16513 | |
| 16514 | static PyObject * |
| 16515 | DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 16516 | { |
| 16517 | int follow_symlinks = 1; |
| 16518 | |
| 16519 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir", |
| 16520 | follow_symlinks_keywords, &follow_symlinks)) |
| 16521 | return NULL; |
| 16522 | |
| 16523 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR); |
| 16524 | } |
| 16525 | |
| 16526 | static PyObject * |
| 16527 | DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs) |
| 16528 | { |
| 16529 | int follow_symlinks = 1; |
| 16530 | |
| 16531 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file", |
| 16532 | follow_symlinks_keywords, &follow_symlinks)) |
| 16533 | return NULL; |
| 16534 | |
| 16535 | return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG); |
| 16536 | } |
| 16537 | |
| 16538 | static PyObject * |
| 16539 | DirEntry_inode(DirEntry *self) |
| 16540 | { |
| 16541 | #ifdef MS_WINDOWS |
| 16542 | if (!self->got_file_index) { |
| 16543 | wchar_t *path; |
| 16544 | struct _Py_stat_struct stat; |
| 16545 | |
| 16546 | path = PyUnicode_AsUnicode(self->path); |
| 16547 | if (!path) |
| 16548 | return NULL; |
| 16549 | |
| 16550 | if (win32_lstat_w(path, &stat) != 0) { |
| 16551 | return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError, |
| 16552 | 0, self->path); |
| 16553 | } |
| 16554 | |
| 16555 | self->win32_file_index = stat.st_ino; |
| 16556 | self->got_file_index = 1; |
| 16557 | } |
| 16558 | return PyLong_FromLongLong((PY_LONG_LONG)self->win32_file_index); |
| 16559 | #else /* POSIX */ |
| 16560 | #ifdef HAVE_LARGEFILE_SUPPORT |
| 16561 | return PyLong_FromLongLong((PY_LONG_LONG)self->d_ino); |
| 16562 | #else |
| 16563 | return PyLong_FromLong((long)self->d_ino); |
| 16564 | #endif |
| 16565 | #endif |
| 16566 | } |
| 16567 | |
| 16568 | static PyObject * |
| 16569 | DirEntry_repr(DirEntry *self) |
| 16570 | { |
| 16571 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); |
| 16572 | } |
| 16573 | |
| 16574 | static PyMemberDef DirEntry_members[] = { |
| 16575 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, |
| 16576 | "the entry's base filename, relative to scandir() \"path\" argument"}, |
| 16577 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, |
| 16578 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, |
| 16579 | {NULL} |
| 16580 | }; |
| 16581 | |
| 16582 | static PyMethodDef DirEntry_methods[] = { |
| 16583 | {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS, |
| 16584 | "return True if the entry is a directory; cached per entry" |
| 16585 | }, |
| 16586 | {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS, |
| 16587 | "return True if the entry is a file; cached per entry" |
| 16588 | }, |
| 16589 | {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS, |
| 16590 | "return True if the entry is a symbolic link; cached per entry" |
| 16591 | }, |
| 16592 | {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS, |
| 16593 | "return stat_result object for the entry; cached per entry" |
| 16594 | }, |
| 16595 | {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS, |
| 16596 | "return inode of the entry; cached per entry", |
| 16597 | }, |
| 16598 | {NULL} |
| 16599 | }; |
| 16600 | |
| 16601 | PyTypeObject DirEntryType = { |
| 16602 | PyVarObject_HEAD_INIT(NULL, 0) |
| 16603 | MODNAME ".DirEntry", /* tp_name */ |
| 16604 | sizeof(DirEntry), /* tp_basicsize */ |
| 16605 | 0, /* tp_itemsize */ |
| 16606 | /* methods */ |
| 16607 | (destructor)DirEntry_dealloc, /* tp_dealloc */ |
| 16608 | 0, /* tp_print */ |
| 16609 | 0, /* tp_getattr */ |
| 16610 | 0, /* tp_setattr */ |
| 16611 | 0, /* tp_compare */ |
| 16612 | (reprfunc)DirEntry_repr, /* tp_repr */ |
| 16613 | 0, /* tp_as_number */ |
| 16614 | 0, /* tp_as_sequence */ |
| 16615 | 0, /* tp_as_mapping */ |
| 16616 | 0, /* tp_hash */ |
| 16617 | 0, /* tp_call */ |
| 16618 | 0, /* tp_str */ |
| 16619 | 0, /* tp_getattro */ |
| 16620 | 0, /* tp_setattro */ |
| 16621 | 0, /* tp_as_buffer */ |
| 16622 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 16623 | 0, /* tp_doc */ |
| 16624 | 0, /* tp_traverse */ |
| 16625 | 0, /* tp_clear */ |
| 16626 | 0, /* tp_richcompare */ |
| 16627 | 0, /* tp_weaklistoffset */ |
| 16628 | 0, /* tp_iter */ |
| 16629 | 0, /* tp_iternext */ |
| 16630 | DirEntry_methods, /* tp_methods */ |
| 16631 | DirEntry_members, /* tp_members */ |
| 16632 | }; |
| 16633 | |
| 16634 | #ifdef MS_WINDOWS |
| 16635 | |
| 16636 | static wchar_t * |
| 16637 | join_path_filenameW(wchar_t *path_wide, wchar_t* filename) |
| 16638 | { |
| 16639 | Py_ssize_t path_len; |
| 16640 | Py_ssize_t size; |
| 16641 | wchar_t *result; |
| 16642 | wchar_t ch; |
| 16643 | |
| 16644 | if (!path_wide) { /* Default arg: "." */ |
| 16645 | path_wide = L"."; |
| 16646 | path_len = 1; |
| 16647 | } |
| 16648 | else { |
| 16649 | path_len = wcslen(path_wide); |
| 16650 | } |
| 16651 | |
| 16652 | /* The +1's are for the path separator and the NUL */ |
| 16653 | size = path_len + 1 + wcslen(filename) + 1; |
| 16654 | result = PyMem_New(wchar_t, size); |
| 16655 | if (!result) { |
| 16656 | PyErr_NoMemory(); |
| 16657 | return NULL; |
| 16658 | } |
| 16659 | wcscpy(result, path_wide); |
| 16660 | if (path_len > 0) { |
| 16661 | ch = result[path_len - 1]; |
| 16662 | if (ch != SEP && ch != ALTSEP && ch != L':') |
| 16663 | result[path_len++] = SEP; |
| 16664 | wcscpy(result + path_len, filename); |
| 16665 | } |
| 16666 | return result; |
| 16667 | } |
| 16668 | |
| 16669 | static PyObject * |
| 16670 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) |
| 16671 | { |
| 16672 | DirEntry *entry; |
| 16673 | BY_HANDLE_FILE_INFORMATION file_info; |
| 16674 | ULONG reparse_tag; |
| 16675 | wchar_t *joined_path; |
| 16676 | |
| 16677 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 16678 | if (!entry) |
| 16679 | return NULL; |
| 16680 | entry->name = NULL; |
| 16681 | entry->path = NULL; |
| 16682 | entry->stat = NULL; |
| 16683 | entry->lstat = NULL; |
| 16684 | entry->got_file_index = 0; |
| 16685 | |
| 16686 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); |
| 16687 | if (!entry->name) |
| 16688 | goto error; |
| 16689 | |
| 16690 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); |
| 16691 | if (!joined_path) |
| 16692 | goto error; |
| 16693 | |
| 16694 | entry->path = PyUnicode_FromWideChar(joined_path, -1); |
| 16695 | PyMem_Free(joined_path); |
| 16696 | if (!entry->path) |
| 16697 | goto error; |
| 16698 | |
| 16699 | find_data_to_file_info_w(dataW, &file_info, &reparse_tag); |
| 16700 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
| 16701 | |
| 16702 | return (PyObject *)entry; |
| 16703 | |
| 16704 | error: |
| 16705 | Py_DECREF(entry); |
| 16706 | return NULL; |
| 16707 | } |
| 16708 | |
| 16709 | #else /* POSIX */ |
| 16710 | |
| 16711 | static char * |
| 16712 | join_path_filename(char *path_narrow, char* filename, Py_ssize_t filename_len) |
| 16713 | { |
| 16714 | Py_ssize_t path_len; |
| 16715 | Py_ssize_t size; |
| 16716 | char *result; |
| 16717 | |
| 16718 | if (!path_narrow) { /* Default arg: "." */ |
| 16719 | path_narrow = "."; |
| 16720 | path_len = 1; |
| 16721 | } |
| 16722 | else { |
| 16723 | path_len = strlen(path_narrow); |
| 16724 | } |
| 16725 | |
| 16726 | if (filename_len == -1) |
| 16727 | filename_len = strlen(filename); |
| 16728 | |
| 16729 | /* The +1's are for the path separator and the NUL */ |
| 16730 | size = path_len + 1 + filename_len + 1; |
| 16731 | result = PyMem_New(char, size); |
| 16732 | if (!result) { |
| 16733 | PyErr_NoMemory(); |
| 16734 | return NULL; |
| 16735 | } |
| 16736 | strcpy(result, path_narrow); |
| 16737 | if (path_len > 0 && result[path_len - 1] != '/') |
| 16738 | result[path_len++] = '/'; |
| 16739 | strcpy(result + path_len, filename); |
| 16740 | return result; |
| 16741 | } |
| 16742 | |
| 16743 | static PyObject * |
| 16744 | 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] | 16745 | ino_t d_ino |
| 16746 | #ifdef HAVE_DIRENT_D_TYPE |
| 16747 | , unsigned char d_type |
| 16748 | #endif |
| 16749 | ) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16750 | { |
| 16751 | DirEntry *entry; |
| 16752 | char *joined_path; |
| 16753 | |
| 16754 | entry = PyObject_New(DirEntry, &DirEntryType); |
| 16755 | if (!entry) |
| 16756 | return NULL; |
| 16757 | entry->name = NULL; |
| 16758 | entry->path = NULL; |
| 16759 | entry->stat = NULL; |
| 16760 | entry->lstat = NULL; |
| 16761 | |
| 16762 | joined_path = join_path_filename(path->narrow, name, name_len); |
| 16763 | if (!joined_path) |
| 16764 | goto error; |
| 16765 | |
| 16766 | if (!path->narrow || !PyBytes_Check(path->object)) { |
| 16767 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
| 16768 | entry->path = PyUnicode_DecodeFSDefault(joined_path); |
| 16769 | } |
| 16770 | else { |
| 16771 | entry->name = PyBytes_FromStringAndSize(name, name_len); |
| 16772 | entry->path = PyBytes_FromString(joined_path); |
| 16773 | } |
| 16774 | PyMem_Free(joined_path); |
| 16775 | if (!entry->name || !entry->path) |
| 16776 | goto error; |
| 16777 | |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16778 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16779 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16780 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16781 | entry->d_ino = d_ino; |
| 16782 | |
| 16783 | return (PyObject *)entry; |
| 16784 | |
| 16785 | error: |
| 16786 | Py_XDECREF(entry); |
| 16787 | return NULL; |
| 16788 | } |
| 16789 | |
| 16790 | #endif |
| 16791 | |
| 16792 | |
| 16793 | typedef struct { |
| 16794 | PyObject_HEAD |
| 16795 | path_t path; |
| 16796 | #ifdef MS_WINDOWS |
| 16797 | HANDLE handle; |
| 16798 | WIN32_FIND_DATAW file_data; |
| 16799 | int first_time; |
| 16800 | #else /* POSIX */ |
| 16801 | DIR *dirp; |
| 16802 | #endif |
| 16803 | } ScandirIterator; |
| 16804 | |
| 16805 | #ifdef MS_WINDOWS |
| 16806 | |
| 16807 | static void |
| 16808 | ScandirIterator_close(ScandirIterator *iterator) |
| 16809 | { |
| 16810 | if (iterator->handle == INVALID_HANDLE_VALUE) |
| 16811 | return; |
| 16812 | |
| 16813 | Py_BEGIN_ALLOW_THREADS |
| 16814 | FindClose(iterator->handle); |
| 16815 | Py_END_ALLOW_THREADS |
| 16816 | iterator->handle = INVALID_HANDLE_VALUE; |
| 16817 | } |
| 16818 | |
| 16819 | static PyObject * |
| 16820 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 16821 | { |
| 16822 | WIN32_FIND_DATAW *file_data = &iterator->file_data; |
| 16823 | BOOL success; |
| 16824 | |
| 16825 | /* Happens if the iterator is iterated twice */ |
| 16826 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 16827 | PyErr_SetNone(PyExc_StopIteration); |
| 16828 | return NULL; |
| 16829 | } |
| 16830 | |
| 16831 | while (1) { |
| 16832 | if (!iterator->first_time) { |
| 16833 | Py_BEGIN_ALLOW_THREADS |
| 16834 | success = FindNextFileW(iterator->handle, file_data); |
| 16835 | Py_END_ALLOW_THREADS |
| 16836 | if (!success) { |
| 16837 | if (GetLastError() != ERROR_NO_MORE_FILES) |
| 16838 | return path_error(&iterator->path); |
| 16839 | /* No more files found in directory, stop iterating */ |
| 16840 | break; |
| 16841 | } |
| 16842 | } |
| 16843 | iterator->first_time = 0; |
| 16844 | |
| 16845 | /* Skip over . and .. */ |
| 16846 | if (wcscmp(file_data->cFileName, L".") != 0 && |
| 16847 | wcscmp(file_data->cFileName, L"..") != 0) |
| 16848 | return DirEntry_from_find_data(&iterator->path, file_data); |
| 16849 | |
| 16850 | /* Loop till we get a non-dot directory or finish iterating */ |
| 16851 | } |
| 16852 | |
| 16853 | ScandirIterator_close(iterator); |
| 16854 | |
| 16855 | PyErr_SetNone(PyExc_StopIteration); |
| 16856 | return NULL; |
| 16857 | } |
| 16858 | |
| 16859 | #else /* POSIX */ |
| 16860 | |
| 16861 | static void |
| 16862 | ScandirIterator_close(ScandirIterator *iterator) |
| 16863 | { |
| 16864 | if (!iterator->dirp) |
| 16865 | return; |
| 16866 | |
| 16867 | Py_BEGIN_ALLOW_THREADS |
| 16868 | closedir(iterator->dirp); |
| 16869 | Py_END_ALLOW_THREADS |
| 16870 | iterator->dirp = NULL; |
| 16871 | return; |
| 16872 | } |
| 16873 | |
| 16874 | static PyObject * |
| 16875 | ScandirIterator_iternext(ScandirIterator *iterator) |
| 16876 | { |
| 16877 | struct dirent *direntp; |
| 16878 | Py_ssize_t name_len; |
| 16879 | int is_dot; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16880 | |
| 16881 | /* Happens if the iterator is iterated twice */ |
| 16882 | if (!iterator->dirp) { |
| 16883 | PyErr_SetNone(PyExc_StopIteration); |
| 16884 | return NULL; |
| 16885 | } |
| 16886 | |
| 16887 | while (1) { |
| 16888 | errno = 0; |
| 16889 | Py_BEGIN_ALLOW_THREADS |
| 16890 | direntp = readdir(iterator->dirp); |
| 16891 | Py_END_ALLOW_THREADS |
| 16892 | |
| 16893 | if (!direntp) { |
| 16894 | if (errno != 0) |
| 16895 | return path_error(&iterator->path); |
| 16896 | /* No more files found in directory, stop iterating */ |
| 16897 | break; |
| 16898 | } |
| 16899 | |
| 16900 | /* Skip over . and .. */ |
| 16901 | name_len = NAMLEN(direntp); |
| 16902 | is_dot = direntp->d_name[0] == '.' && |
| 16903 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); |
| 16904 | if (!is_dot) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16905 | return DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 16906 | name_len, direntp->d_ino |
| 16907 | #ifdef HAVE_DIRENT_D_TYPE |
| 16908 | , direntp->d_type |
| 16909 | #endif |
| 16910 | ); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 16911 | } |
| 16912 | |
| 16913 | /* Loop till we get a non-dot directory or finish iterating */ |
| 16914 | } |
| 16915 | |
| 16916 | ScandirIterator_close(iterator); |
| 16917 | |
| 16918 | PyErr_SetNone(PyExc_StopIteration); |
| 16919 | return NULL; |
| 16920 | } |
| 16921 | |
| 16922 | #endif |
| 16923 | |
| 16924 | static void |
| 16925 | ScandirIterator_dealloc(ScandirIterator *iterator) |
| 16926 | { |
| 16927 | ScandirIterator_close(iterator); |
| 16928 | Py_XDECREF(iterator->path.object); |
| 16929 | path_cleanup(&iterator->path); |
| 16930 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
| 16931 | } |
| 16932 | |
| 16933 | PyTypeObject ScandirIteratorType = { |
| 16934 | PyVarObject_HEAD_INIT(NULL, 0) |
| 16935 | MODNAME ".ScandirIterator", /* tp_name */ |
| 16936 | sizeof(ScandirIterator), /* tp_basicsize */ |
| 16937 | 0, /* tp_itemsize */ |
| 16938 | /* methods */ |
| 16939 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ |
| 16940 | 0, /* tp_print */ |
| 16941 | 0, /* tp_getattr */ |
| 16942 | 0, /* tp_setattr */ |
| 16943 | 0, /* tp_compare */ |
| 16944 | 0, /* tp_repr */ |
| 16945 | 0, /* tp_as_number */ |
| 16946 | 0, /* tp_as_sequence */ |
| 16947 | 0, /* tp_as_mapping */ |
| 16948 | 0, /* tp_hash */ |
| 16949 | 0, /* tp_call */ |
| 16950 | 0, /* tp_str */ |
| 16951 | 0, /* tp_getattro */ |
| 16952 | 0, /* tp_setattro */ |
| 16953 | 0, /* tp_as_buffer */ |
| 16954 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 16955 | 0, /* tp_doc */ |
| 16956 | 0, /* tp_traverse */ |
| 16957 | 0, /* tp_clear */ |
| 16958 | 0, /* tp_richcompare */ |
| 16959 | 0, /* tp_weaklistoffset */ |
| 16960 | PyObject_SelfIter, /* tp_iter */ |
| 16961 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ |
| 16962 | }; |
| 16963 | |
| 16964 | static PyObject * |
| 16965 | posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs) |
| 16966 | { |
| 16967 | ScandirIterator *iterator; |
| 16968 | static char *keywords[] = {"path", NULL}; |
| 16969 | #ifdef MS_WINDOWS |
| 16970 | wchar_t *path_strW; |
| 16971 | #else |
| 16972 | char *path; |
| 16973 | #endif |
| 16974 | |
| 16975 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
| 16976 | if (!iterator) |
| 16977 | return NULL; |
| 16978 | memset(&iterator->path, 0, sizeof(path_t)); |
| 16979 | iterator->path.function_name = "scandir"; |
| 16980 | iterator->path.nullable = 1; |
| 16981 | |
| 16982 | #ifdef MS_WINDOWS |
| 16983 | iterator->handle = INVALID_HANDLE_VALUE; |
| 16984 | #else |
| 16985 | iterator->dirp = NULL; |
| 16986 | #endif |
| 16987 | |
| 16988 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords, |
| 16989 | path_converter, &iterator->path)) |
| 16990 | goto error; |
| 16991 | |
| 16992 | /* path_converter doesn't keep path.object around, so do it |
| 16993 | manually for the lifetime of the iterator here (the refcount |
| 16994 | is decremented in ScandirIterator_dealloc) |
| 16995 | */ |
| 16996 | Py_XINCREF(iterator->path.object); |
| 16997 | |
| 16998 | #ifdef MS_WINDOWS |
| 16999 | if (iterator->path.narrow) { |
| 17000 | PyErr_SetString(PyExc_TypeError, |
| 17001 | "os.scandir() doesn't support bytes path on Windows, use Unicode instead"); |
| 17002 | goto error; |
| 17003 | } |
| 17004 | iterator->first_time = 1; |
| 17005 | |
| 17006 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); |
| 17007 | if (!path_strW) |
| 17008 | goto error; |
| 17009 | |
| 17010 | Py_BEGIN_ALLOW_THREADS |
| 17011 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); |
| 17012 | Py_END_ALLOW_THREADS |
| 17013 | |
| 17014 | PyMem_Free(path_strW); |
| 17015 | |
| 17016 | if (iterator->handle == INVALID_HANDLE_VALUE) { |
| 17017 | path_error(&iterator->path); |
| 17018 | goto error; |
| 17019 | } |
| 17020 | #else /* POSIX */ |
| 17021 | if (iterator->path.narrow) |
| 17022 | path = iterator->path.narrow; |
| 17023 | else |
| 17024 | path = "."; |
| 17025 | |
| 17026 | errno = 0; |
| 17027 | Py_BEGIN_ALLOW_THREADS |
| 17028 | iterator->dirp = opendir(path); |
| 17029 | Py_END_ALLOW_THREADS |
| 17030 | |
| 17031 | if (!iterator->dirp) { |
| 17032 | path_error(&iterator->path); |
| 17033 | goto error; |
| 17034 | } |
| 17035 | #endif |
| 17036 | |
| 17037 | return (PyObject *)iterator; |
| 17038 | |
| 17039 | error: |
| 17040 | Py_DECREF(iterator); |
| 17041 | return NULL; |
| 17042 | } |
| 17043 | |
| 17044 | |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 17045 | /*[clinic input] |
| 17046 | dump buffer |
| 17047 | [clinic start generated code]*/ |
| 17048 | |
| 17049 | #ifndef OS_TTYNAME_METHODDEF |
| 17050 | #define OS_TTYNAME_METHODDEF |
| 17051 | #endif /* !defined(OS_TTYNAME_METHODDEF) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17052 | |
| 17053 | #ifndef OS_CTERMID_METHODDEF |
| 17054 | #define OS_CTERMID_METHODDEF |
| 17055 | #endif /* !defined(OS_CTERMID_METHODDEF) */ |
| 17056 | |
| 17057 | #ifndef OS_FCHDIR_METHODDEF |
| 17058 | #define OS_FCHDIR_METHODDEF |
| 17059 | #endif /* !defined(OS_FCHDIR_METHODDEF) */ |
| 17060 | |
| 17061 | #ifndef OS_FCHMOD_METHODDEF |
| 17062 | #define OS_FCHMOD_METHODDEF |
| 17063 | #endif /* !defined(OS_FCHMOD_METHODDEF) */ |
| 17064 | |
| 17065 | #ifndef OS_LCHMOD_METHODDEF |
| 17066 | #define OS_LCHMOD_METHODDEF |
| 17067 | #endif /* !defined(OS_LCHMOD_METHODDEF) */ |
| 17068 | |
| 17069 | #ifndef OS_CHFLAGS_METHODDEF |
| 17070 | #define OS_CHFLAGS_METHODDEF |
| 17071 | #endif /* !defined(OS_CHFLAGS_METHODDEF) */ |
| 17072 | |
| 17073 | #ifndef OS_LCHFLAGS_METHODDEF |
| 17074 | #define OS_LCHFLAGS_METHODDEF |
| 17075 | #endif /* !defined(OS_LCHFLAGS_METHODDEF) */ |
| 17076 | |
| 17077 | #ifndef OS_CHROOT_METHODDEF |
| 17078 | #define OS_CHROOT_METHODDEF |
| 17079 | #endif /* !defined(OS_CHROOT_METHODDEF) */ |
| 17080 | |
| 17081 | #ifndef OS_FSYNC_METHODDEF |
| 17082 | #define OS_FSYNC_METHODDEF |
| 17083 | #endif /* !defined(OS_FSYNC_METHODDEF) */ |
| 17084 | |
| 17085 | #ifndef OS_SYNC_METHODDEF |
| 17086 | #define OS_SYNC_METHODDEF |
| 17087 | #endif /* !defined(OS_SYNC_METHODDEF) */ |
| 17088 | |
| 17089 | #ifndef OS_FDATASYNC_METHODDEF |
| 17090 | #define OS_FDATASYNC_METHODDEF |
| 17091 | #endif /* !defined(OS_FDATASYNC_METHODDEF) */ |
| 17092 | |
| 17093 | #ifndef OS_CHOWN_METHODDEF |
| 17094 | #define OS_CHOWN_METHODDEF |
| 17095 | #endif /* !defined(OS_CHOWN_METHODDEF) */ |
| 17096 | |
| 17097 | #ifndef OS_FCHOWN_METHODDEF |
| 17098 | #define OS_FCHOWN_METHODDEF |
| 17099 | #endif /* !defined(OS_FCHOWN_METHODDEF) */ |
| 17100 | |
| 17101 | #ifndef OS_LCHOWN_METHODDEF |
| 17102 | #define OS_LCHOWN_METHODDEF |
| 17103 | #endif /* !defined(OS_LCHOWN_METHODDEF) */ |
| 17104 | |
| 17105 | #ifndef OS_LINK_METHODDEF |
| 17106 | #define OS_LINK_METHODDEF |
| 17107 | #endif /* !defined(OS_LINK_METHODDEF) */ |
| 17108 | |
| 17109 | #ifndef OS__GETFINALPATHNAME_METHODDEF |
| 17110 | #define OS__GETFINALPATHNAME_METHODDEF |
| 17111 | #endif /* !defined(OS__GETFINALPATHNAME_METHODDEF) */ |
| 17112 | |
| 17113 | #ifndef OS__GETVOLUMEPATHNAME_METHODDEF |
| 17114 | #define OS__GETVOLUMEPATHNAME_METHODDEF |
| 17115 | #endif /* !defined(OS__GETVOLUMEPATHNAME_METHODDEF) */ |
| 17116 | |
| 17117 | #ifndef OS_NICE_METHODDEF |
| 17118 | #define OS_NICE_METHODDEF |
| 17119 | #endif /* !defined(OS_NICE_METHODDEF) */ |
| 17120 | |
| 17121 | #ifndef OS_GETPRIORITY_METHODDEF |
| 17122 | #define OS_GETPRIORITY_METHODDEF |
| 17123 | #endif /* !defined(OS_GETPRIORITY_METHODDEF) */ |
| 17124 | |
| 17125 | #ifndef OS_SETPRIORITY_METHODDEF |
| 17126 | #define OS_SETPRIORITY_METHODDEF |
| 17127 | #endif /* !defined(OS_SETPRIORITY_METHODDEF) */ |
| 17128 | |
| 17129 | #ifndef OS_SYSTEM_METHODDEF |
| 17130 | #define OS_SYSTEM_METHODDEF |
| 17131 | #endif /* !defined(OS_SYSTEM_METHODDEF) */ |
| 17132 | |
| 17133 | #ifndef OS_SYSTEM_METHODDEF |
| 17134 | #define OS_SYSTEM_METHODDEF |
| 17135 | #endif /* !defined(OS_SYSTEM_METHODDEF) */ |
| 17136 | |
| 17137 | #ifndef OS_UNAME_METHODDEF |
| 17138 | #define OS_UNAME_METHODDEF |
| 17139 | #endif /* !defined(OS_UNAME_METHODDEF) */ |
| 17140 | |
| 17141 | #ifndef OS_EXECV_METHODDEF |
| 17142 | #define OS_EXECV_METHODDEF |
| 17143 | #endif /* !defined(OS_EXECV_METHODDEF) */ |
| 17144 | |
| 17145 | #ifndef OS_EXECVE_METHODDEF |
| 17146 | #define OS_EXECVE_METHODDEF |
| 17147 | #endif /* !defined(OS_EXECVE_METHODDEF) */ |
| 17148 | |
| 17149 | #ifndef OS_SPAWNV_METHODDEF |
| 17150 | #define OS_SPAWNV_METHODDEF |
| 17151 | #endif /* !defined(OS_SPAWNV_METHODDEF) */ |
| 17152 | |
| 17153 | #ifndef OS_SPAWNVE_METHODDEF |
| 17154 | #define OS_SPAWNVE_METHODDEF |
| 17155 | #endif /* !defined(OS_SPAWNVE_METHODDEF) */ |
| 17156 | |
| 17157 | #ifndef OS_FORK1_METHODDEF |
| 17158 | #define OS_FORK1_METHODDEF |
| 17159 | #endif /* !defined(OS_FORK1_METHODDEF) */ |
| 17160 | |
| 17161 | #ifndef OS_FORK_METHODDEF |
| 17162 | #define OS_FORK_METHODDEF |
| 17163 | #endif /* !defined(OS_FORK_METHODDEF) */ |
| 17164 | |
| 17165 | #ifndef OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 17166 | #define OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 17167 | #endif /* !defined(OS_SCHED_GET_PRIORITY_MAX_METHODDEF) */ |
| 17168 | |
| 17169 | #ifndef OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 17170 | #define OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 17171 | #endif /* !defined(OS_SCHED_GET_PRIORITY_MIN_METHODDEF) */ |
| 17172 | |
| 17173 | #ifndef OS_SCHED_GETSCHEDULER_METHODDEF |
| 17174 | #define OS_SCHED_GETSCHEDULER_METHODDEF |
| 17175 | #endif /* !defined(OS_SCHED_GETSCHEDULER_METHODDEF) */ |
| 17176 | |
| 17177 | #ifndef OS_SCHED_SETSCHEDULER_METHODDEF |
| 17178 | #define OS_SCHED_SETSCHEDULER_METHODDEF |
| 17179 | #endif /* !defined(OS_SCHED_SETSCHEDULER_METHODDEF) */ |
| 17180 | |
| 17181 | #ifndef OS_SCHED_GETPARAM_METHODDEF |
| 17182 | #define OS_SCHED_GETPARAM_METHODDEF |
| 17183 | #endif /* !defined(OS_SCHED_GETPARAM_METHODDEF) */ |
| 17184 | |
| 17185 | #ifndef OS_SCHED_SETPARAM_METHODDEF |
| 17186 | #define OS_SCHED_SETPARAM_METHODDEF |
| 17187 | #endif /* !defined(OS_SCHED_SETPARAM_METHODDEF) */ |
| 17188 | |
| 17189 | #ifndef OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 17190 | #define OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 17191 | #endif /* !defined(OS_SCHED_RR_GET_INTERVAL_METHODDEF) */ |
| 17192 | |
| 17193 | #ifndef OS_SCHED_YIELD_METHODDEF |
| 17194 | #define OS_SCHED_YIELD_METHODDEF |
| 17195 | #endif /* !defined(OS_SCHED_YIELD_METHODDEF) */ |
| 17196 | |
| 17197 | #ifndef OS_SCHED_SETAFFINITY_METHODDEF |
| 17198 | #define OS_SCHED_SETAFFINITY_METHODDEF |
| 17199 | #endif /* !defined(OS_SCHED_SETAFFINITY_METHODDEF) */ |
| 17200 | |
| 17201 | #ifndef OS_SCHED_GETAFFINITY_METHODDEF |
| 17202 | #define OS_SCHED_GETAFFINITY_METHODDEF |
| 17203 | #endif /* !defined(OS_SCHED_GETAFFINITY_METHODDEF) */ |
| 17204 | |
| 17205 | #ifndef OS_OPENPTY_METHODDEF |
| 17206 | #define OS_OPENPTY_METHODDEF |
| 17207 | #endif /* !defined(OS_OPENPTY_METHODDEF) */ |
| 17208 | |
| 17209 | #ifndef OS_FORKPTY_METHODDEF |
| 17210 | #define OS_FORKPTY_METHODDEF |
| 17211 | #endif /* !defined(OS_FORKPTY_METHODDEF) */ |
| 17212 | |
| 17213 | #ifndef OS_GETEGID_METHODDEF |
| 17214 | #define OS_GETEGID_METHODDEF |
| 17215 | #endif /* !defined(OS_GETEGID_METHODDEF) */ |
| 17216 | |
| 17217 | #ifndef OS_GETEUID_METHODDEF |
| 17218 | #define OS_GETEUID_METHODDEF |
| 17219 | #endif /* !defined(OS_GETEUID_METHODDEF) */ |
| 17220 | |
| 17221 | #ifndef OS_GETGID_METHODDEF |
| 17222 | #define OS_GETGID_METHODDEF |
| 17223 | #endif /* !defined(OS_GETGID_METHODDEF) */ |
| 17224 | |
| 17225 | #ifndef OS_GETGROUPS_METHODDEF |
| 17226 | #define OS_GETGROUPS_METHODDEF |
| 17227 | #endif /* !defined(OS_GETGROUPS_METHODDEF) */ |
| 17228 | |
| 17229 | #ifndef OS_GETPGID_METHODDEF |
| 17230 | #define OS_GETPGID_METHODDEF |
| 17231 | #endif /* !defined(OS_GETPGID_METHODDEF) */ |
| 17232 | |
| 17233 | #ifndef OS_GETPGRP_METHODDEF |
| 17234 | #define OS_GETPGRP_METHODDEF |
| 17235 | #endif /* !defined(OS_GETPGRP_METHODDEF) */ |
| 17236 | |
| 17237 | #ifndef OS_SETPGRP_METHODDEF |
| 17238 | #define OS_SETPGRP_METHODDEF |
| 17239 | #endif /* !defined(OS_SETPGRP_METHODDEF) */ |
| 17240 | |
| 17241 | #ifndef OS_GETPPID_METHODDEF |
| 17242 | #define OS_GETPPID_METHODDEF |
| 17243 | #endif /* !defined(OS_GETPPID_METHODDEF) */ |
| 17244 | |
| 17245 | #ifndef OS_GETLOGIN_METHODDEF |
| 17246 | #define OS_GETLOGIN_METHODDEF |
| 17247 | #endif /* !defined(OS_GETLOGIN_METHODDEF) */ |
| 17248 | |
| 17249 | #ifndef OS_GETUID_METHODDEF |
| 17250 | #define OS_GETUID_METHODDEF |
| 17251 | #endif /* !defined(OS_GETUID_METHODDEF) */ |
| 17252 | |
| 17253 | #ifndef OS_KILL_METHODDEF |
| 17254 | #define OS_KILL_METHODDEF |
| 17255 | #endif /* !defined(OS_KILL_METHODDEF) */ |
| 17256 | |
| 17257 | #ifndef OS_KILLPG_METHODDEF |
| 17258 | #define OS_KILLPG_METHODDEF |
| 17259 | #endif /* !defined(OS_KILLPG_METHODDEF) */ |
| 17260 | |
| 17261 | #ifndef OS_PLOCK_METHODDEF |
| 17262 | #define OS_PLOCK_METHODDEF |
| 17263 | #endif /* !defined(OS_PLOCK_METHODDEF) */ |
| 17264 | |
| 17265 | #ifndef OS_SETUID_METHODDEF |
| 17266 | #define OS_SETUID_METHODDEF |
| 17267 | #endif /* !defined(OS_SETUID_METHODDEF) */ |
| 17268 | |
| 17269 | #ifndef OS_SETEUID_METHODDEF |
| 17270 | #define OS_SETEUID_METHODDEF |
| 17271 | #endif /* !defined(OS_SETEUID_METHODDEF) */ |
| 17272 | |
| 17273 | #ifndef OS_SETEGID_METHODDEF |
| 17274 | #define OS_SETEGID_METHODDEF |
| 17275 | #endif /* !defined(OS_SETEGID_METHODDEF) */ |
| 17276 | |
| 17277 | #ifndef OS_SETREUID_METHODDEF |
| 17278 | #define OS_SETREUID_METHODDEF |
| 17279 | #endif /* !defined(OS_SETREUID_METHODDEF) */ |
| 17280 | |
| 17281 | #ifndef OS_SETREGID_METHODDEF |
| 17282 | #define OS_SETREGID_METHODDEF |
| 17283 | #endif /* !defined(OS_SETREGID_METHODDEF) */ |
| 17284 | |
| 17285 | #ifndef OS_SETGID_METHODDEF |
| 17286 | #define OS_SETGID_METHODDEF |
| 17287 | #endif /* !defined(OS_SETGID_METHODDEF) */ |
| 17288 | |
| 17289 | #ifndef OS_SETGROUPS_METHODDEF |
| 17290 | #define OS_SETGROUPS_METHODDEF |
| 17291 | #endif /* !defined(OS_SETGROUPS_METHODDEF) */ |
| 17292 | |
| 17293 | #ifndef OS_WAIT3_METHODDEF |
| 17294 | #define OS_WAIT3_METHODDEF |
| 17295 | #endif /* !defined(OS_WAIT3_METHODDEF) */ |
| 17296 | |
| 17297 | #ifndef OS_WAIT4_METHODDEF |
| 17298 | #define OS_WAIT4_METHODDEF |
| 17299 | #endif /* !defined(OS_WAIT4_METHODDEF) */ |
| 17300 | |
| 17301 | #ifndef OS_WAITID_METHODDEF |
| 17302 | #define OS_WAITID_METHODDEF |
| 17303 | #endif /* !defined(OS_WAITID_METHODDEF) */ |
| 17304 | |
| 17305 | #ifndef OS_WAITPID_METHODDEF |
| 17306 | #define OS_WAITPID_METHODDEF |
| 17307 | #endif /* !defined(OS_WAITPID_METHODDEF) */ |
| 17308 | |
| 17309 | #ifndef OS_WAITPID_METHODDEF |
| 17310 | #define OS_WAITPID_METHODDEF |
| 17311 | #endif /* !defined(OS_WAITPID_METHODDEF) */ |
| 17312 | |
| 17313 | #ifndef OS_WAIT_METHODDEF |
| 17314 | #define OS_WAIT_METHODDEF |
| 17315 | #endif /* !defined(OS_WAIT_METHODDEF) */ |
| 17316 | |
| 17317 | #ifndef OS_SYMLINK_METHODDEF |
| 17318 | #define OS_SYMLINK_METHODDEF |
| 17319 | #endif /* !defined(OS_SYMLINK_METHODDEF) */ |
| 17320 | |
| 17321 | #ifndef OS_TIMES_METHODDEF |
| 17322 | #define OS_TIMES_METHODDEF |
| 17323 | #endif /* !defined(OS_TIMES_METHODDEF) */ |
| 17324 | |
| 17325 | #ifndef OS_GETSID_METHODDEF |
| 17326 | #define OS_GETSID_METHODDEF |
| 17327 | #endif /* !defined(OS_GETSID_METHODDEF) */ |
| 17328 | |
| 17329 | #ifndef OS_SETSID_METHODDEF |
| 17330 | #define OS_SETSID_METHODDEF |
| 17331 | #endif /* !defined(OS_SETSID_METHODDEF) */ |
| 17332 | |
| 17333 | #ifndef OS_SETPGID_METHODDEF |
| 17334 | #define OS_SETPGID_METHODDEF |
| 17335 | #endif /* !defined(OS_SETPGID_METHODDEF) */ |
| 17336 | |
| 17337 | #ifndef OS_TCGETPGRP_METHODDEF |
| 17338 | #define OS_TCGETPGRP_METHODDEF |
| 17339 | #endif /* !defined(OS_TCGETPGRP_METHODDEF) */ |
| 17340 | |
| 17341 | #ifndef OS_TCSETPGRP_METHODDEF |
| 17342 | #define OS_TCSETPGRP_METHODDEF |
| 17343 | #endif /* !defined(OS_TCSETPGRP_METHODDEF) */ |
| 17344 | |
| 17345 | #ifndef OS_LOCKF_METHODDEF |
| 17346 | #define OS_LOCKF_METHODDEF |
| 17347 | #endif /* !defined(OS_LOCKF_METHODDEF) */ |
| 17348 | |
| 17349 | #ifndef OS_READV_METHODDEF |
| 17350 | #define OS_READV_METHODDEF |
| 17351 | #endif /* !defined(OS_READV_METHODDEF) */ |
| 17352 | |
| 17353 | #ifndef OS_PREAD_METHODDEF |
| 17354 | #define OS_PREAD_METHODDEF |
| 17355 | #endif /* !defined(OS_PREAD_METHODDEF) */ |
| 17356 | |
| 17357 | #ifndef OS_PIPE_METHODDEF |
| 17358 | #define OS_PIPE_METHODDEF |
| 17359 | #endif /* !defined(OS_PIPE_METHODDEF) */ |
| 17360 | |
| 17361 | #ifndef OS_PIPE2_METHODDEF |
| 17362 | #define OS_PIPE2_METHODDEF |
| 17363 | #endif /* !defined(OS_PIPE2_METHODDEF) */ |
| 17364 | |
| 17365 | #ifndef OS_WRITEV_METHODDEF |
| 17366 | #define OS_WRITEV_METHODDEF |
| 17367 | #endif /* !defined(OS_WRITEV_METHODDEF) */ |
| 17368 | |
| 17369 | #ifndef OS_PWRITE_METHODDEF |
| 17370 | #define OS_PWRITE_METHODDEF |
| 17371 | #endif /* !defined(OS_PWRITE_METHODDEF) */ |
| 17372 | |
| 17373 | #ifndef OS_MKFIFO_METHODDEF |
| 17374 | #define OS_MKFIFO_METHODDEF |
| 17375 | #endif /* !defined(OS_MKFIFO_METHODDEF) */ |
| 17376 | |
| 17377 | #ifndef OS_MKNOD_METHODDEF |
| 17378 | #define OS_MKNOD_METHODDEF |
| 17379 | #endif /* !defined(OS_MKNOD_METHODDEF) */ |
| 17380 | |
| 17381 | #ifndef OS_MAJOR_METHODDEF |
| 17382 | #define OS_MAJOR_METHODDEF |
| 17383 | #endif /* !defined(OS_MAJOR_METHODDEF) */ |
| 17384 | |
| 17385 | #ifndef OS_MINOR_METHODDEF |
| 17386 | #define OS_MINOR_METHODDEF |
| 17387 | #endif /* !defined(OS_MINOR_METHODDEF) */ |
| 17388 | |
| 17389 | #ifndef OS_MAKEDEV_METHODDEF |
| 17390 | #define OS_MAKEDEV_METHODDEF |
| 17391 | #endif /* !defined(OS_MAKEDEV_METHODDEF) */ |
| 17392 | |
| 17393 | #ifndef OS_FTRUNCATE_METHODDEF |
| 17394 | #define OS_FTRUNCATE_METHODDEF |
| 17395 | #endif /* !defined(OS_FTRUNCATE_METHODDEF) */ |
| 17396 | |
| 17397 | #ifndef OS_TRUNCATE_METHODDEF |
| 17398 | #define OS_TRUNCATE_METHODDEF |
| 17399 | #endif /* !defined(OS_TRUNCATE_METHODDEF) */ |
| 17400 | |
| 17401 | #ifndef OS_POSIX_FALLOCATE_METHODDEF |
| 17402 | #define OS_POSIX_FALLOCATE_METHODDEF |
| 17403 | #endif /* !defined(OS_POSIX_FALLOCATE_METHODDEF) */ |
| 17404 | |
| 17405 | #ifndef OS_POSIX_FADVISE_METHODDEF |
| 17406 | #define OS_POSIX_FADVISE_METHODDEF |
| 17407 | #endif /* !defined(OS_POSIX_FADVISE_METHODDEF) */ |
| 17408 | |
| 17409 | #ifndef OS_PUTENV_METHODDEF |
| 17410 | #define OS_PUTENV_METHODDEF |
| 17411 | #endif /* !defined(OS_PUTENV_METHODDEF) */ |
| 17412 | |
| 17413 | #ifndef OS_PUTENV_METHODDEF |
| 17414 | #define OS_PUTENV_METHODDEF |
| 17415 | #endif /* !defined(OS_PUTENV_METHODDEF) */ |
| 17416 | |
| 17417 | #ifndef OS_UNSETENV_METHODDEF |
| 17418 | #define OS_UNSETENV_METHODDEF |
| 17419 | #endif /* !defined(OS_UNSETENV_METHODDEF) */ |
| 17420 | |
| 17421 | #ifndef OS_WCOREDUMP_METHODDEF |
| 17422 | #define OS_WCOREDUMP_METHODDEF |
| 17423 | #endif /* !defined(OS_WCOREDUMP_METHODDEF) */ |
| 17424 | |
| 17425 | #ifndef OS_WIFCONTINUED_METHODDEF |
| 17426 | #define OS_WIFCONTINUED_METHODDEF |
| 17427 | #endif /* !defined(OS_WIFCONTINUED_METHODDEF) */ |
| 17428 | |
| 17429 | #ifndef OS_WIFSTOPPED_METHODDEF |
| 17430 | #define OS_WIFSTOPPED_METHODDEF |
| 17431 | #endif /* !defined(OS_WIFSTOPPED_METHODDEF) */ |
| 17432 | |
| 17433 | #ifndef OS_WIFSIGNALED_METHODDEF |
| 17434 | #define OS_WIFSIGNALED_METHODDEF |
| 17435 | #endif /* !defined(OS_WIFSIGNALED_METHODDEF) */ |
| 17436 | |
| 17437 | #ifndef OS_WIFEXITED_METHODDEF |
| 17438 | #define OS_WIFEXITED_METHODDEF |
| 17439 | #endif /* !defined(OS_WIFEXITED_METHODDEF) */ |
| 17440 | |
| 17441 | #ifndef OS_WEXITSTATUS_METHODDEF |
| 17442 | #define OS_WEXITSTATUS_METHODDEF |
| 17443 | #endif /* !defined(OS_WEXITSTATUS_METHODDEF) */ |
| 17444 | |
| 17445 | #ifndef OS_WTERMSIG_METHODDEF |
| 17446 | #define OS_WTERMSIG_METHODDEF |
| 17447 | #endif /* !defined(OS_WTERMSIG_METHODDEF) */ |
| 17448 | |
| 17449 | #ifndef OS_WSTOPSIG_METHODDEF |
| 17450 | #define OS_WSTOPSIG_METHODDEF |
| 17451 | #endif /* !defined(OS_WSTOPSIG_METHODDEF) */ |
| 17452 | |
| 17453 | #ifndef OS_FSTATVFS_METHODDEF |
| 17454 | #define OS_FSTATVFS_METHODDEF |
| 17455 | #endif /* !defined(OS_FSTATVFS_METHODDEF) */ |
| 17456 | |
| 17457 | #ifndef OS_STATVFS_METHODDEF |
| 17458 | #define OS_STATVFS_METHODDEF |
| 17459 | #endif /* !defined(OS_STATVFS_METHODDEF) */ |
| 17460 | |
| 17461 | #ifndef OS__GETDISKUSAGE_METHODDEF |
| 17462 | #define OS__GETDISKUSAGE_METHODDEF |
| 17463 | #endif /* !defined(OS__GETDISKUSAGE_METHODDEF) */ |
| 17464 | |
| 17465 | #ifndef OS_FPATHCONF_METHODDEF |
| 17466 | #define OS_FPATHCONF_METHODDEF |
| 17467 | #endif /* !defined(OS_FPATHCONF_METHODDEF) */ |
| 17468 | |
| 17469 | #ifndef OS_PATHCONF_METHODDEF |
| 17470 | #define OS_PATHCONF_METHODDEF |
| 17471 | #endif /* !defined(OS_PATHCONF_METHODDEF) */ |
| 17472 | |
| 17473 | #ifndef OS_CONFSTR_METHODDEF |
| 17474 | #define OS_CONFSTR_METHODDEF |
| 17475 | #endif /* !defined(OS_CONFSTR_METHODDEF) */ |
| 17476 | |
| 17477 | #ifndef OS_SYSCONF_METHODDEF |
| 17478 | #define OS_SYSCONF_METHODDEF |
| 17479 | #endif /* !defined(OS_SYSCONF_METHODDEF) */ |
| 17480 | |
| 17481 | #ifndef OS_GETLOADAVG_METHODDEF |
| 17482 | #define OS_GETLOADAVG_METHODDEF |
| 17483 | #endif /* !defined(OS_GETLOADAVG_METHODDEF) */ |
| 17484 | |
| 17485 | #ifndef OS_SETRESUID_METHODDEF |
| 17486 | #define OS_SETRESUID_METHODDEF |
| 17487 | #endif /* !defined(OS_SETRESUID_METHODDEF) */ |
| 17488 | |
| 17489 | #ifndef OS_SETRESGID_METHODDEF |
| 17490 | #define OS_SETRESGID_METHODDEF |
| 17491 | #endif /* !defined(OS_SETRESGID_METHODDEF) */ |
| 17492 | |
| 17493 | #ifndef OS_GETRESUID_METHODDEF |
| 17494 | #define OS_GETRESUID_METHODDEF |
| 17495 | #endif /* !defined(OS_GETRESUID_METHODDEF) */ |
| 17496 | |
| 17497 | #ifndef OS_GETRESGID_METHODDEF |
| 17498 | #define OS_GETRESGID_METHODDEF |
| 17499 | #endif /* !defined(OS_GETRESGID_METHODDEF) */ |
| 17500 | |
| 17501 | #ifndef OS_GETXATTR_METHODDEF |
| 17502 | #define OS_GETXATTR_METHODDEF |
| 17503 | #endif /* !defined(OS_GETXATTR_METHODDEF) */ |
| 17504 | |
| 17505 | #ifndef OS_SETXATTR_METHODDEF |
| 17506 | #define OS_SETXATTR_METHODDEF |
| 17507 | #endif /* !defined(OS_SETXATTR_METHODDEF) */ |
| 17508 | |
| 17509 | #ifndef OS_REMOVEXATTR_METHODDEF |
| 17510 | #define OS_REMOVEXATTR_METHODDEF |
| 17511 | #endif /* !defined(OS_REMOVEXATTR_METHODDEF) */ |
| 17512 | |
| 17513 | #ifndef OS_LISTXATTR_METHODDEF |
| 17514 | #define OS_LISTXATTR_METHODDEF |
| 17515 | #endif /* !defined(OS_LISTXATTR_METHODDEF) */ |
| 17516 | |
| 17517 | #ifndef OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 17518 | #define OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 17519 | #endif /* !defined(OS_GET_HANDLE_INHERITABLE_METHODDEF) */ |
| 17520 | |
| 17521 | #ifndef OS_SET_HANDLE_INHERITABLE_METHODDEF |
| 17522 | #define OS_SET_HANDLE_INHERITABLE_METHODDEF |
| 17523 | #endif /* !defined(OS_SET_HANDLE_INHERITABLE_METHODDEF) */ |
| 17524 | /*[clinic end generated code: output=52a6140b0b052ce6 input=524ce2e021e4eba6]*/ |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 17525 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 17526 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 17527 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 17528 | |
| 17529 | OS_STAT_METHODDEF |
| 17530 | OS_ACCESS_METHODDEF |
| 17531 | OS_TTYNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17532 | OS_CHDIR_METHODDEF |
| 17533 | OS_CHFLAGS_METHODDEF |
| 17534 | OS_CHMOD_METHODDEF |
| 17535 | OS_FCHMOD_METHODDEF |
| 17536 | OS_LCHMOD_METHODDEF |
| 17537 | OS_CHOWN_METHODDEF |
| 17538 | OS_FCHOWN_METHODDEF |
| 17539 | OS_LCHOWN_METHODDEF |
| 17540 | OS_LCHFLAGS_METHODDEF |
| 17541 | OS_CHROOT_METHODDEF |
| 17542 | OS_CTERMID_METHODDEF |
| 17543 | OS_GETCWD_METHODDEF |
| 17544 | OS_GETCWDB_METHODDEF |
| 17545 | OS_LINK_METHODDEF |
| 17546 | OS_LISTDIR_METHODDEF |
| 17547 | OS_LSTAT_METHODDEF |
| 17548 | OS_MKDIR_METHODDEF |
| 17549 | OS_NICE_METHODDEF |
| 17550 | OS_GETPRIORITY_METHODDEF |
| 17551 | OS_SETPRIORITY_METHODDEF |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 17552 | #ifdef HAVE_READLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 17553 | {"readlink", (PyCFunction)posix_readlink, |
| 17554 | METH_VARARGS | METH_KEYWORDS, |
| 17555 | readlink__doc__}, |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 17556 | #endif /* HAVE_READLINK */ |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 17557 | #if !defined(HAVE_READLINK) && defined(MS_WINDOWS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 17558 | {"readlink", (PyCFunction)win_readlink, |
| 17559 | METH_VARARGS | METH_KEYWORDS, |
| 17560 | readlink__doc__}, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 17561 | #endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17562 | OS_RENAME_METHODDEF |
| 17563 | OS_REPLACE_METHODDEF |
| 17564 | OS_RMDIR_METHODDEF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17565 | {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__}, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17566 | OS_SYMLINK_METHODDEF |
| 17567 | OS_SYSTEM_METHODDEF |
| 17568 | OS_UMASK_METHODDEF |
| 17569 | OS_UNAME_METHODDEF |
| 17570 | OS_UNLINK_METHODDEF |
| 17571 | OS_REMOVE_METHODDEF |
| 17572 | OS_UTIME_METHODDEF |
| 17573 | OS_TIMES_METHODDEF |
| 17574 | OS__EXIT_METHODDEF |
| 17575 | OS_EXECV_METHODDEF |
| 17576 | OS_EXECVE_METHODDEF |
| 17577 | OS_SPAWNV_METHODDEF |
| 17578 | OS_SPAWNVE_METHODDEF |
| 17579 | OS_FORK1_METHODDEF |
| 17580 | OS_FORK_METHODDEF |
| 17581 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
| 17582 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF |
| 17583 | OS_SCHED_GETPARAM_METHODDEF |
| 17584 | OS_SCHED_GETSCHEDULER_METHODDEF |
| 17585 | OS_SCHED_RR_GET_INTERVAL_METHODDEF |
| 17586 | OS_SCHED_SETPARAM_METHODDEF |
| 17587 | OS_SCHED_SETSCHEDULER_METHODDEF |
| 17588 | OS_SCHED_YIELD_METHODDEF |
| 17589 | OS_SCHED_SETAFFINITY_METHODDEF |
| 17590 | OS_SCHED_GETAFFINITY_METHODDEF |
| 17591 | OS_OPENPTY_METHODDEF |
| 17592 | OS_FORKPTY_METHODDEF |
| 17593 | OS_GETEGID_METHODDEF |
| 17594 | OS_GETEUID_METHODDEF |
| 17595 | OS_GETGID_METHODDEF |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 17596 | #ifdef HAVE_GETGROUPLIST |
| 17597 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, |
| 17598 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17599 | OS_GETGROUPS_METHODDEF |
| 17600 | OS_GETPID_METHODDEF |
| 17601 | OS_GETPGRP_METHODDEF |
| 17602 | OS_GETPPID_METHODDEF |
| 17603 | OS_GETUID_METHODDEF |
| 17604 | OS_GETLOGIN_METHODDEF |
| 17605 | OS_KILL_METHODDEF |
| 17606 | OS_KILLPG_METHODDEF |
| 17607 | OS_PLOCK_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 17608 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17609 | {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__}, |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 17610 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17611 | OS_SETUID_METHODDEF |
| 17612 | OS_SETEUID_METHODDEF |
| 17613 | OS_SETREUID_METHODDEF |
| 17614 | OS_SETGID_METHODDEF |
| 17615 | OS_SETEGID_METHODDEF |
| 17616 | OS_SETREGID_METHODDEF |
| 17617 | OS_SETGROUPS_METHODDEF |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 17618 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17619 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 17620 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17621 | OS_GETPGID_METHODDEF |
| 17622 | OS_SETPGRP_METHODDEF |
| 17623 | OS_WAIT_METHODDEF |
| 17624 | OS_WAIT3_METHODDEF |
| 17625 | OS_WAIT4_METHODDEF |
| 17626 | OS_WAITID_METHODDEF |
| 17627 | OS_WAITPID_METHODDEF |
| 17628 | OS_GETSID_METHODDEF |
| 17629 | OS_SETSID_METHODDEF |
| 17630 | OS_SETPGID_METHODDEF |
| 17631 | OS_TCGETPGRP_METHODDEF |
| 17632 | OS_TCSETPGRP_METHODDEF |
| 17633 | OS_OPEN_METHODDEF |
| 17634 | OS_CLOSE_METHODDEF |
| 17635 | OS_CLOSERANGE_METHODDEF |
| 17636 | OS_DEVICE_ENCODING_METHODDEF |
| 17637 | OS_DUP_METHODDEF |
| 17638 | OS_DUP2_METHODDEF |
| 17639 | OS_LOCKF_METHODDEF |
| 17640 | OS_LSEEK_METHODDEF |
| 17641 | OS_READ_METHODDEF |
| 17642 | OS_READV_METHODDEF |
| 17643 | OS_PREAD_METHODDEF |
| 17644 | OS_WRITE_METHODDEF |
| 17645 | OS_WRITEV_METHODDEF |
| 17646 | OS_PWRITE_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 17647 | #ifdef HAVE_SENDFILE |
| 17648 | {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
| 17649 | posix_sendfile__doc__}, |
| 17650 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17651 | OS_FSTAT_METHODDEF |
| 17652 | OS_ISATTY_METHODDEF |
| 17653 | OS_PIPE_METHODDEF |
| 17654 | OS_PIPE2_METHODDEF |
| 17655 | OS_MKFIFO_METHODDEF |
| 17656 | OS_MKNOD_METHODDEF |
| 17657 | OS_MAJOR_METHODDEF |
| 17658 | OS_MINOR_METHODDEF |
| 17659 | OS_MAKEDEV_METHODDEF |
| 17660 | OS_FTRUNCATE_METHODDEF |
| 17661 | OS_TRUNCATE_METHODDEF |
| 17662 | OS_POSIX_FALLOCATE_METHODDEF |
| 17663 | OS_POSIX_FADVISE_METHODDEF |
| 17664 | OS_PUTENV_METHODDEF |
| 17665 | OS_UNSETENV_METHODDEF |
| 17666 | OS_STRERROR_METHODDEF |
| 17667 | OS_FCHDIR_METHODDEF |
| 17668 | OS_FSYNC_METHODDEF |
| 17669 | OS_SYNC_METHODDEF |
| 17670 | OS_FDATASYNC_METHODDEF |
| 17671 | OS_WCOREDUMP_METHODDEF |
| 17672 | OS_WIFCONTINUED_METHODDEF |
| 17673 | OS_WIFSTOPPED_METHODDEF |
| 17674 | OS_WIFSIGNALED_METHODDEF |
| 17675 | OS_WIFEXITED_METHODDEF |
| 17676 | OS_WEXITSTATUS_METHODDEF |
| 17677 | OS_WTERMSIG_METHODDEF |
| 17678 | OS_WSTOPSIG_METHODDEF |
| 17679 | OS_FSTATVFS_METHODDEF |
| 17680 | OS_STATVFS_METHODDEF |
| 17681 | OS_CONFSTR_METHODDEF |
| 17682 | OS_SYSCONF_METHODDEF |
| 17683 | OS_FPATHCONF_METHODDEF |
| 17684 | OS_PATHCONF_METHODDEF |
| 17685 | OS_ABORT_METHODDEF |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 17686 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17687 | {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL}, |
Brian Curtin | 95d028f | 2011-06-09 09:10:38 -0500 | [diff] [blame] | 17688 | {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__}, |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 17689 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17690 | OS__GETDISKUSAGE_METHODDEF |
| 17691 | OS__GETFINALPATHNAME_METHODDEF |
| 17692 | OS__GETVOLUMEPATHNAME_METHODDEF |
| 17693 | OS_GETLOADAVG_METHODDEF |
| 17694 | OS_URANDOM_METHODDEF |
| 17695 | OS_SETRESUID_METHODDEF |
| 17696 | OS_SETRESGID_METHODDEF |
| 17697 | OS_GETRESUID_METHODDEF |
| 17698 | OS_GETRESGID_METHODDEF |
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 17699 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17700 | OS_GETXATTR_METHODDEF |
| 17701 | OS_SETXATTR_METHODDEF |
| 17702 | OS_REMOVEXATTR_METHODDEF |
| 17703 | OS_LISTXATTR_METHODDEF |
| 17704 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 17705 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
| 17706 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, |
| 17707 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 17708 | OS_CPU_COUNT_METHODDEF |
| 17709 | OS_GET_INHERITABLE_METHODDEF |
| 17710 | OS_SET_INHERITABLE_METHODDEF |
| 17711 | OS_GET_HANDLE_INHERITABLE_METHODDEF |
| 17712 | OS_SET_HANDLE_INHERITABLE_METHODDEF |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 17713 | #ifndef MS_WINDOWS |
| 17714 | {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__}, |
| 17715 | {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__}, |
| 17716 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 17717 | {"scandir", (PyCFunction)posix_scandir, |
| 17718 | METH_VARARGS | METH_KEYWORDS, |
| 17719 | posix_scandir__doc__}, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17720 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17721 | }; |
| 17722 | |
| 17723 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 17724 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 17725 | static int |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 17726 | enable_symlink() |
| 17727 | { |
| 17728 | HANDLE tok; |
| 17729 | TOKEN_PRIVILEGES tok_priv; |
| 17730 | LUID luid; |
| 17731 | int meth_idx = 0; |
| 17732 | |
| 17733 | if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 17734 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 17735 | |
| 17736 | if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 17737 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 17738 | |
| 17739 | tok_priv.PrivilegeCount = 1; |
| 17740 | tok_priv.Privileges[0].Luid = luid; |
| 17741 | tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 17742 | |
| 17743 | if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv, |
| 17744 | sizeof(TOKEN_PRIVILEGES), |
| 17745 | (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL)) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 17746 | return 0; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 17747 | |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 17748 | /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */ |
| 17749 | return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1; |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 17750 | } |
| 17751 | #endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */ |
| 17752 | |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17753 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17754 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17755 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 17756 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17757 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17758 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 17759 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17760 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17761 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 17762 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17763 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17764 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 17765 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17766 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17767 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 17768 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17769 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 17770 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 17771 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17772 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 17773 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 17774 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17775 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 17776 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17777 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17778 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17779 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 17780 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17781 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 17782 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17783 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17784 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17785 | #endif |
| 17786 | #ifdef O_WRONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17787 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17788 | #endif |
| 17789 | #ifdef O_RDWR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17790 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17791 | #endif |
| 17792 | #ifdef O_NDELAY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17793 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17794 | #endif |
| 17795 | #ifdef O_NONBLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17796 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17797 | #endif |
| 17798 | #ifdef O_APPEND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17799 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17800 | #endif |
| 17801 | #ifdef O_DSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17802 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17803 | #endif |
| 17804 | #ifdef O_RSYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17805 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17806 | #endif |
| 17807 | #ifdef O_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17808 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17809 | #endif |
| 17810 | #ifdef O_NOCTTY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17811 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17812 | #endif |
| 17813 | #ifdef O_CREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17814 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17815 | #endif |
| 17816 | #ifdef O_EXCL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17817 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17818 | #endif |
| 17819 | #ifdef O_TRUNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17820 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 17821 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 17822 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17823 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 17824 | #endif |
| 17825 | #ifdef O_TEXT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17826 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 17827 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 17828 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17829 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 17830 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17831 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17832 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17833 | #endif |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 17834 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17835 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 17836 | #endif |
| 17837 | #ifdef O_EXLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17838 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 17839 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 17840 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17841 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 17842 | #endif |
| 17843 | #ifdef O_SEARCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17844 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 17845 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 17846 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17847 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 17848 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 17849 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17850 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 17851 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 17852 | #ifdef O_TMPFILE |
| 17853 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; |
| 17854 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 17855 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17856 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 17857 | #endif |
| 17858 | #ifdef PRIO_PGRP |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17859 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 17860 | #endif |
| 17861 | #ifdef PRIO_USER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17862 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 17863 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 17864 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17865 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 17866 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 17867 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17868 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 17869 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 17870 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17871 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 17872 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17873 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 17874 | #endif |
| 17875 | #ifdef SEEK_DATA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17876 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 17877 | #endif |
| 17878 | |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17879 | /* MS Windows */ |
| 17880 | #ifdef O_NOINHERIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17881 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17882 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17883 | #endif |
| 17884 | #ifdef _O_SHORT_LIVED |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17885 | /* Optimize for short life (keep in memory). */ |
| 17886 | /* 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] | 17887 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17888 | #endif |
| 17889 | #ifdef O_TEMPORARY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17890 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17891 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17892 | #endif |
| 17893 | #ifdef O_RANDOM |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17894 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17895 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17896 | #endif |
| 17897 | #ifdef O_SEQUENTIAL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17898 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17899 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 17900 | #endif |
| 17901 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17902 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 17903 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17904 | /* Send a SIGIO signal whenever input or output |
| 17905 | becomes available on file descriptor */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17906 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 17907 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17908 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17909 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17910 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17911 | #endif |
| 17912 | #ifdef O_DIRECTORY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17913 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17914 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17915 | #endif |
| 17916 | #ifdef O_NOFOLLOW |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17917 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17918 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 17919 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 17920 | #ifdef O_NOLINKS |
| 17921 | /* 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] | 17922 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 17923 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 17924 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17925 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17926 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 17927 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 17928 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 17929 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17930 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17931 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17932 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17933 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17934 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17935 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17936 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17937 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17938 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17939 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17940 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17941 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17942 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17943 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17944 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17945 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17946 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17947 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17948 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17949 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17950 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17951 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17952 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17953 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17954 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17955 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17956 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17957 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17958 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17959 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17960 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17961 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17962 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17963 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17964 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17965 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17966 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17967 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17968 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17969 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17970 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17971 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17972 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17973 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17974 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17975 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17976 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17977 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17978 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17979 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 17980 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 17981 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 17982 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 17983 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17984 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 17985 | #endif /* ST_RDONLY */ |
| 17986 | #ifdef ST_NOSUID |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 17987 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 17988 | #endif /* ST_NOSUID */ |
| 17989 | |
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 17990 | /* GNU extensions */ |
| 17991 | #ifdef ST_NODEV |
| 17992 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; |
| 17993 | #endif /* ST_NODEV */ |
| 17994 | #ifdef ST_NOEXEC |
| 17995 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; |
| 17996 | #endif /* ST_NOEXEC */ |
| 17997 | #ifdef ST_SYNCHRONOUS |
| 17998 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; |
| 17999 | #endif /* ST_SYNCHRONOUS */ |
| 18000 | #ifdef ST_MANDLOCK |
| 18001 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; |
| 18002 | #endif /* ST_MANDLOCK */ |
| 18003 | #ifdef ST_WRITE |
| 18004 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; |
| 18005 | #endif /* ST_WRITE */ |
| 18006 | #ifdef ST_APPEND |
| 18007 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; |
| 18008 | #endif /* ST_APPEND */ |
| 18009 | #ifdef ST_NOATIME |
| 18010 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; |
| 18011 | #endif /* ST_NOATIME */ |
| 18012 | #ifdef ST_NODIRATIME |
| 18013 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; |
| 18014 | #endif /* ST_NODIRATIME */ |
| 18015 | #ifdef ST_RELATIME |
| 18016 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; |
| 18017 | #endif /* ST_RELATIME */ |
| 18018 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 18019 | /* FreeBSD sendfile() constants */ |
| 18020 | #ifdef SF_NODISKIO |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18021 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 18022 | #endif |
| 18023 | #ifdef SF_MNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18024 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 18025 | #endif |
| 18026 | #ifdef SF_SYNC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18027 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 18028 | #endif |
| 18029 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18030 | /* constants for posix_fadvise */ |
| 18031 | #ifdef POSIX_FADV_NORMAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18032 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18033 | #endif |
| 18034 | #ifdef POSIX_FADV_SEQUENTIAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18035 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18036 | #endif |
| 18037 | #ifdef POSIX_FADV_RANDOM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18038 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18039 | #endif |
| 18040 | #ifdef POSIX_FADV_NOREUSE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18041 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18042 | #endif |
| 18043 | #ifdef POSIX_FADV_WILLNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18044 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18045 | #endif |
| 18046 | #ifdef POSIX_FADV_DONTNEED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18047 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18048 | #endif |
| 18049 | |
| 18050 | /* constants for waitid */ |
| 18051 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18052 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
| 18053 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; |
| 18054 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18055 | #endif |
| 18056 | #ifdef WEXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18057 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18058 | #endif |
| 18059 | #ifdef WNOWAIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18060 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18061 | #endif |
| 18062 | #ifdef WSTOPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18063 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18064 | #endif |
| 18065 | #ifdef CLD_EXITED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18066 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18067 | #endif |
| 18068 | #ifdef CLD_DUMPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18069 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18070 | #endif |
| 18071 | #ifdef CLD_TRAPPED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18072 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18073 | #endif |
| 18074 | #ifdef CLD_CONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18075 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18076 | #endif |
| 18077 | |
| 18078 | /* constants for lockf */ |
| 18079 | #ifdef F_LOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18080 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18081 | #endif |
| 18082 | #ifdef F_TLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18083 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18084 | #endif |
| 18085 | #ifdef F_ULOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18086 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18087 | #endif |
| 18088 | #ifdef F_TEST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18089 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18090 | #endif |
| 18091 | |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 18092 | #ifdef HAVE_SPAWNV |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18093 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
| 18094 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; |
| 18095 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; |
| 18096 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
| 18097 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 18098 | #endif |
| 18099 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18100 | #ifdef HAVE_SCHED_H |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18101 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
| 18102 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
| 18103 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18104 | #ifdef SCHED_SPORADIC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18105 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18106 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18107 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18108 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18109 | #endif |
| 18110 | #ifdef SCHED_IDLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18111 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18112 | #endif |
| 18113 | #ifdef SCHED_RESET_ON_FORK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18114 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18115 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 18116 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18117 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 18118 | #endif |
| 18119 | #ifdef SCHED_IA |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18120 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 18121 | #endif |
| 18122 | #ifdef SCHED_FSS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18123 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 18124 | #endif |
| 18125 | #ifdef SCHED_FX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18126 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 18127 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18128 | #endif |
| 18129 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 18130 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18131 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
| 18132 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; |
| 18133 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 18134 | #endif |
| 18135 | |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18136 | #ifdef RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18137 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18138 | #endif |
| 18139 | #ifdef RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18140 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18141 | #endif |
| 18142 | #ifdef RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18143 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18144 | #endif |
| 18145 | #ifdef RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18146 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18147 | #endif |
| 18148 | #ifdef RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18149 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18150 | #endif |
| 18151 | #ifdef RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18152 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18153 | #endif |
| 18154 | #ifdef RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 18155 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 18156 | #endif |
| 18157 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18158 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 18159 | } |
| 18160 | |
| 18161 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 18162 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18163 | PyModuleDef_HEAD_INIT, |
| 18164 | MODNAME, |
| 18165 | posix__doc__, |
| 18166 | -1, |
| 18167 | posix_methods, |
| 18168 | NULL, |
| 18169 | NULL, |
| 18170 | NULL, |
| 18171 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 18172 | }; |
| 18173 | |
| 18174 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 18175 | static char *have_functions[] = { |
| 18176 | |
| 18177 | #ifdef HAVE_FACCESSAT |
| 18178 | "HAVE_FACCESSAT", |
| 18179 | #endif |
| 18180 | |
| 18181 | #ifdef HAVE_FCHDIR |
| 18182 | "HAVE_FCHDIR", |
| 18183 | #endif |
| 18184 | |
| 18185 | #ifdef HAVE_FCHMOD |
| 18186 | "HAVE_FCHMOD", |
| 18187 | #endif |
| 18188 | |
| 18189 | #ifdef HAVE_FCHMODAT |
| 18190 | "HAVE_FCHMODAT", |
| 18191 | #endif |
| 18192 | |
| 18193 | #ifdef HAVE_FCHOWN |
| 18194 | "HAVE_FCHOWN", |
| 18195 | #endif |
| 18196 | |
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 18197 | #ifdef HAVE_FCHOWNAT |
| 18198 | "HAVE_FCHOWNAT", |
| 18199 | #endif |
| 18200 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 18201 | #ifdef HAVE_FEXECVE |
| 18202 | "HAVE_FEXECVE", |
| 18203 | #endif |
| 18204 | |
| 18205 | #ifdef HAVE_FDOPENDIR |
| 18206 | "HAVE_FDOPENDIR", |
| 18207 | #endif |
| 18208 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 18209 | #ifdef HAVE_FPATHCONF |
| 18210 | "HAVE_FPATHCONF", |
| 18211 | #endif |
| 18212 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 18213 | #ifdef HAVE_FSTATAT |
| 18214 | "HAVE_FSTATAT", |
| 18215 | #endif |
| 18216 | |
| 18217 | #ifdef HAVE_FSTATVFS |
| 18218 | "HAVE_FSTATVFS", |
| 18219 | #endif |
| 18220 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 18221 | #ifdef HAVE_FTRUNCATE |
| 18222 | "HAVE_FTRUNCATE", |
| 18223 | #endif |
| 18224 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 18225 | #ifdef HAVE_FUTIMENS |
| 18226 | "HAVE_FUTIMENS", |
| 18227 | #endif |
| 18228 | |
| 18229 | #ifdef HAVE_FUTIMES |
| 18230 | "HAVE_FUTIMES", |
| 18231 | #endif |
| 18232 | |
| 18233 | #ifdef HAVE_FUTIMESAT |
| 18234 | "HAVE_FUTIMESAT", |
| 18235 | #endif |
| 18236 | |
| 18237 | #ifdef HAVE_LINKAT |
| 18238 | "HAVE_LINKAT", |
| 18239 | #endif |
| 18240 | |
| 18241 | #ifdef HAVE_LCHFLAGS |
| 18242 | "HAVE_LCHFLAGS", |
| 18243 | #endif |
| 18244 | |
| 18245 | #ifdef HAVE_LCHMOD |
| 18246 | "HAVE_LCHMOD", |
| 18247 | #endif |
| 18248 | |
| 18249 | #ifdef HAVE_LCHOWN |
| 18250 | "HAVE_LCHOWN", |
| 18251 | #endif |
| 18252 | |
| 18253 | #ifdef HAVE_LSTAT |
| 18254 | "HAVE_LSTAT", |
| 18255 | #endif |
| 18256 | |
| 18257 | #ifdef HAVE_LUTIMES |
| 18258 | "HAVE_LUTIMES", |
| 18259 | #endif |
| 18260 | |
| 18261 | #ifdef HAVE_MKDIRAT |
| 18262 | "HAVE_MKDIRAT", |
| 18263 | #endif |
| 18264 | |
| 18265 | #ifdef HAVE_MKFIFOAT |
| 18266 | "HAVE_MKFIFOAT", |
| 18267 | #endif |
| 18268 | |
| 18269 | #ifdef HAVE_MKNODAT |
| 18270 | "HAVE_MKNODAT", |
| 18271 | #endif |
| 18272 | |
| 18273 | #ifdef HAVE_OPENAT |
| 18274 | "HAVE_OPENAT", |
| 18275 | #endif |
| 18276 | |
| 18277 | #ifdef HAVE_READLINKAT |
| 18278 | "HAVE_READLINKAT", |
| 18279 | #endif |
| 18280 | |
| 18281 | #ifdef HAVE_RENAMEAT |
| 18282 | "HAVE_RENAMEAT", |
| 18283 | #endif |
| 18284 | |
| 18285 | #ifdef HAVE_SYMLINKAT |
| 18286 | "HAVE_SYMLINKAT", |
| 18287 | #endif |
| 18288 | |
| 18289 | #ifdef HAVE_UNLINKAT |
| 18290 | "HAVE_UNLINKAT", |
| 18291 | #endif |
| 18292 | |
| 18293 | #ifdef HAVE_UTIMENSAT |
| 18294 | "HAVE_UTIMENSAT", |
| 18295 | #endif |
| 18296 | |
| 18297 | #ifdef MS_WINDOWS |
| 18298 | "MS_WINDOWS", |
| 18299 | #endif |
| 18300 | |
| 18301 | NULL |
| 18302 | }; |
| 18303 | |
| 18304 | |
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 18305 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 18306 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 18307 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18308 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 18309 | PyObject *list; |
| 18310 | char **trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 18311 | |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 18312 | #if defined(HAVE_SYMLINK) && defined(MS_WINDOWS) |
Brian Curtin | 3b4499c | 2010-12-28 14:31:47 +0000 | [diff] [blame] | 18313 | win32_can_symlink = enable_symlink(); |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 18314 | #endif |
| 18315 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18316 | m = PyModule_Create(&posixmodule); |
| 18317 | if (m == NULL) |
| 18318 | return NULL; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 18319 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18320 | /* Initialize environ dictionary */ |
| 18321 | v = convertenviron(); |
| 18322 | Py_XINCREF(v); |
| 18323 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) |
| 18324 | return NULL; |
| 18325 | Py_DECREF(v); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 18326 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18327 | if (all_ins(m)) |
| 18328 | return NULL; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 18329 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18330 | if (setup_confname_tables(m)) |
| 18331 | return NULL; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 18332 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18333 | Py_INCREF(PyExc_OSError); |
| 18334 | PyModule_AddObject(m, "error", PyExc_OSError); |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 18335 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 18336 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18337 | if (posix_putenv_garbage == NULL) |
| 18338 | posix_putenv_garbage = PyDict_New(); |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 18339 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 18340 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18341 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18342 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 18343 | waitid_result_desc.name = MODNAME ".waitid_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18344 | if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0) |
| 18345 | return NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18346 | #endif |
| 18347 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 18348 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18349 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
| 18350 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; |
| 18351 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18352 | if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0) |
| 18353 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18354 | structseq_new = StatResultType.tp_new; |
| 18355 | StatResultType.tp_new = statresult_new; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 18356 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 18357 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18358 | if (PyStructSequence_InitType2(&StatVFSResultType, |
| 18359 | &statvfs_result_desc) < 0) |
| 18360 | return NULL; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 18361 | #ifdef NEED_TICKS_PER_SECOND |
| 18362 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18363 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 18364 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18365 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 18366 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18367 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 18368 | # endif |
| 18369 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18370 | |
Benjamin Peterson | 0163c9a | 2011-08-02 18:11:38 -0500 | [diff] [blame] | 18371 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18372 | sched_param_desc.name = MODNAME ".sched_param"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18373 | if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0) |
| 18374 | return NULL; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 18375 | SchedParamType.tp_new = os_sched_param; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18376 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 18377 | |
| 18378 | /* initialize TerminalSize_info */ |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18379 | if (PyStructSequence_InitType2(&TerminalSizeType, |
| 18380 | &TerminalSize_desc) < 0) |
| 18381 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 18382 | |
| 18383 | /* initialize scandir types */ |
| 18384 | if (PyType_Ready(&ScandirIteratorType) < 0) |
| 18385 | return NULL; |
| 18386 | if (PyType_Ready(&DirEntryType) < 0) |
| 18387 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18388 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 18389 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
| 18390 | Py_INCREF((PyObject*) &WaitidResultType); |
| 18391 | PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType); |
| 18392 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18393 | Py_INCREF((PyObject*) &StatResultType); |
| 18394 | PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType); |
| 18395 | Py_INCREF((PyObject*) &StatVFSResultType); |
| 18396 | PyModule_AddObject(m, "statvfs_result", |
| 18397 | (PyObject*) &StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 18398 | |
| 18399 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 18400 | Py_INCREF(&SchedParamType); |
| 18401 | PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 18402 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18403 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 18404 | times_result_desc.name = MODNAME ".times_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18405 | if (PyStructSequence_InitType2(&TimesResultType, ×_result_desc) < 0) |
| 18406 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 18407 | PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType); |
| 18408 | |
| 18409 | uname_result_desc.name = MODNAME ".uname_result"; |
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 18410 | if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0) |
| 18411 | return NULL; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 18412 | PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType); |
| 18413 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18414 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18415 | /* |
| 18416 | * Step 2 of weak-linking support on Mac OS X. |
| 18417 | * |
| 18418 | * The code below removes functions that are not available on the |
| 18419 | * currently active platform. |
| 18420 | * |
| 18421 | * 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] | 18422 | * 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] | 18423 | * OSX 10.4. |
| 18424 | */ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18425 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18426 | if (fstatvfs == NULL) { |
| 18427 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { |
| 18428 | return NULL; |
| 18429 | } |
| 18430 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18431 | #endif /* HAVE_FSTATVFS */ |
| 18432 | |
| 18433 | #ifdef HAVE_STATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18434 | if (statvfs == NULL) { |
| 18435 | if (PyObject_DelAttrString(m, "statvfs") == -1) { |
| 18436 | return NULL; |
| 18437 | } |
| 18438 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18439 | #endif /* HAVE_STATVFS */ |
| 18440 | |
| 18441 | # ifdef HAVE_LCHOWN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18442 | if (lchown == NULL) { |
| 18443 | if (PyObject_DelAttrString(m, "lchown") == -1) { |
| 18444 | return NULL; |
| 18445 | } |
| 18446 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 18447 | #endif /* HAVE_LCHOWN */ |
| 18448 | |
| 18449 | |
| 18450 | #endif /* __APPLE__ */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 18451 | |
Antoine Pitrou | 9d20e0e | 2012-09-12 18:01:36 +0200 | [diff] [blame] | 18452 | Py_INCREF(&TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 18453 | PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType); |
| 18454 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 18455 | billion = PyLong_FromLong(1000000000); |
| 18456 | if (!billion) |
| 18457 | return NULL; |
| 18458 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 18459 | /* suppress "function not used" warnings */ |
| 18460 | { |
| 18461 | int ignored; |
| 18462 | fd_specified("", -1); |
| 18463 | follow_symlinks_specified("", 1); |
| 18464 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); |
| 18465 | dir_fd_converter(Py_None, &ignored); |
| 18466 | dir_fd_unavailable(Py_None, &ignored); |
| 18467 | } |
| 18468 | |
| 18469 | /* |
| 18470 | * provide list of locally available functions |
| 18471 | * so os.py can populate support_* lists |
| 18472 | */ |
| 18473 | list = PyList_New(0); |
| 18474 | if (!list) |
| 18475 | return NULL; |
| 18476 | for (trace = have_functions; *trace; trace++) { |
| 18477 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); |
| 18478 | if (!unicode) |
| 18479 | return NULL; |
| 18480 | if (PyList_Append(list, unicode)) |
| 18481 | return NULL; |
| 18482 | Py_DECREF(unicode); |
| 18483 | } |
| 18484 | PyModule_AddObject(m, "_have_functions", list); |
| 18485 | |
| 18486 | initialized = 1; |
| 18487 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 18488 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 18489 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 18490 | |
| 18491 | #ifdef __cplusplus |
| 18492 | } |
| 18493 | #endif |