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 | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 28 | #ifdef MS_WINDOWS |
29 | /* include <windows.h> early to avoid conflict with pycore_condvar.h: | ||||
30 | |||||
31 | #define WIN32_LEAN_AND_MEAN | ||||
32 | #include <windows.h> | ||||
33 | |||||
34 | FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */ | ||||
35 | # include <windows.h> | ||||
36 | #endif | ||||
37 | |||||
38 | #include "pycore_ceval.h" /* _PyEval_ReInitThreads() */ | ||||
39 | #include "pycore_pystate.h" /* _PyRuntime */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 40 | #include "pythread.h" |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 41 | #include "structmember.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 42 | #ifndef MS_WINDOWS |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 43 | # include "posixmodule.h" |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 44 | #else |
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 45 | # include "winreparse.h" |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 46 | #endif |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 47 | |
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 48 | /* On android API level 21, 'AT_EACCESS' is not declared although |
49 | * HAVE_FACCESSAT is defined. */ | ||||
50 | #ifdef __ANDROID__ | ||||
51 | #undef HAVE_FACCESSAT | ||||
52 | #endif | ||||
53 | |||||
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D) | fa76eee | 2016-05-28 21:03:48 +0000 | [diff] [blame] | 54 | #include <stdio.h> /* needed for ctermid() */ |
55 | |||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 56 | #ifdef __cplusplus |
57 | extern "C" { | ||||
58 | #endif | ||||
59 | |||||
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 60 | PyDoc_STRVAR(posix__doc__, |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 61 | "This module provides access to operating system functionality that is\n\ |
62 | standardized by the C Standard and the POSIX standard (a thinly\n\ | ||||
63 | disguised Unix interface). Refer to the library manual and\n\ | ||||
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 64 | corresponding Unix manual entries for more information on calls."); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 66 | |
Ross Lagerwall | 4d076da | 2011-03-18 06:56:53 +0200 | [diff] [blame] | 67 | #ifdef HAVE_SYS_UIO_H |
68 | #include <sys/uio.h> | ||||
69 | #endif | ||||
70 | |||||
Christian Heimes | 75b9618 | 2017-09-05 15:53:09 +0200 | [diff] [blame] | 71 | #ifdef HAVE_SYS_SYSMACROS_H |
72 | /* GNU C Library: major(), minor(), makedev() */ | ||||
73 | #include <sys/sysmacros.h> | ||||
74 | #endif | ||||
75 | |||||
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 76 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 77 | #include <sys/types.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 78 | #endif /* HAVE_SYS_TYPES_H */ |
79 | |||||
80 | #ifdef HAVE_SYS_STAT_H | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 81 | #include <sys/stat.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 82 | #endif /* HAVE_SYS_STAT_H */ |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 84 | #ifdef HAVE_SYS_WAIT_H |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 85 | #include <sys/wait.h> /* For WNOHANG */ |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 86 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 88 | #ifdef HAVE_SIGNAL_H |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 89 | #include <signal.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 90 | #endif |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 92 | #ifdef HAVE_FCNTL_H |
93 | #include <fcntl.h> | ||||
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 94 | #endif /* HAVE_FCNTL_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | a6535fd | 2001-10-18 19:44:10 +0000 | [diff] [blame] | 96 | #ifdef HAVE_GRP_H |
97 | #include <grp.h> | ||||
98 | #endif | ||||
99 | |||||
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 100 | #ifdef HAVE_SYSEXITS_H |
101 | #include <sysexits.h> | ||||
102 | #endif /* HAVE_SYSEXITS_H */ | ||||
103 | |||||
Anthony Baxter | 8a560de | 2004-10-13 15:30:56 +0000 | [diff] [blame] | 104 | #ifdef HAVE_SYS_LOADAVG_H |
105 | #include <sys/loadavg.h> | ||||
106 | #endif | ||||
107 | |||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 108 | #ifdef HAVE_SYS_SENDFILE_H |
109 | #include <sys/sendfile.h> | ||||
110 | #endif | ||||
111 | |||||
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 112 | #if defined(__APPLE__) |
113 | #include <copyfile.h> | ||||
114 | #endif | ||||
115 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 116 | #ifdef HAVE_SCHED_H |
117 | #include <sched.h> | ||||
118 | #endif | ||||
119 | |||||
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 120 | #ifdef HAVE_COPY_FILE_RANGE |
121 | #include <unistd.h> | ||||
122 | #endif | ||||
123 | |||||
Benjamin Peterson | 2dbda07 | 2012-03-16 10:12:55 -0500 | [diff] [blame] | 124 | #if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY) |
Benjamin Peterson | 7b51b8d | 2012-03-14 22:28:25 -0500 | [diff] [blame] | 125 | #undef HAVE_SCHED_SETAFFINITY |
126 | #endif | ||||
127 | |||||
doko@ubuntu.com | 4a173bc | 2014-04-17 19:47:16 +0200 | [diff] [blame] | 128 | #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] | 129 | #define USE_XATTRS |
130 | #endif | ||||
131 | |||||
132 | #ifdef USE_XATTRS | ||||
Benjamin Peterson | b77fe17 | 2011-09-13 17:20:47 -0400 | [diff] [blame] | 133 | #include <sys/xattr.h> |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 134 | #endif |
135 | |||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 136 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) |
137 | #ifdef HAVE_SYS_SOCKET_H | ||||
138 | #include <sys/socket.h> | ||||
139 | #endif | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 140 | #endif |
141 | |||||
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 142 | #ifdef HAVE_DLFCN_H |
143 | #include <dlfcn.h> | ||||
144 | #endif | ||||
145 | |||||
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 146 | #ifdef __hpux |
147 | #include <sys/mpctl.h> | ||||
148 | #endif | ||||
149 | |||||
150 | #if defined(__DragonFly__) || \ | ||||
151 | defined(__OpenBSD__) || \ | ||||
152 | defined(__FreeBSD__) || \ | ||||
153 | defined(__NetBSD__) || \ | ||||
154 | defined(__APPLE__) | ||||
155 | #include <sys/sysctl.h> | ||||
156 | #endif | ||||
157 | |||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 158 | #ifdef HAVE_LINUX_RANDOM_H |
159 | # include <linux/random.h> | ||||
160 | #endif | ||||
161 | #ifdef HAVE_GETRANDOM_SYSCALL | ||||
162 | # include <sys/syscall.h> | ||||
163 | #endif | ||||
164 | |||||
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 165 | #if defined(MS_WINDOWS) |
166 | # define TERMSIZE_USE_CONIO | ||||
167 | #elif defined(HAVE_SYS_IOCTL_H) | ||||
168 | # include <sys/ioctl.h> | ||||
169 | # if defined(HAVE_TERMIOS_H) | ||||
170 | # include <termios.h> | ||||
171 | # endif | ||||
172 | # if defined(TIOCGWINSZ) | ||||
173 | # define TERMSIZE_USE_IOCTL | ||||
174 | # endif | ||||
175 | #endif /* MS_WINDOWS */ | ||||
176 | |||||
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 177 | /* Various compilers have only certain posix functions */ |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 178 | /* XXX Gosh I wish these were all moved into pyconfig.h */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 179 | #if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 180 | #define HAVE_OPENDIR 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 181 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 182 | #include <process.h> |
183 | #else | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 184 | #ifdef _MSC_VER /* Microsoft compiler */ |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 185 | #define HAVE_GETPPID 1 |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 186 | #define HAVE_GETLOGIN 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 187 | #define HAVE_SPAWNV 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 188 | #define HAVE_EXECV 1 |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 189 | #define HAVE_WSPAWNV 1 |
190 | #define HAVE_WEXECV 1 | ||||
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 191 | #define HAVE_PIPE 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 192 | #define HAVE_SYSTEM 1 |
193 | #define HAVE_CWAIT 1 | ||||
194 | #define HAVE_FSYNC 1 | ||||
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 195 | #define fsync _commit |
Andrew MacIntyre | 6c73af2 | 2002-03-03 03:07:07 +0000 | [diff] [blame] | 196 | #else |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 197 | /* Unix functions that the configure script doesn't check for */ |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 198 | #ifndef __VXWORKS__ |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 199 | #define HAVE_EXECV 1 |
200 | #define HAVE_FORK 1 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 201 | #if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 202 | #define HAVE_FORK1 1 |
203 | #endif | ||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 204 | #endif |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 205 | #define HAVE_GETEGID 1 |
206 | #define HAVE_GETEUID 1 | ||||
207 | #define HAVE_GETGID 1 | ||||
208 | #define HAVE_GETPPID 1 | ||||
209 | #define HAVE_GETUID 1 | ||||
210 | #define HAVE_KILL 1 | ||||
211 | #define HAVE_OPENDIR 1 | ||||
212 | #define HAVE_PIPE 1 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 213 | #define HAVE_SYSTEM 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 214 | #define HAVE_WAIT 1 |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 215 | #define HAVE_TTYNAME 1 |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 216 | #endif /* _MSC_VER */ |
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 217 | #endif /* ! __WATCOMC__ || __QNX__ */ |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 218 | |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 219 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 220 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 221 | # one of the few times we lie about this name! |
Larry Hastings | 44e2eaa | 2013-11-23 15:37:55 -0800 | [diff] [blame] | 222 | module os |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 223 | [clinic start generated code]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 224 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/ |
Victor Stinner | a2f7c00 | 2012-02-08 03:36:25 +0100 | [diff] [blame] | 225 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 226 | #ifndef _MSC_VER |
Guido van Rossum | 36bc680 | 1995-06-14 22:54:23 +0000 | [diff] [blame] | 227 | |
Martin v. Löwis | 8eb92a0 | 2002-09-19 08:03:21 +0000 | [diff] [blame] | 228 | #if defined(__sgi)&&_COMPILER_VERSION>=700 |
229 | /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode | ||||
230 | (default) */ | ||||
231 | extern char *ctermid_r(char *); | ||||
232 | #endif | ||||
233 | |||||
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 234 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 235 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 236 | #if defined(__VXWORKS__) |
237 | #include <vxCpuLib.h> | ||||
238 | #include <rtpLib.h> | ||||
239 | #include <wait.h> | ||||
240 | #include <taskLib.h> | ||||
241 | #ifndef _P_WAIT | ||||
242 | #define _P_WAIT 0 | ||||
243 | #define _P_NOWAIT 1 | ||||
244 | #define _P_NOWAITO 1 | ||||
245 | #endif | ||||
246 | #endif /* __VXWORKS__ */ | ||||
247 | |||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 248 | #ifdef HAVE_POSIX_SPAWN |
249 | #include <spawn.h> | ||||
250 | #endif | ||||
251 | |||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 252 | #ifdef HAVE_UTIME_H |
253 | #include <utime.h> | ||||
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 254 | #endif /* HAVE_UTIME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 255 | |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 256 | #ifdef HAVE_SYS_UTIME_H |
257 | #include <sys/utime.h> | ||||
258 | #define HAVE_UTIME_H /* pretend we do for the rest of this file */ | ||||
259 | #endif /* HAVE_SYS_UTIME_H */ | ||||
260 | |||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 261 | #ifdef HAVE_SYS_TIMES_H |
262 | #include <sys/times.h> | ||||
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 263 | #endif /* HAVE_SYS_TIMES_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | |
265 | #ifdef HAVE_SYS_PARAM_H | ||||
266 | #include <sys/param.h> | ||||
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 267 | #endif /* HAVE_SYS_PARAM_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | |
269 | #ifdef HAVE_SYS_UTSNAME_H | ||||
270 | #include <sys/utsname.h> | ||||
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 271 | #endif /* HAVE_SYS_UTSNAME_H */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 273 | #ifdef HAVE_DIRENT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 274 | #include <dirent.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 275 | #define NAMLEN(dirent) strlen((dirent)->d_name) |
276 | #else | ||||
Guido van Rossum | c5a0f53 | 1997-12-02 20:36:02 +0000 | [diff] [blame] | 277 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 278 | #include <direct.h> |
279 | #define NAMLEN(dirent) strlen((dirent)->d_name) | ||||
280 | #else | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 281 | #define dirent direct |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 282 | #define NAMLEN(dirent) (dirent)->d_namlen |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 283 | #endif |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 284 | #ifdef HAVE_SYS_NDIR_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 285 | #include <sys/ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 286 | #endif |
287 | #ifdef HAVE_SYS_DIR_H | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 288 | #include <sys/dir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 289 | #endif |
290 | #ifdef HAVE_NDIR_H | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 291 | #include <ndir.h> |
Guido van Rossum | 3bbc62e | 1995-01-02 19:30:30 +0000 | [diff] [blame] | 292 | #endif |
293 | #endif | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 295 | #ifdef _MSC_VER |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 296 | #ifdef HAVE_DIRECT_H |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 297 | #include <direct.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 298 | #endif |
299 | #ifdef HAVE_IO_H | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 300 | #include <io.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 301 | #endif |
302 | #ifdef HAVE_PROCESS_H | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 303 | #include <process.h> |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 304 | #endif |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 305 | #ifndef IO_REPARSE_TAG_SYMLINK |
Amaury Forgeot d'Arc | 844807e | 2010-08-16 22:16:51 +0000 | [diff] [blame] | 306 | #define IO_REPARSE_TAG_SYMLINK (0xA000000CL) |
Raymond Hettinger | 0291c9f | 2010-08-01 21:10:35 +0000 | [diff] [blame] | 307 | #endif |
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 308 | #ifndef IO_REPARSE_TAG_MOUNT_POINT |
309 | #define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L) | ||||
310 | #endif | ||||
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 311 | #include "osdefs.h" |
Kristján Valur Jónsson | f64e651 | 2009-04-13 10:16:14 +0000 | [diff] [blame] | 312 | #include <malloc.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 313 | #include <windows.h> |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 314 | #include <shellapi.h> /* for ShellExecute() */ |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 315 | #include <lmcons.h> /* for UNLEN */ |
Brian Curtin | 52173d4 | 2010-12-02 18:29:18 +0000 | [diff] [blame] | 316 | #define HAVE_SYMLINK |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 317 | #endif /* _MSC_VER */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 319 | #ifndef MAXPATHLEN |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 320 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
321 | #define MAXPATHLEN PATH_MAX | ||||
322 | #else | ||||
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 323 | #define MAXPATHLEN 1024 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 324 | #endif |
Tim Peters | bc2e10e | 2002-03-03 23:17:02 +0000 | [diff] [blame] | 325 | #endif /* MAXPATHLEN */ |
326 | |||||
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 327 | #ifdef UNION_WAIT |
328 | /* Emulate some macros on systems that have a union instead of macros */ | ||||
329 | |||||
330 | #ifndef WIFEXITED | ||||
331 | #define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump) | ||||
332 | #endif | ||||
333 | |||||
334 | #ifndef WEXITSTATUS | ||||
335 | #define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1) | ||||
336 | #endif | ||||
337 | |||||
338 | #ifndef WTERMSIG | ||||
339 | #define WTERMSIG(u_wait) ((u_wait).w_termsig) | ||||
340 | #endif | ||||
341 | |||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 342 | #define WAIT_TYPE union wait |
343 | #define WAIT_STATUS_INT(s) (s.w_status) | ||||
344 | |||||
345 | #else /* !UNION_WAIT */ | ||||
346 | #define WAIT_TYPE int | ||||
347 | #define WAIT_STATUS_INT(s) (s) | ||||
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 348 | #endif /* UNION_WAIT */ |
349 | |||||
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 350 | /* Don't use the "_r" form if we don't need it (also, won't have a |
351 | prototype for it, at least on Solaris -- maybe others as well?). */ | ||||
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 352 | #if defined(HAVE_CTERMID_R) |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 353 | #define USE_CTERMID_R |
354 | #endif | ||||
355 | |||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 356 | /* choose the appropriate stat and fstat functions and return structs */ |
Guido van Rossum | 64529cd | 2000-06-30 22:45:12 +0000 | [diff] [blame] | 357 | #undef STAT |
Antoine Pitrou | e47e093 | 2011-01-19 15:21:35 +0000 | [diff] [blame] | 358 | #undef FSTAT |
359 | #undef STRUCT_STAT | ||||
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 360 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 361 | # define STAT win32_stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 362 | # define LSTAT win32_lstat |
Victor Stinner | e134a7f | 2015-03-30 10:09:31 +0200 | [diff] [blame] | 363 | # define FSTAT _Py_fstat_noraise |
Steve Dower | f2f373f | 2015-02-21 08:44:05 -0800 | [diff] [blame] | 364 | # define STRUCT_STAT struct _Py_stat_struct |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 365 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 366 | # define STAT stat |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 367 | # define LSTAT lstat |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 368 | # define FSTAT fstat |
369 | # define STRUCT_STAT struct stat | ||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 370 | #endif |
371 | |||||
Tim Peters | 11b2306 | 2003-04-23 02:39:17 +0000 | [diff] [blame] | 372 | #if defined(MAJOR_IN_MKDEV) |
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 373 | #include <sys/mkdev.h> |
374 | #else | ||||
375 | #if defined(MAJOR_IN_SYSMACROS) | ||||
376 | #include <sys/sysmacros.h> | ||||
377 | #endif | ||||
Neal Norwitz | 3d94942 | 2002-04-20 13:46:43 +0000 | [diff] [blame] | 378 | #if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H) |
379 | #include <sys/mkdev.h> | ||||
380 | #endif | ||||
Martin v. Löwis | dbe3f76 | 2002-10-10 14:27:30 +0000 | [diff] [blame] | 381 | #endif |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 382 | |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 383 | #ifdef MS_WINDOWS |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 384 | #define INITFUNC PyInit_nt |
385 | #define MODNAME "nt" | ||||
386 | #else | ||||
387 | #define INITFUNC PyInit_posix | ||||
388 | #define MODNAME "posix" | ||||
389 | #endif | ||||
390 | |||||
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 391 | #if defined(__sun) |
392 | /* Something to implement in autoconf, not present in autoconf 2.69 */ | ||||
393 | #define HAVE_STRUCT_STAT_ST_FSTYPE 1 | ||||
394 | #endif | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 395 | |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 396 | /* memfd_create is either defined in sys/mman.h or sys/memfd.h |
397 | * linux/memfd.h defines additional flags | ||||
398 | */ | ||||
399 | #ifdef HAVE_SYS_MMAN_H | ||||
400 | #include <sys/mman.h> | ||||
401 | #endif | ||||
402 | #ifdef HAVE_SYS_MEMFD_H | ||||
403 | #include <sys/memfd.h> | ||||
404 | #endif | ||||
405 | #ifdef HAVE_LINUX_MEMFD_H | ||||
406 | #include <linux/memfd.h> | ||||
407 | #endif | ||||
408 | |||||
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 409 | #ifdef _Py_MEMORY_SANITIZER |
410 | # include <sanitizer/msan_interface.h> | ||||
411 | #endif | ||||
412 | |||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 413 | #ifdef HAVE_FORK |
414 | static void | ||||
415 | run_at_forkers(PyObject *lst, int reverse) | ||||
416 | { | ||||
417 | Py_ssize_t i; | ||||
418 | PyObject *cpy; | ||||
419 | |||||
420 | if (lst != NULL) { | ||||
421 | assert(PyList_CheckExact(lst)); | ||||
422 | |||||
423 | /* Use a list copy in case register_at_fork() is called from | ||||
424 | * one of the callbacks. | ||||
425 | */ | ||||
426 | cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst)); | ||||
427 | if (cpy == NULL) | ||||
428 | PyErr_WriteUnraisable(lst); | ||||
429 | else { | ||||
430 | if (reverse) | ||||
431 | PyList_Reverse(cpy); | ||||
432 | for (i = 0; i < PyList_GET_SIZE(cpy); i++) { | ||||
433 | PyObject *func, *res; | ||||
434 | func = PyList_GET_ITEM(cpy, i); | ||||
435 | res = PyObject_CallObject(func, NULL); | ||||
436 | if (res == NULL) | ||||
437 | PyErr_WriteUnraisable(func); | ||||
438 | else | ||||
439 | Py_DECREF(res); | ||||
440 | } | ||||
441 | Py_DECREF(cpy); | ||||
442 | } | ||||
443 | } | ||||
444 | } | ||||
445 | |||||
446 | void | ||||
447 | PyOS_BeforeFork(void) | ||||
448 | { | ||||
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 449 | run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 450 | |
451 | _PyImport_AcquireLock(); | ||||
452 | } | ||||
453 | |||||
454 | void | ||||
455 | PyOS_AfterFork_Parent(void) | ||||
456 | { | ||||
457 | if (_PyImport_ReleaseLock() <= 0) | ||||
458 | Py_FatalError("failed releasing import lock after fork"); | ||||
459 | |||||
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 460 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 461 | } |
462 | |||||
463 | void | ||||
464 | PyOS_AfterFork_Child(void) | ||||
465 | { | ||||
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 466 | _PyRuntimeState *runtime = &_PyRuntime; |
467 | _PyGILState_Reinit(runtime); | ||||
Victor Stinner | d5d9e81 | 2019-05-13 12:35:37 +0200 | [diff] [blame] | 468 | _PyEval_ReInitThreads(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 469 | _PyImport_ReInitLock(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 470 | _PySignal_AfterFork(); |
Victor Stinner | b930a2d | 2019-04-24 17:14:33 +0200 | [diff] [blame] | 471 | _PyRuntimeState_ReInitThreads(runtime); |
Victor Stinner | b49858b | 2019-05-24 15:20:23 +0200 | [diff] [blame] | 472 | _PyInterpreterState_DeleteExceptMain(runtime); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 473 | |
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 474 | run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 475 | } |
476 | |||||
477 | static int | ||||
478 | register_at_forker(PyObject **lst, PyObject *func) | ||||
479 | { | ||||
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 480 | if (func == NULL) /* nothing to register? do nothing. */ |
481 | return 0; | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 482 | if (*lst == NULL) { |
483 | *lst = PyList_New(0); | ||||
484 | if (*lst == NULL) | ||||
485 | return -1; | ||||
486 | } | ||||
487 | return PyList_Append(*lst, func); | ||||
488 | } | ||||
489 | #endif | ||||
490 | |||||
491 | /* Legacy wrapper */ | ||||
492 | void | ||||
493 | PyOS_AfterFork(void) | ||||
494 | { | ||||
495 | #ifdef HAVE_FORK | ||||
496 | PyOS_AfterFork_Child(); | ||||
497 | #endif | ||||
498 | } | ||||
499 | |||||
500 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 501 | #ifdef MS_WINDOWS |
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 502 | /* defined in fileutils.c */ |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 503 | void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *); |
504 | void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *, | ||||
Serhiy Storchaka | 06a13f8 | 2015-02-22 21:34:54 +0200 | [diff] [blame] | 505 | ULONG, struct _Py_stat_struct *); |
506 | #endif | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 507 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 508 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 509 | #ifndef MS_WINDOWS |
510 | PyObject * | ||||
511 | _PyLong_FromUid(uid_t uid) | ||||
512 | { | ||||
513 | if (uid == (uid_t)-1) | ||||
514 | return PyLong_FromLong(-1); | ||||
515 | return PyLong_FromUnsignedLong(uid); | ||||
516 | } | ||||
517 | |||||
518 | PyObject * | ||||
519 | _PyLong_FromGid(gid_t gid) | ||||
520 | { | ||||
521 | if (gid == (gid_t)-1) | ||||
522 | return PyLong_FromLong(-1); | ||||
523 | return PyLong_FromUnsignedLong(gid); | ||||
524 | } | ||||
525 | |||||
526 | int | ||||
527 | _Py_Uid_Converter(PyObject *obj, void *p) | ||||
528 | { | ||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 529 | uid_t uid; |
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 | "uid 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 uid_t is complicated for two reasons: | ||||
545 | * * Although uid_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 | uid = (uid_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 | /* Ensure the value wasn't truncated. */ | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 573 | if (sizeof(uid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 574 | (long)uid != result) |
575 | goto underflow; | ||||
576 | goto success; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 577 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 578 | |
579 | if (overflow < 0) | ||||
580 | goto underflow; | ||||
581 | |||||
582 | /* | ||||
583 | * Okay, the value overflowed a signed long. If it | ||||
584 | * fits in an *unsigned* long, it may still be okay, | ||||
585 | * as uid_t may be unsigned long on this platform. | ||||
586 | */ | ||||
587 | uresult = PyLong_AsUnsignedLong(index); | ||||
588 | if (PyErr_Occurred()) { | ||||
589 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) | ||||
590 | goto overflow; | ||||
591 | goto fail; | ||||
592 | } | ||||
593 | |||||
594 | uid = (uid_t)uresult; | ||||
595 | |||||
596 | /* | ||||
597 | * If uid == (uid_t)-1, the user actually passed in ULONG_MAX, | ||||
598 | * but this value would get interpreted as (uid_t)-1 by chown | ||||
599 | * and its siblings. That's not what the user meant! So we | ||||
600 | * throw an overflow exception instead. (We already | ||||
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 601 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 602 | */ |
603 | if (uid == (uid_t)-1) | ||||
604 | goto overflow; | ||||
605 | |||||
606 | /* Ensure the value wasn't truncated. */ | ||||
607 | if (sizeof(uid_t) < sizeof(long) && | ||||
608 | (unsigned long)uid != uresult) | ||||
609 | goto overflow; | ||||
610 | /* fallthrough */ | ||||
611 | |||||
612 | success: | ||||
613 | Py_DECREF(index); | ||||
614 | *(uid_t *)p = uid; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 615 | return 1; |
616 | |||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 617 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 618 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 619 | "uid is less than minimum"); |
620 | goto fail; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 621 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 622 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 623 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 624 | "uid is greater than maximum"); |
625 | /* fallthrough */ | ||||
626 | |||||
627 | fail: | ||||
628 | Py_DECREF(index); | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 629 | return 0; |
630 | } | ||||
631 | |||||
632 | int | ||||
633 | _Py_Gid_Converter(PyObject *obj, void *p) | ||||
634 | { | ||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 635 | gid_t gid; |
636 | PyObject *index; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 637 | int overflow; |
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 638 | long result; |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 639 | unsigned long uresult; |
640 | |||||
641 | index = PyNumber_Index(obj); | ||||
642 | if (index == NULL) { | ||||
643 | PyErr_Format(PyExc_TypeError, | ||||
644 | "gid should be integer, not %.200s", | ||||
645 | Py_TYPE(obj)->tp_name); | ||||
Serhiy Storchaka | b462189 | 2013-02-10 23:28:02 +0200 | [diff] [blame] | 646 | return 0; |
647 | } | ||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 648 | |
649 | /* | ||||
650 | * Handling gid_t is complicated for two reasons: | ||||
651 | * * Although gid_t is (always?) unsigned, it still | ||||
652 | * accepts -1. | ||||
653 | * * We don't know its size in advance--it may be | ||||
654 | * bigger than an int, or it may be smaller than | ||||
655 | * a long. | ||||
656 | * | ||||
657 | * So a bit of defensive programming is in order. | ||||
658 | * Start with interpreting the value passed | ||||
659 | * in as a signed long and see if it works. | ||||
660 | */ | ||||
661 | |||||
662 | result = PyLong_AsLongAndOverflow(index, &overflow); | ||||
663 | |||||
664 | if (!overflow) { | ||||
665 | gid = (gid_t)result; | ||||
666 | |||||
667 | if (result == -1) { | ||||
668 | if (PyErr_Occurred()) | ||||
669 | goto fail; | ||||
670 | /* It's a legitimate -1, we're done. */ | ||||
671 | goto success; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 672 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 673 | |
674 | /* Any other negative number is disallowed. */ | ||||
675 | if (result < 0) { | ||||
676 | goto underflow; | ||||
677 | } | ||||
678 | |||||
679 | /* Ensure the value wasn't truncated. */ | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 680 | if (sizeof(gid_t) < sizeof(long) && |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 681 | (long)gid != result) |
682 | goto underflow; | ||||
683 | goto success; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 684 | } |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 685 | |
686 | if (overflow < 0) | ||||
687 | goto underflow; | ||||
688 | |||||
689 | /* | ||||
690 | * Okay, the value overflowed a signed long. If it | ||||
691 | * fits in an *unsigned* long, it may still be okay, | ||||
692 | * as gid_t may be unsigned long on this platform. | ||||
693 | */ | ||||
694 | uresult = PyLong_AsUnsignedLong(index); | ||||
695 | if (PyErr_Occurred()) { | ||||
696 | if (PyErr_ExceptionMatches(PyExc_OverflowError)) | ||||
697 | goto overflow; | ||||
698 | goto fail; | ||||
699 | } | ||||
700 | |||||
701 | gid = (gid_t)uresult; | ||||
702 | |||||
703 | /* | ||||
704 | * If gid == (gid_t)-1, the user actually passed in ULONG_MAX, | ||||
705 | * but this value would get interpreted as (gid_t)-1 by chown | ||||
706 | * and its siblings. That's not what the user meant! So we | ||||
707 | * throw an overflow exception instead. (We already | ||||
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 708 | * handled a real -1 with PyLong_AsLongAndOverflow() above.) |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 709 | */ |
710 | if (gid == (gid_t)-1) | ||||
711 | goto overflow; | ||||
712 | |||||
713 | /* Ensure the value wasn't truncated. */ | ||||
714 | if (sizeof(gid_t) < sizeof(long) && | ||||
715 | (unsigned long)gid != uresult) | ||||
716 | goto overflow; | ||||
717 | /* fallthrough */ | ||||
718 | |||||
719 | success: | ||||
720 | Py_DECREF(index); | ||||
721 | *(gid_t *)p = gid; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 722 | return 1; |
723 | |||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 724 | underflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 725 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 726 | "gid is less than minimum"); |
727 | goto fail; | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 728 | |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 729 | overflow: |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 730 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 731 | "gid is greater than maximum"); |
732 | /* fallthrough */ | ||||
733 | |||||
734 | fail: | ||||
735 | Py_DECREF(index); | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 736 | return 0; |
737 | } | ||||
738 | #endif /* MS_WINDOWS */ | ||||
739 | |||||
740 | |||||
Benjamin Peterson | ed4aa83 | 2016-09-05 17:44:18 -0700 | [diff] [blame] | 741 | #define _PyLong_FromDev PyLong_FromLongLong |
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 742 | |
743 | |||||
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 744 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) |
745 | static int | ||||
746 | _Py_Dev_Converter(PyObject *obj, void *p) | ||||
747 | { | ||||
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 748 | *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj); |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 749 | if (PyErr_Occurred()) |
750 | return 0; | ||||
751 | return 1; | ||||
752 | } | ||||
Gregory P. Smith | 702dada | 2015-01-28 16:07:52 -0800 | [diff] [blame] | 753 | #endif /* HAVE_MKNOD && HAVE_MAKEDEV */ |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 754 | |
755 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 756 | #ifdef AT_FDCWD |
Trent Nelson | 9a46105 | 2012-09-18 21:50:06 -0400 | [diff] [blame] | 757 | /* |
758 | * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965); | ||||
759 | * without the int cast, the value gets interpreted as uint (4291925331), | ||||
760 | * which doesn't play nicely with all the initializer lines in this file that | ||||
761 | * look like this: | ||||
762 | * int dir_fd = DEFAULT_DIR_FD; | ||||
763 | */ | ||||
764 | #define DEFAULT_DIR_FD (int)AT_FDCWD | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 765 | #else |
766 | #define DEFAULT_DIR_FD (-100) | ||||
767 | #endif | ||||
768 | |||||
769 | static int | ||||
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 770 | _fd_converter(PyObject *o, int *p) |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 771 | { |
772 | int overflow; | ||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 773 | long long_value; |
774 | |||||
775 | PyObject *index = PyNumber_Index(o); | ||||
776 | if (index == NULL) { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 777 | return 0; |
778 | } | ||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 779 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 780 | assert(PyLong_Check(index)); |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 781 | long_value = PyLong_AsLongAndOverflow(index, &overflow); |
782 | Py_DECREF(index); | ||||
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 783 | assert(!PyErr_Occurred()); |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 784 | if (overflow > 0 || long_value > INT_MAX) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 785 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 786 | "fd is greater than maximum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 787 | return 0; |
788 | } | ||||
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 789 | if (overflow < 0 || long_value < INT_MIN) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 790 | PyErr_SetString(PyExc_OverflowError, |
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 791 | "fd is less than minimum"); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 792 | return 0; |
793 | } | ||||
Larry Hastings | a27b83a | 2013-08-08 00:19:50 -0700 | [diff] [blame] | 794 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 795 | *p = (int)long_value; |
796 | return 1; | ||||
797 | } | ||||
798 | |||||
799 | static int | ||||
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 800 | dir_fd_converter(PyObject *o, void *p) |
801 | { | ||||
802 | if (o == Py_None) { | ||||
803 | *(int *)p = DEFAULT_DIR_FD; | ||||
804 | return 1; | ||||
805 | } | ||||
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 806 | else if (PyIndex_Check(o)) { |
807 | return _fd_converter(o, (int *)p); | ||||
808 | } | ||||
809 | else { | ||||
810 | PyErr_Format(PyExc_TypeError, | ||||
811 | "argument should be integer or None, not %.200s", | ||||
812 | Py_TYPE(o)->tp_name); | ||||
813 | return 0; | ||||
814 | } | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 815 | } |
816 | |||||
817 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 818 | /* |
819 | * A PyArg_ParseTuple "converter" function | ||||
820 | * that handles filesystem paths in the manner | ||||
821 | * preferred by the os module. | ||||
822 | * | ||||
823 | * path_converter accepts (Unicode) strings and their | ||||
824 | * subclasses, and bytes and their subclasses. What | ||||
825 | * it does with the argument depends on the platform: | ||||
826 | * | ||||
827 | * * On Windows, if we get a (Unicode) string we | ||||
828 | * extract the wchar_t * and return it; if we get | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 829 | * bytes we decode to wchar_t * and return that. |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 830 | * |
831 | * * On all other platforms, strings are encoded | ||||
832 | * to bytes using PyUnicode_FSConverter, then we | ||||
833 | * extract the char * from the bytes object and | ||||
834 | * return that. | ||||
835 | * | ||||
836 | * path_converter also optionally accepts signed | ||||
837 | * integers (representing open file descriptors) instead | ||||
838 | * of path strings. | ||||
839 | * | ||||
840 | * Input fields: | ||||
841 | * path.nullable | ||||
842 | * If nonzero, the path is permitted to be None. | ||||
843 | * path.allow_fd | ||||
844 | * If nonzero, the path is permitted to be a file handle | ||||
845 | * (a signed int) instead of a string. | ||||
846 | * path.function_name | ||||
847 | * If non-NULL, path_converter will use that as the name | ||||
848 | * of the function in error messages. | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 849 | * (If path.function_name is NULL it omits the function name.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 850 | * path.argument_name |
851 | * If non-NULL, path_converter will use that as the name | ||||
852 | * of the parameter in error messages. | ||||
853 | * (If path.argument_name is NULL it uses "path".) | ||||
854 | * | ||||
855 | * Output fields: | ||||
856 | * path.wide | ||||
857 | * Points to the path if it was expressed as Unicode | ||||
858 | * and was not encoded. (Only used on Windows.) | ||||
859 | * path.narrow | ||||
860 | * Points to the path if it was expressed as bytes, | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 861 | * or it was Unicode and was encoded to bytes. (On Windows, |
Martin Panter | b1321fb | 2016-10-10 00:38:21 +0000 | [diff] [blame] | 862 | * is a non-zero integer if the path was expressed as bytes. |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 863 | * The type is deliberately incompatible to prevent misuse.) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 864 | * path.fd |
865 | * Contains a file descriptor if path.accept_fd was true | ||||
866 | * and the caller provided a signed integer instead of any | ||||
867 | * sort of string. | ||||
868 | * | ||||
869 | * WARNING: if your "path" parameter is optional, and is | ||||
870 | * unspecified, path_converter will never get called. | ||||
871 | * So if you set allow_fd, you *MUST* initialize path.fd = -1 | ||||
872 | * yourself! | ||||
873 | * path.length | ||||
874 | * The length of the path in characters, if specified as | ||||
875 | * a string. | ||||
876 | * path.object | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 877 | * The original object passed in (if get a PathLike object, |
878 | * the result of PyOS_FSPath() is treated as the original object). | ||||
879 | * Own a reference to the object. | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 880 | * path.cleanup |
881 | * For internal use only. May point to a temporary object. | ||||
882 | * (Pay no attention to the man behind the curtain.) | ||||
883 | * | ||||
884 | * At most one of path.wide or path.narrow will be non-NULL. | ||||
885 | * If path was None and path.nullable was set, | ||||
886 | * or if path was an integer and path.allow_fd was set, | ||||
887 | * both path.wide and path.narrow will be NULL | ||||
888 | * and path.length will be 0. | ||||
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 889 | * |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 890 | * path_converter takes care to not write to the path_t |
891 | * unless it's successful. However it must reset the | ||||
892 | * "cleanup" field each time it's called. | ||||
893 | * | ||||
894 | * Use as follows: | ||||
895 | * path_t path; | ||||
896 | * memset(&path, 0, sizeof(path)); | ||||
897 | * PyArg_ParseTuple(args, "O&", path_converter, &path); | ||||
898 | * // ... use values from path ... | ||||
899 | * path_cleanup(&path); | ||||
900 | * | ||||
901 | * (Note that if PyArg_Parse fails you don't need to call | ||||
902 | * path_cleanup(). However it is safe to do so.) | ||||
903 | */ | ||||
904 | typedef struct { | ||||
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 905 | const char *function_name; |
906 | const char *argument_name; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 907 | int nullable; |
908 | int allow_fd; | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 909 | const wchar_t *wide; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 910 | #ifdef MS_WINDOWS |
911 | BOOL narrow; | ||||
912 | #else | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 913 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 914 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 915 | int fd; |
916 | Py_ssize_t length; | ||||
917 | PyObject *object; | ||||
918 | PyObject *cleanup; | ||||
919 | } path_t; | ||||
920 | |||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 921 | #ifdef MS_WINDOWS |
922 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ | ||||
923 | {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL} | ||||
924 | #else | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 925 | #define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \ |
926 | {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL} | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 927 | #endif |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 928 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 929 | static void |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 930 | path_cleanup(path_t *path) |
931 | { | ||||
932 | Py_CLEAR(path->object); | ||||
933 | Py_CLEAR(path->cleanup); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 934 | } |
935 | |||||
936 | static int | ||||
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 937 | path_converter(PyObject *o, void *p) |
938 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 939 | path_t *path = (path_t *)p; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 940 | PyObject *bytes = NULL; |
941 | Py_ssize_t length = 0; | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 942 | int is_index, is_buffer, is_bytes, is_unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 943 | const char *narrow; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 944 | #ifdef MS_WINDOWS |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 945 | PyObject *wo = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 946 | const wchar_t *wide; |
947 | #endif | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 948 | |
949 | #define FORMAT_EXCEPTION(exc, fmt) \ | ||||
950 | PyErr_Format(exc, "%s%s" fmt, \ | ||||
951 | path->function_name ? path->function_name : "", \ | ||||
952 | path->function_name ? ": " : "", \ | ||||
953 | path->argument_name ? path->argument_name : "path") | ||||
954 | |||||
955 | /* Py_CLEANUP_SUPPORTED support */ | ||||
956 | if (o == NULL) { | ||||
957 | path_cleanup(path); | ||||
958 | return 1; | ||||
959 | } | ||||
960 | |||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 961 | /* Ensure it's always safe to call path_cleanup(). */ |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 962 | path->object = path->cleanup = NULL; |
963 | /* path->object owns a reference to the original object */ | ||||
964 | Py_INCREF(o); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 965 | |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 966 | if ((o == Py_None) && path->nullable) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 967 | path->wide = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 968 | #ifdef MS_WINDOWS |
969 | path->narrow = FALSE; | ||||
970 | #else | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 971 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 972 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 973 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 974 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 975 | } |
976 | |||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 977 | /* Only call this here so that we don't treat the return value of |
978 | os.fspath() as an fd or buffer. */ | ||||
979 | is_index = path->allow_fd && PyIndex_Check(o); | ||||
980 | is_buffer = PyObject_CheckBuffer(o); | ||||
981 | is_bytes = PyBytes_Check(o); | ||||
982 | is_unicode = PyUnicode_Check(o); | ||||
983 | |||||
984 | if (!is_index && !is_buffer && !is_unicode && !is_bytes) { | ||||
985 | /* Inline PyOS_FSPath() for better error messages. */ | ||||
986 | _Py_IDENTIFIER(__fspath__); | ||||
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 987 | PyObject *func, *res; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 988 | |
989 | func = _PyObject_LookupSpecial(o, &PyId___fspath__); | ||||
990 | if (NULL == func) { | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 991 | goto error_format; |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 992 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 993 | res = _PyObject_CallNoArg(func); |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 994 | Py_DECREF(func); |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 995 | if (NULL == res) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 996 | goto error_exit; |
997 | } | ||||
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 998 | else if (PyUnicode_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 999 | is_unicode = 1; |
1000 | } | ||||
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1001 | else if (PyBytes_Check(res)) { |
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1002 | is_bytes = 1; |
1003 | } | ||||
1004 | else { | ||||
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1005 | PyErr_Format(PyExc_TypeError, |
1006 | "expected %.200s.__fspath__() to return str or bytes, " | ||||
1007 | "not %.200s", Py_TYPE(o)->tp_name, | ||||
1008 | Py_TYPE(res)->tp_name); | ||||
1009 | Py_DECREF(res); | ||||
1010 | goto error_exit; | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1011 | } |
Pablo Galindo | 09fbcd6 | 2019-02-18 10:46:34 +0000 | [diff] [blame] | 1012 | |
1013 | /* still owns a reference to the original object */ | ||||
1014 | Py_DECREF(o); | ||||
1015 | o = res; | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1016 | } |
1017 | |||||
1018 | if (is_unicode) { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1019 | #ifdef MS_WINDOWS |
Victor Stinner | 26c03bd | 2016-09-19 11:55:44 +0200 | [diff] [blame] | 1020 | wide = PyUnicode_AsUnicodeAndSize(o, &length); |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1021 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1022 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1023 | } |
Victor Stinner | 59799a8 | 2013-11-13 14:17:30 +0100 | [diff] [blame] | 1024 | if (length > 32767) { |
1025 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1026 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1027 | } |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1028 | if (wcslen(wide) != length) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1029 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1030 | goto error_exit; |
Serhiy Storchaka | 2b0d200 | 2015-04-20 09:53:58 +0300 | [diff] [blame] | 1031 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1032 | |
1033 | path->wide = wide; | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1034 | path->narrow = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1035 | path->fd = -1; |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1036 | goto success_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1037 | #else |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1038 | if (!PyUnicode_FSConverter(o, &bytes)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1039 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1040 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1041 | #endif |
1042 | } | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1043 | else if (is_bytes) { |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1044 | bytes = o; |
1045 | Py_INCREF(bytes); | ||||
1046 | } | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1047 | else if (is_buffer) { |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 1048 | /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code |
Ville Skyttä | 49b2734 | 2017-08-03 09:00:59 +0300 | [diff] [blame] | 1049 | after removing support of non-bytes buffer objects. */ |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1050 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
1051 | "%s%s%s should be %s, not %.200s", | ||||
1052 | path->function_name ? path->function_name : "", | ||||
1053 | path->function_name ? ": " : "", | ||||
1054 | path->argument_name ? path->argument_name : "path", | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1055 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
1056 | "integer or None" : | ||||
1057 | path->allow_fd ? "string, bytes, os.PathLike or integer" : | ||||
1058 | path->nullable ? "string, bytes, os.PathLike or None" : | ||||
1059 | "string, bytes or os.PathLike", | ||||
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1060 | Py_TYPE(o)->tp_name)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1061 | goto error_exit; |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1062 | } |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1063 | bytes = PyBytes_FromObject(o); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1064 | if (!bytes) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1065 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1066 | } |
1067 | } | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1068 | else if (is_index) { |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1069 | if (!_fd_converter(o, &path->fd)) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1070 | goto error_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1071 | } |
1072 | path->wide = NULL; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1073 | #ifdef MS_WINDOWS |
1074 | path->narrow = FALSE; | ||||
1075 | #else | ||||
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1076 | path->narrow = NULL; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1077 | #endif |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1078 | goto success_exit; |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1079 | } |
1080 | else { | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1081 | error_format: |
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1082 | PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s", |
1083 | path->function_name ? path->function_name : "", | ||||
1084 | path->function_name ? ": " : "", | ||||
1085 | path->argument_name ? path->argument_name : "path", | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 1086 | path->allow_fd && path->nullable ? "string, bytes, os.PathLike, " |
1087 | "integer or None" : | ||||
1088 | path->allow_fd ? "string, bytes, os.PathLike or integer" : | ||||
1089 | path->nullable ? "string, bytes, os.PathLike or None" : | ||||
1090 | "string, bytes or os.PathLike", | ||||
Serhiy Storchaka | 819399b | 2016-04-06 22:17:52 +0300 | [diff] [blame] | 1091 | Py_TYPE(o)->tp_name); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1092 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1093 | } |
1094 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1095 | length = PyBytes_GET_SIZE(bytes); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1096 | narrow = PyBytes_AS_STRING(bytes); |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1097 | if ((size_t)length != strlen(narrow)) { |
Serhiy Storchaka | d8a1447 | 2014-09-06 20:07:17 +0300 | [diff] [blame] | 1098 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1099 | goto error_exit; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1100 | } |
1101 | |||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1102 | #ifdef MS_WINDOWS |
1103 | wo = PyUnicode_DecodeFSDefaultAndSize( | ||||
1104 | narrow, | ||||
1105 | length | ||||
1106 | ); | ||||
1107 | if (!wo) { | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1108 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1109 | } |
1110 | |||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1111 | wide = PyUnicode_AsUnicodeAndSize(wo, &length); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1112 | if (!wide) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1113 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1114 | } |
1115 | if (length > 32767) { | ||||
1116 | FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows"); | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1117 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1118 | } |
1119 | if (wcslen(wide) != length) { | ||||
1120 | FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s"); | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1121 | goto error_exit; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1122 | } |
1123 | path->wide = wide; | ||||
1124 | path->narrow = TRUE; | ||||
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1125 | path->cleanup = wo; |
1126 | Py_DECREF(bytes); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1127 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1128 | path->wide = NULL; |
1129 | path->narrow = narrow; | ||||
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1130 | if (bytes == o) { |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1131 | /* Still a reference owned by path->object, don't have to |
1132 | worry about path->narrow is used after free. */ | ||||
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1133 | Py_DECREF(bytes); |
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1134 | } |
1135 | else { | ||||
1136 | path->cleanup = bytes; | ||||
Serhiy Storchaka | d73c318 | 2016-08-06 23:22:08 +0300 | [diff] [blame] | 1137 | } |
Xiang Zhang | 04316c4 | 2017-01-08 23:26:57 +0800 | [diff] [blame] | 1138 | #endif |
1139 | path->fd = -1; | ||||
1140 | |||||
1141 | success_exit: | ||||
1142 | path->length = length; | ||||
1143 | path->object = o; | ||||
1144 | return Py_CLEANUP_SUPPORTED; | ||||
1145 | |||||
1146 | error_exit: | ||||
1147 | Py_XDECREF(o); | ||||
1148 | Py_XDECREF(bytes); | ||||
1149 | #ifdef MS_WINDOWS | ||||
1150 | Py_XDECREF(wo); | ||||
1151 | #endif | ||||
1152 | return 0; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1153 | } |
1154 | |||||
1155 | static void | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1156 | argument_unavailable_error(const char *function_name, const char *argument_name) |
1157 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1158 | PyErr_Format(PyExc_NotImplementedError, |
1159 | "%s%s%s unavailable on this platform", | ||||
1160 | (function_name != NULL) ? function_name : "", | ||||
1161 | (function_name != NULL) ? ": ": "", | ||||
1162 | argument_name); | ||||
1163 | } | ||||
1164 | |||||
1165 | static int | ||||
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1166 | dir_fd_unavailable(PyObject *o, void *p) |
1167 | { | ||||
1168 | int dir_fd; | ||||
1169 | if (!dir_fd_converter(o, &dir_fd)) | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1170 | return 0; |
Serhiy Storchaka | a2ad5c3 | 2013-01-07 23:13:46 +0200 | [diff] [blame] | 1171 | if (dir_fd != DEFAULT_DIR_FD) { |
1172 | argument_unavailable_error(NULL, "dir_fd"); | ||||
1173 | return 0; | ||||
1174 | } | ||||
1175 | *(int *)p = dir_fd; | ||||
1176 | return 1; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1177 | } |
1178 | |||||
1179 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1180 | fd_specified(const char *function_name, int fd) |
1181 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1182 | if (fd == -1) |
1183 | return 0; | ||||
1184 | |||||
1185 | argument_unavailable_error(function_name, "fd"); | ||||
1186 | return 1; | ||||
1187 | } | ||||
1188 | |||||
1189 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1190 | follow_symlinks_specified(const char *function_name, int follow_symlinks) |
1191 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1192 | if (follow_symlinks) |
1193 | return 0; | ||||
1194 | |||||
1195 | argument_unavailable_error(function_name, "follow_symlinks"); | ||||
1196 | return 1; | ||||
1197 | } | ||||
1198 | |||||
1199 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1200 | path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd) |
1201 | { | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1202 | if (!path->wide && (dir_fd != DEFAULT_DIR_FD) |
1203 | #ifndef MS_WINDOWS | ||||
1204 | && !path->narrow | ||||
1205 | #endif | ||||
1206 | ) { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1207 | PyErr_Format(PyExc_ValueError, |
1208 | "%s: can't specify dir_fd without matching path", | ||||
1209 | function_name); | ||||
1210 | return 1; | ||||
1211 | } | ||||
1212 | return 0; | ||||
1213 | } | ||||
1214 | |||||
1215 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1216 | dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd) |
1217 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1218 | if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) { |
1219 | PyErr_Format(PyExc_ValueError, | ||||
1220 | "%s: can't specify both dir_fd and fd", | ||||
1221 | function_name); | ||||
1222 | return 1; | ||||
1223 | } | ||||
1224 | return 0; | ||||
1225 | } | ||||
1226 | |||||
1227 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1228 | fd_and_follow_symlinks_invalid(const char *function_name, int fd, |
1229 | int follow_symlinks) | ||||
1230 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1231 | if ((fd > 0) && (!follow_symlinks)) { |
1232 | PyErr_Format(PyExc_ValueError, | ||||
1233 | "%s: cannot use fd and follow_symlinks together", | ||||
1234 | function_name); | ||||
1235 | return 1; | ||||
1236 | } | ||||
1237 | return 0; | ||||
1238 | } | ||||
1239 | |||||
1240 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1241 | dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd, |
1242 | int follow_symlinks) | ||||
1243 | { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1244 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { |
1245 | PyErr_Format(PyExc_ValueError, | ||||
1246 | "%s: cannot use dir_fd and follow_symlinks together", | ||||
1247 | function_name); | ||||
1248 | return 1; | ||||
1249 | } | ||||
1250 | return 0; | ||||
1251 | } | ||||
1252 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1253 | #ifdef MS_WINDOWS |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 1254 | typedef long long Py_off_t; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1255 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1256 | typedef off_t Py_off_t; |
1257 | #endif | ||||
1258 | |||||
1259 | static int | ||||
1260 | Py_off_t_converter(PyObject *arg, void *addr) | ||||
1261 | { | ||||
1262 | #ifdef HAVE_LARGEFILE_SUPPORT | ||||
1263 | *((Py_off_t *)addr) = PyLong_AsLongLong(arg); | ||||
1264 | #else | ||||
1265 | *((Py_off_t *)addr) = PyLong_AsLong(arg); | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1266 | #endif |
1267 | if (PyErr_Occurred()) | ||||
1268 | return 0; | ||||
1269 | return 1; | ||||
1270 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1271 | |
1272 | static PyObject * | ||||
1273 | PyLong_FromPy_off_t(Py_off_t offset) | ||||
1274 | { | ||||
1275 | #ifdef HAVE_LARGEFILE_SUPPORT | ||||
1276 | return PyLong_FromLongLong(offset); | ||||
1277 | #else | ||||
1278 | return PyLong_FromLong(offset); | ||||
Ross Lagerwall | b1e5d59 | 2011-09-19 08:30:43 +0200 | [diff] [blame] | 1279 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1280 | } |
1281 | |||||
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1282 | #ifdef HAVE_SIGSET_T |
1283 | /* Convert an iterable of integers to a sigset. | ||||
1284 | Return 1 on success, return 0 and raise an exception on error. */ | ||||
1285 | int | ||||
1286 | _Py_Sigset_Converter(PyObject *obj, void *addr) | ||||
1287 | { | ||||
1288 | sigset_t *mask = (sigset_t *)addr; | ||||
1289 | PyObject *iterator, *item; | ||||
1290 | long signum; | ||||
1291 | int overflow; | ||||
1292 | |||||
Rémi Lapeyre | f090019 | 2019-05-04 01:30:53 +0200 | [diff] [blame] | 1293 | // The extra parens suppress the unreachable-code warning with clang on MacOS |
1294 | if (sigemptyset(mask) < (0)) { | ||||
Serhiy Storchaka | d54cfb1 | 2018-05-08 07:48:50 +0300 | [diff] [blame] | 1295 | /* Probably only if mask == NULL. */ |
1296 | PyErr_SetFromErrno(PyExc_OSError); | ||||
1297 | return 0; | ||||
1298 | } | ||||
1299 | |||||
1300 | iterator = PyObject_GetIter(obj); | ||||
1301 | if (iterator == NULL) { | ||||
1302 | return 0; | ||||
1303 | } | ||||
1304 | |||||
1305 | while ((item = PyIter_Next(iterator)) != NULL) { | ||||
1306 | signum = PyLong_AsLongAndOverflow(item, &overflow); | ||||
1307 | Py_DECREF(item); | ||||
1308 | if (signum <= 0 || signum >= NSIG) { | ||||
1309 | if (overflow || signum != -1 || !PyErr_Occurred()) { | ||||
1310 | PyErr_Format(PyExc_ValueError, | ||||
1311 | "signal number %ld out of range", signum); | ||||
1312 | } | ||||
1313 | goto error; | ||||
1314 | } | ||||
1315 | if (sigaddset(mask, (int)signum)) { | ||||
1316 | if (errno != EINVAL) { | ||||
1317 | /* Probably impossible */ | ||||
1318 | PyErr_SetFromErrno(PyExc_OSError); | ||||
1319 | goto error; | ||||
1320 | } | ||||
1321 | /* For backwards compatibility, allow idioms such as | ||||
1322 | * `range(1, NSIG)` but warn about invalid signal numbers | ||||
1323 | */ | ||||
1324 | const char msg[] = | ||||
1325 | "invalid signal number %ld, please use valid_signals()"; | ||||
1326 | if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) { | ||||
1327 | goto error; | ||||
1328 | } | ||||
1329 | } | ||||
1330 | } | ||||
1331 | if (!PyErr_Occurred()) { | ||||
1332 | Py_DECREF(iterator); | ||||
1333 | return 1; | ||||
1334 | } | ||||
1335 | |||||
1336 | error: | ||||
1337 | Py_DECREF(iterator); | ||||
1338 | return 0; | ||||
1339 | } | ||||
1340 | #endif /* HAVE_SIGSET_T */ | ||||
1341 | |||||
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1342 | #ifdef MS_WINDOWS |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1343 | |
1344 | static int | ||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1345 | win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1346 | { |
Martin Panter | 70214ad | 2016-08-04 02:38:59 +0000 | [diff] [blame] | 1347 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
1348 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; | ||||
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1349 | DWORD n_bytes_returned; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1350 | |
1351 | if (0 == DeviceIoControl( | ||||
1352 | reparse_point_handle, | ||||
1353 | FSCTL_GET_REPARSE_POINT, | ||||
1354 | NULL, 0, /* in buffer */ | ||||
1355 | target_buffer, sizeof(target_buffer), | ||||
1356 | &n_bytes_returned, | ||||
1357 | NULL)) /* we're not using OVERLAPPED_IO */ | ||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1358 | return FALSE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1359 | |
1360 | if (reparse_tag) | ||||
1361 | *reparse_tag = rdb->ReparseTag; | ||||
1362 | |||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1363 | return TRUE; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1364 | } |
Victor Stinner | 1ab6c2d | 2011-11-15 22:27:41 +0100 | [diff] [blame] | 1365 | |
Brian Curtin | fc1be6d | 2010-11-24 13:23:18 +0000 | [diff] [blame] | 1366 | #endif /* MS_WINDOWS */ |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1367 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1368 | /* Return a dictionary corresponding to the POSIX environment table */ |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1369 | #if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1370 | /* On Darwin/MacOSX a shared library or framework has no access to |
Ronald Oussoren | 697e56d | 2013-01-25 17:57:13 +0100 | [diff] [blame] | 1371 | ** environ directly, we must obtain it with _NSGetEnviron(). See also |
1372 | ** man environ(7). | ||||
Jack Jansen | ea0c382 | 2002-08-01 21:57:49 +0000 | [diff] [blame] | 1373 | */ |
1374 | #include <crt_externs.h> | ||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 1375 | #elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1376 | extern char **environ; |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 1377 | #endif /* !_MSC_VER */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1378 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 1379 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1380 | convertenviron(void) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1381 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1382 | PyObject *d; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1383 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1384 | wchar_t **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1385 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1386 | char **e; |
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 1387 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1388 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1389 | d = PyDict_New(); |
1390 | if (d == NULL) | ||||
1391 | return NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1392 | #ifdef MS_WINDOWS |
1393 | /* _wenviron must be initialized in this way if the program is started | ||||
1394 | through main() instead of wmain(). */ | ||||
1395 | _wgetenv(L""); | ||||
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1396 | e = _wenviron; |
Miss Islington (bot) | 836cf31 | 2019-12-06 11:32:33 -0800 | [diff] [blame] | 1397 | #elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED)) |
1398 | /* environ is not accessible as an extern in a shared object on OSX; use | ||||
1399 | _NSGetEnviron to resolve it. The value changes if you add environment | ||||
1400 | variables between calls to Py_Initialize, so don't cache the value. */ | ||||
1401 | e = *_NSGetEnviron(); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1402 | #else |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1403 | e = environ; |
1404 | #endif | ||||
1405 | if (e == NULL) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1406 | return d; |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1407 | for (; *e != NULL; e++) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1408 | PyObject *k; |
1409 | PyObject *v; | ||||
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1410 | #ifdef MS_WINDOWS |
1411 | const wchar_t *p = wcschr(*e, L'='); | ||||
1412 | #else | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 1413 | const char *p = strchr(*e, '='); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1414 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1415 | if (p == NULL) |
1416 | continue; | ||||
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1417 | #ifdef MS_WINDOWS |
1418 | k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e)); | ||||
1419 | #else | ||||
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1420 | k = PyBytes_FromStringAndSize(*e, (int)(p-*e)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1421 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1422 | if (k == NULL) { |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1423 | Py_DECREF(d); |
1424 | return NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1425 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1426 | #ifdef MS_WINDOWS |
1427 | v = PyUnicode_FromWideChar(p+1, wcslen(p+1)); | ||||
1428 | #else | ||||
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 1429 | v = PyBytes_FromStringAndSize(p+1, strlen(p+1)); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1430 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1431 | if (v == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1432 | Py_DECREF(k); |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1433 | Py_DECREF(d); |
1434 | return NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1435 | } |
Serhiy Storchaka | 6fef0f1 | 2018-12-10 12:10:56 +0200 | [diff] [blame] | 1436 | if (PyDict_GetItemWithError(d, k) == NULL) { |
1437 | if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) { | ||||
1438 | Py_DECREF(v); | ||||
1439 | Py_DECREF(k); | ||||
1440 | Py_DECREF(d); | ||||
1441 | return NULL; | ||||
1442 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1443 | } |
1444 | Py_DECREF(k); | ||||
1445 | Py_DECREF(v); | ||||
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 1446 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1447 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1448 | } |
1449 | |||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1450 | /* Set a POSIX-specific error from errno, and return NULL */ |
1451 | |||||
Barry Warsaw | d58d764 | 1998-07-23 16:14:40 +0000 | [diff] [blame] | 1452 | static PyObject * |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 1453 | posix_error(void) |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 1454 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1455 | return PyErr_SetFromErrno(PyExc_OSError); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1456 | } |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 1457 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 1458 | #ifdef MS_WINDOWS |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1459 | static PyObject * |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 1460 | win32_error(const char* function, const char* filename) |
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1461 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1462 | /* XXX We should pass the function name along in the future. |
1463 | (winreg.c also wants to pass the function name.) | ||||
1464 | This would however require an additional param to the | ||||
1465 | Windows error object, which is non-trivial. | ||||
1466 | */ | ||||
1467 | errno = GetLastError(); | ||||
1468 | if (filename) | ||||
1469 | return PyErr_SetFromWindowsErrWithFilename(errno, filename); | ||||
1470 | else | ||||
1471 | return PyErr_SetFromWindowsErr(errno); | ||||
Fredrik Lundh | ffb9c77 | 2000-07-09 14:49:51 +0000 | [diff] [blame] | 1472 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1473 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1474 | static PyObject * |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1475 | win32_error_object_err(const char* function, PyObject* filename, DWORD err) |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1476 | { |
1477 | /* XXX - see win32_error for comments on 'function' */ | ||||
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1478 | if (filename) |
1479 | return PyErr_SetExcFromWindowsErrWithFilenameObject( | ||||
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 1480 | PyExc_OSError, |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1481 | err, |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1482 | filename); |
1483 | else | ||||
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 1484 | return PyErr_SetFromWindowsErr(err); |
1485 | } | ||||
1486 | |||||
1487 | static PyObject * | ||||
1488 | win32_error_object(const char* function, PyObject* filename) | ||||
1489 | { | ||||
1490 | errno = GetLastError(); | ||||
1491 | return win32_error_object_err(function, filename, errno); | ||||
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 1492 | } |
1493 | |||||
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1494 | #endif /* MS_WINDOWS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1495 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1496 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1497 | posix_path_object_error(PyObject *path) |
1498 | { | ||||
1499 | return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path); | ||||
1500 | } | ||||
1501 | |||||
1502 | static PyObject * | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1503 | path_object_error(PyObject *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1504 | { |
1505 | #ifdef MS_WINDOWS | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1506 | return PyErr_SetExcFromWindowsErrWithFilenameObject( |
1507 | PyExc_OSError, 0, path); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1508 | #else |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1509 | return posix_path_object_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1510 | #endif |
1511 | } | ||||
1512 | |||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1513 | static PyObject * |
1514 | path_object_error2(PyObject *path, PyObject *path2) | ||||
1515 | { | ||||
1516 | #ifdef MS_WINDOWS | ||||
1517 | return PyErr_SetExcFromWindowsErrWithFilenameObjects( | ||||
1518 | PyExc_OSError, 0, path, path2); | ||||
1519 | #else | ||||
1520 | return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2); | ||||
1521 | #endif | ||||
1522 | } | ||||
1523 | |||||
1524 | static PyObject * | ||||
1525 | path_error(path_t *path) | ||||
1526 | { | ||||
1527 | return path_object_error(path->object); | ||||
1528 | } | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 1529 | |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1530 | static PyObject * |
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 1531 | posix_path_error(path_t *path) |
1532 | { | ||||
1533 | return posix_path_object_error(path->object); | ||||
1534 | } | ||||
1535 | |||||
1536 | static PyObject * | ||||
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1537 | path_error2(path_t *path, path_t *path2) |
1538 | { | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 1539 | return path_object_error2(path->object, path2->object); |
Larry Hastings | b082731 | 2014-02-09 22:05:19 -0800 | [diff] [blame] | 1540 | } |
1541 | |||||
1542 | |||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1543 | /* POSIX generic methods */ |
1544 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1545 | static int |
1546 | fildes_converter(PyObject *o, void *p) | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1547 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1548 | int fd; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1549 | int *pointer = (int *)p; |
1550 | fd = PyObject_AsFileDescriptor(o); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1551 | if (fd < 0) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1552 | return 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 1553 | *pointer = fd; |
1554 | return 1; | ||||
1555 | } | ||||
1556 | |||||
1557 | static PyObject * | ||||
1558 | posix_fildes_fd(int fd, int (*func)(int)) | ||||
1559 | { | ||||
1560 | int res; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1561 | int async_err = 0; |
1562 | |||||
1563 | do { | ||||
1564 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1565 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1566 | res = (*func)(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 1567 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 1568 | Py_END_ALLOW_THREADS |
1569 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
1570 | if (res != 0) | ||||
1571 | return (!async_err) ? posix_error() : NULL; | ||||
1572 | Py_RETURN_NONE; | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 1573 | } |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 1574 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1575 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 1576 | #ifdef MS_WINDOWS |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1577 | /* This is a reimplementation of the C library's chdir function, |
1578 | but one that produces Win32 errors instead of DOS error codes. | ||||
1579 | chdir is essentially a wrapper around SetCurrentDirectory; however, | ||||
1580 | it also needs to set "magic" environment variables indicating | ||||
1581 | the per-drive current directory, which are of the form =<drive>: */ | ||||
Benjamin Peterson | 206e307 | 2008-10-19 14:07:49 +0000 | [diff] [blame] | 1582 | static BOOL __stdcall |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1583 | win32_wchdir(LPCWSTR path) |
1584 | { | ||||
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1585 | wchar_t path_buf[MAX_PATH], *new_path = path_buf; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1586 | int result; |
1587 | wchar_t env[4] = L"=x:"; | ||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1588 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1589 | if(!SetCurrentDirectoryW(path)) |
1590 | return FALSE; | ||||
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1591 | result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1592 | if (!result) |
1593 | return FALSE; | ||||
Victor Stinner | e847d71 | 2015-12-14 00:21:50 +0100 | [diff] [blame] | 1594 | if (result > Py_ARRAY_LENGTH(path_buf)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1595 | new_path = PyMem_RawMalloc(result * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1596 | if (!new_path) { |
1597 | SetLastError(ERROR_OUTOFMEMORY); | ||||
1598 | return FALSE; | ||||
1599 | } | ||||
1600 | result = GetCurrentDirectoryW(result, new_path); | ||||
1601 | if (!result) { | ||||
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1602 | PyMem_RawFree(new_path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1603 | return FALSE; |
1604 | } | ||||
1605 | } | ||||
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1606 | int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 || |
1607 | wcsncmp(new_path, L"//", 2) == 0); | ||||
1608 | if (!is_unc_like_path) { | ||||
1609 | env[1] = new_path[0]; | ||||
1610 | result = SetEnvironmentVariableW(env, new_path); | ||||
1611 | } | ||||
Victor Stinner | ed53782 | 2015-12-13 21:40:26 +0100 | [diff] [blame] | 1612 | if (new_path != path_buf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 1613 | PyMem_RawFree(new_path); |
Alexey Izbyshev | 3e197c7 | 2018-03-01 12:13:56 +0300 | [diff] [blame] | 1614 | return result ? TRUE : FALSE; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1615 | } |
1616 | #endif | ||||
1617 | |||||
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1618 | #ifdef MS_WINDOWS |
1619 | /* The CRT of Windows has a number of flaws wrt. its stat() implementation: | ||||
1620 | - time stamps are restricted to second resolution | ||||
1621 | - file modification times suffer from forth-and-back conversions between | ||||
1622 | UTC and local time | ||||
1623 | Therefore, we implement our own stat, based on the Win32 API directly. | ||||
1624 | */ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1625 | #define HAVE_STAT_NSEC 1 |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1626 | #define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1 |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1627 | #define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1 |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1628 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1629 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1630 | find_data_to_file_info(WIN32_FIND_DATAW *pFileData, |
1631 | BY_HANDLE_FILE_INFORMATION *info, | ||||
1632 | ULONG *reparse_tag) | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 1633 | { |
1634 | memset(info, 0, sizeof(*info)); | ||||
1635 | info->dwFileAttributes = pFileData->dwFileAttributes; | ||||
1636 | info->ftCreationTime = pFileData->ftCreationTime; | ||||
1637 | info->ftLastAccessTime = pFileData->ftLastAccessTime; | ||||
1638 | info->ftLastWriteTime = pFileData->ftLastWriteTime; | ||||
1639 | info->nFileSizeHigh = pFileData->nFileSizeHigh; | ||||
1640 | info->nFileSizeLow = pFileData->nFileSizeLow; | ||||
1641 | /* info->nNumberOfLinks = 1; */ | ||||
1642 | if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) | ||||
1643 | *reparse_tag = pFileData->dwReserved0; | ||||
1644 | else | ||||
1645 | *reparse_tag = 0; | ||||
1646 | } | ||||
1647 | |||||
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1648 | static BOOL |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1649 | attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1650 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1651 | HANDLE hFindFile; |
1652 | WIN32_FIND_DATAW FileData; | ||||
1653 | hFindFile = FindFirstFileW(pszFile, &FileData); | ||||
1654 | if (hFindFile == INVALID_HANDLE_VALUE) | ||||
1655 | return FALSE; | ||||
1656 | FindClose(hFindFile); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1657 | find_data_to_file_info(&FileData, info, reparse_tag); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1658 | return TRUE; |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1659 | } |
1660 | |||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1661 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1662 | win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result, |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1663 | BOOL traverse) |
1664 | { | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1665 | HANDLE hFile; |
1666 | BY_HANDLE_FILE_INFORMATION fileInfo; | ||||
1667 | FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 }; | ||||
1668 | DWORD fileType, error; | ||||
1669 | BOOL isUnhandledTag = FALSE; | ||||
1670 | int retval = 0; | ||||
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1671 | |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1672 | DWORD access = FILE_READ_ATTRIBUTES; |
1673 | DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */ | ||||
1674 | if (!traverse) { | ||||
1675 | flags |= FILE_FLAG_OPEN_REPARSE_POINT; | ||||
1676 | } | ||||
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1677 | |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1678 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1679 | if (hFile == INVALID_HANDLE_VALUE) { |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1680 | /* Either the path doesn't exist, or the caller lacks access. */ |
1681 | error = GetLastError(); | ||||
1682 | switch (error) { | ||||
1683 | case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */ | ||||
1684 | case ERROR_SHARING_VIOLATION: /* It's a paging file. */ | ||||
1685 | /* Try reading the parent directory. */ | ||||
1686 | if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) { | ||||
1687 | /* Cannot read the parent directory. */ | ||||
1688 | SetLastError(error); | ||||
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1689 | return -1; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1690 | } |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1691 | if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
1692 | if (traverse || | ||||
1693 | !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { | ||||
1694 | /* The stat call has to traverse but cannot, so fail. */ | ||||
1695 | SetLastError(error); | ||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1696 | return -1; |
Mark Becwar | b82bfac | 2019-02-02 16:08:23 -0500 | [diff] [blame] | 1697 | } |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1698 | } |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1699 | break; |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1700 | |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1701 | case ERROR_INVALID_PARAMETER: |
1702 | /* \\.\con requires read or write access. */ | ||||
1703 | hFile = CreateFileW(path, access | GENERIC_READ, | ||||
1704 | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | ||||
1705 | OPEN_EXISTING, flags, NULL); | ||||
1706 | if (hFile == INVALID_HANDLE_VALUE) { | ||||
1707 | SetLastError(error); | ||||
1708 | return -1; | ||||
1709 | } | ||||
1710 | break; | ||||
1711 | |||||
1712 | case ERROR_CANT_ACCESS_FILE: | ||||
1713 | /* bpo37834: open unhandled reparse points if traverse fails. */ | ||||
1714 | if (traverse) { | ||||
1715 | traverse = FALSE; | ||||
1716 | isUnhandledTag = TRUE; | ||||
1717 | hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, | ||||
1718 | flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL); | ||||
1719 | } | ||||
1720 | if (hFile == INVALID_HANDLE_VALUE) { | ||||
1721 | SetLastError(error); | ||||
1722 | return -1; | ||||
1723 | } | ||||
1724 | break; | ||||
1725 | |||||
1726 | default: | ||||
1727 | return -1; | ||||
1728 | } | ||||
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1729 | } |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1730 | |
1731 | if (hFile != INVALID_HANDLE_VALUE) { | ||||
1732 | /* Handle types other than files on disk. */ | ||||
1733 | fileType = GetFileType(hFile); | ||||
1734 | if (fileType != FILE_TYPE_DISK) { | ||||
1735 | if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) { | ||||
1736 | retval = -1; | ||||
1737 | goto cleanup; | ||||
1738 | } | ||||
1739 | DWORD fileAttributes = GetFileAttributesW(path); | ||||
1740 | memset(result, 0, sizeof(*result)); | ||||
1741 | if (fileAttributes != INVALID_FILE_ATTRIBUTES && | ||||
1742 | fileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | ||||
1743 | /* \\.\pipe\ or \\.\mailslot\ */ | ||||
1744 | result->st_mode = _S_IFDIR; | ||||
1745 | } else if (fileType == FILE_TYPE_CHAR) { | ||||
1746 | /* \\.\nul */ | ||||
1747 | result->st_mode = _S_IFCHR; | ||||
1748 | } else if (fileType == FILE_TYPE_PIPE) { | ||||
1749 | /* \\.\pipe\spam */ | ||||
1750 | result->st_mode = _S_IFIFO; | ||||
1751 | } | ||||
1752 | /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */ | ||||
1753 | goto cleanup; | ||||
1754 | } | ||||
1755 | |||||
1756 | /* Query the reparse tag, and traverse a non-link. */ | ||||
1757 | if (!traverse) { | ||||
1758 | if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo, | ||||
1759 | &tagInfo, sizeof(tagInfo))) { | ||||
1760 | /* Allow devices that do not support FileAttributeTagInfo. */ | ||||
1761 | switch (GetLastError()) { | ||||
1762 | case ERROR_INVALID_PARAMETER: | ||||
1763 | case ERROR_INVALID_FUNCTION: | ||||
1764 | case ERROR_NOT_SUPPORTED: | ||||
1765 | tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; | ||||
1766 | tagInfo.ReparseTag = 0; | ||||
1767 | break; | ||||
1768 | default: | ||||
1769 | retval = -1; | ||||
1770 | goto cleanup; | ||||
1771 | } | ||||
1772 | } else if (tagInfo.FileAttributes & | ||||
1773 | FILE_ATTRIBUTE_REPARSE_POINT) { | ||||
1774 | if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) { | ||||
1775 | if (isUnhandledTag) { | ||||
1776 | /* Traversing previously failed for either this link | ||||
1777 | or its target. */ | ||||
1778 | SetLastError(ERROR_CANT_ACCESS_FILE); | ||||
1779 | retval = -1; | ||||
1780 | goto cleanup; | ||||
1781 | } | ||||
1782 | /* Traverse a non-link, but not if traversing already failed | ||||
1783 | for an unhandled tag. */ | ||||
1784 | } else if (!isUnhandledTag) { | ||||
1785 | CloseHandle(hFile); | ||||
1786 | return win32_xstat_impl(path, result, TRUE); | ||||
1787 | } | ||||
1788 | } | ||||
1789 | } | ||||
1790 | |||||
1791 | if (!GetFileInformationByHandle(hFile, &fileInfo)) { | ||||
1792 | switch (GetLastError()) { | ||||
1793 | case ERROR_INVALID_PARAMETER: | ||||
1794 | case ERROR_INVALID_FUNCTION: | ||||
1795 | case ERROR_NOT_SUPPORTED: | ||||
Miss Islington (bot) | cad7abf | 2019-09-04 15:18:05 -0700 | [diff] [blame] | 1796 | /* Volumes and physical disks are block devices, e.g. |
1797 | \\.\C: and \\.\PhysicalDrive0. */ | ||||
1798 | memset(result, 0, sizeof(*result)); | ||||
1799 | result->st_mode = 0x6000; /* S_IFBLK */ | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1800 | goto cleanup; |
1801 | } | ||||
Miss Islington (bot) | cad7abf | 2019-09-04 15:18:05 -0700 | [diff] [blame] | 1802 | retval = -1; |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1803 | goto cleanup; |
1804 | } | ||||
1805 | } | ||||
1806 | |||||
1807 | _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result); | ||||
1808 | |||||
1809 | if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { | ||||
1810 | /* Fix the file execute permissions. This hack sets S_IEXEC if | ||||
1811 | the filename has an extension that is commonly used by files | ||||
1812 | that CreateProcessW can execute. A real implementation calls | ||||
1813 | GetSecurityInfo, OpenThreadToken/OpenProcessToken, and | ||||
1814 | AccessCheck to check for generic read, write, and execute | ||||
1815 | access. */ | ||||
1816 | const wchar_t *fileExtension = wcsrchr(path, '.'); | ||||
1817 | if (fileExtension) { | ||||
1818 | if (_wcsicmp(fileExtension, L".exe") == 0 || | ||||
1819 | _wcsicmp(fileExtension, L".bat") == 0 || | ||||
1820 | _wcsicmp(fileExtension, L".cmd") == 0 || | ||||
1821 | _wcsicmp(fileExtension, L".com") == 0) { | ||||
1822 | result->st_mode |= 0111; | ||||
1823 | } | ||||
1824 | } | ||||
1825 | } | ||||
1826 | |||||
1827 | cleanup: | ||||
1828 | if (hFile != INVALID_HANDLE_VALUE) { | ||||
Miss Islington (bot) | cad7abf | 2019-09-04 15:18:05 -0700 | [diff] [blame] | 1829 | /* Preserve last error if we are failing */ |
1830 | error = retval ? GetLastError() : 0; | ||||
1831 | if (!CloseHandle(hFile)) { | ||||
1832 | retval = -1; | ||||
1833 | } else if (retval) { | ||||
1834 | /* Restore last error */ | ||||
1835 | SetLastError(error); | ||||
1836 | } | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1837 | } |
1838 | |||||
1839 | return retval; | ||||
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1840 | } |
1841 | |||||
1842 | static int | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1843 | win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse) |
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 1844 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1845 | /* Protocol violation: we explicitly clear errno, instead of |
1846 | setting it to a POSIX error. Callers should use GetLastError. */ | ||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1847 | int code = win32_xstat_impl(path, result, traverse); |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1848 | errno = 0; |
1849 | return code; | ||||
1850 | } | ||||
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 1851 | /* About the following functions: win32_lstat_w, win32_stat, win32_stat_w |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1852 | |
1853 | In Posix, stat automatically traverses symlinks and returns the stat | ||||
1854 | structure for the target. In Windows, the equivalent GetFileAttributes by | ||||
1855 | default does not traverse symlinks and instead returns attributes for | ||||
1856 | the symlink. | ||||
1857 | |||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1858 | Instead, we will open the file (which *does* traverse symlinks by default) |
1859 | and GetFileInformationByHandle(). */ | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1860 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 1861 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1862 | win32_lstat(const wchar_t* path, struct _Py_stat_struct *result) |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1863 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1864 | return win32_xstat(path, result, FALSE); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1865 | } |
1866 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1867 | static int |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1868 | win32_stat(const wchar_t* path, struct _Py_stat_struct *result) |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1869 | { |
Hirokazu Yamamoto | 427d314 | 2010-12-04 10:16:05 +0000 | [diff] [blame] | 1870 | return win32_xstat(path, result, TRUE); |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 1871 | } |
1872 | |||||
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 1873 | #endif /* MS_WINDOWS */ |
1874 | |||||
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1875 | PyDoc_STRVAR(stat_result__doc__, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 1876 | "stat_result: Result from stat, fstat, or lstat.\n\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1877 | This object may be accessed either as a tuple of\n\ |
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1878 | (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\ |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1879 | or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\ |
1880 | \n\ | ||||
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1881 | Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\ |
1882 | or st_flags, they are available as attributes only.\n\ | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1883 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1884 | See os.stat for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1885 | |
1886 | static PyStructSequence_Field stat_result_fields[] = { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1887 | {"st_mode", "protection bits"}, |
1888 | {"st_ino", "inode"}, | ||||
1889 | {"st_dev", "device"}, | ||||
1890 | {"st_nlink", "number of hard links"}, | ||||
1891 | {"st_uid", "user ID of owner"}, | ||||
1892 | {"st_gid", "group ID of owner"}, | ||||
1893 | {"st_size", "total size, in bytes"}, | ||||
1894 | /* The NULL is replaced with PyStructSequence_UnnamedField later. */ | ||||
1895 | {NULL, "integer time of last access"}, | ||||
1896 | {NULL, "integer time of last modification"}, | ||||
1897 | {NULL, "integer time of last change"}, | ||||
1898 | {"st_atime", "time of last access"}, | ||||
1899 | {"st_mtime", "time of last modification"}, | ||||
1900 | {"st_ctime", "time of last change"}, | ||||
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1901 | {"st_atime_ns", "time of last access in nanoseconds"}, |
1902 | {"st_mtime_ns", "time of last modification in nanoseconds"}, | ||||
1903 | {"st_ctime_ns", "time of last change in nanoseconds"}, | ||||
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1904 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1905 | {"st_blksize", "blocksize for filesystem I/O"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1906 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1907 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1908 | {"st_blocks", "number of blocks allocated"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1909 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1910 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1911 | {"st_rdev", "device type (if inode device)"}, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1912 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1913 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1914 | {"st_flags", "user defined flags for file"}, |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1915 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1916 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1917 | {"st_gen", "generation number"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1918 | #endif |
1919 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1920 | {"st_birthtime", "time of creation"}, |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1921 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1922 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
1923 | {"st_file_attributes", "Windows file attribute bits"}, | ||||
1924 | #endif | ||||
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1925 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
1926 | {"st_fstype", "Type of filesystem"}, | ||||
1927 | #endif | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1928 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
1929 | {"st_reparse_tag", "Windows reparse tag"}, | ||||
1930 | #endif | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1931 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1932 | }; |
1933 | |||||
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1934 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1935 | #define ST_BLKSIZE_IDX 16 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1936 | #else |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 1937 | #define ST_BLKSIZE_IDX 15 |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1938 | #endif |
1939 | |||||
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1940 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1941 | #define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1) |
1942 | #else | ||||
1943 | #define ST_BLOCKS_IDX ST_BLKSIZE_IDX | ||||
1944 | #endif | ||||
1945 | |||||
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 1946 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1947 | #define ST_RDEV_IDX (ST_BLOCKS_IDX+1) |
1948 | #else | ||||
1949 | #define ST_RDEV_IDX ST_BLOCKS_IDX | ||||
1950 | #endif | ||||
1951 | |||||
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 1952 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
1953 | #define ST_FLAGS_IDX (ST_RDEV_IDX+1) | ||||
1954 | #else | ||||
1955 | #define ST_FLAGS_IDX ST_RDEV_IDX | ||||
1956 | #endif | ||||
1957 | |||||
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1958 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1959 | #define ST_GEN_IDX (ST_FLAGS_IDX+1) |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1960 | #else |
Martin v. Löwis | f09582e | 2005-08-14 21:42:34 +0000 | [diff] [blame] | 1961 | #define ST_GEN_IDX ST_FLAGS_IDX |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 1962 | #endif |
1963 | |||||
1964 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME | ||||
1965 | #define ST_BIRTHTIME_IDX (ST_GEN_IDX+1) | ||||
1966 | #else | ||||
1967 | #define ST_BIRTHTIME_IDX ST_GEN_IDX | ||||
1968 | #endif | ||||
1969 | |||||
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 1970 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
1971 | #define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1) | ||||
1972 | #else | ||||
1973 | #define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX | ||||
1974 | #endif | ||||
1975 | |||||
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 1976 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
1977 | #define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1) | ||||
1978 | #else | ||||
1979 | #define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX | ||||
1980 | #endif | ||||
1981 | |||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 1982 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
1983 | #define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1) | ||||
1984 | #else | ||||
1985 | #define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX | ||||
1986 | #endif | ||||
1987 | |||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1988 | static PyStructSequence_Desc stat_result_desc = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 1989 | "stat_result", /* name */ |
1990 | stat_result__doc__, /* doc */ | ||||
1991 | stat_result_fields, | ||||
1992 | 10 | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1993 | }; |
1994 | |||||
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1995 | PyDoc_STRVAR(statvfs_result__doc__, |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 1996 | "statvfs_result: Result from statvfs or fstatvfs.\n\n\ |
1997 | This object may be accessed either as a tuple of\n\ | ||||
Fred Drake | f7ce04d | 2002-06-20 18:31:21 +0000 | [diff] [blame] | 1998 | (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\ |
Guido van Rossum | a4dc73e | 2001-10-18 20:53:15 +0000 | [diff] [blame] | 1999 | 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] | 2000 | \n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2001 | See os.statvfs for more information."); |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2002 | |
2003 | static PyStructSequence_Field statvfs_result_fields[] = { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2004 | {"f_bsize", }, |
2005 | {"f_frsize", }, | ||||
2006 | {"f_blocks", }, | ||||
2007 | {"f_bfree", }, | ||||
2008 | {"f_bavail", }, | ||||
2009 | {"f_files", }, | ||||
2010 | {"f_ffree", }, | ||||
2011 | {"f_favail", }, | ||||
2012 | {"f_flag", }, | ||||
2013 | {"f_namemax",}, | ||||
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 2014 | {"f_fsid", }, |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2015 | {0} |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2016 | }; |
2017 | |||||
2018 | static PyStructSequence_Desc statvfs_result_desc = { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2019 | "statvfs_result", /* name */ |
2020 | statvfs_result__doc__, /* doc */ | ||||
2021 | statvfs_result_fields, | ||||
2022 | 10 | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2023 | }; |
2024 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2025 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
2026 | PyDoc_STRVAR(waitid_result__doc__, | ||||
2027 | "waitid_result: Result from waitid.\n\n\ | ||||
2028 | This object may be accessed either as a tuple of\n\ | ||||
2029 | (si_pid, si_uid, si_signo, si_status, si_code),\n\ | ||||
2030 | or via the attributes si_pid, si_uid, and so on.\n\ | ||||
2031 | \n\ | ||||
2032 | See os.waitid for more information."); | ||||
2033 | |||||
2034 | static PyStructSequence_Field waitid_result_fields[] = { | ||||
2035 | {"si_pid", }, | ||||
2036 | {"si_uid", }, | ||||
2037 | {"si_signo", }, | ||||
2038 | {"si_status", }, | ||||
2039 | {"si_code", }, | ||||
2040 | {0} | ||||
2041 | }; | ||||
2042 | |||||
2043 | static PyStructSequence_Desc waitid_result_desc = { | ||||
2044 | "waitid_result", /* name */ | ||||
2045 | waitid_result__doc__, /* doc */ | ||||
2046 | waitid_result_fields, | ||||
2047 | 5 | ||||
2048 | }; | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2049 | static PyTypeObject* WaitidResultType; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 2050 | #endif |
2051 | |||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2052 | static int initialized; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2053 | static PyTypeObject* StatResultType; |
2054 | static PyTypeObject* StatVFSResultType; | ||||
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 2055 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2056 | static PyTypeObject* SchedParamType; |
Benjamin Peterson | bad9c2f | 2011-08-02 18:42:14 -0500 | [diff] [blame] | 2057 | #endif |
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2058 | static newfunc structseq_new; |
2059 | |||||
2060 | static PyObject * | ||||
2061 | statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||||
2062 | { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2063 | PyStructSequence *result; |
2064 | int i; | ||||
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2065 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2066 | result = (PyStructSequence*)structseq_new(type, args, kwds); |
2067 | if (!result) | ||||
2068 | return NULL; | ||||
2069 | /* If we have been initialized from a tuple, | ||||
2070 | st_?time might be set to None. Initialize it | ||||
2071 | from the int slots. */ | ||||
2072 | for (i = 7; i <= 9; i++) { | ||||
2073 | if (result->ob_item[i+3] == Py_None) { | ||||
2074 | Py_DECREF(Py_None); | ||||
2075 | Py_INCREF(result->ob_item[i]); | ||||
2076 | result->ob_item[i+3] = result->ob_item[i]; | ||||
2077 | } | ||||
2078 | } | ||||
2079 | return (PyObject*)result; | ||||
Martin v. Löwis | f607bda | 2002-10-16 18:27:39 +0000 | [diff] [blame] | 2080 | } |
2081 | |||||
2082 | |||||
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2083 | static PyObject *billion = NULL; |
2084 | |||||
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2085 | static void |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2086 | 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] | 2087 | { |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2088 | PyObject *s = _PyLong_FromTime_t(sec); |
2089 | PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec); | ||||
2090 | PyObject *s_in_ns = NULL; | ||||
2091 | PyObject *ns_total = NULL; | ||||
2092 | PyObject *float_s = NULL; | ||||
2093 | |||||
2094 | if (!(s && ns_fractional)) | ||||
2095 | goto exit; | ||||
2096 | |||||
2097 | s_in_ns = PyNumber_Multiply(s, billion); | ||||
2098 | if (!s_in_ns) | ||||
2099 | goto exit; | ||||
2100 | |||||
2101 | ns_total = PyNumber_Add(s_in_ns, ns_fractional); | ||||
2102 | if (!ns_total) | ||||
2103 | goto exit; | ||||
2104 | |||||
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2105 | float_s = PyFloat_FromDouble(sec + 1e-9*nsec); |
2106 | if (!float_s) { | ||||
2107 | goto exit; | ||||
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 2108 | } |
2109 | |||||
2110 | PyStructSequence_SET_ITEM(v, index, s); | ||||
2111 | PyStructSequence_SET_ITEM(v, index+3, float_s); | ||||
2112 | PyStructSequence_SET_ITEM(v, index+6, ns_total); | ||||
2113 | s = NULL; | ||||
2114 | float_s = NULL; | ||||
2115 | ns_total = NULL; | ||||
2116 | exit: | ||||
2117 | Py_XDECREF(s); | ||||
2118 | Py_XDECREF(ns_fractional); | ||||
2119 | Py_XDECREF(s_in_ns); | ||||
2120 | Py_XDECREF(ns_total); | ||||
2121 | Py_XDECREF(float_s); | ||||
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2122 | } |
2123 | |||||
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 2124 | /* pack a system stat C structure into the Python stat tuple |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2125 | (used by posix_stat() and posix_fstat()) */ |
2126 | static PyObject* | ||||
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2127 | _pystat_fromstructstat(STRUCT_STAT *st) |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2128 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2129 | unsigned long ansec, mnsec, cnsec; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 2130 | PyObject *v = PyStructSequence_New(StatResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2131 | if (v == NULL) |
2132 | return NULL; | ||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2133 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2134 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode)); |
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 2135 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino)); |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2136 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino)); |
Serhiy Storchaka | 404fa92 | 2013-01-02 18:22:23 +0200 | [diff] [blame] | 2137 | #ifdef MS_WINDOWS |
2138 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev)); | ||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2139 | #else |
Serhiy Storchaka | b2653b3 | 2015-01-18 11:12:11 +0200 | [diff] [blame] | 2140 | PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev)); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2141 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2142 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink)); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 2143 | #if defined(MS_WINDOWS) |
2144 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0)); | ||||
2145 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0)); | ||||
2146 | #else | ||||
2147 | PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid)); | ||||
2148 | PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid)); | ||||
2149 | #endif | ||||
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 2150 | Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size)); |
2151 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size)); | ||||
Martin v. Löwis | 94717ed | 2002-09-09 14:24:16 +0000 | [diff] [blame] | 2152 | |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2153 | #if defined(HAVE_STAT_TV_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2154 | ansec = st->st_atim.tv_nsec; |
2155 | mnsec = st->st_mtim.tv_nsec; | ||||
2156 | cnsec = st->st_ctim.tv_nsec; | ||||
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2157 | #elif defined(HAVE_STAT_TV_NSEC2) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2158 | ansec = st->st_atimespec.tv_nsec; |
2159 | mnsec = st->st_mtimespec.tv_nsec; | ||||
2160 | cnsec = st->st_ctimespec.tv_nsec; | ||||
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 2161 | #elif defined(HAVE_STAT_NSEC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2162 | ansec = st->st_atime_nsec; |
2163 | mnsec = st->st_mtime_nsec; | ||||
2164 | cnsec = st->st_ctime_nsec; | ||||
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2165 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2166 | ansec = mnsec = cnsec = 0; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2167 | #endif |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2168 | fill_time(v, 7, st->st_atime, ansec); |
2169 | fill_time(v, 8, st->st_mtime, mnsec); | ||||
2170 | fill_time(v, 9, st->st_ctime, cnsec); | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2171 | |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2172 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2173 | PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX, |
2174 | PyLong_FromLong((long)st->st_blksize)); | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2175 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2176 | #ifdef HAVE_STRUCT_STAT_ST_BLOCKS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2177 | PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX, |
2178 | PyLong_FromLong((long)st->st_blocks)); | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 2179 | #endif |
Martin v. Löwis | 60a5d72 | 2002-10-16 20:28:25 +0000 | [diff] [blame] | 2180 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2181 | PyStructSequence_SET_ITEM(v, ST_RDEV_IDX, |
2182 | PyLong_FromLong((long)st->st_rdev)); | ||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2183 | #endif |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2184 | #ifdef HAVE_STRUCT_STAT_ST_GEN |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2185 | PyStructSequence_SET_ITEM(v, ST_GEN_IDX, |
2186 | PyLong_FromLong((long)st->st_gen)); | ||||
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2187 | #endif |
2188 | #ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2189 | { |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2190 | PyObject *val; |
2191 | unsigned long bsec,bnsec; | ||||
2192 | bsec = (long)st->st_birthtime; | ||||
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2193 | #ifdef HAVE_STAT_TV_NSEC2 |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2194 | bnsec = st->st_birthtimespec.tv_nsec; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2195 | #else |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2196 | bnsec = 0; |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2197 | #endif |
Victor Stinner | 01b5aab | 2017-10-24 02:02:00 -0700 | [diff] [blame] | 2198 | val = PyFloat_FromDouble(bsec + 1e-9*bnsec); |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 2199 | PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX, |
2200 | val); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2201 | } |
Martin v. Löwis | ebd9d5b | 2005-08-09 15:00:59 +0000 | [diff] [blame] | 2202 | #endif |
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2203 | #ifdef HAVE_STRUCT_STAT_ST_FLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2204 | PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX, |
2205 | PyLong_FromLong((long)st->st_flags)); | ||||
Hye-Shik Chang | 5f937a7 | 2005-06-02 13:09:30 +0000 | [diff] [blame] | 2206 | #endif |
Zachary Ware | 63f277b | 2014-06-19 09:46:37 -0500 | [diff] [blame] | 2207 | #ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES |
2208 | PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX, | ||||
2209 | PyLong_FromUnsignedLong(st->st_file_attributes)); | ||||
2210 | #endif | ||||
jcea | 6c51d51 | 2018-01-28 14:00:08 +0100 | [diff] [blame] | 2211 | #ifdef HAVE_STRUCT_STAT_ST_FSTYPE |
2212 | PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX, | ||||
2213 | PyUnicode_FromString(st->st_fstype)); | ||||
2214 | #endif | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 2215 | #ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG |
2216 | PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX, | ||||
2217 | PyLong_FromUnsignedLong(st->st_reparse_tag)); | ||||
2218 | #endif | ||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2219 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2220 | if (PyErr_Occurred()) { |
2221 | Py_DECREF(v); | ||||
2222 | return NULL; | ||||
2223 | } | ||||
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2224 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2225 | return v; |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 2226 | } |
2227 | |||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2228 | /* POSIX methods */ |
2229 | |||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2230 | |
2231 | static PyObject * | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2232 | posix_do_stat(const char *function_name, path_t *path, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2233 | int dir_fd, int follow_symlinks) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2234 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2235 | STRUCT_STAT st; |
2236 | int result; | ||||
2237 | |||||
2238 | #if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT) | ||||
2239 | if (follow_symlinks_specified(function_name, follow_symlinks)) | ||||
2240 | return NULL; | ||||
2241 | #endif | ||||
2242 | |||||
2243 | if (path_and_dir_fd_invalid("stat", path, dir_fd) || | ||||
2244 | dir_fd_and_fd_invalid("stat", dir_fd, path->fd) || | ||||
2245 | fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks)) | ||||
2246 | return NULL; | ||||
2247 | |||||
2248 | Py_BEGIN_ALLOW_THREADS | ||||
2249 | if (path->fd != -1) | ||||
2250 | result = FSTAT(path->fd, &st); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2251 | #ifdef MS_WINDOWS |
Steve Dower | 513d747 | 2016-09-08 10:41:50 -0700 | [diff] [blame] | 2252 | else if (follow_symlinks) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2253 | result = win32_stat(path->wide, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2254 | else |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2255 | result = win32_lstat(path->wide, &st); |
2256 | #else | ||||
2257 | else | ||||
2258 | #if defined(HAVE_LSTAT) | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2259 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
2260 | result = LSTAT(path->narrow, &st); | ||||
2261 | else | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2262 | #endif /* HAVE_LSTAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2263 | #ifdef HAVE_FSTATAT |
2264 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) | ||||
2265 | result = fstatat(dir_fd, path->narrow, &st, | ||||
2266 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); | ||||
2267 | else | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2268 | #endif /* HAVE_FSTATAT */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2269 | result = STAT(path->narrow, &st); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2270 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2271 | Py_END_ALLOW_THREADS |
2272 | |||||
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 2273 | if (result != 0) { |
2274 | return path_error(path); | ||||
2275 | } | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2276 | |
2277 | return _pystat_fromstructstat(&st); | ||||
2278 | } | ||||
2279 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2280 | /*[python input] |
2281 | |||||
2282 | for s in """ | ||||
2283 | |||||
2284 | FACCESSAT | ||||
2285 | FCHMODAT | ||||
2286 | FCHOWNAT | ||||
2287 | FSTATAT | ||||
2288 | LINKAT | ||||
2289 | MKDIRAT | ||||
2290 | MKFIFOAT | ||||
2291 | MKNODAT | ||||
2292 | OPENAT | ||||
2293 | READLINKAT | ||||
2294 | SYMLINKAT | ||||
2295 | UNLINKAT | ||||
2296 | |||||
2297 | """.strip().split(): | ||||
2298 | s = s.strip() | ||||
2299 | print(""" | ||||
2300 | #ifdef HAVE_{s} | ||||
2301 | #define {s}_DIR_FD_CONVERTER dir_fd_converter | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2302 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2303 | #define {s}_DIR_FD_CONVERTER dir_fd_unavailable |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2304 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2305 | """.rstrip().format(s=s)) |
2306 | |||||
2307 | for s in """ | ||||
2308 | |||||
2309 | FCHDIR | ||||
2310 | FCHMOD | ||||
2311 | FCHOWN | ||||
2312 | FDOPENDIR | ||||
2313 | FEXECVE | ||||
2314 | FPATHCONF | ||||
2315 | FSTATVFS | ||||
2316 | FTRUNCATE | ||||
2317 | |||||
2318 | """.strip().split(): | ||||
2319 | s = s.strip() | ||||
2320 | print(""" | ||||
2321 | #ifdef HAVE_{s} | ||||
2322 | #define PATH_HAVE_{s} 1 | ||||
2323 | #else | ||||
2324 | #define PATH_HAVE_{s} 0 | ||||
2325 | #endif | ||||
2326 | |||||
2327 | """.rstrip().format(s=s)) | ||||
2328 | [python start generated code]*/ | ||||
2329 | |||||
2330 | #ifdef HAVE_FACCESSAT | ||||
2331 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2332 | #else | ||||
2333 | #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2334 | #endif | ||||
2335 | |||||
2336 | #ifdef HAVE_FCHMODAT | ||||
2337 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2338 | #else | ||||
2339 | #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2340 | #endif | ||||
2341 | |||||
2342 | #ifdef HAVE_FCHOWNAT | ||||
2343 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2344 | #else | ||||
2345 | #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2346 | #endif | ||||
2347 | |||||
2348 | #ifdef HAVE_FSTATAT | ||||
2349 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2350 | #else | ||||
2351 | #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2352 | #endif | ||||
2353 | |||||
2354 | #ifdef HAVE_LINKAT | ||||
2355 | #define LINKAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2356 | #else | ||||
2357 | #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2358 | #endif | ||||
2359 | |||||
2360 | #ifdef HAVE_MKDIRAT | ||||
2361 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2362 | #else | ||||
2363 | #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2364 | #endif | ||||
2365 | |||||
2366 | #ifdef HAVE_MKFIFOAT | ||||
2367 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2368 | #else | ||||
2369 | #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2370 | #endif | ||||
2371 | |||||
2372 | #ifdef HAVE_MKNODAT | ||||
2373 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2374 | #else | ||||
2375 | #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2376 | #endif | ||||
2377 | |||||
2378 | #ifdef HAVE_OPENAT | ||||
2379 | #define OPENAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2380 | #else | ||||
2381 | #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2382 | #endif | ||||
2383 | |||||
2384 | #ifdef HAVE_READLINKAT | ||||
2385 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2386 | #else | ||||
2387 | #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2388 | #endif | ||||
2389 | |||||
2390 | #ifdef HAVE_SYMLINKAT | ||||
2391 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2392 | #else | ||||
2393 | #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2394 | #endif | ||||
2395 | |||||
2396 | #ifdef HAVE_UNLINKAT | ||||
2397 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter | ||||
2398 | #else | ||||
2399 | #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
2400 | #endif | ||||
2401 | |||||
2402 | #ifdef HAVE_FCHDIR | ||||
2403 | #define PATH_HAVE_FCHDIR 1 | ||||
2404 | #else | ||||
2405 | #define PATH_HAVE_FCHDIR 0 | ||||
2406 | #endif | ||||
2407 | |||||
2408 | #ifdef HAVE_FCHMOD | ||||
2409 | #define PATH_HAVE_FCHMOD 1 | ||||
2410 | #else | ||||
2411 | #define PATH_HAVE_FCHMOD 0 | ||||
2412 | #endif | ||||
2413 | |||||
2414 | #ifdef HAVE_FCHOWN | ||||
2415 | #define PATH_HAVE_FCHOWN 1 | ||||
2416 | #else | ||||
2417 | #define PATH_HAVE_FCHOWN 0 | ||||
2418 | #endif | ||||
2419 | |||||
2420 | #ifdef HAVE_FDOPENDIR | ||||
2421 | #define PATH_HAVE_FDOPENDIR 1 | ||||
2422 | #else | ||||
2423 | #define PATH_HAVE_FDOPENDIR 0 | ||||
2424 | #endif | ||||
2425 | |||||
2426 | #ifdef HAVE_FEXECVE | ||||
2427 | #define PATH_HAVE_FEXECVE 1 | ||||
2428 | #else | ||||
2429 | #define PATH_HAVE_FEXECVE 0 | ||||
2430 | #endif | ||||
2431 | |||||
2432 | #ifdef HAVE_FPATHCONF | ||||
2433 | #define PATH_HAVE_FPATHCONF 1 | ||||
2434 | #else | ||||
2435 | #define PATH_HAVE_FPATHCONF 0 | ||||
2436 | #endif | ||||
2437 | |||||
2438 | #ifdef HAVE_FSTATVFS | ||||
2439 | #define PATH_HAVE_FSTATVFS 1 | ||||
2440 | #else | ||||
2441 | #define PATH_HAVE_FSTATVFS 0 | ||||
2442 | #endif | ||||
2443 | |||||
2444 | #ifdef HAVE_FTRUNCATE | ||||
2445 | #define PATH_HAVE_FTRUNCATE 1 | ||||
2446 | #else | ||||
2447 | #define PATH_HAVE_FTRUNCATE 0 | ||||
2448 | #endif | ||||
2449 | /*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/ | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2450 | |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 2451 | #ifdef MS_WINDOWS |
2452 | #undef PATH_HAVE_FTRUNCATE | ||||
2453 | #define PATH_HAVE_FTRUNCATE 1 | ||||
2454 | #endif | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2455 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2456 | /*[python input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2457 | |
2458 | class path_t_converter(CConverter): | ||||
2459 | |||||
2460 | type = "path_t" | ||||
2461 | impl_by_reference = True | ||||
2462 | parse_by_reference = True | ||||
2463 | |||||
2464 | converter = 'path_converter' | ||||
2465 | |||||
2466 | def converter_init(self, *, allow_fd=False, nullable=False): | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2467 | # right now path_t doesn't support default values. |
2468 | # to support a default value, you'll need to override initialize(). | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2469 | if self.default not in (unspecified, None): |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2470 | fail("Can't specify a default to the path_t converter!") |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2471 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2472 | if self.c_default not in (None, 'Py_None'): |
2473 | 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] | 2474 | |
2475 | self.nullable = nullable | ||||
2476 | self.allow_fd = allow_fd | ||||
2477 | |||||
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2478 | def pre_render(self): |
2479 | def strify(value): | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2480 | if isinstance(value, str): |
2481 | return value | ||||
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2482 | return str(int(bool(value))) |
2483 | |||||
2484 | # add self.py_name here when merging with posixmodule conversion | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2485 | self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format( |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2486 | self.function.name, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2487 | self.name, |
Larry Hastings | 7726ac9 | 2014-01-31 22:03:12 -0800 | [diff] [blame] | 2488 | strify(self.nullable), |
2489 | strify(self.allow_fd), | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2490 | ) |
2491 | |||||
2492 | def cleanup(self): | ||||
2493 | return "path_cleanup(&" + self.name + ");\n" | ||||
2494 | |||||
2495 | |||||
2496 | class dir_fd_converter(CConverter): | ||||
2497 | type = 'int' | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2498 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2499 | def converter_init(self, requires=None): |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2500 | if self.default in (unspecified, None): |
2501 | self.c_default = 'DEFAULT_DIR_FD' | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2502 | if isinstance(requires, str): |
2503 | self.converter = requires.upper() + '_DIR_FD_CONVERTER' | ||||
2504 | else: | ||||
2505 | self.converter = 'dir_fd_converter' | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2506 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2507 | class fildes_converter(CConverter): |
2508 | type = 'int' | ||||
2509 | converter = 'fildes_converter' | ||||
2510 | |||||
2511 | class uid_t_converter(CConverter): | ||||
2512 | type = "uid_t" | ||||
2513 | converter = '_Py_Uid_Converter' | ||||
2514 | |||||
2515 | class gid_t_converter(CConverter): | ||||
2516 | type = "gid_t" | ||||
2517 | converter = '_Py_Gid_Converter' | ||||
2518 | |||||
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 2519 | class dev_t_converter(CConverter): |
2520 | type = 'dev_t' | ||||
2521 | converter = '_Py_Dev_Converter' | ||||
2522 | |||||
2523 | class dev_t_return_converter(unsigned_long_return_converter): | ||||
2524 | type = 'dev_t' | ||||
2525 | conversion_fn = '_PyLong_FromDev' | ||||
2526 | unsigned_cast = '(dev_t)' | ||||
2527 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2528 | class FSConverter_converter(CConverter): |
2529 | type = 'PyObject *' | ||||
2530 | converter = 'PyUnicode_FSConverter' | ||||
2531 | def converter_init(self): | ||||
2532 | if self.default is not unspecified: | ||||
2533 | fail("FSConverter_converter does not support default values") | ||||
2534 | self.c_default = 'NULL' | ||||
2535 | |||||
2536 | def cleanup(self): | ||||
2537 | return "Py_XDECREF(" + self.name + ");\n" | ||||
2538 | |||||
2539 | class pid_t_converter(CConverter): | ||||
2540 | type = 'pid_t' | ||||
2541 | format_unit = '" _Py_PARSE_PID "' | ||||
2542 | |||||
2543 | class idtype_t_converter(int_converter): | ||||
2544 | type = 'idtype_t' | ||||
2545 | |||||
2546 | class id_t_converter(CConverter): | ||||
2547 | type = 'id_t' | ||||
2548 | format_unit = '" _Py_PARSE_PID "' | ||||
2549 | |||||
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 2550 | class intptr_t_converter(CConverter): |
2551 | type = 'intptr_t' | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2552 | format_unit = '" _Py_PARSE_INTPTR "' |
2553 | |||||
2554 | class Py_off_t_converter(CConverter): | ||||
2555 | type = 'Py_off_t' | ||||
2556 | converter = 'Py_off_t_converter' | ||||
2557 | |||||
2558 | class Py_off_t_return_converter(long_return_converter): | ||||
2559 | type = 'Py_off_t' | ||||
2560 | conversion_fn = 'PyLong_FromPy_off_t' | ||||
2561 | |||||
2562 | class path_confname_converter(CConverter): | ||||
2563 | type="int" | ||||
2564 | converter="conv_path_confname" | ||||
2565 | |||||
2566 | class confstr_confname_converter(path_confname_converter): | ||||
2567 | converter='conv_confstr_confname' | ||||
2568 | |||||
2569 | class sysconf_confname_converter(path_confname_converter): | ||||
2570 | converter="conv_sysconf_confname" | ||||
2571 | |||||
2572 | class sched_param_converter(CConverter): | ||||
2573 | type = 'struct sched_param' | ||||
2574 | converter = 'convert_sched_param' | ||||
2575 | impl_by_reference = True; | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2576 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2577 | [python start generated code]*/ |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 2578 | /*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2579 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2580 | /*[clinic input] |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2581 | |
Larry Hastings | 2a72791 | 2014-01-16 11:32:01 -0800 | [diff] [blame] | 2582 | os.stat |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2583 | |
2584 | path : path_t(allow_fd=True) | ||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2585 | Path to be examined; can be string, bytes, a path-like object or |
Xiang Zhang | 4459e00 | 2017-01-22 13:04:17 +0800 | [diff] [blame] | 2586 | open-file-descriptor int. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2587 | |
2588 | * | ||||
2589 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2590 | dir_fd : dir_fd(requires='fstatat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2591 | If not None, it should be a file descriptor open to a directory, |
2592 | and path should be a relative string; path will then be relative to | ||||
2593 | that directory. | ||||
2594 | |||||
2595 | follow_symlinks: bool = True | ||||
2596 | If False, and the last element of the path is a symbolic link, | ||||
2597 | stat will examine the symbolic link itself instead of the file | ||||
2598 | the link points to. | ||||
2599 | |||||
2600 | Perform a stat system call on the given path. | ||||
2601 | |||||
2602 | dir_fd and follow_symlinks may not be implemented | ||||
2603 | on your platform. If they are unavailable, using them will raise a | ||||
2604 | NotImplementedError. | ||||
2605 | |||||
2606 | It's an error to use dir_fd or follow_symlinks when specifying path as | ||||
2607 | an open file descriptor. | ||||
2608 | |||||
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2609 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2610 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2611 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2612 | os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2613 | /*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2614 | { |
2615 | return posix_do_stat("stat", path, dir_fd, follow_symlinks); | ||||
2616 | } | ||||
2617 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2618 | |
2619 | /*[clinic input] | ||||
2620 | os.lstat | ||||
2621 | |||||
2622 | path : path_t | ||||
2623 | |||||
2624 | * | ||||
2625 | |||||
2626 | dir_fd : dir_fd(requires='fstatat') = None | ||||
2627 | |||||
2628 | Perform a stat system call on the given path, without following symbolic links. | ||||
2629 | |||||
2630 | Like stat(), but do not follow symbolic links. | ||||
2631 | Equivalent to stat(path, follow_symlinks=False). | ||||
2632 | [clinic start generated code]*/ | ||||
2633 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2634 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2635 | os_lstat_impl(PyObject *module, path_t *path, int dir_fd) |
2636 | /*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2637 | { |
2638 | int follow_symlinks = 0; | ||||
2639 | return posix_do_stat("lstat", path, dir_fd, follow_symlinks); | ||||
2640 | } | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2641 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2642 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2643 | /*[clinic input] |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2644 | os.access -> bool |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2645 | |
Benjamin Peterson | 768f3b4 | 2016-09-05 15:29:33 -0700 | [diff] [blame] | 2646 | path: path_t |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2647 | Path to be tested; can be string, bytes, or a path-like object. |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2648 | |
2649 | mode: int | ||||
2650 | Operating-system mode bitfield. Can be F_OK to test existence, | ||||
2651 | or the inclusive-OR of R_OK, W_OK, and X_OK. | ||||
2652 | |||||
2653 | * | ||||
2654 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2655 | dir_fd : dir_fd(requires='faccessat') = None |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2656 | If not None, it should be a file descriptor open to a directory, |
2657 | and path should be relative; path will then be relative to that | ||||
2658 | directory. | ||||
2659 | |||||
2660 | effective_ids: bool = False | ||||
2661 | If True, access will use the effective uid/gid instead of | ||||
2662 | the real uid/gid. | ||||
2663 | |||||
2664 | follow_symlinks: bool = True | ||||
2665 | If False, and the last element of the path is a symbolic link, | ||||
2666 | access will examine the symbolic link itself instead of the file | ||||
2667 | the link points to. | ||||
2668 | |||||
2669 | Use the real uid/gid to test for access to a path. | ||||
2670 | |||||
2671 | {parameters} | ||||
2672 | dir_fd, effective_ids, and follow_symlinks may not be implemented | ||||
2673 | on your platform. If they are unavailable, using them will raise a | ||||
2674 | NotImplementedError. | ||||
2675 | |||||
2676 | Note that most operations will use the effective uid/gid, therefore this | ||||
2677 | routine can be used in a suid/sgid environment to test if the invoking user | ||||
2678 | has the specified access to the path. | ||||
2679 | |||||
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2680 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2681 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2682 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2683 | os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2684 | int effective_ids, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2685 | /*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2686 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2687 | int return_value; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2688 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2689 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2690 | DWORD attr; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2691 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2692 | int result; |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2693 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2694 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2695 | #ifndef HAVE_FACCESSAT |
2696 | if (follow_symlinks_specified("access", follow_symlinks)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2697 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2698 | |
2699 | if (effective_ids) { | ||||
2700 | argument_unavailable_error("access", "effective_ids"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2701 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2702 | } |
2703 | #endif | ||||
2704 | |||||
2705 | #ifdef MS_WINDOWS | ||||
2706 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2707 | attr = GetFileAttributesW(path->wide); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2708 | Py_END_ALLOW_THREADS |
2709 | |||||
2710 | /* | ||||
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 2711 | * Access is possible if |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2712 | * * we didn't get a -1, and |
2713 | * * write access wasn't requested, | ||||
2714 | * * or the file isn't read-only, | ||||
2715 | * * or it's a directory. | ||||
2716 | * (Directories cannot be read-only on Windows.) | ||||
2717 | */ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2718 | return_value = (attr != INVALID_FILE_ATTRIBUTES) && |
Georg Brandl | 5bb7aa9 | 2012-06-23 12:48:40 +0200 | [diff] [blame] | 2719 | (!(mode & 2) || |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2720 | !(attr & FILE_ATTRIBUTE_READONLY) || |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2721 | (attr & FILE_ATTRIBUTE_DIRECTORY)); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2722 | #else |
2723 | |||||
2724 | Py_BEGIN_ALLOW_THREADS | ||||
2725 | #ifdef HAVE_FACCESSAT | ||||
2726 | if ((dir_fd != DEFAULT_DIR_FD) || | ||||
2727 | effective_ids || | ||||
2728 | !follow_symlinks) { | ||||
2729 | int flags = 0; | ||||
2730 | if (!follow_symlinks) | ||||
2731 | flags |= AT_SYMLINK_NOFOLLOW; | ||||
2732 | if (effective_ids) | ||||
2733 | flags |= AT_EACCESS; | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2734 | result = faccessat(dir_fd, path->narrow, mode, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2735 | } |
2736 | else | ||||
2737 | #endif | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2738 | result = access(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2739 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2740 | return_value = !result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2741 | #endif |
2742 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2743 | return return_value; |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2744 | } |
2745 | |||||
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2746 | #ifndef F_OK |
2747 | #define F_OK 0 | ||||
2748 | #endif | ||||
2749 | #ifndef R_OK | ||||
2750 | #define R_OK 4 | ||||
2751 | #endif | ||||
2752 | #ifndef W_OK | ||||
2753 | #define W_OK 2 | ||||
2754 | #endif | ||||
2755 | #ifndef X_OK | ||||
2756 | #define X_OK 1 | ||||
2757 | #endif | ||||
2758 | |||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2759 | |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2760 | #ifdef HAVE_TTYNAME |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2761 | /*[clinic input] |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2762 | os.ttyname |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2763 | |
2764 | fd: int | ||||
2765 | Integer file descriptor handle. | ||||
2766 | |||||
2767 | / | ||||
2768 | |||||
2769 | Return the name of the terminal device connected to 'fd'. | ||||
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2770 | [clinic start generated code]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2771 | |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2772 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2773 | os_ttyname_impl(PyObject *module, int fd) |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2774 | /*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/ |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2775 | { |
2776 | char *ret; | ||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2777 | |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 2778 | ret = ttyname(fd); |
Serhiy Storchaka | 4db62e1 | 2018-12-17 16:47:45 +0200 | [diff] [blame] | 2779 | if (ret == NULL) { |
2780 | return posix_error(); | ||||
2781 | } | ||||
2782 | return PyUnicode_DecodeFSDefault(ret); | ||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2783 | } |
Guido van Rossum | d371ff1 | 1999-01-25 16:12:23 +0000 | [diff] [blame] | 2784 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 2785 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2786 | #ifdef HAVE_CTERMID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2787 | /*[clinic input] |
2788 | os.ctermid | ||||
2789 | |||||
2790 | Return the name of the controlling terminal for this process. | ||||
2791 | [clinic start generated code]*/ | ||||
2792 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2793 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2794 | os_ctermid_impl(PyObject *module) |
2795 | /*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/ | ||||
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2796 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2797 | char *ret; |
2798 | char buffer[L_ctermid]; | ||||
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2799 | |
Greg Ward | b48bc17 | 2000-03-01 21:51:56 +0000 | [diff] [blame] | 2800 | #ifdef USE_CTERMID_R |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2801 | ret = ctermid_r(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2802 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2803 | ret = ctermid(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2804 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2805 | if (ret == NULL) |
2806 | return posix_error(); | ||||
Victor Stinner | 5fe6de8 | 2010-08-15 09:12:51 +0000 | [diff] [blame] | 2807 | return PyUnicode_DecodeFSDefault(buffer); |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2808 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2809 | #endif /* HAVE_CTERMID */ |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 2810 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2811 | |
2812 | /*[clinic input] | ||||
2813 | os.chdir | ||||
2814 | |||||
2815 | path: path_t(allow_fd='PATH_HAVE_FCHDIR') | ||||
2816 | |||||
2817 | Change the current working directory to the specified path. | ||||
2818 | |||||
2819 | path may always be specified as a string. | ||||
2820 | On some platforms, path may also be specified as an open file descriptor. | ||||
2821 | If this functionality is unavailable, using it raises an exception. | ||||
2822 | [clinic start generated code]*/ | ||||
2823 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2824 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2825 | os_chdir_impl(PyObject *module, path_t *path) |
2826 | /*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2827 | { |
2828 | int result; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2829 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 2830 | if (PySys_Audit("os.chdir", "(O)", path->object) < 0) { |
2831 | return NULL; | ||||
2832 | } | ||||
2833 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2834 | Py_BEGIN_ALLOW_THREADS |
2835 | #ifdef MS_WINDOWS | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2836 | /* on unix, success = 0, on windows, success = !0 */ |
2837 | result = !win32_wchdir(path->wide); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2838 | #else |
2839 | #ifdef HAVE_FCHDIR | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2840 | if (path->fd != -1) |
2841 | result = fchdir(path->fd); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2842 | else |
2843 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2844 | result = chdir(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2845 | #endif |
2846 | Py_END_ALLOW_THREADS | ||||
2847 | |||||
2848 | if (result) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2849 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2850 | } |
2851 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2852 | Py_RETURN_NONE; |
2853 | } | ||||
2854 | |||||
2855 | |||||
2856 | #ifdef HAVE_FCHDIR | ||||
2857 | /*[clinic input] | ||||
2858 | os.fchdir | ||||
2859 | |||||
2860 | fd: fildes | ||||
2861 | |||||
2862 | Change to the directory of the given file descriptor. | ||||
2863 | |||||
2864 | fd must be opened on a directory, not a file. | ||||
2865 | Equivalent to os.chdir(fd). | ||||
2866 | |||||
2867 | [clinic start generated code]*/ | ||||
2868 | |||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2869 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2870 | os_fchdir_impl(PyObject *module, int fd) |
2871 | /*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/ | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2872 | { |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 2873 | if (PySys_Audit("os.chdir", "(i)", fd) < 0) { |
2874 | return NULL; | ||||
2875 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2876 | return posix_fildes_fd(fd, fchdir); |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 2877 | } |
2878 | #endif /* HAVE_FCHDIR */ | ||||
2879 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 2880 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2881 | /*[clinic input] |
2882 | os.chmod | ||||
2883 | |||||
2884 | path: path_t(allow_fd='PATH_HAVE_FCHMOD') | ||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2885 | Path to be modified. May always be specified as a str, bytes, or a path-like object. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2886 | On some platforms, path may also be specified as an open file descriptor. |
2887 | If this functionality is unavailable, using it raises an exception. | ||||
2888 | |||||
2889 | mode: int | ||||
2890 | Operating-system mode bitfield. | ||||
2891 | |||||
2892 | * | ||||
2893 | |||||
2894 | dir_fd : dir_fd(requires='fchmodat') = None | ||||
2895 | If not None, it should be a file descriptor open to a directory, | ||||
2896 | and path should be relative; path will then be relative to that | ||||
2897 | directory. | ||||
2898 | |||||
2899 | follow_symlinks: bool = True | ||||
2900 | If False, and the last element of the path is a symbolic link, | ||||
2901 | chmod will modify the symbolic link itself instead of the file | ||||
2902 | the link points to. | ||||
2903 | |||||
2904 | Change the access permissions of a file. | ||||
2905 | |||||
2906 | It is an error to use dir_fd or follow_symlinks when specifying path as | ||||
2907 | an open file descriptor. | ||||
2908 | dir_fd and follow_symlinks may not be implemented on your platform. | ||||
2909 | If they are unavailable, using them will raise a NotImplementedError. | ||||
2910 | |||||
2911 | [clinic start generated code]*/ | ||||
2912 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2913 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2914 | os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 2915 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 2916 | /*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2917 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2918 | int result; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2919 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 2920 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2921 | DWORD attr; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2922 | #endif |
Hirokazu Yamamoto | 892a37a | 2009-06-28 11:07:03 +0000 | [diff] [blame] | 2923 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2924 | #ifdef HAVE_FCHMODAT |
2925 | int fchmodat_nofollow_unsupported = 0; | ||||
2926 | #endif | ||||
2927 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2928 | #if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD)) |
2929 | if (follow_symlinks_specified("chmod", follow_symlinks)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2930 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2931 | #endif |
2932 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 2933 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, |
2934 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
2935 | return NULL; | ||||
2936 | } | ||||
2937 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2938 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2939 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2940 | attr = GetFileAttributesW(path->wide); |
Tim Golden | 2300508 | 2013-10-25 11:22:37 +0100 | [diff] [blame] | 2941 | if (attr == INVALID_FILE_ATTRIBUTES) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2942 | result = 0; |
2943 | else { | ||||
2944 | if (mode & _S_IWRITE) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2945 | attr &= ~FILE_ATTRIBUTE_READONLY; |
2946 | else | ||||
2947 | attr |= FILE_ATTRIBUTE_READONLY; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2948 | result = SetFileAttributesW(path->wide, attr); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2949 | } |
2950 | Py_END_ALLOW_THREADS | ||||
2951 | |||||
2952 | if (!result) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2953 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2954 | } |
2955 | #else /* MS_WINDOWS */ | ||||
2956 | Py_BEGIN_ALLOW_THREADS | ||||
2957 | #ifdef HAVE_FCHMOD | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2958 | if (path->fd != -1) |
2959 | result = fchmod(path->fd, mode); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2960 | else |
2961 | #endif | ||||
2962 | #ifdef HAVE_LCHMOD | ||||
2963 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2964 | result = lchmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2965 | else |
2966 | #endif | ||||
2967 | #ifdef HAVE_FCHMODAT | ||||
2968 | if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) { | ||||
2969 | /* | ||||
2970 | * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW! | ||||
2971 | * The documentation specifically shows how to use it, | ||||
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2972 | * and then says it isn't implemented yet. |
2973 | * (true on linux with glibc 2.15, and openindiana 3.x) | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2974 | * |
2975 | * Once it is supported, os.chmod will automatically | ||||
2976 | * support dir_fd and follow_symlinks=False. (Hopefully.) | ||||
2977 | * Until then, we need to be careful what exception we raise. | ||||
2978 | */ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2979 | result = fchmodat(dir_fd, path->narrow, mode, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2980 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
2981 | /* | ||||
2982 | * But wait! We can't throw the exception without allowing threads, | ||||
2983 | * and we can't do that in this nested scope. (Macro trickery, sigh.) | ||||
2984 | */ | ||||
2985 | fchmodat_nofollow_unsupported = | ||||
Larry Hastings | dbbc0c8 | 2012-06-22 19:50:21 -0700 | [diff] [blame] | 2986 | result && |
2987 | ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) && | ||||
2988 | !follow_symlinks; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 2989 | } |
2990 | else | ||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 2991 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 2992 | result = chmod(path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 2993 | Py_END_ALLOW_THREADS |
2994 | |||||
2995 | if (result) { | ||||
2996 | #ifdef HAVE_FCHMODAT | ||||
2997 | if (fchmodat_nofollow_unsupported) { | ||||
2998 | if (dir_fd != DEFAULT_DIR_FD) | ||||
2999 | dir_fd_and_follow_symlinks_invalid("chmod", | ||||
3000 | dir_fd, follow_symlinks); | ||||
3001 | else | ||||
3002 | follow_symlinks_specified("chmod", follow_symlinks); | ||||
Anthony Sottile | 233ef24 | 2017-12-14 08:57:55 -0800 | [diff] [blame] | 3003 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3004 | } |
3005 | else | ||||
3006 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3007 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3008 | } |
3009 | #endif | ||||
3010 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3011 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3012 | } |
3013 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3014 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3015 | #ifdef HAVE_FCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3016 | /*[clinic input] |
3017 | os.fchmod | ||||
3018 | |||||
3019 | fd: int | ||||
3020 | mode: int | ||||
3021 | |||||
3022 | Change the access permissions of the file given by file descriptor fd. | ||||
3023 | |||||
3024 | Equivalent to os.chmod(fd, mode). | ||||
3025 | [clinic start generated code]*/ | ||||
3026 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3027 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3028 | os_fchmod_impl(PyObject *module, int fd, int mode) |
3029 | /*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3030 | { |
3031 | int res; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3032 | int async_err = 0; |
3033 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3034 | if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) { |
3035 | return NULL; | ||||
3036 | } | ||||
3037 | |||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3038 | do { |
3039 | Py_BEGIN_ALLOW_THREADS | ||||
3040 | res = fchmod(fd, mode); | ||||
3041 | Py_END_ALLOW_THREADS | ||||
3042 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
3043 | if (res != 0) | ||||
3044 | return (!async_err) ? posix_error() : NULL; | ||||
3045 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3046 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3047 | } |
3048 | #endif /* HAVE_FCHMOD */ | ||||
3049 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3050 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3051 | #ifdef HAVE_LCHMOD |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3052 | /*[clinic input] |
3053 | os.lchmod | ||||
3054 | |||||
3055 | path: path_t | ||||
3056 | mode: int | ||||
3057 | |||||
3058 | Change the access permissions of a file, without following symbolic links. | ||||
3059 | |||||
3060 | If path is a symlink, this affects the link itself rather than the target. | ||||
3061 | Equivalent to chmod(path, mode, follow_symlinks=False)." | ||||
3062 | [clinic start generated code]*/ | ||||
3063 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3064 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3065 | os_lchmod_impl(PyObject *module, path_t *path, int mode) |
3066 | /*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3067 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3068 | int res; |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3069 | if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) { |
3070 | return NULL; | ||||
3071 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3072 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3073 | res = lchmod(path->narrow, mode); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3074 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3075 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3076 | path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3077 | return NULL; |
3078 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3079 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3080 | } |
3081 | #endif /* HAVE_LCHMOD */ | ||||
3082 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3083 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3084 | #ifdef HAVE_CHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3085 | /*[clinic input] |
3086 | os.chflags | ||||
3087 | |||||
3088 | path: path_t | ||||
3089 | flags: unsigned_long(bitwise=True) | ||||
3090 | follow_symlinks: bool=True | ||||
3091 | |||||
3092 | Set file flags. | ||||
3093 | |||||
3094 | If follow_symlinks is False, and the last element of the path is a symbolic | ||||
3095 | link, chflags will change flags on the symbolic link itself instead of the | ||||
3096 | file the link points to. | ||||
3097 | follow_symlinks may not be implemented on your platform. If it is | ||||
3098 | unavailable, using it will raise a NotImplementedError. | ||||
3099 | |||||
3100 | [clinic start generated code]*/ | ||||
3101 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3102 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3103 | os_chflags_impl(PyObject *module, path_t *path, unsigned long flags, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3104 | int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3105 | /*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3106 | { |
3107 | int result; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3108 | |
3109 | #ifndef HAVE_LCHFLAGS | ||||
3110 | if (follow_symlinks_specified("chflags", follow_symlinks)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3111 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3112 | #endif |
3113 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3114 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
3115 | return NULL; | ||||
3116 | } | ||||
3117 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3118 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3119 | #ifdef HAVE_LCHFLAGS |
3120 | if (!follow_symlinks) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3121 | result = lchflags(path->narrow, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3122 | else |
3123 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3124 | result = chflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3125 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3126 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3127 | if (result) |
3128 | return path_error(path); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3129 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3130 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3131 | } |
3132 | #endif /* HAVE_CHFLAGS */ | ||||
3133 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3134 | |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3135 | #ifdef HAVE_LCHFLAGS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3136 | /*[clinic input] |
3137 | os.lchflags | ||||
3138 | |||||
3139 | path: path_t | ||||
3140 | flags: unsigned_long(bitwise=True) | ||||
3141 | |||||
3142 | Set file flags. | ||||
3143 | |||||
3144 | This function will not follow symbolic links. | ||||
3145 | Equivalent to chflags(path, flags, follow_symlinks=False). | ||||
3146 | [clinic start generated code]*/ | ||||
3147 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3148 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3149 | os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags) |
3150 | /*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3151 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3152 | int res; |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3153 | if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) { |
3154 | return NULL; | ||||
3155 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3156 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | b1dc112 | 2014-08-05 16:06:16 +1000 | [diff] [blame] | 3157 | res = lchflags(path->narrow, flags); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3158 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3159 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3160 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3161 | } |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3162 | Py_RETURN_NONE; |
Thomas Wouters | cf297e4 | 2007-02-23 15:07:44 +0000 | [diff] [blame] | 3163 | } |
3164 | #endif /* HAVE_LCHFLAGS */ | ||||
3165 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3166 | |
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3167 | #ifdef HAVE_CHROOT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3168 | /*[clinic input] |
3169 | os.chroot | ||||
3170 | path: path_t | ||||
3171 | |||||
3172 | Change root directory to path. | ||||
3173 | |||||
3174 | [clinic start generated code]*/ | ||||
3175 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3176 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3177 | os_chroot_impl(PyObject *module, path_t *path) |
3178 | /*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3179 | { |
3180 | int res; | ||||
3181 | Py_BEGIN_ALLOW_THREADS | ||||
3182 | res = chroot(path->narrow); | ||||
3183 | Py_END_ALLOW_THREADS | ||||
3184 | if (res < 0) | ||||
3185 | return path_error(path); | ||||
3186 | Py_RETURN_NONE; | ||||
3187 | } | ||||
3188 | #endif /* HAVE_CHROOT */ | ||||
3189 | |||||
Martin v. Löwis | 244edc8 | 2001-10-04 22:44:26 +0000 | [diff] [blame] | 3190 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3191 | #ifdef HAVE_FSYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3192 | /*[clinic input] |
3193 | os.fsync | ||||
3194 | |||||
3195 | fd: fildes | ||||
3196 | |||||
3197 | Force write of fd to disk. | ||||
3198 | [clinic start generated code]*/ | ||||
3199 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3200 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3201 | os_fsync_impl(PyObject *module, int fd) |
3202 | /*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3203 | { |
3204 | return posix_fildes_fd(fd, fsync); | ||||
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3205 | } |
3206 | #endif /* HAVE_FSYNC */ | ||||
3207 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3208 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3209 | #ifdef HAVE_SYNC |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3210 | /*[clinic input] |
3211 | os.sync | ||||
3212 | |||||
3213 | Force write of everything to disk. | ||||
3214 | [clinic start generated code]*/ | ||||
3215 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3216 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3217 | os_sync_impl(PyObject *module) |
3218 | /*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/ | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3219 | { |
3220 | Py_BEGIN_ALLOW_THREADS | ||||
3221 | sync(); | ||||
3222 | Py_END_ALLOW_THREADS | ||||
3223 | Py_RETURN_NONE; | ||||
3224 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3225 | #endif /* HAVE_SYNC */ |
3226 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 3227 | |
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3228 | #ifdef HAVE_FDATASYNC |
Guido van Rossum | 7f58e2e | 2000-09-22 17:26:14 +0000 | [diff] [blame] | 3229 | #ifdef __hpux |
Guido van Rossum | ecc23b0 | 2000-09-22 16:01:05 +0000 | [diff] [blame] | 3230 | extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */ |
3231 | #endif | ||||
3232 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3233 | /*[clinic input] |
3234 | os.fdatasync | ||||
3235 | |||||
3236 | fd: fildes | ||||
3237 | |||||
3238 | Force write of fd to disk without forcing update of metadata. | ||||
3239 | [clinic start generated code]*/ | ||||
3240 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3241 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3242 | os_fdatasync_impl(PyObject *module, int fd) |
3243 | /*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3244 | { |
3245 | return posix_fildes_fd(fd, fdatasync); | ||||
Guido van Rossum | 21142a0 | 1999-01-08 21:05:37 +0000 | [diff] [blame] | 3246 | } |
3247 | #endif /* HAVE_FDATASYNC */ | ||||
3248 | |||||
3249 | |||||
Fredrik Lundh | 1072334 | 2000-07-10 16:38:09 +0000 | [diff] [blame] | 3250 | #ifdef HAVE_CHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3251 | /*[clinic input] |
3252 | os.chown | ||||
3253 | |||||
3254 | path : path_t(allow_fd='PATH_HAVE_FCHOWN') | ||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3255 | Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3256 | |
3257 | uid: uid_t | ||||
3258 | |||||
3259 | gid: gid_t | ||||
3260 | |||||
3261 | * | ||||
3262 | |||||
3263 | dir_fd : dir_fd(requires='fchownat') = None | ||||
3264 | If not None, it should be a file descriptor open to a directory, | ||||
3265 | and path should be relative; path will then be relative to that | ||||
3266 | directory. | ||||
3267 | |||||
3268 | follow_symlinks: bool = True | ||||
3269 | If False, and the last element of the path is a symbolic link, | ||||
3270 | stat will examine the symbolic link itself instead of the file | ||||
3271 | the link points to. | ||||
3272 | |||||
3273 | Change the owner and group id of path to the numeric uid and gid.\ | ||||
3274 | |||||
3275 | path may always be specified as a string. | ||||
3276 | On some platforms, path may also be specified as an open file descriptor. | ||||
3277 | If this functionality is unavailable, using it raises an exception. | ||||
3278 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
3279 | and path should be relative; path will then be relative to that directory. | ||||
3280 | If follow_symlinks is False, and the last element of the path is a symbolic | ||||
3281 | link, chown will modify the symbolic link itself instead of the file the | ||||
3282 | link points to. | ||||
3283 | It is an error to use dir_fd or follow_symlinks when specifying path as | ||||
3284 | an open file descriptor. | ||||
3285 | dir_fd and follow_symlinks may not be implemented on your platform. | ||||
3286 | If they are unavailable, using them will raise a NotImplementedError. | ||||
3287 | |||||
3288 | [clinic start generated code]*/ | ||||
3289 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3290 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3291 | os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3292 | int dir_fd, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3293 | /*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3294 | { |
3295 | int result; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3296 | |
3297 | #if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT)) | ||||
3298 | if (follow_symlinks_specified("chown", follow_symlinks)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3299 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3300 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3301 | if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) || |
3302 | fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks)) | ||||
3303 | return NULL; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3304 | |
3305 | #ifdef __APPLE__ | ||||
3306 | /* | ||||
3307 | * This is for Mac OS X 10.3, which doesn't have lchown. | ||||
3308 | * (But we still have an lchown symbol because of weak-linking.) | ||||
3309 | * It doesn't have fchownat either. So there's no possibility | ||||
3310 | * of a graceful failover. | ||||
Georg Brandl | f787559 | 2012-06-24 13:58:31 +0200 | [diff] [blame] | 3311 | */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3312 | if ((!follow_symlinks) && (lchown == NULL)) { |
3313 | follow_symlinks_specified("chown", follow_symlinks); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3314 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3315 | } |
3316 | #endif | ||||
3317 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3318 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, |
3319 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
3320 | return NULL; | ||||
3321 | } | ||||
3322 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3323 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3324 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3325 | if (path->fd != -1) |
3326 | result = fchown(path->fd, uid, gid); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3327 | else |
3328 | #endif | ||||
3329 | #ifdef HAVE_LCHOWN | ||||
3330 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3331 | result = lchown(path->narrow, uid, gid); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3332 | else |
3333 | #endif | ||||
3334 | #ifdef HAVE_FCHOWNAT | ||||
3335 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3336 | result = fchownat(dir_fd, path->narrow, uid, gid, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3337 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); |
3338 | else | ||||
3339 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3340 | result = chown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3341 | Py_END_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3342 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3343 | if (result) |
3344 | return path_error(path); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3345 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3346 | Py_RETURN_NONE; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3347 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3348 | #endif /* HAVE_CHOWN */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3349 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3350 | |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3351 | #ifdef HAVE_FCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3352 | /*[clinic input] |
3353 | os.fchown | ||||
3354 | |||||
3355 | fd: int | ||||
3356 | uid: uid_t | ||||
3357 | gid: gid_t | ||||
3358 | |||||
3359 | Change the owner and group id of the file specified by file descriptor. | ||||
3360 | |||||
3361 | Equivalent to os.chown(fd, uid, gid). | ||||
3362 | |||||
3363 | [clinic start generated code]*/ | ||||
3364 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3365 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3366 | os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid) |
3367 | /*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3368 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3369 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3370 | int async_err = 0; |
3371 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3372 | if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) { |
3373 | return NULL; | ||||
3374 | } | ||||
3375 | |||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 3376 | do { |
3377 | Py_BEGIN_ALLOW_THREADS | ||||
3378 | res = fchown(fd, uid, gid); | ||||
3379 | Py_END_ALLOW_THREADS | ||||
3380 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
3381 | if (res != 0) | ||||
3382 | return (!async_err) ? posix_error() : NULL; | ||||
3383 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3384 | Py_RETURN_NONE; |
Christian Heimes | 4e30a84 | 2007-11-30 22:12:06 +0000 | [diff] [blame] | 3385 | } |
3386 | #endif /* HAVE_FCHOWN */ | ||||
3387 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3388 | |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3389 | #ifdef HAVE_LCHOWN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3390 | /*[clinic input] |
3391 | os.lchown | ||||
3392 | |||||
3393 | path : path_t | ||||
3394 | uid: uid_t | ||||
3395 | gid: gid_t | ||||
3396 | |||||
3397 | Change the owner and group id of path to the numeric uid and gid. | ||||
3398 | |||||
3399 | This function will not follow symbolic links. | ||||
3400 | Equivalent to os.chown(path, uid, gid, follow_symlinks=False). | ||||
3401 | [clinic start generated code]*/ | ||||
3402 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3403 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3404 | os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid) |
3405 | /*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3406 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3407 | int res; |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3408 | if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) { |
3409 | return NULL; | ||||
3410 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3411 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3412 | res = lchown(path->narrow, uid, gid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3413 | Py_END_ALLOW_THREADS |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3414 | if (res < 0) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3415 | return path_error(path); |
Victor Stinner | 292c835 | 2012-10-30 02:17:38 +0100 | [diff] [blame] | 3416 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3417 | Py_RETURN_NONE; |
Martin v. Löwis | 0cec0ff | 2002-07-28 16:33:45 +0000 | [diff] [blame] | 3418 | } |
3419 | #endif /* HAVE_LCHOWN */ | ||||
3420 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3421 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3422 | static PyObject * |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3423 | posix_getcwd(int use_bytes) |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3424 | { |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 3425 | #ifdef MS_WINDOWS |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3426 | wchar_t wbuf[MAXPATHLEN]; |
3427 | wchar_t *wbuf2 = wbuf; | ||||
3428 | DWORD len; | ||||
3429 | |||||
3430 | Py_BEGIN_ALLOW_THREADS | ||||
3431 | len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf); | ||||
3432 | /* If the buffer is large enough, len does not include the | ||||
3433 | terminating \0. If the buffer is too small, len includes | ||||
3434 | the space needed for the terminator. */ | ||||
3435 | if (len >= Py_ARRAY_LENGTH(wbuf)) { | ||||
Miss Islington (bot) | 68c1c39 | 2019-06-28 09:23:06 -0700 | [diff] [blame] | 3436 | if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) { |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3437 | wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3438 | } |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3439 | else { |
3440 | wbuf2 = NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3441 | } |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3442 | if (wbuf2) { |
3443 | len = GetCurrentDirectoryW(len, wbuf2); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3444 | } |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3445 | } |
3446 | Py_END_ALLOW_THREADS | ||||
3447 | |||||
3448 | if (!wbuf2) { | ||||
3449 | PyErr_NoMemory(); | ||||
3450 | return NULL; | ||||
3451 | } | ||||
3452 | if (!len) { | ||||
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 3453 | if (wbuf2 != wbuf) |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3454 | PyMem_RawFree(wbuf2); |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3455 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3456 | } |
Victor Stinner | f7c5ae2 | 2011-11-16 23:43:07 +0100 | [diff] [blame] | 3457 | |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3458 | PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len); |
3459 | if (wbuf2 != wbuf) { | ||||
3460 | PyMem_RawFree(wbuf2); | ||||
3461 | } | ||||
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3462 | |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3463 | if (use_bytes) { |
3464 | if (resobj == NULL) { | ||||
3465 | return NULL; | ||||
3466 | } | ||||
3467 | Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj)); | ||||
3468 | } | ||||
3469 | |||||
3470 | return resobj; | ||||
3471 | #else | ||||
3472 | const size_t chunk = 1024; | ||||
3473 | |||||
3474 | char *buf = NULL; | ||||
3475 | char *cwd = NULL; | ||||
3476 | size_t buflen = 0; | ||||
3477 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3478 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3479 | do { |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3480 | char *newbuf; |
3481 | if (buflen <= PY_SSIZE_T_MAX - chunk) { | ||||
3482 | buflen += chunk; | ||||
3483 | newbuf = PyMem_RawRealloc(buf, buflen); | ||||
3484 | } | ||||
3485 | else { | ||||
3486 | newbuf = NULL; | ||||
3487 | } | ||||
3488 | if (newbuf == NULL) { | ||||
3489 | PyMem_RawFree(buf); | ||||
3490 | buf = NULL; | ||||
Victor Stinner | c44f707 | 2016-03-14 18:07:53 +0100 | [diff] [blame] | 3491 | break; |
3492 | } | ||||
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3493 | buf = newbuf; |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3494 | |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3495 | cwd = getcwd(buf, buflen); |
3496 | } while (cwd == NULL && errno == ERANGE); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3497 | Py_END_ALLOW_THREADS |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3498 | |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3499 | if (buf == NULL) { |
3500 | return PyErr_NoMemory(); | ||||
3501 | } | ||||
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3502 | if (cwd == NULL) { |
3503 | PyMem_RawFree(buf); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3504 | return posix_error(); |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3505 | } |
3506 | |||||
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3507 | PyObject *obj; |
3508 | if (use_bytes) { | ||||
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3509 | obj = PyBytes_FromStringAndSize(buf, strlen(buf)); |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3510 | } |
3511 | else { | ||||
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3512 | obj = PyUnicode_DecodeFSDefault(buf); |
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3513 | } |
Victor Stinner | 4403d7d | 2015-04-25 00:16:10 +0200 | [diff] [blame] | 3514 | PyMem_RawFree(buf); |
3515 | |||||
3516 | return obj; | ||||
Miss Islington (bot) | 63429c8 | 2019-06-26 09:14:30 -0700 | [diff] [blame] | 3517 | #endif /* !MS_WINDOWS */ |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 3518 | } |
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3519 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3520 | |
3521 | /*[clinic input] | ||||
3522 | os.getcwd | ||||
3523 | |||||
3524 | Return a unicode string representing the current working directory. | ||||
3525 | [clinic start generated code]*/ | ||||
3526 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3527 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3528 | os_getcwd_impl(PyObject *module) |
3529 | /*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/ | ||||
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3530 | { |
3531 | return posix_getcwd(0); | ||||
3532 | } | ||||
3533 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3534 | |
3535 | /*[clinic input] | ||||
3536 | os.getcwdb | ||||
3537 | |||||
3538 | Return a bytes string representing the current working directory. | ||||
3539 | [clinic start generated code]*/ | ||||
3540 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3541 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3542 | os_getcwdb_impl(PyObject *module) |
3543 | /*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/ | ||||
Guido van Rossum | f0af3e3 | 2008-10-02 18:55:37 +0000 | [diff] [blame] | 3544 | { |
3545 | return posix_getcwd(1); | ||||
3546 | } | ||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3547 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3548 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3549 | #if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS)) |
3550 | #define HAVE_LINK 1 | ||||
3551 | #endif | ||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 3552 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3553 | #ifdef HAVE_LINK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3554 | /*[clinic input] |
3555 | |||||
3556 | os.link | ||||
3557 | |||||
3558 | src : path_t | ||||
3559 | dst : path_t | ||||
3560 | * | ||||
3561 | src_dir_fd : dir_fd = None | ||||
3562 | dst_dir_fd : dir_fd = None | ||||
3563 | follow_symlinks: bool = True | ||||
3564 | |||||
3565 | Create a hard link to a file. | ||||
3566 | |||||
3567 | If either src_dir_fd or dst_dir_fd is not None, it should be a file | ||||
3568 | descriptor open to a directory, and the respective path string (src or dst) | ||||
3569 | should be relative; the path will then be relative to that directory. | ||||
3570 | If follow_symlinks is False, and the last element of src is a symbolic | ||||
3571 | link, link will create a link to the symbolic link itself instead of the | ||||
3572 | file the link points to. | ||||
3573 | src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your | ||||
3574 | platform. If they are unavailable, using them will raise a | ||||
3575 | NotImplementedError. | ||||
3576 | [clinic start generated code]*/ | ||||
3577 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3578 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3579 | os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 3580 | int dst_dir_fd, int follow_symlinks) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3581 | /*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3582 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3583 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3584 | BOOL result = FALSE; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3585 | #else |
3586 | int result; | ||||
3587 | #endif | ||||
3588 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3589 | #ifndef HAVE_LINKAT |
3590 | if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) { | ||||
3591 | argument_unavailable_error("link", "src_dir_fd and dst_dir_fd"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3592 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3593 | } |
3594 | #endif | ||||
3595 | |||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3596 | #ifndef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3597 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3598 | PyErr_SetString(PyExc_NotImplementedError, |
3599 | "link: src and dst must be the same type"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3600 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3601 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3602 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 3603 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 3604 | if (PySys_Audit("os.link", "OOii", src->object, dst->object, |
3605 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, | ||||
3606 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { | ||||
3607 | return NULL; | ||||
3608 | } | ||||
3609 | |||||
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3610 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3611 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 3612 | result = CreateHardLinkW(dst->wide, src->wide, NULL); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3613 | Py_END_ALLOW_THREADS |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3614 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3615 | if (!result) |
3616 | return path_error2(src, dst); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3617 | #else |
3618 | Py_BEGIN_ALLOW_THREADS | ||||
Larry Hastings | 67cbf7b | 2012-06-22 17:06:48 -0700 | [diff] [blame] | 3619 | #ifdef HAVE_LINKAT |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3620 | if ((src_dir_fd != DEFAULT_DIR_FD) || |
3621 | (dst_dir_fd != DEFAULT_DIR_FD) || | ||||
3622 | (!follow_symlinks)) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3623 | result = linkat(src_dir_fd, src->narrow, |
3624 | dst_dir_fd, dst->narrow, | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3625 | follow_symlinks ? AT_SYMLINK_FOLLOW : 0); |
3626 | else | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3627 | #endif /* HAVE_LINKAT */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3628 | result = link(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3629 | Py_END_ALLOW_THREADS |
Brian Curtin | fc889c4 | 2010-11-28 23:59:46 +0000 | [diff] [blame] | 3630 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3631 | if (result) |
3632 | return path_error2(src, dst); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3633 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3634 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3635 | Py_RETURN_NONE; |
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3636 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3637 | #endif |
3638 | |||||
Brian Curtin | 1b9df39 | 2010-11-24 20:24:31 +0000 | [diff] [blame] | 3639 | |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3640 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 3641 | static PyObject * |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3642 | _listdir_windows_no_opendir(path_t *path, PyObject *list) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3643 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3644 | PyObject *v; |
3645 | HANDLE hFindFile = INVALID_HANDLE_VALUE; | ||||
3646 | BOOL result; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3647 | wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3648 | /* only claim to have space for MAX_PATH */ |
Victor Stinner | 7587507 | 2013-11-24 19:23:25 +0100 | [diff] [blame] | 3649 | Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3650 | wchar_t *wnamebuf = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3651 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3652 | WIN32_FIND_DATAW wFileData; |
3653 | const wchar_t *po_wchars; | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3654 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3655 | if (!path->wide) { /* Default arg: "." */ |
3656 | po_wchars = L"."; | ||||
3657 | len = 1; | ||||
3658 | } else { | ||||
3659 | po_wchars = path->wide; | ||||
3660 | len = wcslen(path->wide); | ||||
3661 | } | ||||
3662 | /* The +5 is so we can append "\\*.*\0" */ | ||||
3663 | wnamebuf = PyMem_New(wchar_t, len + 5); | ||||
3664 | if (!wnamebuf) { | ||||
3665 | PyErr_NoMemory(); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3666 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3667 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3668 | wcscpy(wnamebuf, po_wchars); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3669 | if (len > 0) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3670 | wchar_t wch = wnamebuf[len-1]; |
3671 | if (wch != SEP && wch != ALTSEP && wch != L':') | ||||
3672 | wnamebuf[len++] = SEP; | ||||
3673 | wcscpy(wnamebuf + len, L"*.*"); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3674 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3675 | if ((list = PyList_New(0)) == NULL) { |
3676 | goto exit; | ||||
3677 | } | ||||
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3678 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3679 | hFindFile = FindFirstFileW(wnamebuf, &wFileData); |
Antoine Pitrou | b73caab | 2010-08-09 23:39:31 +0000 | [diff] [blame] | 3680 | Py_END_ALLOW_THREADS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3681 | if (hFindFile == INVALID_HANDLE_VALUE) { |
3682 | int error = GetLastError(); | ||||
3683 | if (error == ERROR_FILE_NOT_FOUND) | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3684 | goto exit; |
3685 | Py_DECREF(list); | ||||
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3686 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3687 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3688 | } |
3689 | do { | ||||
3690 | /* Skip over . and .. */ | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3691 | if (wcscmp(wFileData.cFileName, L".") != 0 && |
3692 | wcscmp(wFileData.cFileName, L"..") != 0) { | ||||
3693 | v = PyUnicode_FromWideChar(wFileData.cFileName, | ||||
3694 | wcslen(wFileData.cFileName)); | ||||
3695 | if (path->narrow && v) { | ||||
3696 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); | ||||
3697 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3698 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3699 | Py_DECREF(list); |
3700 | list = NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3701 | break; |
3702 | } | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3703 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3704 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3705 | Py_DECREF(list); |
3706 | list = NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3707 | break; |
3708 | } | ||||
3709 | Py_DECREF(v); | ||||
3710 | } | ||||
3711 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3712 | result = FindNextFileW(hFindFile, &wFileData); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3713 | Py_END_ALLOW_THREADS |
3714 | /* FindNextFile sets error to ERROR_NO_MORE_FILES if | ||||
3715 | it got to the end of the directory. */ | ||||
3716 | if (!result && GetLastError() != ERROR_NO_MORE_FILES) { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3717 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3718 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3719 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3720 | } |
3721 | } while (result == TRUE); | ||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3722 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3723 | exit: |
3724 | if (hFindFile != INVALID_HANDLE_VALUE) { | ||||
3725 | if (FindClose(hFindFile) == FALSE) { | ||||
3726 | if (list != NULL) { | ||||
3727 | Py_DECREF(list); | ||||
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3728 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3729 | } |
3730 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3731 | } |
Victor Stinner | b640491 | 2013-07-07 16:21:41 +0200 | [diff] [blame] | 3732 | PyMem_Free(wnamebuf); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 3733 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3734 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3735 | } /* end of _listdir_windows_no_opendir */ |
3736 | |||||
3737 | #else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */ | ||||
3738 | |||||
3739 | static PyObject * | ||||
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3740 | _posix_listdir(path_t *path, PyObject *list) |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3741 | { |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3742 | PyObject *v; |
3743 | DIR *dirp = NULL; | ||||
3744 | struct dirent *ep; | ||||
3745 | int return_str; /* if false, return bytes */ | ||||
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3746 | #ifdef HAVE_FDOPENDIR |
3747 | int fd = -1; | ||||
3748 | #endif | ||||
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3749 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3750 | errno = 0; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3751 | #ifdef HAVE_FDOPENDIR |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3752 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3753 | /* closedir() closes the FD, so we duplicate it */ |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 3754 | fd = _Py_dup(path->fd); |
Victor Stinner | f326665 | 2013-12-19 13:24:49 +0100 | [diff] [blame] | 3755 | if (fd == -1) |
3756 | return NULL; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3757 | |
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3758 | return_str = 1; |
3759 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3760 | Py_BEGIN_ALLOW_THREADS |
3761 | dirp = fdopendir(fd); | ||||
3762 | Py_END_ALLOW_THREADS | ||||
3763 | } | ||||
3764 | else | ||||
3765 | #endif | ||||
3766 | { | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 3767 | const char *name; |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3768 | if (path->narrow) { |
3769 | name = path->narrow; | ||||
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 3770 | /* only return bytes if they specified a bytes-like object */ |
3771 | return_str = !PyObject_CheckBuffer(path->object); | ||||
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3772 | } |
3773 | else { | ||||
3774 | name = "."; | ||||
3775 | return_str = 1; | ||||
3776 | } | ||||
3777 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3778 | Py_BEGIN_ALLOW_THREADS |
3779 | dirp = opendir(name); | ||||
3780 | Py_END_ALLOW_THREADS | ||||
3781 | } | ||||
3782 | |||||
3783 | if (dirp == NULL) { | ||||
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3784 | list = path_error(path); |
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3785 | #ifdef HAVE_FDOPENDIR |
3786 | if (fd != -1) { | ||||
3787 | Py_BEGIN_ALLOW_THREADS | ||||
3788 | close(fd); | ||||
3789 | Py_END_ALLOW_THREADS | ||||
3790 | } | ||||
3791 | #endif | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3792 | goto exit; |
3793 | } | ||||
3794 | if ((list = PyList_New(0)) == NULL) { | ||||
3795 | goto exit; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3796 | } |
3797 | for (;;) { | ||||
3798 | errno = 0; | ||||
3799 | Py_BEGIN_ALLOW_THREADS | ||||
3800 | ep = readdir(dirp); | ||||
3801 | Py_END_ALLOW_THREADS | ||||
3802 | if (ep == NULL) { | ||||
3803 | if (errno == 0) { | ||||
3804 | break; | ||||
3805 | } else { | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3806 | Py_DECREF(list); |
Gregory P. Smith | 40a2160 | 2013-03-20 20:52:50 -0700 | [diff] [blame] | 3807 | list = path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3808 | goto exit; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3809 | } |
3810 | } | ||||
3811 | if (ep->d_name[0] == '.' && | ||||
3812 | (NAMLEN(ep) == 1 || | ||||
3813 | (ep->d_name[1] == '.' && NAMLEN(ep) == 2))) | ||||
3814 | continue; | ||||
Larry Hastings | fdaea06 | 2012-06-25 04:42:23 -0700 | [diff] [blame] | 3815 | if (return_str) |
Victor Stinner | a45598a | 2010-05-14 16:35:39 +0000 | [diff] [blame] | 3816 | v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep)); |
3817 | else | ||||
3818 | v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep)); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3819 | if (v == NULL) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3820 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3821 | break; |
3822 | } | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3823 | if (PyList_Append(list, v) != 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3824 | Py_DECREF(v); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3825 | Py_CLEAR(list); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3826 | break; |
3827 | } | ||||
3828 | Py_DECREF(v); | ||||
3829 | } | ||||
Guido van Rossum | 0ee42cd | 1991-04-08 21:01:03 +0000 | [diff] [blame] | 3830 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3831 | exit: |
3832 | if (dirp != NULL) { | ||||
3833 | Py_BEGIN_ALLOW_THREADS | ||||
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3834 | #ifdef HAVE_FDOPENDIR |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3835 | if (fd > -1) |
3836 | rewinddir(dirp); | ||||
Larry Hastings | 4dbc95e | 2013-08-01 18:18:56 -0700 | [diff] [blame] | 3837 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3838 | closedir(dirp); |
3839 | Py_END_ALLOW_THREADS | ||||
3840 | } | ||||
3841 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 3842 | return list; |
Gregory P. Smith | 16ea14a | 2013-03-20 18:51:33 -0700 | [diff] [blame] | 3843 | } /* end of _posix_listdir */ |
3844 | #endif /* which OS */ | ||||
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 3845 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3846 | |
3847 | /*[clinic input] | ||||
3848 | os.listdir | ||||
3849 | |||||
3850 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None | ||||
3851 | |||||
3852 | Return a list containing the names of the files in the directory. | ||||
3853 | |||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3854 | path can be specified as either str, bytes, or a path-like object. If path is bytes, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3855 | the filenames returned will also be bytes; in all other circumstances |
3856 | the filenames returned will be str. | ||||
3857 | If path is None, uses the path='.'. | ||||
3858 | On some platforms, path may also be specified as an open file descriptor;\ | ||||
3859 | the file descriptor must refer to a directory. | ||||
3860 | If this functionality is unavailable, using it raises NotImplementedError. | ||||
3861 | |||||
3862 | The list is in arbitrary order. It does not include the special | ||||
3863 | entries '.' and '..' even if they are present in the directory. | ||||
3864 | |||||
3865 | |||||
3866 | [clinic start generated code]*/ | ||||
3867 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3868 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3869 | os_listdir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 3870 | /*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3871 | { |
Miss Islington (bot) | 8763d43 | 2019-06-24 09:09:47 -0700 | [diff] [blame] | 3872 | if (PySys_Audit("os.listdir", "O", |
3873 | path->object ? path->object : Py_None) < 0) { | ||||
3874 | return NULL; | ||||
3875 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3876 | #if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR) |
3877 | return _listdir_windows_no_opendir(path, NULL); | ||||
3878 | #else | ||||
3879 | return _posix_listdir(path, NULL); | ||||
3880 | #endif | ||||
3881 | } | ||||
3882 | |||||
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 3883 | #ifdef MS_WINDOWS |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 3884 | /* A helper function for abspath on win32 */ |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3885 | /*[clinic input] |
3886 | os._getfullpathname | ||||
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3887 | |
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3888 | path: path_t |
3889 | / | ||||
3890 | |||||
3891 | [clinic start generated code]*/ | ||||
3892 | |||||
3893 | static PyObject * | ||||
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 3894 | os__getfullpathname_impl(PyObject *module, path_t *path) |
3895 | /*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/ | ||||
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 3896 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3897 | wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf; |
3898 | wchar_t *wtemp; | ||||
3899 | DWORD result; | ||||
3900 | PyObject *v; | ||||
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 3901 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3902 | result = GetFullPathNameW(path->wide, |
3903 | Py_ARRAY_LENGTH(woutbuf), | ||||
3904 | woutbuf, &wtemp); | ||||
3905 | if (result > Py_ARRAY_LENGTH(woutbuf)) { | ||||
3906 | woutbufp = PyMem_New(wchar_t, result); | ||||
3907 | if (!woutbufp) | ||||
3908 | return PyErr_NoMemory(); | ||||
3909 | result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 3910 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 3911 | if (result) { |
3912 | v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp)); | ||||
3913 | if (path->narrow) | ||||
3914 | Py_SETREF(v, PyUnicode_EncodeFSDefault(v)); | ||||
3915 | } else | ||||
3916 | v = win32_error_object("GetFullPathNameW", path->object); | ||||
3917 | if (woutbufp != woutbuf) | ||||
3918 | PyMem_Free(woutbufp); | ||||
3919 | return v; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3920 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3921 | |
Brian Curtin | d25aef5 | 2011-06-13 15:16:04 -0500 | [diff] [blame] | 3922 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3923 | /*[clinic input] |
3924 | os._getfinalpathname | ||||
Brian Curtin | f5e76d0 | 2010-11-24 13:14:05 +0000 | [diff] [blame] | 3925 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3926 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3927 | / |
3928 | |||||
3929 | A helper function for samepath on windows. | ||||
3930 | [clinic start generated code]*/ | ||||
3931 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3932 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3933 | os__getfinalpathname_impl(PyObject *module, path_t *path) |
3934 | /*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/ | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3935 | { |
3936 | HANDLE hFile; | ||||
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3937 | wchar_t buf[MAXPATHLEN], *target_path = buf; |
3938 | int buf_size = Py_ARRAY_LENGTH(buf); | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3939 | int result_length; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3940 | PyObject *result; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3941 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3942 | Py_BEGIN_ALLOW_THREADS |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3943 | hFile = CreateFileW( |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3944 | path->wide, |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3945 | 0, /* desired access */ |
3946 | 0, /* share mode */ | ||||
3947 | NULL, /* security attributes */ | ||||
3948 | OPEN_EXISTING, | ||||
3949 | /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */ | ||||
3950 | FILE_FLAG_BACKUP_SEMANTICS, | ||||
3951 | NULL); | ||||
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3952 | Py_END_ALLOW_THREADS |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 3953 | |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3954 | if (hFile == INVALID_HANDLE_VALUE) { |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3955 | return win32_error_object("CreateFileW", path->object); |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3956 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3957 | |
3958 | /* We have a good handle to the target, use it to determine the | ||||
3959 | target path name. */ | ||||
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3960 | while (1) { |
3961 | Py_BEGIN_ALLOW_THREADS | ||||
3962 | result_length = GetFinalPathNameByHandleW(hFile, target_path, | ||||
3963 | buf_size, VOLUME_NAME_DOS); | ||||
3964 | Py_END_ALLOW_THREADS | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3965 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3966 | if (!result_length) { |
3967 | result = win32_error_object("GetFinalPathNameByHandleW", | ||||
3968 | path->object); | ||||
3969 | goto cleanup; | ||||
3970 | } | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3971 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3972 | if (result_length < buf_size) { |
3973 | break; | ||||
3974 | } | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3975 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3976 | wchar_t *tmp; |
3977 | tmp = PyMem_Realloc(target_path != buf ? target_path : NULL, | ||||
3978 | result_length * sizeof(*tmp)); | ||||
3979 | if (!tmp) { | ||||
3980 | result = PyErr_NoMemory(); | ||||
3981 | goto cleanup; | ||||
3982 | } | ||||
3983 | |||||
3984 | buf_size = result_length; | ||||
3985 | target_path = tmp; | ||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3986 | } |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 3987 | |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 3988 | result = PyUnicode_FromWideChar(target_path, result_length); |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 3989 | if (result && path->narrow) { |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3990 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 3991 | } |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 3992 | |
Alexey Izbyshev | 3b20d34 | 2018-03-08 19:03:25 +0300 | [diff] [blame] | 3993 | cleanup: |
3994 | if (target_path != buf) { | ||||
3995 | PyMem_Free(target_path); | ||||
3996 | } | ||||
3997 | CloseHandle(hFile); | ||||
3998 | return result; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 3999 | } |
Brian Curtin | 6285774 | 2010-09-06 17:07:27 +0000 | [diff] [blame] | 4000 | |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4001 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4002 | /*[clinic input] |
4003 | os._getvolumepathname | ||||
4004 | |||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4005 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4006 | |
4007 | A helper function for ismount on Win32. | ||||
4008 | [clinic start generated code]*/ | ||||
4009 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4010 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4011 | os__getvolumepathname_impl(PyObject *module, path_t *path) |
4012 | /*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4013 | { |
4014 | PyObject *result; | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4015 | wchar_t *mountpath=NULL; |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4016 | size_t buflen; |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4017 | BOOL ret; |
4018 | |||||
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4019 | /* Volume path should be shorter than entire path */ |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4020 | buflen = Py_MAX(path->length, MAX_PATH); |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4021 | |
Victor Stinner | 850a18e | 2017-10-24 16:53:32 -0700 | [diff] [blame] | 4022 | if (buflen > PY_DWORD_MAX) { |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4023 | PyErr_SetString(PyExc_OverflowError, "path too long"); |
4024 | return NULL; | ||||
4025 | } | ||||
4026 | |||||
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 4027 | mountpath = PyMem_New(wchar_t, buflen); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4028 | if (mountpath == NULL) |
4029 | return PyErr_NoMemory(); | ||||
4030 | |||||
4031 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4032 | ret = GetVolumePathNameW(path->wide, mountpath, |
Victor Stinner | 6edddfa | 2013-11-24 19:22:57 +0100 | [diff] [blame] | 4033 | Py_SAFE_DOWNCAST(buflen, size_t, DWORD)); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4034 | Py_END_ALLOW_THREADS |
4035 | |||||
4036 | if (!ret) { | ||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4037 | result = win32_error_object("_getvolumepathname", path->object); |
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4038 | goto exit; |
4039 | } | ||||
4040 | result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath)); | ||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 4041 | if (path->narrow) |
4042 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); | ||||
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4043 | |
4044 | exit: | ||||
4045 | PyMem_Free(mountpath); | ||||
4046 | return result; | ||||
4047 | } | ||||
Tim Golden | 6b52806 | 2013-08-01 12:44:00 +0100 | [diff] [blame] | 4048 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 4049 | #endif /* MS_WINDOWS */ |
Mark Hammond | ef8b654 | 2001-05-13 08:04:26 +0000 | [diff] [blame] | 4050 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4051 | |
4052 | /*[clinic input] | ||||
4053 | os.mkdir | ||||
4054 | |||||
4055 | path : path_t | ||||
4056 | |||||
4057 | mode: int = 0o777 | ||||
4058 | |||||
4059 | * | ||||
4060 | |||||
4061 | dir_fd : dir_fd(requires='mkdirat') = None | ||||
4062 | |||||
4063 | # "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\ | ||||
4064 | |||||
4065 | Create a directory. | ||||
4066 | |||||
4067 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
4068 | and path should be relative; path will then be relative to that directory. | ||||
4069 | dir_fd may not be implemented on your platform. | ||||
4070 | If it is unavailable, using it will raise a NotImplementedError. | ||||
4071 | |||||
4072 | The mode argument is ignored on Windows. | ||||
4073 | [clinic start generated code]*/ | ||||
4074 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4075 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4076 | os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
4077 | /*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4078 | { |
4079 | int result; | ||||
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4080 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 4081 | if (PySys_Audit("os.mkdir", "Oii", path->object, mode, |
4082 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
4083 | return NULL; | ||||
4084 | } | ||||
4085 | |||||
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4086 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4087 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4088 | result = CreateDirectoryW(path->wide, NULL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4089 | Py_END_ALLOW_THREADS |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4090 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4091 | if (!result) |
4092 | return path_error(path); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4093 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4094 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4095 | #if HAVE_MKDIRAT |
4096 | if (dir_fd != DEFAULT_DIR_FD) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4097 | result = mkdirat(dir_fd, path->narrow, mode); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4098 | else |
4099 | #endif | ||||
Erik Bray | 03eb11f | 2017-10-27 14:27:06 +0200 | [diff] [blame] | 4100 | #if defined(__WATCOMC__) && !defined(__QNX__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4101 | result = mkdir(path->narrow); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4102 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4103 | result = mkdir(path->narrow, mode); |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4104 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4105 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4106 | if (result < 0) |
4107 | return path_error(path); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4108 | #endif /* MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4109 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4110 | } |
4111 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4112 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4113 | /* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */ |
4114 | #if defined(HAVE_SYS_RESOURCE_H) | ||||
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4115 | #include <sys/resource.h> |
4116 | #endif | ||||
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4117 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4118 | |
4119 | #ifdef HAVE_NICE | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4120 | /*[clinic input] |
4121 | os.nice | ||||
4122 | |||||
4123 | increment: int | ||||
4124 | / | ||||
4125 | |||||
4126 | Add increment to the priority of process and return the new priority. | ||||
4127 | [clinic start generated code]*/ | ||||
4128 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4129 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4130 | os_nice_impl(PyObject *module, int increment) |
4131 | /*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4132 | { |
4133 | int value; | ||||
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4134 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4135 | /* There are two flavours of 'nice': one that returns the new |
4136 | priority (as required by almost all standards out there) and the | ||||
Benjamin Peterson | 288d1da | 2017-09-28 22:44:27 -0700 | [diff] [blame] | 4137 | Linux/FreeBSD one, which returns '0' on success and advices |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4138 | the use of getpriority() to get the new priority. |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 4139 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4140 | If we are of the nice family that returns the new priority, we |
4141 | need to clear errno before the call, and check if errno is filled | ||||
4142 | before calling posix_error() on a returnvalue of -1, because the | ||||
4143 | -1 may be the actual new priority! */ | ||||
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4144 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4145 | errno = 0; |
4146 | value = nice(increment); | ||||
Thomas Wouters | e38b2f1 | 2001-07-11 22:35:31 +0000 | [diff] [blame] | 4147 | #if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4148 | if (value == 0) |
4149 | value = getpriority(PRIO_PROCESS, 0); | ||||
Thomas Wouters | c2c12dc | 2001-07-11 14:45:34 +0000 | [diff] [blame] | 4150 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4151 | if (value == -1 && errno != 0) |
4152 | /* either nice() or getpriority() returned an error */ | ||||
4153 | return posix_error(); | ||||
4154 | return PyLong_FromLong((long) value); | ||||
Guido van Rossum | 775f4da | 1993-01-09 17:18:52 +0000 | [diff] [blame] | 4155 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4156 | #endif /* HAVE_NICE */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4157 | |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4158 | |
4159 | #ifdef HAVE_GETPRIORITY | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4160 | /*[clinic input] |
4161 | os.getpriority | ||||
4162 | |||||
4163 | which: int | ||||
4164 | who: int | ||||
4165 | |||||
4166 | Return program scheduling priority. | ||||
4167 | [clinic start generated code]*/ | ||||
4168 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4169 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4170 | os_getpriority_impl(PyObject *module, int which, int who) |
4171 | /*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4172 | { |
4173 | int retval; | ||||
4174 | |||||
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4175 | errno = 0; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4176 | retval = getpriority(which, who); |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4177 | if (errno != 0) |
4178 | return posix_error(); | ||||
4179 | return PyLong_FromLong((long)retval); | ||||
4180 | } | ||||
4181 | #endif /* HAVE_GETPRIORITY */ | ||||
4182 | |||||
4183 | |||||
4184 | #ifdef HAVE_SETPRIORITY | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4185 | /*[clinic input] |
4186 | os.setpriority | ||||
4187 | |||||
4188 | which: int | ||||
4189 | who: int | ||||
4190 | priority: int | ||||
4191 | |||||
4192 | Set program scheduling priority. | ||||
4193 | [clinic start generated code]*/ | ||||
4194 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4195 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4196 | os_setpriority_impl(PyObject *module, int which, int who, int priority) |
4197 | /*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4198 | { |
4199 | int retval; | ||||
4200 | |||||
4201 | retval = setpriority(which, who, priority); | ||||
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 4202 | if (retval == -1) |
4203 | return posix_error(); | ||||
4204 | Py_RETURN_NONE; | ||||
4205 | } | ||||
4206 | #endif /* HAVE_SETPRIORITY */ | ||||
4207 | |||||
4208 | |||||
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 4209 | static PyObject * |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4210 | 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] | 4211 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4212 | const char *function_name = is_replace ? "replace" : "rename"; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4213 | int dir_fd_specified; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4214 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4215 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4216 | BOOL result; |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4217 | int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4218 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4219 | int result; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 4220 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4221 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4222 | dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) || |
4223 | (dst_dir_fd != DEFAULT_DIR_FD); | ||||
4224 | #ifndef HAVE_RENAMEAT | ||||
4225 | if (dir_fd_specified) { | ||||
4226 | argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4227 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4228 | } |
4229 | #endif | ||||
4230 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 4231 | if (PySys_Audit("os.rename", "OOii", src->object, dst->object, |
4232 | src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd, | ||||
4233 | dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) { | ||||
4234 | return NULL; | ||||
4235 | } | ||||
4236 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4237 | #ifdef MS_WINDOWS |
4238 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4239 | result = MoveFileExW(src->wide, dst->wide, flags); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4240 | Py_END_ALLOW_THREADS |
4241 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4242 | if (!result) |
4243 | return path_error2(src, dst); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4244 | |
4245 | #else | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4246 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
4247 | PyErr_Format(PyExc_ValueError, | ||||
4248 | "%s: src and dst must be the same type", function_name); | ||||
4249 | return NULL; | ||||
4250 | } | ||||
4251 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4252 | Py_BEGIN_ALLOW_THREADS |
4253 | #ifdef HAVE_RENAMEAT | ||||
4254 | if (dir_fd_specified) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4255 | result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4256 | else |
4257 | #endif | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4258 | result = rename(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4259 | Py_END_ALLOW_THREADS |
4260 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4261 | if (result) |
4262 | return path_error2(src, dst); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4263 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4264 | Py_RETURN_NONE; |
4265 | } | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4266 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4267 | |
4268 | /*[clinic input] | ||||
4269 | os.rename | ||||
4270 | |||||
4271 | src : path_t | ||||
4272 | dst : path_t | ||||
4273 | * | ||||
4274 | src_dir_fd : dir_fd = None | ||||
4275 | dst_dir_fd : dir_fd = None | ||||
4276 | |||||
4277 | Rename a file or directory. | ||||
4278 | |||||
4279 | If either src_dir_fd or dst_dir_fd is not None, it should be a file | ||||
4280 | descriptor open to a directory, and the respective path string (src or dst) | ||||
4281 | should be relative; the path will then be relative to that directory. | ||||
4282 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. | ||||
4283 | If they are unavailable, using them will raise a NotImplementedError. | ||||
4284 | [clinic start generated code]*/ | ||||
4285 | |||||
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4286 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4287 | os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4288 | int dst_dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4289 | /*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/ |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4290 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4291 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0); |
Antoine Pitrou | f3b2d88 | 2012-01-30 22:08:52 +0100 | [diff] [blame] | 4292 | } |
4293 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4294 | |
4295 | /*[clinic input] | ||||
4296 | os.replace = os.rename | ||||
4297 | |||||
4298 | Rename a file or directory, overwriting the destination. | ||||
4299 | |||||
4300 | If either src_dir_fd or dst_dir_fd is not None, it should be a file | ||||
4301 | descriptor open to a directory, and the respective path string (src or dst) | ||||
4302 | should be relative; the path will then be relative to that directory. | ||||
4303 | src_dir_fd and dst_dir_fd, may not be implemented on your platform. | ||||
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4304 | If they are unavailable, using them will raise a NotImplementedError. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4305 | [clinic start generated code]*/ |
4306 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4307 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4308 | os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd, |
4309 | int dst_dir_fd) | ||||
Anthony Sottile | 73d6002 | 2019-02-12 23:15:54 -0500 | [diff] [blame] | 4310 | /*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4311 | { |
4312 | return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1); | ||||
4313 | } | ||||
4314 | |||||
4315 | |||||
4316 | /*[clinic input] | ||||
4317 | os.rmdir | ||||
4318 | |||||
4319 | path: path_t | ||||
4320 | * | ||||
4321 | dir_fd: dir_fd(requires='unlinkat') = None | ||||
4322 | |||||
4323 | Remove a directory. | ||||
4324 | |||||
4325 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
4326 | and path should be relative; path will then be relative to that directory. | ||||
4327 | dir_fd may not be implemented on your platform. | ||||
4328 | If it is unavailable, using it will raise a NotImplementedError. | ||||
4329 | [clinic start generated code]*/ | ||||
4330 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4331 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4332 | os_rmdir_impl(PyObject *module, path_t *path, int dir_fd) |
4333 | /*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4334 | { |
4335 | int result; | ||||
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4336 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 4337 | if (PySys_Audit("os.rmdir", "Oi", path->object, |
4338 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
4339 | return NULL; | ||||
4340 | } | ||||
4341 | |||||
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4342 | Py_BEGIN_ALLOW_THREADS |
4343 | #ifdef MS_WINDOWS | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4344 | /* Windows, success=1, UNIX, success=0 */ |
4345 | result = !RemoveDirectoryW(path->wide); | ||||
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4346 | #else |
4347 | #ifdef HAVE_UNLINKAT | ||||
4348 | if (dir_fd != DEFAULT_DIR_FD) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4349 | result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4350 | else |
4351 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4352 | result = rmdir(path->narrow); |
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4353 | #endif |
4354 | Py_END_ALLOW_THREADS | ||||
4355 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4356 | if (result) |
4357 | return path_error(path); | ||||
Larry Hastings | b698d8e | 2012-06-23 16:55:07 -0700 | [diff] [blame] | 4358 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4359 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4360 | } |
4361 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4362 | |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4363 | #ifdef HAVE_SYSTEM |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4364 | #ifdef MS_WINDOWS |
4365 | /*[clinic input] | ||||
4366 | os.system -> long | ||||
4367 | |||||
4368 | command: Py_UNICODE | ||||
4369 | |||||
4370 | Execute the command in a subshell. | ||||
4371 | [clinic start generated code]*/ | ||||
4372 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4373 | static long |
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 4374 | os_system_impl(PyObject *module, const Py_UNICODE *command) |
4375 | /*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4376 | { |
4377 | long result; | ||||
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4378 | |
Miss Islington (bot) | 5fb8142 | 2019-10-18 09:32:14 -0700 | [diff] [blame] | 4379 | if (PySys_Audit("os.system", "(u)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4380 | return -1; |
4381 | } | ||||
4382 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4383 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4384 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4385 | result = _wsystem(command); |
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 4386 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4387 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4388 | return result; |
4389 | } | ||||
4390 | #else /* MS_WINDOWS */ | ||||
4391 | /*[clinic input] | ||||
4392 | os.system -> long | ||||
4393 | |||||
4394 | command: FSConverter | ||||
4395 | |||||
4396 | Execute the command in a subshell. | ||||
4397 | [clinic start generated code]*/ | ||||
4398 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4399 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4400 | os_system_impl(PyObject *module, PyObject *command) |
4401 | /*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4402 | { |
4403 | long result; | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 4404 | const char *bytes = PyBytes_AsString(command); |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4405 | |
Miss Islington (bot) | 5fb8142 | 2019-10-18 09:32:14 -0700 | [diff] [blame] | 4406 | if (PySys_Audit("os.system", "(O)", command) < 0) { |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 4407 | return -1; |
4408 | } | ||||
4409 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4410 | Py_BEGIN_ALLOW_THREADS |
4411 | result = system(bytes); | ||||
4412 | Py_END_ALLOW_THREADS | ||||
4413 | return result; | ||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4414 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 4415 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4416 | #endif /* HAVE_SYSTEM */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4417 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4418 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4419 | /*[clinic input] |
4420 | os.umask | ||||
4421 | |||||
4422 | mask: int | ||||
4423 | / | ||||
4424 | |||||
4425 | Set the current numeric umask and return the previous umask. | ||||
4426 | [clinic start generated code]*/ | ||||
4427 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4428 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4429 | os_umask_impl(PyObject *module, int mask) |
4430 | /*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4431 | { |
4432 | int i = (int)umask(mask); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4433 | if (i < 0) |
4434 | return posix_error(); | ||||
4435 | return PyLong_FromLong((long)i); | ||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4436 | } |
4437 | |||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4438 | #ifdef MS_WINDOWS |
4439 | |||||
4440 | /* override the default DeleteFileW behavior so that directory | ||||
4441 | symlinks can be removed with this function, the same as with | ||||
4442 | Unix symlinks */ | ||||
4443 | BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName) | ||||
4444 | { | ||||
4445 | WIN32_FILE_ATTRIBUTE_DATA info; | ||||
4446 | WIN32_FIND_DATAW find_data; | ||||
4447 | HANDLE find_data_handle; | ||||
4448 | int is_directory = 0; | ||||
4449 | int is_link = 0; | ||||
4450 | |||||
4451 | if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) { | ||||
4452 | is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY; | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 4453 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4454 | /* Get WIN32_FIND_DATA structure for the path to determine if |
4455 | it is a symlink */ | ||||
4456 | if(is_directory && | ||||
4457 | info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { | ||||
4458 | find_data_handle = FindFirstFileW(lpFileName, &find_data); | ||||
4459 | |||||
4460 | if(find_data_handle != INVALID_HANDLE_VALUE) { | ||||
Tim Golden | 0321cf2 | 2014-05-05 19:46:17 +0100 | [diff] [blame] | 4461 | /* IO_REPARSE_TAG_SYMLINK if it is a symlink and |
4462 | IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */ | ||||
4463 | is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK || | ||||
4464 | find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT; | ||||
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 4465 | FindClose(find_data_handle); |
4466 | } | ||||
4467 | } | ||||
4468 | } | ||||
4469 | |||||
4470 | if (is_directory && is_link) | ||||
4471 | return RemoveDirectoryW(lpFileName); | ||||
4472 | |||||
4473 | return DeleteFileW(lpFileName); | ||||
4474 | } | ||||
4475 | #endif /* MS_WINDOWS */ | ||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4476 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4477 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4478 | /*[clinic input] |
4479 | os.unlink | ||||
4480 | |||||
4481 | path: path_t | ||||
4482 | * | ||||
4483 | dir_fd: dir_fd(requires='unlinkat')=None | ||||
4484 | |||||
4485 | Remove a file (same as remove()). | ||||
4486 | |||||
4487 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
4488 | and path should be relative; path will then be relative to that directory. | ||||
4489 | dir_fd may not be implemented on your platform. | ||||
4490 | If it is unavailable, using it will raise a NotImplementedError. | ||||
4491 | |||||
4492 | [clinic start generated code]*/ | ||||
4493 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4494 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4495 | os_unlink_impl(PyObject *module, path_t *path, int dir_fd) |
4496 | /*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4497 | { |
4498 | int result; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4499 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 4500 | if (PySys_Audit("os.remove", "Oi", path->object, |
4501 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
4502 | return NULL; | ||||
4503 | } | ||||
4504 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4505 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4506 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4507 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4508 | /* Windows, success=1, UNIX, success=0 */ |
4509 | result = !Py_DeleteFileW(path->wide); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4510 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4511 | #ifdef HAVE_UNLINKAT |
4512 | if (dir_fd != DEFAULT_DIR_FD) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4513 | result = unlinkat(dir_fd, path->narrow, 0); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4514 | else |
4515 | #endif /* HAVE_UNLINKAT */ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4516 | result = unlink(path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4517 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 4518 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4519 | Py_END_ALLOW_THREADS |
4520 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4521 | if (result) |
4522 | return path_error(path); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4523 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4524 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4525 | } |
4526 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4527 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4528 | /*[clinic input] |
4529 | os.remove = os.unlink | ||||
4530 | |||||
4531 | Remove a file (same as unlink()). | ||||
4532 | |||||
4533 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
4534 | and path should be relative; path will then be relative to that directory. | ||||
4535 | dir_fd may not be implemented on your platform. | ||||
4536 | If it is unavailable, using it will raise a NotImplementedError. | ||||
4537 | [clinic start generated code]*/ | ||||
4538 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4539 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4540 | os_remove_impl(PyObject *module, path_t *path, int dir_fd) |
4541 | /*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4542 | { |
4543 | return os_unlink_impl(module, path, dir_fd); | ||||
4544 | } | ||||
4545 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 4546 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4547 | static PyStructSequence_Field uname_result_fields[] = { |
4548 | {"sysname", "operating system name"}, | ||||
4549 | {"nodename", "name of machine on network (implementation-defined)"}, | ||||
4550 | {"release", "operating system release"}, | ||||
4551 | {"version", "operating system version"}, | ||||
4552 | {"machine", "hardware identifier"}, | ||||
4553 | {NULL} | ||||
4554 | }; | ||||
4555 | |||||
4556 | PyDoc_STRVAR(uname_result__doc__, | ||||
4557 | "uname_result: Result from os.uname().\n\n\ | ||||
4558 | This object may be accessed either as a tuple of\n\ | ||||
4559 | (sysname, nodename, release, version, machine),\n\ | ||||
4560 | or via the attributes sysname, nodename, release, version, and machine.\n\ | ||||
4561 | \n\ | ||||
4562 | See os.uname for more information."); | ||||
4563 | |||||
4564 | static PyStructSequence_Desc uname_result_desc = { | ||||
4565 | "uname_result", /* name */ | ||||
4566 | uname_result__doc__, /* doc */ | ||||
4567 | uname_result_fields, | ||||
4568 | 5 | ||||
4569 | }; | ||||
4570 | |||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4571 | static PyTypeObject* UnameResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4572 | |
4573 | |||||
4574 | #ifdef HAVE_UNAME | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4575 | /*[clinic input] |
4576 | os.uname | ||||
4577 | |||||
4578 | Return an object identifying the current operating system. | ||||
4579 | |||||
4580 | The object behaves like a named tuple with the following fields: | ||||
4581 | (sysname, nodename, release, version, machine) | ||||
4582 | |||||
4583 | [clinic start generated code]*/ | ||||
4584 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4585 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4586 | os_uname_impl(PyObject *module) |
4587 | /*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/ | ||||
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4588 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4589 | struct utsname u; |
4590 | int res; | ||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4591 | PyObject *value; |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 4592 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4593 | Py_BEGIN_ALLOW_THREADS |
4594 | res = uname(&u); | ||||
4595 | Py_END_ALLOW_THREADS | ||||
4596 | if (res < 0) | ||||
4597 | return posix_error(); | ||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4598 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 4599 | value = PyStructSequence_New(UnameResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4600 | if (value == NULL) |
4601 | return NULL; | ||||
4602 | |||||
4603 | #define SET(i, field) \ | ||||
4604 | { \ | ||||
Victor Stinner | a534fc4 | 2013-06-03 22:07:27 +0200 | [diff] [blame] | 4605 | PyObject *o = PyUnicode_DecodeFSDefault(field); \ |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 4606 | if (!o) { \ |
4607 | Py_DECREF(value); \ | ||||
4608 | return NULL; \ | ||||
4609 | } \ | ||||
4610 | PyStructSequence_SET_ITEM(value, i, o); \ | ||||
4611 | } \ | ||||
4612 | |||||
4613 | SET(0, u.sysname); | ||||
4614 | SET(1, u.nodename); | ||||
4615 | SET(2, u.release); | ||||
4616 | SET(3, u.version); | ||||
4617 | SET(4, u.machine); | ||||
4618 | |||||
4619 | #undef SET | ||||
4620 | |||||
4621 | return value; | ||||
Guido van Rossum | c39de5f | 1992-02-05 11:15:54 +0000 | [diff] [blame] | 4622 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 4623 | #endif /* HAVE_UNAME */ |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4624 | |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4625 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4626 | |
4627 | typedef struct { | ||||
4628 | int now; | ||||
4629 | time_t atime_s; | ||||
4630 | long atime_ns; | ||||
4631 | time_t mtime_s; | ||||
4632 | long mtime_ns; | ||||
4633 | } utime_t; | ||||
4634 | |||||
4635 | /* | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4636 | * these macros assume that "ut" is a pointer to a utime_t |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4637 | * they also intentionally leak the declaration of a pointer named "time" |
4638 | */ | ||||
4639 | #define UTIME_TO_TIMESPEC \ | ||||
4640 | struct timespec ts[2]; \ | ||||
4641 | struct timespec *time; \ | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4642 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4643 | time = NULL; \ |
4644 | else { \ | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4645 | ts[0].tv_sec = ut->atime_s; \ |
4646 | ts[0].tv_nsec = ut->atime_ns; \ | ||||
4647 | ts[1].tv_sec = ut->mtime_s; \ | ||||
4648 | ts[1].tv_nsec = ut->mtime_ns; \ | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4649 | time = ts; \ |
4650 | } \ | ||||
4651 | |||||
4652 | #define UTIME_TO_TIMEVAL \ | ||||
4653 | struct timeval tv[2]; \ | ||||
4654 | struct timeval *time; \ | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4655 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4656 | time = NULL; \ |
4657 | else { \ | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4658 | tv[0].tv_sec = ut->atime_s; \ |
4659 | tv[0].tv_usec = ut->atime_ns / 1000; \ | ||||
4660 | tv[1].tv_sec = ut->mtime_s; \ | ||||
4661 | tv[1].tv_usec = ut->mtime_ns / 1000; \ | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4662 | time = tv; \ |
4663 | } \ | ||||
4664 | |||||
4665 | #define UTIME_TO_UTIMBUF \ | ||||
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4666 | struct utimbuf u; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4667 | struct utimbuf *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4668 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4669 | time = NULL; \ |
4670 | else { \ | ||||
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4671 | u.actime = ut->atime_s; \ |
4672 | u.modtime = ut->mtime_s; \ | ||||
4673 | time = &u; \ | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4674 | } |
4675 | |||||
4676 | #define UTIME_TO_TIME_T \ | ||||
4677 | time_t timet[2]; \ | ||||
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4678 | time_t *time; \ |
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4679 | if (ut->now) \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4680 | time = NULL; \ |
4681 | else { \ | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4682 | timet[0] = ut->atime_s; \ |
4683 | timet[1] = ut->mtime_s; \ | ||||
Georg Brandl | e1a7d9d | 2014-10-12 08:45:15 +0200 | [diff] [blame] | 4684 | time = timet; \ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4685 | } \ |
4686 | |||||
4687 | |||||
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4688 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4689 | |
4690 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4691 | utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4692 | { |
4693 | #ifdef HAVE_UTIMENSAT | ||||
4694 | int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW; | ||||
4695 | UTIME_TO_TIMESPEC; | ||||
4696 | return utimensat(dir_fd, path, time, flags); | ||||
4697 | #elif defined(HAVE_FUTIMESAT) | ||||
4698 | UTIME_TO_TIMEVAL; | ||||
4699 | /* | ||||
4700 | * follow_symlinks will never be false here; | ||||
4701 | * we only allow !follow_symlinks and dir_fd together | ||||
4702 | * if we have utimensat() | ||||
4703 | */ | ||||
4704 | assert(follow_symlinks); | ||||
4705 | return futimesat(dir_fd, path, time); | ||||
4706 | #endif | ||||
4707 | } | ||||
4708 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4709 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter |
4710 | #else | ||||
4711 | #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4712 | #endif |
4713 | |||||
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4714 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4715 | |
4716 | static int | ||||
Victor Stinner | 484df00 | 2014-10-09 13:52:31 +0200 | [diff] [blame] | 4717 | utime_fd(utime_t *ut, int fd) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4718 | { |
4719 | #ifdef HAVE_FUTIMENS | ||||
4720 | UTIME_TO_TIMESPEC; | ||||
4721 | return futimens(fd, time); | ||||
4722 | #else | ||||
4723 | UTIME_TO_TIMEVAL; | ||||
4724 | return futimes(fd, time); | ||||
4725 | #endif | ||||
4726 | } | ||||
4727 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4728 | #define PATH_UTIME_HAVE_FD 1 |
4729 | #else | ||||
4730 | #define PATH_UTIME_HAVE_FD 0 | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4731 | #endif |
4732 | |||||
Victor Stinner | 5ebae87 | 2015-09-22 01:29:33 +0200 | [diff] [blame] | 4733 | #if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES) |
4734 | # define UTIME_HAVE_NOFOLLOW_SYMLINKS | ||||
4735 | #endif | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4736 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4737 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4738 | |
4739 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4740 | utime_nofollow_symlinks(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4741 | { |
4742 | #ifdef HAVE_UTIMENSAT | ||||
4743 | UTIME_TO_TIMESPEC; | ||||
4744 | return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW); | ||||
4745 | #else | ||||
4746 | UTIME_TO_TIMEVAL; | ||||
4747 | return lutimes(path, time); | ||||
4748 | #endif | ||||
4749 | } | ||||
4750 | |||||
4751 | #endif | ||||
4752 | |||||
4753 | #ifndef MS_WINDOWS | ||||
4754 | |||||
4755 | static int | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 4756 | utime_default(utime_t *ut, const char *path) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4757 | { |
4758 | #ifdef HAVE_UTIMENSAT | ||||
4759 | UTIME_TO_TIMESPEC; | ||||
4760 | return utimensat(DEFAULT_DIR_FD, path, time, 0); | ||||
4761 | #elif defined(HAVE_UTIMES) | ||||
4762 | UTIME_TO_TIMEVAL; | ||||
4763 | return utimes(path, time); | ||||
4764 | #elif defined(HAVE_UTIME_H) | ||||
4765 | UTIME_TO_UTIMBUF; | ||||
4766 | return utime(path, time); | ||||
4767 | #else | ||||
4768 | UTIME_TO_TIME_T; | ||||
4769 | return utime(path, time); | ||||
4770 | #endif | ||||
4771 | } | ||||
4772 | |||||
4773 | #endif | ||||
4774 | |||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4775 | static int |
4776 | split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) | ||||
4777 | { | ||||
4778 | int result = 0; | ||||
Benjamin Peterson | fbd85a0 | 2012-05-04 11:06:09 -0400 | [diff] [blame] | 4779 | PyObject *divmod; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4780 | divmod = PyNumber_Divmod(py_long, billion); |
4781 | if (!divmod) | ||||
4782 | goto exit; | ||||
Oren Milman | 0bd1a2d | 2018-09-12 22:14:35 +0300 | [diff] [blame] | 4783 | if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { |
4784 | PyErr_Format(PyExc_TypeError, | ||||
4785 | "%.200s.__divmod__() must return a 2-tuple, not %.200s", | ||||
4786 | Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); | ||||
4787 | goto exit; | ||||
4788 | } | ||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4789 | *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); |
4790 | if ((*s == -1) && PyErr_Occurred()) | ||||
4791 | goto exit; | ||||
4792 | *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1)); | ||||
Benjamin Peterson | 35a8f0d | 2012-05-04 01:10:59 -0400 | [diff] [blame] | 4793 | if ((*ns == -1) && PyErr_Occurred()) |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4794 | goto exit; |
4795 | |||||
4796 | result = 1; | ||||
4797 | exit: | ||||
4798 | Py_XDECREF(divmod); | ||||
4799 | return result; | ||||
4800 | } | ||||
4801 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4802 | |
4803 | /*[clinic input] | ||||
4804 | os.utime | ||||
4805 | |||||
4806 | path: path_t(allow_fd='PATH_UTIME_HAVE_FD') | ||||
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 4807 | times: object = None |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4808 | * |
4809 | ns: object = NULL | ||||
4810 | dir_fd: dir_fd(requires='futimensat') = None | ||||
4811 | follow_symlinks: bool=True | ||||
4812 | |||||
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4813 | # "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4814 | |
4815 | Set the access and modified time of path. | ||||
4816 | |||||
4817 | path may always be specified as a string. | ||||
4818 | On some platforms, path may also be specified as an open file descriptor. | ||||
4819 | If this functionality is unavailable, using it raises an exception. | ||||
4820 | |||||
4821 | If times is not None, it must be a tuple (atime, mtime); | ||||
4822 | atime and mtime should be expressed as float seconds since the epoch. | ||||
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4823 | If ns is specified, it must be a tuple (atime_ns, mtime_ns); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4824 | atime_ns and mtime_ns should be expressed as integer nanoseconds |
4825 | since the epoch. | ||||
Martin Panter | 0ff8909 | 2015-09-09 01:56:53 +0000 | [diff] [blame] | 4826 | If times is None and ns is unspecified, utime uses the current time. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4827 | Specifying tuples for both times and ns is an error. |
4828 | |||||
4829 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
4830 | and path should be relative; path will then be relative to that directory. | ||||
4831 | If follow_symlinks is False, and the last element of the path is a symbolic | ||||
4832 | link, utime will modify the symbolic link itself instead of the file the | ||||
4833 | link points to. | ||||
4834 | It is an error to use dir_fd or follow_symlinks when specifying path | ||||
4835 | as an open file descriptor. | ||||
4836 | dir_fd and follow_symlinks may not be available on your platform. | ||||
4837 | If they are unavailable, using them will raise a NotImplementedError. | ||||
4838 | |||||
4839 | [clinic start generated code]*/ | ||||
4840 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4841 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 4842 | os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns, |
4843 | int dir_fd, int follow_symlinks) | ||||
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 4844 | /*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4845 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4846 | #ifdef MS_WINDOWS |
4847 | HANDLE hFile; | ||||
4848 | FILETIME atime, mtime; | ||||
4849 | #else | ||||
4850 | int result; | ||||
4851 | #endif | ||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4852 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4853 | utime_t utime; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4854 | |
Christian Heimes | b3c8724 | 2013-08-01 00:08:16 +0200 | [diff] [blame] | 4855 | memset(&utime, 0, sizeof(utime_t)); |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4856 | |
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 4857 | if (times != Py_None && ns) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4858 | PyErr_SetString(PyExc_ValueError, |
4859 | "utime: you may specify either 'times'" | ||||
4860 | " or 'ns' but not both"); | ||||
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4861 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4862 | } |
4863 | |||||
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 4864 | if (times != Py_None) { |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4865 | time_t a_sec, m_sec; |
4866 | long a_nsec, m_nsec; | ||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4867 | if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4868 | PyErr_SetString(PyExc_TypeError, |
4869 | "utime: 'times' must be either" | ||||
4870 | " a tuple of two ints or None"); | ||||
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4871 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4872 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4873 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4874 | if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4875 | &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4876 | _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1), |
Victor Stinner | dca028b | 2015-03-30 01:02:57 +0200 | [diff] [blame] | 4877 | &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4878 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4879 | } |
Antoine Pitrou | cf8a1e5 | 2013-04-17 22:06:44 +0200 | [diff] [blame] | 4880 | utime.atime_s = a_sec; |
4881 | utime.atime_ns = a_nsec; | ||||
4882 | utime.mtime_s = m_sec; | ||||
4883 | utime.mtime_ns = m_nsec; | ||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4884 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4885 | else if (ns) { |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4886 | if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4887 | PyErr_SetString(PyExc_TypeError, |
4888 | "utime: 'ns' must be a tuple of two ints"); | ||||
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4889 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4890 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4891 | utime.now = 0; |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4892 | 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] | 4893 | &utime.atime_s, &utime.atime_ns) || |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4894 | !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1), |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4895 | &utime.mtime_s, &utime.mtime_ns)) { |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4896 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4897 | } |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4898 | } |
4899 | else { | ||||
4900 | /* times and ns are both None/unspecified. use "now". */ | ||||
4901 | utime.now = 1; | ||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4902 | } |
4903 | |||||
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4904 | #if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4905 | if (follow_symlinks_specified("utime", follow_symlinks)) |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4906 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4907 | #endif |
Benjamin Peterson | b399ab2 | 2012-05-04 01:31:13 -0400 | [diff] [blame] | 4908 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4909 | if (path_and_dir_fd_invalid("utime", path, dir_fd) || |
4910 | dir_fd_and_fd_invalid("utime", dir_fd, path->fd) || | ||||
4911 | fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks)) | ||||
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4912 | return NULL; |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4913 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4914 | #if !defined(HAVE_UTIMENSAT) |
4915 | if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) { | ||||
Georg Brandl | 969288e | 2012-06-26 09:25:44 +0200 | [diff] [blame] | 4916 | PyErr_SetString(PyExc_ValueError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4917 | "utime: cannot use dir_fd and follow_symlinks " |
4918 | "together on this platform"); | ||||
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4919 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4920 | } |
4921 | #endif | ||||
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4922 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 4923 | if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None, |
4924 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
4925 | return NULL; | ||||
4926 | } | ||||
4927 | |||||
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4928 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4929 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 4930 | hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0, |
4931 | NULL, OPEN_EXISTING, | ||||
4932 | FILE_FLAG_BACKUP_SEMANTICS, NULL); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4933 | Py_END_ALLOW_THREADS |
4934 | if (hFile == INVALID_HANDLE_VALUE) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4935 | path_error(path); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4936 | return NULL; |
Larry Hastings | b333640 | 2012-05-04 02:31:57 -0700 | [diff] [blame] | 4937 | } |
4938 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4939 | if (utime.now) { |
Antoine Pitrou | 91a7af3 | 2013-11-23 15:23:26 +0100 | [diff] [blame] | 4940 | GetSystemTimeAsFileTime(&mtime); |
4941 | atime = mtime; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4942 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4943 | else { |
Steve Dower | bf1f376 | 2015-02-21 15:26:02 -0800 | [diff] [blame] | 4944 | _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime); |
4945 | _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] | 4946 | } |
4947 | if (!SetFileTime(hFile, NULL, &atime, &mtime)) { | ||||
4948 | /* Avoid putting the file name into the error here, | ||||
4949 | as that may confuse the user into believing that | ||||
4950 | something is wrong with the file, when it also | ||||
4951 | could be the time stamp that gives a problem. */ | ||||
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 4952 | PyErr_SetFromWindowsErr(0); |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4953 | CloseHandle(hFile); |
4954 | return NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4955 | } |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4956 | CloseHandle(hFile); |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4957 | #else /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4958 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 1ff6cb4 | 1991-04-08 20:59:13 +0000 | [diff] [blame] | 4959 | |
Victor Stinner | 4552ced | 2015-09-21 22:37:15 +0200 | [diff] [blame] | 4960 | #ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4961 | if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4962 | result = utime_nofollow_symlinks(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4963 | else |
Larry Hastings | 9e3e70b | 2011-09-08 19:29:07 -0700 | [diff] [blame] | 4964 | #endif |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4965 | |
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4966 | #if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4967 | if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4968 | result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4969 | else |
4970 | #endif | ||||
4971 | |||||
Victor Stinner | 528a9ab | 2015-09-03 21:30:26 +0200 | [diff] [blame] | 4972 | #if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4973 | if (path->fd != -1) |
4974 | result = utime_fd(&utime, path->fd); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4975 | else |
4976 | #endif | ||||
4977 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4978 | result = utime_default(&utime, path->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4979 | |
4980 | Py_END_ALLOW_THREADS | ||||
4981 | |||||
4982 | if (result < 0) { | ||||
4983 | /* see previous comment about not putting filename in error here */ | ||||
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4984 | posix_error(); |
4985 | return NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 4986 | } |
Larry Hastings | 76ad59b | 2012-05-03 00:30:07 -0700 | [diff] [blame] | 4987 | |
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 4988 | #endif /* MS_WINDOWS */ |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 4989 | |
Serhiy Storchaka | 32bc11c | 2018-12-01 14:30:20 +0200 | [diff] [blame] | 4990 | Py_RETURN_NONE; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 4991 | } |
4992 | |||||
Guido van Rossum | 3b06619 | 1991-06-04 19:40:25 +0000 | [diff] [blame] | 4993 | /* Process operations */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 4994 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 4995 | |
4996 | /*[clinic input] | ||||
4997 | os._exit | ||||
4998 | |||||
4999 | status: int | ||||
5000 | |||||
5001 | Exit to the system with specified status, without normal exit processing. | ||||
5002 | [clinic start generated code]*/ | ||||
5003 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5004 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5005 | os__exit_impl(PyObject *module, int status) |
5006 | /*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5007 | { |
5008 | _exit(status); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5009 | return NULL; /* Make gcc -Wall happy */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 5010 | } |
5011 | |||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5012 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
5013 | #define EXECV_CHAR wchar_t | ||||
5014 | #else | ||||
5015 | #define EXECV_CHAR char | ||||
5016 | #endif | ||||
5017 | |||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5018 | #if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5019 | static void |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5020 | free_string_array(EXECV_CHAR **array, Py_ssize_t count) |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5021 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5022 | Py_ssize_t i; |
5023 | for (i = 0; i < count; i++) | ||||
5024 | PyMem_Free(array[i]); | ||||
5025 | PyMem_DEL(array); | ||||
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5026 | } |
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5027 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5028 | static int |
5029 | fsconvert_strdup(PyObject *o, EXECV_CHAR **out) | ||||
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5030 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5031 | Py_ssize_t size; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5032 | PyObject *ub; |
5033 | int result = 0; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5034 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5035 | if (!PyUnicode_FSDecoder(o, &ub)) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5036 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5037 | *out = PyUnicode_AsWideCharString(ub, &size); |
5038 | if (*out) | ||||
5039 | result = 1; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5040 | #else |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5041 | if (!PyUnicode_FSConverter(o, &ub)) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5042 | return 0; |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5043 | size = PyBytes_GET_SIZE(ub); |
5044 | *out = PyMem_Malloc(size + 1); | ||||
5045 | if (*out) { | ||||
5046 | memcpy(*out, PyBytes_AS_STRING(ub), size + 1); | ||||
5047 | result = 1; | ||||
5048 | } else | ||||
Victor Stinner | 50abf22 | 2013-11-07 23:56:10 +0100 | [diff] [blame] | 5049 | PyErr_NoMemory(); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5050 | #endif |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5051 | Py_DECREF(ub); |
5052 | return result; | ||||
Martin v. Löwis | 011e842 | 2009-05-05 04:43:17 +0000 | [diff] [blame] | 5053 | } |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 5054 | #endif |
5055 | |||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5056 | #if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5057 | static EXECV_CHAR** |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5058 | parse_envlist(PyObject* env, Py_ssize_t *envc_ptr) |
5059 | { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5060 | Py_ssize_t i, pos, envc; |
5061 | PyObject *keys=NULL, *vals=NULL; | ||||
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5062 | PyObject *key, *val, *key2, *val2, *keyval; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5063 | EXECV_CHAR **envlist; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5064 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5065 | i = PyMapping_Size(env); |
5066 | if (i < 0) | ||||
5067 | return NULL; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5068 | envlist = PyMem_NEW(EXECV_CHAR *, i + 1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5069 | if (envlist == NULL) { |
5070 | PyErr_NoMemory(); | ||||
5071 | return NULL; | ||||
5072 | } | ||||
5073 | envc = 0; | ||||
5074 | keys = PyMapping_Keys(env); | ||||
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5075 | if (!keys) |
5076 | goto error; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5077 | vals = PyMapping_Values(env); |
Victor Stinner | b031427 | 2013-11-14 21:37:05 +0100 | [diff] [blame] | 5078 | if (!vals) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5079 | goto error; |
5080 | if (!PyList_Check(keys) || !PyList_Check(vals)) { | ||||
5081 | PyErr_Format(PyExc_TypeError, | ||||
5082 | "env.keys() or env.values() is not a list"); | ||||
5083 | goto error; | ||||
5084 | } | ||||
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5085 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5086 | for (pos = 0; pos < i; pos++) { |
5087 | key = PyList_GetItem(keys, pos); | ||||
5088 | val = PyList_GetItem(vals, pos); | ||||
5089 | if (!key || !val) | ||||
5090 | goto error; | ||||
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5091 | |
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5092 | #if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV) |
5093 | if (!PyUnicode_FSDecoder(key, &key2)) | ||||
5094 | goto error; | ||||
5095 | if (!PyUnicode_FSDecoder(val, &val2)) { | ||||
5096 | Py_DECREF(key2); | ||||
5097 | goto error; | ||||
5098 | } | ||||
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5099 | /* Search from index 1 because on Windows starting '=' is allowed for |
5100 | defining hidden environment variables. */ | ||||
5101 | if (PyUnicode_GET_LENGTH(key2) == 0 || | ||||
5102 | PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1) | ||||
5103 | { | ||||
5104 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); | ||||
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5105 | Py_DECREF(key2); |
5106 | Py_DECREF(val2); | ||||
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5107 | goto error; |
5108 | } | ||||
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5109 | keyval = PyUnicode_FromFormat("%U=%U", key2, val2); |
5110 | #else | ||||
5111 | if (!PyUnicode_FSConverter(key, &key2)) | ||||
5112 | goto error; | ||||
5113 | if (!PyUnicode_FSConverter(val, &val2)) { | ||||
5114 | Py_DECREF(key2); | ||||
5115 | goto error; | ||||
5116 | } | ||||
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5117 | if (PyBytes_GET_SIZE(key2) == 0 || |
5118 | strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL) | ||||
5119 | { | ||||
5120 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); | ||||
Eric N. Vander Weele | a7874c7 | 2017-06-26 21:35:20 -0400 | [diff] [blame] | 5121 | Py_DECREF(key2); |
5122 | Py_DECREF(val2); | ||||
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 5123 | goto error; |
5124 | } | ||||
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5125 | keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2), |
5126 | PyBytes_AS_STRING(val2)); | ||||
5127 | #endif | ||||
5128 | Py_DECREF(key2); | ||||
5129 | Py_DECREF(val2); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5130 | if (!keyval) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5131 | goto error; |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5132 | |
5133 | if (!fsconvert_strdup(keyval, &envlist[envc++])) { | ||||
5134 | Py_DECREF(keyval); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5135 | goto error; |
5136 | } | ||||
Berker Peksag | 8181646 | 2016-09-15 20:19:47 +0300 | [diff] [blame] | 5137 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5138 | Py_DECREF(keyval); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5139 | } |
5140 | Py_DECREF(vals); | ||||
5141 | Py_DECREF(keys); | ||||
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5142 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5143 | envlist[envc] = 0; |
5144 | *envc_ptr = envc; | ||||
5145 | return envlist; | ||||
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5146 | |
5147 | error: | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5148 | Py_XDECREF(keys); |
5149 | Py_XDECREF(vals); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5150 | free_string_array(envlist, envc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5151 | return NULL; |
Victor Stinner | 13bb71c | 2010-04-23 21:41:56 +0000 | [diff] [blame] | 5152 | } |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5153 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5154 | static EXECV_CHAR** |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5155 | parse_arglist(PyObject* argv, Py_ssize_t *argc) |
5156 | { | ||||
5157 | int i; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5158 | EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5159 | if (argvlist == NULL) { |
5160 | PyErr_NoMemory(); | ||||
5161 | return NULL; | ||||
5162 | } | ||||
5163 | for (i = 0; i < *argc; i++) { | ||||
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5164 | PyObject* item = PySequence_ITEM(argv, i); |
5165 | if (item == NULL) | ||||
5166 | goto fail; | ||||
5167 | if (!fsconvert_strdup(item, &argvlist[i])) { | ||||
5168 | Py_DECREF(item); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5169 | goto fail; |
5170 | } | ||||
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5171 | Py_DECREF(item); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5172 | } |
5173 | argvlist[*argc] = NULL; | ||||
5174 | return argvlist; | ||||
5175 | fail: | ||||
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 5176 | *argc = i; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5177 | free_string_array(argvlist, *argc); |
5178 | return NULL; | ||||
5179 | } | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5180 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5181 | #endif |
5182 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5183 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5184 | #ifdef HAVE_EXECV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5185 | /*[clinic input] |
5186 | os.execv | ||||
5187 | |||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5188 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5189 | Path of executable file. |
5190 | argv: object | ||||
5191 | Tuple or list of strings. | ||||
5192 | / | ||||
5193 | |||||
5194 | Execute an executable path with arguments, replacing current process. | ||||
5195 | [clinic start generated code]*/ | ||||
5196 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5197 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5198 | os_execv_impl(PyObject *module, path_t *path, PyObject *argv) |
5199 | /*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5200 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5201 | EXECV_CHAR **argvlist; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5202 | Py_ssize_t argc; |
5203 | |||||
5204 | /* execv has two arguments: (path, argv), where | ||||
5205 | argv is a list or tuple of strings. */ | ||||
5206 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5207 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
5208 | PyErr_SetString(PyExc_TypeError, | ||||
5209 | "execv() arg 2 must be a tuple or list"); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5210 | return NULL; |
5211 | } | ||||
5212 | argc = PySequence_Size(argv); | ||||
5213 | if (argc < 1) { | ||||
5214 | PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty"); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5215 | return NULL; |
5216 | } | ||||
5217 | |||||
5218 | argvlist = parse_arglist(argv, &argc); | ||||
5219 | if (argvlist == NULL) { | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5220 | return NULL; |
5221 | } | ||||
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5222 | if (!argvlist[0][0]) { |
5223 | PyErr_SetString(PyExc_ValueError, | ||||
5224 | "execv() arg 2 first element cannot be empty"); | ||||
5225 | free_string_array(argvlist, argc); | ||||
5226 | return NULL; | ||||
5227 | } | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5228 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 5229 | if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) { |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5230 | free_string_array(argvlist, argc); |
5231 | return NULL; | ||||
5232 | } | ||||
5233 | |||||
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5234 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5235 | #ifdef HAVE_WEXECV |
5236 | _wexecv(path->wide, argvlist); | ||||
5237 | #else | ||||
5238 | execv(path->narrow, argvlist); | ||||
5239 | #endif | ||||
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5240 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5241 | |
5242 | /* If we get here it's definitely an error */ | ||||
5243 | |||||
5244 | free_string_array(argvlist, argc); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5245 | return posix_error(); |
5246 | } | ||||
5247 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5248 | |
5249 | /*[clinic input] | ||||
5250 | os.execve | ||||
5251 | |||||
5252 | path: path_t(allow_fd='PATH_HAVE_FEXECVE') | ||||
5253 | Path of executable file. | ||||
5254 | argv: object | ||||
5255 | Tuple or list of strings. | ||||
5256 | env: object | ||||
5257 | Dictionary of strings mapping to strings. | ||||
5258 | |||||
5259 | Execute an executable path with arguments, replacing current process. | ||||
5260 | [clinic start generated code]*/ | ||||
5261 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5262 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5263 | os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env) |
5264 | /*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5265 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5266 | EXECV_CHAR **argvlist = NULL; |
5267 | EXECV_CHAR **envlist; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5268 | Py_ssize_t argc, envc; |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5269 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5270 | /* execve has three arguments: (path, argv, env), where |
5271 | argv is a list or tuple of strings and env is a dictionary | ||||
5272 | like posix.environ. */ | ||||
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5273 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5274 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5275 | PyErr_SetString(PyExc_TypeError, |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5276 | "execve: argv must be a tuple or list"); |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5277 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5278 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5279 | argc = PySequence_Size(argv); |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5280 | if (argc < 1) { |
5281 | PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty"); | ||||
5282 | return NULL; | ||||
5283 | } | ||||
5284 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5285 | if (!PyMapping_Check(env)) { |
5286 | PyErr_SetString(PyExc_TypeError, | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5287 | "execve: environment must be a mapping object"); |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5288 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5289 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5290 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5291 | argvlist = parse_arglist(argv, &argc); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5292 | if (argvlist == NULL) { |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5293 | goto fail_0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5294 | } |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5295 | if (!argvlist[0][0]) { |
5296 | PyErr_SetString(PyExc_ValueError, | ||||
5297 | "execve: argv first element cannot be empty"); | ||||
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5298 | goto fail_0; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5299 | } |
Guido van Rossum | c6dcc9f | 1993-11-05 10:15:19 +0000 | [diff] [blame] | 5300 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5301 | envlist = parse_envlist(env, &envc); |
5302 | if (envlist == NULL) | ||||
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5303 | goto fail_0; |
5304 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 5305 | if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) { |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5306 | goto fail_1; |
5307 | } | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5308 | |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5309 | _Py_BEGIN_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5310 | #ifdef HAVE_FEXECVE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5311 | if (path->fd > -1) |
5312 | fexecve(path->fd, argvlist, envlist); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5313 | else |
5314 | #endif | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5315 | #ifdef HAVE_WEXECV |
5316 | _wexecve(path->wide, argvlist, envlist); | ||||
5317 | #else | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5318 | execve(path->narrow, argvlist, envlist); |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5319 | #endif |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5320 | _Py_END_SUPPRESS_IPH |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5321 | |
5322 | /* If we get here it's definitely an error */ | ||||
5323 | |||||
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 5324 | posix_path_error(path); |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5325 | fail_1: |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5326 | free_string_array(envlist, envc); |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5327 | fail_0: |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5328 | if (argvlist) |
5329 | free_string_array(argvlist, argc); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 5330 | return NULL; |
5331 | } | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5332 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 5333 | #endif /* HAVE_EXECV */ |
5334 | |||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5335 | #ifdef HAVE_POSIX_SPAWN |
5336 | |||||
5337 | enum posix_spawn_file_actions_identifier { | ||||
5338 | POSIX_SPAWN_OPEN, | ||||
5339 | POSIX_SPAWN_CLOSE, | ||||
5340 | POSIX_SPAWN_DUP2 | ||||
5341 | }; | ||||
5342 | |||||
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5343 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5344 | static int |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5345 | convert_sched_param(PyObject *param, struct sched_param *res); |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 5346 | #endif |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5347 | |
5348 | static int | ||||
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5349 | parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup, |
5350 | int resetids, int setsid, PyObject *setsigmask, | ||||
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5351 | PyObject *setsigdef, PyObject *scheduler, |
5352 | posix_spawnattr_t *attrp) | ||||
5353 | { | ||||
5354 | long all_flags = 0; | ||||
5355 | |||||
5356 | errno = posix_spawnattr_init(attrp); | ||||
5357 | if (errno) { | ||||
5358 | posix_error(); | ||||
5359 | return -1; | ||||
5360 | } | ||||
5361 | |||||
5362 | if (setpgroup) { | ||||
5363 | pid_t pgid = PyLong_AsPid(setpgroup); | ||||
5364 | if (pgid == (pid_t)-1 && PyErr_Occurred()) { | ||||
5365 | goto fail; | ||||
5366 | } | ||||
5367 | errno = posix_spawnattr_setpgroup(attrp, pgid); | ||||
5368 | if (errno) { | ||||
5369 | posix_error(); | ||||
5370 | goto fail; | ||||
5371 | } | ||||
5372 | all_flags |= POSIX_SPAWN_SETPGROUP; | ||||
5373 | } | ||||
5374 | |||||
5375 | if (resetids) { | ||||
5376 | all_flags |= POSIX_SPAWN_RESETIDS; | ||||
5377 | } | ||||
5378 | |||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5379 | if (setsid) { |
5380 | #ifdef POSIX_SPAWN_SETSID | ||||
5381 | all_flags |= POSIX_SPAWN_SETSID; | ||||
5382 | #elif defined(POSIX_SPAWN_SETSID_NP) | ||||
5383 | all_flags |= POSIX_SPAWN_SETSID_NP; | ||||
5384 | #else | ||||
5385 | argument_unavailable_error(func_name, "setsid"); | ||||
5386 | return -1; | ||||
5387 | #endif | ||||
5388 | } | ||||
5389 | |||||
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5390 | if (setsigmask) { |
5391 | sigset_t set; | ||||
5392 | if (!_Py_Sigset_Converter(setsigmask, &set)) { | ||||
5393 | goto fail; | ||||
5394 | } | ||||
5395 | errno = posix_spawnattr_setsigmask(attrp, &set); | ||||
5396 | if (errno) { | ||||
5397 | posix_error(); | ||||
5398 | goto fail; | ||||
5399 | } | ||||
5400 | all_flags |= POSIX_SPAWN_SETSIGMASK; | ||||
5401 | } | ||||
5402 | |||||
5403 | if (setsigdef) { | ||||
5404 | sigset_t set; | ||||
5405 | if (!_Py_Sigset_Converter(setsigdef, &set)) { | ||||
5406 | goto fail; | ||||
5407 | } | ||||
5408 | errno = posix_spawnattr_setsigdefault(attrp, &set); | ||||
5409 | if (errno) { | ||||
5410 | posix_error(); | ||||
5411 | goto fail; | ||||
5412 | } | ||||
5413 | all_flags |= POSIX_SPAWN_SETSIGDEF; | ||||
5414 | } | ||||
5415 | |||||
5416 | if (scheduler) { | ||||
5417 | #ifdef POSIX_SPAWN_SETSCHEDULER | ||||
5418 | PyObject *py_schedpolicy; | ||||
5419 | struct sched_param schedparam; | ||||
5420 | |||||
5421 | if (!PyArg_ParseTuple(scheduler, "OO&" | ||||
5422 | ";A scheduler tuple must have two elements", | ||||
5423 | &py_schedpolicy, convert_sched_param, &schedparam)) { | ||||
5424 | goto fail; | ||||
5425 | } | ||||
5426 | if (py_schedpolicy != Py_None) { | ||||
5427 | int schedpolicy = _PyLong_AsInt(py_schedpolicy); | ||||
5428 | |||||
5429 | if (schedpolicy == -1 && PyErr_Occurred()) { | ||||
5430 | goto fail; | ||||
5431 | } | ||||
5432 | errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy); | ||||
5433 | if (errno) { | ||||
5434 | posix_error(); | ||||
5435 | goto fail; | ||||
5436 | } | ||||
5437 | all_flags |= POSIX_SPAWN_SETSCHEDULER; | ||||
5438 | } | ||||
5439 | errno = posix_spawnattr_setschedparam(attrp, &schedparam); | ||||
5440 | if (errno) { | ||||
5441 | posix_error(); | ||||
5442 | goto fail; | ||||
5443 | } | ||||
5444 | all_flags |= POSIX_SPAWN_SETSCHEDPARAM; | ||||
5445 | #else | ||||
5446 | PyErr_SetString(PyExc_NotImplementedError, | ||||
5447 | "The scheduler option is not supported in this system."); | ||||
5448 | goto fail; | ||||
5449 | #endif | ||||
5450 | } | ||||
5451 | |||||
5452 | errno = posix_spawnattr_setflags(attrp, all_flags); | ||||
5453 | if (errno) { | ||||
5454 | posix_error(); | ||||
5455 | goto fail; | ||||
5456 | } | ||||
5457 | |||||
5458 | return 0; | ||||
5459 | |||||
5460 | fail: | ||||
5461 | (void)posix_spawnattr_destroy(attrp); | ||||
5462 | return -1; | ||||
5463 | } | ||||
5464 | |||||
5465 | static int | ||||
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5466 | parse_file_actions(PyObject *file_actions, |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5467 | posix_spawn_file_actions_t *file_actionsp, |
5468 | PyObject *temp_buffer) | ||||
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5469 | { |
5470 | PyObject *seq; | ||||
5471 | PyObject *file_action = NULL; | ||||
5472 | PyObject *tag_obj; | ||||
5473 | |||||
5474 | seq = PySequence_Fast(file_actions, | ||||
5475 | "file_actions must be a sequence or None"); | ||||
5476 | if (seq == NULL) { | ||||
5477 | return -1; | ||||
5478 | } | ||||
5479 | |||||
5480 | errno = posix_spawn_file_actions_init(file_actionsp); | ||||
5481 | if (errno) { | ||||
5482 | posix_error(); | ||||
5483 | Py_DECREF(seq); | ||||
5484 | return -1; | ||||
5485 | } | ||||
5486 | |||||
Miss Islington (bot) | 04d4692 | 2019-06-26 14:20:09 -0700 | [diff] [blame] | 5487 | for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) { |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5488 | file_action = PySequence_Fast_GET_ITEM(seq, i); |
5489 | Py_INCREF(file_action); | ||||
5490 | if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) { | ||||
5491 | PyErr_SetString(PyExc_TypeError, | ||||
5492 | "Each file_actions element must be a non-empty tuple"); | ||||
5493 | goto fail; | ||||
5494 | } | ||||
5495 | long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0)); | ||||
5496 | if (tag == -1 && PyErr_Occurred()) { | ||||
5497 | goto fail; | ||||
5498 | } | ||||
5499 | |||||
5500 | /* Populate the file_actions object */ | ||||
5501 | switch (tag) { | ||||
5502 | case POSIX_SPAWN_OPEN: { | ||||
5503 | int fd, oflag; | ||||
5504 | PyObject *path; | ||||
5505 | unsigned long mode; | ||||
5506 | if (!PyArg_ParseTuple(file_action, "OiO&ik" | ||||
5507 | ";A open file_action tuple must have 5 elements", | ||||
5508 | &tag_obj, &fd, PyUnicode_FSConverter, &path, | ||||
5509 | &oflag, &mode)) | ||||
5510 | { | ||||
5511 | goto fail; | ||||
5512 | } | ||||
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5513 | if (PyList_Append(temp_buffer, path)) { |
5514 | Py_DECREF(path); | ||||
5515 | goto fail; | ||||
5516 | } | ||||
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5517 | errno = posix_spawn_file_actions_addopen(file_actionsp, |
5518 | fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode); | ||||
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5519 | Py_DECREF(path); |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5520 | if (errno) { |
5521 | posix_error(); | ||||
5522 | goto fail; | ||||
5523 | } | ||||
5524 | break; | ||||
5525 | } | ||||
5526 | case POSIX_SPAWN_CLOSE: { | ||||
5527 | int fd; | ||||
5528 | if (!PyArg_ParseTuple(file_action, "Oi" | ||||
5529 | ";A close file_action tuple must have 2 elements", | ||||
5530 | &tag_obj, &fd)) | ||||
5531 | { | ||||
5532 | goto fail; | ||||
5533 | } | ||||
5534 | errno = posix_spawn_file_actions_addclose(file_actionsp, fd); | ||||
5535 | if (errno) { | ||||
5536 | posix_error(); | ||||
5537 | goto fail; | ||||
5538 | } | ||||
5539 | break; | ||||
5540 | } | ||||
5541 | case POSIX_SPAWN_DUP2: { | ||||
5542 | int fd1, fd2; | ||||
5543 | if (!PyArg_ParseTuple(file_action, "Oii" | ||||
5544 | ";A dup2 file_action tuple must have 3 elements", | ||||
5545 | &tag_obj, &fd1, &fd2)) | ||||
5546 | { | ||||
5547 | goto fail; | ||||
5548 | } | ||||
5549 | errno = posix_spawn_file_actions_adddup2(file_actionsp, | ||||
5550 | fd1, fd2); | ||||
5551 | if (errno) { | ||||
5552 | posix_error(); | ||||
5553 | goto fail; | ||||
5554 | } | ||||
5555 | break; | ||||
5556 | } | ||||
5557 | default: { | ||||
5558 | PyErr_SetString(PyExc_TypeError, | ||||
5559 | "Unknown file_actions identifier"); | ||||
5560 | goto fail; | ||||
5561 | } | ||||
5562 | } | ||||
5563 | Py_DECREF(file_action); | ||||
5564 | } | ||||
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5565 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5566 | Py_DECREF(seq); |
5567 | return 0; | ||||
5568 | |||||
5569 | fail: | ||||
5570 | Py_DECREF(seq); | ||||
5571 | Py_DECREF(file_action); | ||||
5572 | (void)posix_spawn_file_actions_destroy(file_actionsp); | ||||
5573 | return -1; | ||||
5574 | } | ||||
5575 | |||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5576 | |
5577 | static PyObject * | ||||
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5578 | py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv, |
5579 | PyObject *env, PyObject *file_actions, | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5580 | PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5581 | PyObject *setsigdef, PyObject *scheduler) |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5582 | { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5583 | const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn"; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5584 | EXECV_CHAR **argvlist = NULL; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5585 | EXECV_CHAR **envlist = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5586 | posix_spawn_file_actions_t file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5587 | posix_spawn_file_actions_t *file_actionsp = NULL; |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5588 | posix_spawnattr_t attr; |
5589 | posix_spawnattr_t *attrp = NULL; | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5590 | Py_ssize_t argc, envc; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5591 | PyObject *result = NULL; |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5592 | PyObject *temp_buffer = NULL; |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5593 | pid_t pid; |
5594 | int err_code; | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5595 | |
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5596 | /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5597 | argv is a list or tuple of strings and env is a dictionary |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5598 | like posix.environ. */ |
5599 | |||||
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5600 | if (!PyList_Check(argv) && !PyTuple_Check(argv)) { |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5601 | PyErr_Format(PyExc_TypeError, |
5602 | "%s: argv must be a tuple or list", func_name); | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5603 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5604 | } |
5605 | argc = PySequence_Size(argv); | ||||
5606 | if (argc < 1) { | ||||
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5607 | PyErr_Format(PyExc_ValueError, |
5608 | "%s: argv must not be empty", func_name); | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5609 | return NULL; |
5610 | } | ||||
5611 | |||||
5612 | if (!PyMapping_Check(env)) { | ||||
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5613 | PyErr_Format(PyExc_TypeError, |
5614 | "%s: environment must be a mapping object", func_name); | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5615 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5616 | } |
5617 | |||||
5618 | argvlist = parse_arglist(argv, &argc); | ||||
5619 | if (argvlist == NULL) { | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5620 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5621 | } |
5622 | if (!argvlist[0][0]) { | ||||
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5623 | PyErr_Format(PyExc_ValueError, |
5624 | "%s: argv first element cannot be empty", func_name); | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5625 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5626 | } |
5627 | |||||
5628 | envlist = parse_envlist(env, &envc); | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5629 | if (envlist == NULL) { |
5630 | goto exit; | ||||
5631 | } | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5632 | |
Anthony Shaw | 948ed8c | 2019-05-10 12:00:06 +1000 | [diff] [blame] | 5633 | if (file_actions != NULL && file_actions != Py_None) { |
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5634 | /* There is a bug in old versions of glibc that makes some of the |
5635 | * helper functions for manipulating file actions not copy the provided | ||||
5636 | * buffers. The problem is that posix_spawn_file_actions_addopen does not | ||||
5637 | * copy the value of path for some old versions of glibc (<2.20). | ||||
5638 | * The use of temp_buffer here is a workaround that keeps the | ||||
5639 | * python objects that own the buffers alive until posix_spawn gets called. | ||||
5640 | * Check https://bugs.python.org/issue33630 and | ||||
5641 | * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/ | ||||
5642 | temp_buffer = PyList_New(0); | ||||
5643 | if (!temp_buffer) { | ||||
5644 | goto exit; | ||||
5645 | } | ||||
5646 | if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) { | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5647 | goto exit; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5648 | } |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5649 | file_actionsp = &file_actions_buf; |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5650 | } |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5651 | |
Victor Stinner | 325e4ba | 2019-02-01 15:47:24 +0100 | [diff] [blame] | 5652 | if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid, |
5653 | setsigmask, setsigdef, scheduler, &attr)) { | ||||
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5654 | goto exit; |
5655 | } | ||||
5656 | attrp = &attr; | ||||
5657 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 5658 | if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) { |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5659 | goto exit; |
5660 | } | ||||
5661 | |||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5662 | _Py_BEGIN_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5663 | #ifdef HAVE_POSIX_SPAWNP |
5664 | if (use_posix_spawnp) { | ||||
5665 | err_code = posix_spawnp(&pid, path->narrow, | ||||
5666 | file_actionsp, attrp, argvlist, envlist); | ||||
5667 | } | ||||
5668 | else | ||||
5669 | #endif /* HAVE_POSIX_SPAWNP */ | ||||
5670 | { | ||||
5671 | err_code = posix_spawn(&pid, path->narrow, | ||||
5672 | file_actionsp, attrp, argvlist, envlist); | ||||
5673 | } | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5674 | _Py_END_SUPPRESS_IPH |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5675 | |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5676 | if (err_code) { |
5677 | errno = err_code; | ||||
5678 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5679 | goto exit; |
5680 | } | ||||
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 5681 | #ifdef _Py_MEMORY_SANITIZER |
5682 | __msan_unpoison(&pid, sizeof(pid)); | ||||
5683 | #endif | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5684 | result = PyLong_FromPid(pid); |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5685 | |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5686 | exit: |
Serhiy Storchaka | ef34753 | 2018-05-01 16:45:04 +0300 | [diff] [blame] | 5687 | if (file_actionsp) { |
5688 | (void)posix_spawn_file_actions_destroy(file_actionsp); | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5689 | } |
Pablo Galindo | 254a466 | 2018-09-07 16:44:24 +0100 | [diff] [blame] | 5690 | if (attrp) { |
5691 | (void)posix_spawnattr_destroy(attrp); | ||||
5692 | } | ||||
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5693 | if (envlist) { |
5694 | free_string_array(envlist, envc); | ||||
5695 | } | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5696 | if (argvlist) { |
5697 | free_string_array(argvlist, argc); | ||||
5698 | } | ||||
Pablo Galindo | cb97073 | 2018-06-19 09:19:50 +0100 | [diff] [blame] | 5699 | Py_XDECREF(temp_buffer); |
Pablo Galindo | 0cd6bca | 2018-01-29 20:34:42 +0000 | [diff] [blame] | 5700 | return result; |
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5701 | } |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5702 | |
5703 | |||||
5704 | /*[clinic input] | ||||
5705 | |||||
5706 | os.posix_spawn | ||||
5707 | path: path_t | ||||
5708 | Path of executable file. | ||||
5709 | argv: object | ||||
5710 | Tuple or list of strings. | ||||
5711 | env: object | ||||
5712 | Dictionary of strings mapping to strings. | ||||
5713 | / | ||||
5714 | * | ||||
5715 | file_actions: object(c_default='NULL') = () | ||||
5716 | A sequence of file action tuples. | ||||
5717 | setpgroup: object = NULL | ||||
5718 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. | ||||
5719 | resetids: bool(accept={int}) = False | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5720 | If the value is `true` the POSIX_SPAWN_RESETIDS will be activated. |
5721 | setsid: bool(accept={int}) = False | ||||
5722 | If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated. | ||||
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5723 | setsigmask: object(c_default='NULL') = () |
5724 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. | ||||
5725 | setsigdef: object(c_default='NULL') = () | ||||
5726 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. | ||||
5727 | scheduler: object = NULL | ||||
5728 | A tuple with the scheduler policy (optional) and parameters. | ||||
5729 | |||||
5730 | Execute the program specified by path in a new process. | ||||
5731 | [clinic start generated code]*/ | ||||
5732 | |||||
5733 | static PyObject * | ||||
5734 | os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv, | ||||
5735 | PyObject *env, PyObject *file_actions, | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5736 | PyObject *setpgroup, int resetids, int setsid, |
5737 | PyObject *setsigmask, PyObject *setsigdef, | ||||
5738 | PyObject *scheduler) | ||||
5739 | /*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/ | ||||
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5740 | { |
5741 | return py_posix_spawn(0, module, path, argv, env, file_actions, | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5742 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5743 | scheduler); |
5744 | } | ||||
5745 | #endif /* HAVE_POSIX_SPAWN */ | ||||
5746 | |||||
5747 | |||||
5748 | |||||
5749 | #ifdef HAVE_POSIX_SPAWNP | ||||
5750 | /*[clinic input] | ||||
5751 | |||||
5752 | os.posix_spawnp | ||||
5753 | path: path_t | ||||
5754 | Path of executable file. | ||||
5755 | argv: object | ||||
5756 | Tuple or list of strings. | ||||
5757 | env: object | ||||
5758 | Dictionary of strings mapping to strings. | ||||
5759 | / | ||||
5760 | * | ||||
5761 | file_actions: object(c_default='NULL') = () | ||||
5762 | A sequence of file action tuples. | ||||
5763 | setpgroup: object = NULL | ||||
5764 | The pgroup to use with the POSIX_SPAWN_SETPGROUP flag. | ||||
5765 | resetids: bool(accept={int}) = False | ||||
5766 | If the value is `True` the POSIX_SPAWN_RESETIDS will be activated. | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5767 | setsid: bool(accept={int}) = False |
5768 | If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated. | ||||
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5769 | setsigmask: object(c_default='NULL') = () |
5770 | The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag. | ||||
5771 | setsigdef: object(c_default='NULL') = () | ||||
5772 | The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag. | ||||
5773 | scheduler: object = NULL | ||||
5774 | A tuple with the scheduler policy (optional) and parameters. | ||||
5775 | |||||
5776 | Execute the program specified by path in a new process. | ||||
5777 | [clinic start generated code]*/ | ||||
5778 | |||||
5779 | static PyObject * | ||||
5780 | os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv, | ||||
5781 | PyObject *env, PyObject *file_actions, | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5782 | PyObject *setpgroup, int resetids, int setsid, |
5783 | PyObject *setsigmask, PyObject *setsigdef, | ||||
5784 | PyObject *scheduler) | ||||
5785 | /*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/ | ||||
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5786 | { |
5787 | return py_posix_spawn(1, module, path, argv, env, file_actions, | ||||
Joannah Nanjekye | 80c5dfe | 2019-02-01 13:05:22 +0300 | [diff] [blame] | 5788 | setpgroup, resetids, setsid, setsigmask, setsigdef, |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 5789 | scheduler); |
5790 | } | ||||
5791 | #endif /* HAVE_POSIX_SPAWNP */ | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 5792 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5793 | #ifdef HAVE_RTPSPAWN |
5794 | static intptr_t | ||||
5795 | _rtp_spawn(int mode, const char *rtpFileName, const char *argv[], | ||||
5796 | const char *envp[]) | ||||
5797 | { | ||||
5798 | RTP_ID rtpid; | ||||
5799 | int status; | ||||
5800 | pid_t res; | ||||
5801 | int async_err = 0; | ||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 5802 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5803 | /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes. |
5804 | uStackSize=0 cannot be used, the default stack size is too small for | ||||
5805 | Python. */ | ||||
5806 | if (envp) { | ||||
5807 | rtpid = rtpSpawn(rtpFileName, argv, envp, | ||||
5808 | 100, 0x1000000, 0, VX_FP_TASK); | ||||
5809 | } | ||||
5810 | else { | ||||
5811 | rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ, | ||||
5812 | 100, 0x1000000, 0, VX_FP_TASK); | ||||
5813 | } | ||||
5814 | if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) { | ||||
5815 | do { | ||||
5816 | res = waitpid((pid_t)rtpid, &status, 0); | ||||
5817 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
5818 | |||||
5819 | if (res < 0) | ||||
5820 | return RTP_ID_ERROR; | ||||
5821 | return ((intptr_t)status); | ||||
5822 | } | ||||
5823 | return ((intptr_t)rtpid); | ||||
5824 | } | ||||
5825 | #endif | ||||
5826 | |||||
5827 | #if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5828 | /*[clinic input] |
5829 | os.spawnv | ||||
5830 | |||||
5831 | mode: int | ||||
5832 | Mode of process creation. | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5833 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5834 | Path of executable file. |
5835 | argv: object | ||||
5836 | Tuple or list of strings. | ||||
5837 | / | ||||
5838 | |||||
5839 | Execute the program specified by path in a new process. | ||||
5840 | [clinic start generated code]*/ | ||||
5841 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5842 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5843 | os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv) |
5844 | /*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5845 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5846 | EXECV_CHAR **argvlist; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5847 | int i; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5848 | Py_ssize_t argc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5849 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5850 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5851 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5852 | /* spawnv has three arguments: (mode, path, argv), where |
5853 | argv is a list or tuple of strings. */ | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5854 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5855 | if (PyList_Check(argv)) { |
5856 | argc = PyList_Size(argv); | ||||
5857 | getitem = PyList_GetItem; | ||||
5858 | } | ||||
5859 | else if (PyTuple_Check(argv)) { | ||||
5860 | argc = PyTuple_Size(argv); | ||||
5861 | getitem = PyTuple_GetItem; | ||||
5862 | } | ||||
5863 | else { | ||||
5864 | PyErr_SetString(PyExc_TypeError, | ||||
5865 | "spawnv() arg 2 must be a tuple or list"); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5866 | return NULL; |
5867 | } | ||||
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5868 | if (argc == 0) { |
5869 | PyErr_SetString(PyExc_ValueError, | ||||
5870 | "spawnv() arg 2 cannot be empty"); | ||||
5871 | return NULL; | ||||
5872 | } | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5873 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5874 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5875 | if (argvlist == NULL) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5876 | return PyErr_NoMemory(); |
5877 | } | ||||
5878 | for (i = 0; i < argc; i++) { | ||||
5879 | if (!fsconvert_strdup((*getitem)(argv, i), | ||||
5880 | &argvlist[i])) { | ||||
5881 | free_string_array(argvlist, i); | ||||
5882 | PyErr_SetString( | ||||
5883 | PyExc_TypeError, | ||||
5884 | "spawnv() arg 2 must contain only strings"); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5885 | return NULL; |
5886 | } | ||||
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5887 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | 8acb4cf | 2017-06-15 15:30:40 +0200 | [diff] [blame] | 5888 | free_string_array(argvlist, i + 1); |
Steve Dower | 93ff872 | 2016-11-19 19:03:54 -0800 | [diff] [blame] | 5889 | PyErr_SetString( |
5890 | PyExc_ValueError, | ||||
5891 | "spawnv() arg 2 first element cannot be empty"); | ||||
5892 | return NULL; | ||||
5893 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5894 | } |
5895 | argvlist[argc] = NULL; | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5896 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5897 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5898 | if (mode == _OLD_P_OVERLAY) |
5899 | mode = _P_OVERLAY; | ||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5900 | #endif |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5901 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 5902 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 5903 | Py_None) < 0) { |
5904 | free_string_array(argvlist, argc); | ||||
5905 | return NULL; | ||||
5906 | } | ||||
5907 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5908 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5909 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5910 | #ifdef HAVE_WSPAWNV |
5911 | spawnval = _wspawnv(mode, path->wide, argvlist); | ||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 5912 | #elif defined(HAVE_RTPSPAWN) |
5913 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5914 | #else |
5915 | spawnval = _spawnv(mode, path->narrow, argvlist); | ||||
5916 | #endif | ||||
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 5917 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5918 | Py_END_ALLOW_THREADS |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 5919 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5920 | free_string_array(argvlist, argc); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5921 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5922 | if (spawnval == -1) |
5923 | return posix_error(); | ||||
5924 | else | ||||
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 5925 | return Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5926 | } |
5927 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5928 | /*[clinic input] |
5929 | os.spawnve | ||||
5930 | |||||
5931 | mode: int | ||||
5932 | Mode of process creation. | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5933 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5934 | Path of executable file. |
5935 | argv: object | ||||
5936 | Tuple or list of strings. | ||||
5937 | env: object | ||||
5938 | Dictionary of strings mapping to strings. | ||||
5939 | / | ||||
5940 | |||||
5941 | Execute the program specified by path in a new process. | ||||
5942 | [clinic start generated code]*/ | ||||
5943 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5944 | static PyObject * |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5945 | os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv, |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 5946 | PyObject *env) |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5947 | /*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 5948 | { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5949 | EXECV_CHAR **argvlist; |
5950 | EXECV_CHAR **envlist; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5951 | PyObject *res = NULL; |
Antoine Pitrou | 22e4155 | 2010-08-15 18:07:50 +0000 | [diff] [blame] | 5952 | Py_ssize_t argc, i, envc; |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 5953 | intptr_t spawnval; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5954 | PyObject *(*getitem)(PyObject *, Py_ssize_t); |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5955 | Py_ssize_t lastarg = 0; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5956 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5957 | /* spawnve has four arguments: (mode, path, argv, env), where |
5958 | argv is a list or tuple of strings and env is a dictionary | ||||
5959 | like posix.environ. */ | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5960 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5961 | if (PyList_Check(argv)) { |
5962 | argc = PyList_Size(argv); | ||||
5963 | getitem = PyList_GetItem; | ||||
5964 | } | ||||
5965 | else if (PyTuple_Check(argv)) { | ||||
5966 | argc = PyTuple_Size(argv); | ||||
5967 | getitem = PyTuple_GetItem; | ||||
5968 | } | ||||
5969 | else { | ||||
5970 | PyErr_SetString(PyExc_TypeError, | ||||
5971 | "spawnve() arg 2 must be a tuple or list"); | ||||
5972 | goto fail_0; | ||||
5973 | } | ||||
Steve Dower | 859fd7b | 2016-11-19 18:53:19 -0800 | [diff] [blame] | 5974 | if (argc == 0) { |
5975 | PyErr_SetString(PyExc_ValueError, | ||||
5976 | "spawnve() arg 2 cannot be empty"); | ||||
5977 | goto fail_0; | ||||
5978 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5979 | if (!PyMapping_Check(env)) { |
5980 | PyErr_SetString(PyExc_TypeError, | ||||
5981 | "spawnve() arg 3 must be a mapping object"); | ||||
5982 | goto fail_0; | ||||
5983 | } | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 5984 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 5985 | argvlist = PyMem_NEW(EXECV_CHAR *, argc+1); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 5986 | if (argvlist == NULL) { |
5987 | PyErr_NoMemory(); | ||||
5988 | goto fail_0; | ||||
5989 | } | ||||
5990 | for (i = 0; i < argc; i++) { | ||||
5991 | if (!fsconvert_strdup((*getitem)(argv, i), | ||||
5992 | &argvlist[i])) | ||||
5993 | { | ||||
5994 | lastarg = i; | ||||
5995 | goto fail_1; | ||||
5996 | } | ||||
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5997 | if (i == 0 && !argvlist[0][0]) { |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 5998 | lastarg = i + 1; |
Steve Dower | bce2626 | 2016-11-19 19:17:26 -0800 | [diff] [blame] | 5999 | PyErr_SetString( |
6000 | PyExc_ValueError, | ||||
6001 | "spawnv() arg 2 first element cannot be empty"); | ||||
6002 | goto fail_1; | ||||
6003 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6004 | } |
6005 | lastarg = argc; | ||||
6006 | argvlist[argc] = NULL; | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6007 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6008 | envlist = parse_envlist(env, &envc); |
6009 | if (envlist == NULL) | ||||
6010 | goto fail_1; | ||||
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6011 | |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6012 | #if !defined(HAVE_RTPSPAWN) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6013 | if (mode == _OLD_P_OVERLAY) |
6014 | mode = _P_OVERLAY; | ||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6015 | #endif |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6016 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 6017 | if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) { |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 6018 | goto fail_2; |
6019 | } | ||||
6020 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6021 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6022 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6023 | #ifdef HAVE_WSPAWNV |
6024 | spawnval = _wspawnve(mode, path->wide, argvlist, envlist); | ||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 6025 | #elif defined(HAVE_RTPSPAWN) |
6026 | spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, | ||||
6027 | (const char **)envlist); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 6028 | #else |
6029 | spawnval = _spawnve(mode, path->narrow, argvlist, envlist); | ||||
6030 | #endif | ||||
Steve Dower | 654a7bd | 2016-09-11 20:19:32 -0700 | [diff] [blame] | 6031 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6032 | Py_END_ALLOW_THREADS |
Tim Peters | 25059d3 | 2001-12-07 20:35:43 +0000 | [diff] [blame] | 6033 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6034 | if (spawnval == -1) |
6035 | (void) posix_error(); | ||||
6036 | else | ||||
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 6037 | res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval); |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6038 | |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 6039 | fail_2: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6040 | while (--envc >= 0) |
6041 | PyMem_DEL(envlist[envc]); | ||||
6042 | PyMem_DEL(envlist); | ||||
Guido van Rossum | 0847c5c | 2002-12-13 18:36:22 +0000 | [diff] [blame] | 6043 | fail_1: |
Victor Stinner | c8d6ab2 | 2017-06-23 15:04:46 +0200 | [diff] [blame] | 6044 | free_string_array(argvlist, lastarg); |
Martin v. Löwis | 114619e | 2002-10-07 06:44:21 +0000 | [diff] [blame] | 6045 | fail_0: |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6046 | return res; |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6047 | } |
Andrew MacIntyre | 69e18c9 | 2004-04-04 07:11:43 +0000 | [diff] [blame] | 6048 | |
Guido van Rossum | a106568 | 1999-01-25 23:20:23 +0000 | [diff] [blame] | 6049 | #endif /* HAVE_SPAWNV */ |
6050 | |||||
6051 | |||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6052 | #ifdef HAVE_FORK |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6053 | |
6054 | /* Helper function to validate arguments. | ||||
6055 | Returns 0 on success. non-zero on failure with a TypeError raised. | ||||
6056 | If obj is non-NULL it must be callable. */ | ||||
6057 | static int | ||||
6058 | check_null_or_callable(PyObject *obj, const char* obj_name) | ||||
6059 | { | ||||
6060 | if (obj && !PyCallable_Check(obj)) { | ||||
6061 | PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s", | ||||
6062 | obj_name, Py_TYPE(obj)->tp_name); | ||||
6063 | return -1; | ||||
6064 | } | ||||
6065 | return 0; | ||||
6066 | } | ||||
6067 | |||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6068 | /*[clinic input] |
6069 | os.register_at_fork | ||||
6070 | |||||
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6071 | * |
6072 | before: object=NULL | ||||
6073 | A callable to be called in the parent before the fork() syscall. | ||||
6074 | after_in_child: object=NULL | ||||
6075 | A callable to be called in the child after fork(). | ||||
6076 | after_in_parent: object=NULL | ||||
6077 | A callable to be called in the parent after fork(). | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6078 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6079 | Register callables to be called when forking a new process. |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6080 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6081 | 'before' callbacks are called in reverse order. |
6082 | 'after_in_child' and 'after_in_parent' callbacks are called in order. | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6083 | |
6084 | [clinic start generated code]*/ | ||||
6085 | |||||
6086 | static PyObject * | ||||
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6087 | os_register_at_fork_impl(PyObject *module, PyObject *before, |
6088 | PyObject *after_in_child, PyObject *after_in_parent) | ||||
6089 | /*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6090 | { |
6091 | PyInterpreterState *interp; | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6092 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6093 | if (!before && !after_in_child && !after_in_parent) { |
6094 | PyErr_SetString(PyExc_TypeError, "At least one argument is required."); | ||||
6095 | return NULL; | ||||
6096 | } | ||||
6097 | if (check_null_or_callable(before, "before") || | ||||
6098 | check_null_or_callable(after_in_child, "after_in_child") || | ||||
6099 | check_null_or_callable(after_in_parent, "after_in_parent")) { | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6100 | return NULL; |
6101 | } | ||||
Victor Stinner | caba55b | 2018-08-03 15:33:52 +0200 | [diff] [blame] | 6102 | interp = _PyInterpreterState_Get(); |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6103 | |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6104 | if (register_at_forker(&interp->before_forkers, before)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6105 | return NULL; |
6106 | } | ||||
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6107 | if (register_at_forker(&interp->after_forkers_child, after_in_child)) { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6108 | return NULL; |
Gregory P. Smith | 163468a | 2017-05-29 10:03:41 -0700 | [diff] [blame] | 6109 | } |
6110 | if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) { | ||||
6111 | return NULL; | ||||
6112 | } | ||||
6113 | Py_RETURN_NONE; | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6114 | } |
6115 | #endif /* HAVE_FORK */ | ||||
6116 | |||||
6117 | |||||
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6118 | #ifdef HAVE_FORK1 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6119 | /*[clinic input] |
6120 | os.fork1 | ||||
6121 | |||||
6122 | Fork a child process with a single multiplexed (i.e., not bound) thread. | ||||
6123 | |||||
6124 | Return 0 to child process and PID of child to parent process. | ||||
6125 | [clinic start generated code]*/ | ||||
6126 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6127 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6128 | os_fork1_impl(PyObject *module) |
6129 | /*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/ | ||||
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6130 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6131 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6132 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6133 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
6134 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); | ||||
6135 | return NULL; | ||||
6136 | } | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6137 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6138 | pid = fork1(); |
6139 | if (pid == 0) { | ||||
6140 | /* child: this clobbers and resets the import lock. */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6141 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6142 | } else { |
6143 | /* parent: release the import lock. */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6144 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6145 | } |
6146 | if (pid == -1) | ||||
6147 | return posix_error(); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6148 | return PyLong_FromPid(pid); |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6149 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6150 | #endif /* HAVE_FORK1 */ |
Guido van Rossum | 2242f2f | 2001-04-11 20:58:20 +0000 | [diff] [blame] | 6151 | |
6152 | |||||
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6153 | #ifdef HAVE_FORK |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6154 | /*[clinic input] |
6155 | os.fork | ||||
6156 | |||||
6157 | Fork a child process. | ||||
6158 | |||||
6159 | Return 0 to child process and PID of child to parent process. | ||||
6160 | [clinic start generated code]*/ | ||||
6161 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6162 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6163 | os_fork_impl(PyObject *module) |
6164 | /*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/ | ||||
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6165 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6166 | pid_t pid; |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6167 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6168 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
6169 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); | ||||
6170 | return NULL; | ||||
6171 | } | ||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 6172 | if (PySys_Audit("os.fork", NULL) < 0) { |
6173 | return NULL; | ||||
6174 | } | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6175 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6176 | pid = fork(); |
6177 | if (pid == 0) { | ||||
6178 | /* child: this clobbers and resets the import lock. */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6179 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6180 | } else { |
6181 | /* parent: release the import lock. */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6182 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6183 | } |
6184 | if (pid == -1) | ||||
6185 | return posix_error(); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6186 | return PyLong_FromPid(pid); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6187 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6188 | #endif /* HAVE_FORK */ |
6189 | |||||
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6190 | |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6191 | #ifdef HAVE_SCHED_H |
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6192 | #ifdef HAVE_SCHED_GET_PRIORITY_MAX |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6193 | /*[clinic input] |
6194 | os.sched_get_priority_max | ||||
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6195 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6196 | policy: int |
6197 | |||||
6198 | Get the maximum scheduling priority for policy. | ||||
6199 | [clinic start generated code]*/ | ||||
6200 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6201 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6202 | os_sched_get_priority_max_impl(PyObject *module, int policy) |
6203 | /*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6204 | { |
6205 | int max; | ||||
6206 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6207 | max = sched_get_priority_max(policy); |
6208 | if (max < 0) | ||||
6209 | return posix_error(); | ||||
6210 | return PyLong_FromLong(max); | ||||
6211 | } | ||||
6212 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6213 | |
6214 | /*[clinic input] | ||||
6215 | os.sched_get_priority_min | ||||
6216 | |||||
6217 | policy: int | ||||
6218 | |||||
6219 | Get the minimum scheduling priority for policy. | ||||
6220 | [clinic start generated code]*/ | ||||
6221 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6222 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6223 | os_sched_get_priority_min_impl(PyObject *module, int policy) |
6224 | /*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6225 | { |
6226 | int min = sched_get_priority_min(policy); | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6227 | if (min < 0) |
6228 | return posix_error(); | ||||
6229 | return PyLong_FromLong(min); | ||||
6230 | } | ||||
Charles-François Natali | ea0d5fc | 2011-09-06 19:03:35 +0200 | [diff] [blame] | 6231 | #endif /* HAVE_SCHED_GET_PRIORITY_MAX */ |
6232 | |||||
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6233 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6234 | #ifdef HAVE_SCHED_SETSCHEDULER |
6235 | /*[clinic input] | ||||
6236 | os.sched_getscheduler | ||||
6237 | pid: pid_t | ||||
6238 | / | ||||
6239 | |||||
6240 | Get the scheduling policy for the process identifiedy by pid. | ||||
6241 | |||||
6242 | Passing 0 for pid returns the scheduling policy for the calling process. | ||||
6243 | [clinic start generated code]*/ | ||||
6244 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6245 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6246 | os_sched_getscheduler_impl(PyObject *module, pid_t pid) |
6247 | /*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6248 | { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6249 | int policy; |
6250 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6251 | policy = sched_getscheduler(pid); |
6252 | if (policy < 0) | ||||
6253 | return posix_error(); | ||||
6254 | return PyLong_FromLong(policy); | ||||
6255 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6256 | #endif /* HAVE_SCHED_SETSCHEDULER */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6257 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6258 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6259 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6260 | /*[clinic input] |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6261 | class os.sched_param "PyObject *" "SchedParamType" |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6262 | |
6263 | @classmethod | ||||
6264 | os.sched_param.__new__ | ||||
6265 | |||||
6266 | sched_priority: object | ||||
6267 | A scheduling parameter. | ||||
6268 | |||||
6269 | Current has only one field: sched_priority"); | ||||
6270 | [clinic start generated code]*/ | ||||
6271 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6272 | static PyObject * |
6273 | os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6274 | /*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6275 | { |
6276 | PyObject *res; | ||||
6277 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6278 | res = PyStructSequence_New(type); |
6279 | if (!res) | ||||
6280 | return NULL; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6281 | Py_INCREF(sched_priority); |
6282 | PyStructSequence_SET_ITEM(res, 0, sched_priority); | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6283 | return res; |
6284 | } | ||||
6285 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6286 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 6287 | PyDoc_VAR(os_sched_param__doc__); |
6288 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6289 | static PyStructSequence_Field sched_param_fields[] = { |
6290 | {"sched_priority", "the scheduling priority"}, | ||||
6291 | {0} | ||||
6292 | }; | ||||
6293 | |||||
6294 | static PyStructSequence_Desc sched_param_desc = { | ||||
6295 | "sched_param", /* name */ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6296 | os_sched_param__doc__, /* doc */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6297 | sched_param_fields, |
6298 | 1 | ||||
6299 | }; | ||||
6300 | |||||
6301 | static int | ||||
6302 | convert_sched_param(PyObject *param, struct sched_param *res) | ||||
6303 | { | ||||
6304 | long priority; | ||||
6305 | |||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6306 | if (Py_TYPE(param) != SchedParamType) { |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6307 | PyErr_SetString(PyExc_TypeError, "must have a sched_param object"); |
6308 | return 0; | ||||
6309 | } | ||||
6310 | priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0)); | ||||
6311 | if (priority == -1 && PyErr_Occurred()) | ||||
6312 | return 0; | ||||
6313 | if (priority > INT_MAX || priority < INT_MIN) { | ||||
6314 | PyErr_SetString(PyExc_OverflowError, "sched_priority out of range"); | ||||
6315 | return 0; | ||||
6316 | } | ||||
6317 | res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int); | ||||
6318 | return 1; | ||||
6319 | } | ||||
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 6320 | #endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6321 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6322 | |
6323 | #ifdef HAVE_SCHED_SETSCHEDULER | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6324 | /*[clinic input] |
6325 | os.sched_setscheduler | ||||
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6326 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6327 | pid: pid_t |
6328 | policy: int | ||||
6329 | param: sched_param | ||||
6330 | / | ||||
6331 | |||||
6332 | Set the scheduling policy for the process identified by pid. | ||||
6333 | |||||
6334 | If pid is 0, the calling process is changed. | ||||
6335 | param is an instance of sched_param. | ||||
6336 | [clinic start generated code]*/ | ||||
6337 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6338 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6339 | os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6340 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6341 | /*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6342 | { |
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6343 | /* |
Jesus Cea | 54b0149 | 2011-09-10 01:53:19 +0200 | [diff] [blame] | 6344 | ** sched_setscheduler() returns 0 in Linux, but the previous |
6345 | ** scheduling policy under Solaris/Illumos, and others. | ||||
6346 | ** On error, -1 is returned in all Operating Systems. | ||||
Jesus Cea | 9c82227 | 2011-09-10 01:40:52 +0200 | [diff] [blame] | 6347 | */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6348 | if (sched_setscheduler(pid, policy, param) == -1) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6349 | return posix_error(); |
6350 | Py_RETURN_NONE; | ||||
6351 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6352 | #endif /* HAVE_SCHED_SETSCHEDULER*/ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6353 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6354 | |
6355 | #ifdef HAVE_SCHED_SETPARAM | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6356 | /*[clinic input] |
6357 | os.sched_getparam | ||||
6358 | pid: pid_t | ||||
6359 | / | ||||
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6360 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6361 | Returns scheduling parameters for the process identified by pid. |
6362 | |||||
6363 | If pid is 0, returns parameters for the calling process. | ||||
6364 | Return value is an instance of sched_param. | ||||
6365 | [clinic start generated code]*/ | ||||
6366 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6367 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6368 | os_sched_getparam_impl(PyObject *module, pid_t pid) |
6369 | /*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6370 | { |
6371 | struct sched_param param; | ||||
6372 | PyObject *result; | ||||
6373 | PyObject *priority; | ||||
6374 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6375 | if (sched_getparam(pid, ¶m)) |
6376 | return posix_error(); | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 6377 | result = PyStructSequence_New(SchedParamType); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6378 | if (!result) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6379 | return NULL; |
6380 | priority = PyLong_FromLong(param.sched_priority); | ||||
6381 | if (!priority) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6382 | Py_DECREF(result); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6383 | return NULL; |
6384 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6385 | PyStructSequence_SET_ITEM(result, 0, priority); |
6386 | return result; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6387 | } |
6388 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6389 | |
6390 | /*[clinic input] | ||||
6391 | os.sched_setparam | ||||
6392 | pid: pid_t | ||||
6393 | param: sched_param | ||||
6394 | / | ||||
6395 | |||||
6396 | Set scheduling parameters for the process identified by pid. | ||||
6397 | |||||
6398 | If pid is 0, sets parameters for the calling process. | ||||
6399 | param should be an instance of sched_param. | ||||
6400 | [clinic start generated code]*/ | ||||
6401 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6402 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6403 | os_sched_setparam_impl(PyObject *module, pid_t pid, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6404 | struct sched_param *param) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6405 | /*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6406 | { |
6407 | if (sched_setparam(pid, param)) | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6408 | return posix_error(); |
6409 | Py_RETURN_NONE; | ||||
6410 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6411 | #endif /* HAVE_SCHED_SETPARAM */ |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6412 | |
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6413 | |
6414 | #ifdef HAVE_SCHED_RR_GET_INTERVAL | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6415 | /*[clinic input] |
6416 | os.sched_rr_get_interval -> double | ||||
6417 | pid: pid_t | ||||
6418 | / | ||||
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6419 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6420 | Return the round-robin quantum for the process identified by pid, in seconds. |
6421 | |||||
6422 | Value returned is a float. | ||||
6423 | [clinic start generated code]*/ | ||||
6424 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6425 | static double |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6426 | os_sched_rr_get_interval_impl(PyObject *module, pid_t pid) |
6427 | /*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6428 | { |
6429 | struct timespec interval; | ||||
6430 | if (sched_rr_get_interval(pid, &interval)) { | ||||
6431 | posix_error(); | ||||
6432 | return -1.0; | ||||
6433 | } | ||||
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6434 | #ifdef _Py_MEMORY_SANITIZER |
6435 | __msan_unpoison(&interval, sizeof(interval)); | ||||
6436 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6437 | return (double)interval.tv_sec + 1e-9*interval.tv_nsec; |
6438 | } | ||||
6439 | #endif /* HAVE_SCHED_RR_GET_INTERVAL */ | ||||
Benjamin Peterson | c5fce4d | 2011-08-02 18:07:32 -0500 | [diff] [blame] | 6440 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6441 | |
6442 | /*[clinic input] | ||||
6443 | os.sched_yield | ||||
6444 | |||||
6445 | Voluntarily relinquish the CPU. | ||||
6446 | [clinic start generated code]*/ | ||||
6447 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6448 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6449 | os_sched_yield_impl(PyObject *module) |
6450 | /*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/ | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6451 | { |
6452 | if (sched_yield()) | ||||
6453 | return posix_error(); | ||||
6454 | Py_RETURN_NONE; | ||||
6455 | } | ||||
6456 | |||||
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6457 | #ifdef HAVE_SCHED_SETAFFINITY |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6458 | /* The minimum number of CPUs allocated in a cpu_set_t */ |
6459 | static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6460 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6461 | /*[clinic input] |
6462 | os.sched_setaffinity | ||||
6463 | pid: pid_t | ||||
6464 | mask : object | ||||
6465 | / | ||||
6466 | |||||
6467 | Set the CPU affinity of the process identified by pid to mask. | ||||
6468 | |||||
6469 | mask should be an iterable of integers identifying CPUs. | ||||
6470 | [clinic start generated code]*/ | ||||
6471 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6472 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6473 | os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask) |
6474 | /*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6475 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6476 | int ncpus; |
6477 | size_t setsize; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6478 | cpu_set_t *cpu_set = NULL; |
6479 | PyObject *iterator = NULL, *item; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6480 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6481 | iterator = PyObject_GetIter(mask); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6482 | if (iterator == NULL) |
6483 | return NULL; | ||||
6484 | |||||
6485 | ncpus = NCPUS_START; | ||||
6486 | setsize = CPU_ALLOC_SIZE(ncpus); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6487 | cpu_set = CPU_ALLOC(ncpus); |
6488 | if (cpu_set == NULL) { | ||||
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6489 | PyErr_NoMemory(); |
6490 | goto error; | ||||
6491 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6492 | CPU_ZERO_S(setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6493 | |
6494 | while ((item = PyIter_Next(iterator))) { | ||||
6495 | long cpu; | ||||
6496 | if (!PyLong_Check(item)) { | ||||
6497 | PyErr_Format(PyExc_TypeError, | ||||
6498 | "expected an iterator of ints, " | ||||
6499 | "but iterator yielded %R", | ||||
6500 | Py_TYPE(item)); | ||||
6501 | Py_DECREF(item); | ||||
6502 | goto error; | ||||
6503 | } | ||||
6504 | cpu = PyLong_AsLong(item); | ||||
6505 | Py_DECREF(item); | ||||
6506 | if (cpu < 0) { | ||||
6507 | if (!PyErr_Occurred()) | ||||
6508 | PyErr_SetString(PyExc_ValueError, "negative CPU number"); | ||||
6509 | goto error; | ||||
6510 | } | ||||
6511 | if (cpu > INT_MAX - 1) { | ||||
6512 | PyErr_SetString(PyExc_OverflowError, "CPU number too large"); | ||||
6513 | goto error; | ||||
6514 | } | ||||
6515 | if (cpu >= ncpus) { | ||||
6516 | /* Grow CPU mask to fit the CPU number */ | ||||
6517 | int newncpus = ncpus; | ||||
6518 | cpu_set_t *newmask; | ||||
6519 | size_t newsetsize; | ||||
6520 | while (newncpus <= cpu) { | ||||
6521 | if (newncpus > INT_MAX / 2) | ||||
6522 | newncpus = cpu + 1; | ||||
6523 | else | ||||
6524 | newncpus = newncpus * 2; | ||||
6525 | } | ||||
6526 | newmask = CPU_ALLOC(newncpus); | ||||
6527 | if (newmask == NULL) { | ||||
6528 | PyErr_NoMemory(); | ||||
6529 | goto error; | ||||
6530 | } | ||||
6531 | newsetsize = CPU_ALLOC_SIZE(newncpus); | ||||
6532 | CPU_ZERO_S(newsetsize, newmask); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6533 | memcpy(newmask, cpu_set, setsize); |
6534 | CPU_FREE(cpu_set); | ||||
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6535 | setsize = newsetsize; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6536 | cpu_set = newmask; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6537 | ncpus = newncpus; |
6538 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6539 | CPU_SET_S(cpu, setsize, cpu_set); |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6540 | } |
Miss Islington (bot) | 6fbed53 | 2019-06-27 09:45:30 -0700 | [diff] [blame] | 6541 | if (PyErr_Occurred()) { |
6542 | goto error; | ||||
6543 | } | ||||
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6544 | Py_CLEAR(iterator); |
6545 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6546 | if (sched_setaffinity(pid, setsize, cpu_set)) { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6547 | posix_error(); |
6548 | goto error; | ||||
6549 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6550 | CPU_FREE(cpu_set); |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6551 | Py_RETURN_NONE; |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6552 | |
6553 | error: | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6554 | if (cpu_set) |
6555 | CPU_FREE(cpu_set); | ||||
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6556 | Py_XDECREF(iterator); |
6557 | return NULL; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6558 | } |
6559 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6560 | |
6561 | /*[clinic input] | ||||
6562 | os.sched_getaffinity | ||||
6563 | pid: pid_t | ||||
6564 | / | ||||
6565 | |||||
Charles-François Natali | dc87e4b | 2015-07-13 21:01:39 +0100 | [diff] [blame] | 6566 | Return the affinity of the process identified by pid (or the current process if zero). |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6567 | |
6568 | The affinity is returned as a set of CPU identifiers. | ||||
6569 | [clinic start generated code]*/ | ||||
6570 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6571 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6572 | os_sched_getaffinity_impl(PyObject *module, pid_t pid) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 6573 | /*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6574 | { |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6575 | int cpu, ncpus, count; |
6576 | size_t setsize; | ||||
6577 | cpu_set_t *mask = NULL; | ||||
6578 | PyObject *res = NULL; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6579 | |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6580 | ncpus = NCPUS_START; |
6581 | while (1) { | ||||
6582 | setsize = CPU_ALLOC_SIZE(ncpus); | ||||
6583 | mask = CPU_ALLOC(ncpus); | ||||
6584 | if (mask == NULL) | ||||
6585 | return PyErr_NoMemory(); | ||||
6586 | if (sched_getaffinity(pid, setsize, mask) == 0) | ||||
6587 | break; | ||||
6588 | CPU_FREE(mask); | ||||
6589 | if (errno != EINVAL) | ||||
6590 | return posix_error(); | ||||
6591 | if (ncpus > INT_MAX / 2) { | ||||
6592 | PyErr_SetString(PyExc_OverflowError, "could not allocate " | ||||
6593 | "a large enough CPU set"); | ||||
6594 | return NULL; | ||||
6595 | } | ||||
6596 | ncpus = ncpus * 2; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6597 | } |
Antoine Pitrou | 8486987 | 2012-08-04 16:16:35 +0200 | [diff] [blame] | 6598 | |
6599 | res = PySet_New(NULL); | ||||
6600 | if (res == NULL) | ||||
6601 | goto error; | ||||
6602 | for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) { | ||||
6603 | if (CPU_ISSET_S(cpu, setsize, mask)) { | ||||
6604 | PyObject *cpu_num = PyLong_FromLong(cpu); | ||||
6605 | --count; | ||||
6606 | if (cpu_num == NULL) | ||||
6607 | goto error; | ||||
6608 | if (PySet_Add(res, cpu_num)) { | ||||
6609 | Py_DECREF(cpu_num); | ||||
6610 | goto error; | ||||
6611 | } | ||||
6612 | Py_DECREF(cpu_num); | ||||
6613 | } | ||||
6614 | } | ||||
6615 | CPU_FREE(mask); | ||||
6616 | return res; | ||||
6617 | |||||
6618 | error: | ||||
6619 | if (mask) | ||||
6620 | CPU_FREE(mask); | ||||
6621 | Py_XDECREF(res); | ||||
6622 | return NULL; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6623 | } |
6624 | |||||
Benjamin Peterson | 2740af8 | 2011-08-02 17:41:34 -0500 | [diff] [blame] | 6625 | #endif /* HAVE_SCHED_SETAFFINITY */ |
6626 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 6627 | #endif /* HAVE_SCHED_H */ |
6628 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6629 | |
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6630 | /* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */ |
Neal Norwitz | 2deaddb | 2003-03-21 03:08:31 +0000 | [diff] [blame] | 6631 | /* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */ |
6632 | #if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX) | ||||
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6633 | #define DEV_PTY_FILE "/dev/ptc" |
6634 | #define HAVE_DEV_PTMX | ||||
6635 | #else | ||||
6636 | #define DEV_PTY_FILE "/dev/ptmx" | ||||
6637 | #endif | ||||
6638 | |||||
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6639 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6640 | #ifdef HAVE_PTY_H |
6641 | #include <pty.h> | ||||
6642 | #else | ||||
6643 | #ifdef HAVE_LIBUTIL_H | ||||
6644 | #include <libutil.h> | ||||
Ronald Oussoren | 755740f | 2010-02-07 19:56:39 +0000 | [diff] [blame] | 6645 | #else |
6646 | #ifdef HAVE_UTIL_H | ||||
6647 | #include <util.h> | ||||
6648 | #endif /* HAVE_UTIL_H */ | ||||
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6649 | #endif /* HAVE_LIBUTIL_H */ |
6650 | #endif /* HAVE_PTY_H */ | ||||
Martin v. Löwis | 14e73b1 | 2003-01-01 09:51:12 +0000 | [diff] [blame] | 6651 | #ifdef HAVE_STROPTS_H |
6652 | #include <stropts.h> | ||||
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6653 | #endif |
ngie-eign | 7745ec4 | 2018-02-14 11:54:28 -0800 | [diff] [blame] | 6654 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6655 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6656 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6657 | #if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6658 | /*[clinic input] |
6659 | os.openpty | ||||
6660 | |||||
6661 | Open a pseudo-terminal. | ||||
6662 | |||||
6663 | Return a tuple of (master_fd, slave_fd) containing open file descriptors | ||||
6664 | for both the master and slave ends. | ||||
6665 | [clinic start generated code]*/ | ||||
6666 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6667 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6668 | os_openpty_impl(PyObject *module) |
6669 | /*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/ | ||||
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6670 | { |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6671 | int master_fd = -1, slave_fd = -1; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6672 | #ifndef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6673 | char * slave_name; |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6674 | #endif |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6675 | #if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6676 | PyOS_sighandler_t sig_saved; |
Jakub Kulík | 6f9bc72 | 2018-12-31 03:16:40 +0100 | [diff] [blame] | 6677 | #if defined(__sun) && defined(__SVR4) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6678 | extern char *ptsname(int fildes); |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6679 | #endif |
6680 | #endif | ||||
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6681 | |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6682 | #ifdef HAVE_OPENPTY |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6683 | if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6684 | goto posix_error; |
6685 | |||||
6686 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) | ||||
6687 | goto error; | ||||
6688 | if (_Py_set_inheritable(slave_fd, 0, NULL) < 0) | ||||
6689 | goto error; | ||||
6690 | |||||
Neal Norwitz | b59798b | 2003-03-21 01:43:31 +0000 | [diff] [blame] | 6691 | #elif defined(HAVE__GETPTY) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6692 | slave_name = _getpty(&master_fd, O_RDWR, 0666, 0); |
6693 | if (slave_name == NULL) | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6694 | goto posix_error; |
6695 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) | ||||
6696 | goto error; | ||||
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6697 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6698 | slave_fd = _Py_open(slave_name, O_RDWR); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6699 | if (slave_fd < 0) |
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6700 | goto error; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6701 | |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6702 | #else |
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6703 | master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6704 | if (master_fd < 0) |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6705 | goto posix_error; |
6706 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6707 | sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6708 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6709 | /* change permission of slave */ |
6710 | if (grantpt(master_fd) < 0) { | ||||
6711 | PyOS_setsig(SIGCHLD, sig_saved); | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6712 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6713 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6714 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6715 | /* unlock slave */ |
6716 | if (unlockpt(master_fd) < 0) { | ||||
6717 | PyOS_setsig(SIGCHLD, sig_saved); | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6718 | goto posix_error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6719 | } |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6720 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6721 | PyOS_setsig(SIGCHLD, sig_saved); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6722 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6723 | slave_name = ptsname(master_fd); /* get name of slave */ |
6724 | if (slave_name == NULL) | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6725 | goto posix_error; |
6726 | |||||
6727 | slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */ | ||||
Victor Stinner | a555cfc | 2015-03-18 00:22:14 +0100 | [diff] [blame] | 6728 | if (slave_fd == -1) |
6729 | goto error; | ||||
Victor Stinner | 000de53 | 2013-11-25 23:19:58 +0100 | [diff] [blame] | 6730 | |
6731 | if (_Py_set_inheritable(master_fd, 0, NULL) < 0) | ||||
6732 | goto posix_error; | ||||
6733 | |||||
Stefan Krah | fb7c8ae | 2016-04-26 17:04:18 +0200 | [diff] [blame] | 6734 | #if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6735 | ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */ |
6736 | ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */ | ||||
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6737 | #ifndef __hpux |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6738 | ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */ |
Neal Norwitz | 6700e47 | 2002-12-31 16:16:07 +0000 | [diff] [blame] | 6739 | #endif /* __hpux */ |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6740 | #endif /* HAVE_CYGWIN */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 6741 | #endif /* HAVE_OPENPTY */ |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6742 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6743 | return Py_BuildValue("(ii)", master_fd, slave_fd); |
Thomas Wouters | 70c21a1 | 2000-07-14 14:28:33 +0000 | [diff] [blame] | 6744 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 6745 | posix_error: |
6746 | posix_error(); | ||||
6747 | error: | ||||
6748 | if (master_fd != -1) | ||||
6749 | close(master_fd); | ||||
6750 | if (slave_fd != -1) | ||||
6751 | close(slave_fd); | ||||
6752 | return NULL; | ||||
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6753 | } |
Martin v. Löwis | 24a880b | 2002-12-31 12:55:15 +0000 | [diff] [blame] | 6754 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */ |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6755 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6756 | |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6757 | #ifdef HAVE_FORKPTY |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6758 | /*[clinic input] |
6759 | os.forkpty | ||||
6760 | |||||
6761 | Fork a new process with a new pseudo-terminal as controlling tty. | ||||
6762 | |||||
6763 | Returns a tuple of (pid, master_fd). | ||||
6764 | Like fork(), return pid of 0 to the child process, | ||||
6765 | and pid of child to the parent process. | ||||
6766 | To both, return fd of newly opened pseudo-terminal. | ||||
6767 | [clinic start generated code]*/ | ||||
6768 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6769 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6770 | os_forkpty_impl(PyObject *module) |
6771 | /*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/ | ||||
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6772 | { |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6773 | int master_fd = -1; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6774 | pid_t pid; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 6775 | |
Eric Snow | 5903296 | 2018-09-14 14:17:20 -0700 | [diff] [blame] | 6776 | if (_PyInterpreterState_Get() != PyInterpreterState_Main()) { |
6777 | PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters"); | ||||
6778 | return NULL; | ||||
6779 | } | ||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 6780 | if (PySys_Audit("os.forkpty", NULL) < 0) { |
6781 | return NULL; | ||||
6782 | } | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6783 | PyOS_BeforeFork(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6784 | pid = forkpty(&master_fd, NULL, NULL, NULL); |
6785 | if (pid == 0) { | ||||
6786 | /* child: this clobbers and resets the import lock. */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6787 | PyOS_AfterFork_Child(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6788 | } else { |
6789 | /* parent: release the import lock. */ | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 6790 | PyOS_AfterFork_Parent(); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6791 | } |
6792 | if (pid == -1) | ||||
6793 | return posix_error(); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6794 | return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); |
Fred Drake | 8cef4cf | 2000-06-28 16:40:38 +0000 | [diff] [blame] | 6795 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6796 | #endif /* HAVE_FORKPTY */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6797 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 6798 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6799 | #ifdef HAVE_GETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6800 | /*[clinic input] |
6801 | os.getegid | ||||
6802 | |||||
6803 | Return the current process's effective group id. | ||||
6804 | [clinic start generated code]*/ | ||||
6805 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6806 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6807 | os_getegid_impl(PyObject *module) |
6808 | /*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/ | ||||
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6809 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6810 | return _PyLong_FromGid(getegid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6811 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6812 | #endif /* HAVE_GETEGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6813 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6814 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6815 | #ifdef HAVE_GETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6816 | /*[clinic input] |
6817 | os.geteuid | ||||
6818 | |||||
6819 | Return the current process's effective user id. | ||||
6820 | [clinic start generated code]*/ | ||||
6821 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6822 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6823 | os_geteuid_impl(PyObject *module) |
6824 | /*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/ | ||||
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6825 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6826 | return _PyLong_FromUid(geteuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6827 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6828 | #endif /* HAVE_GETEUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6829 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6830 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 6831 | #ifdef HAVE_GETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6832 | /*[clinic input] |
6833 | os.getgid | ||||
6834 | |||||
6835 | Return the current process's group id. | ||||
6836 | [clinic start generated code]*/ | ||||
6837 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6838 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6839 | os_getgid_impl(PyObject *module) |
6840 | /*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/ | ||||
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6841 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6842 | return _PyLong_FromGid(getgid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6843 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6844 | #endif /* HAVE_GETGID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 6845 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6846 | |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6847 | #ifdef HAVE_GETPID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6848 | /*[clinic input] |
6849 | os.getpid | ||||
6850 | |||||
6851 | Return the current process id. | ||||
6852 | [clinic start generated code]*/ | ||||
6853 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6854 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6855 | os_getpid_impl(PyObject *module) |
6856 | /*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/ | ||||
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6857 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6858 | return PyLong_FromPid(getpid()); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6859 | } |
Berker Peksag | 3940499 | 2016-09-15 20:45:16 +0300 | [diff] [blame] | 6860 | #endif /* HAVE_GETPID */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 6861 | |
Miss Islington (bot) | c80183e | 2019-06-13 00:27:23 -0700 | [diff] [blame] | 6862 | #ifdef NGROUPS_MAX |
6863 | #define MAX_GROUPS NGROUPS_MAX | ||||
6864 | #else | ||||
6865 | /* defined to be 16 on Solaris7, so this should be a small number */ | ||||
6866 | #define MAX_GROUPS 64 | ||||
6867 | #endif | ||||
6868 | |||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6869 | #ifdef HAVE_GETGROUPLIST |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6870 | |
6871 | /* AC 3.5: funny apple logic below */ | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6872 | PyDoc_STRVAR(posix_getgrouplist__doc__, |
6873 | "getgrouplist(user, group) -> list of groups to which a user belongs\n\n\ | ||||
6874 | Returns a list of groups to which a user belongs.\n\n\ | ||||
6875 | user: username to lookup\n\ | ||||
6876 | group: base group id of the user"); | ||||
6877 | |||||
6878 | static PyObject * | ||||
6879 | posix_getgrouplist(PyObject *self, PyObject *args) | ||||
6880 | { | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6881 | const char *user; |
6882 | int i, ngroups; | ||||
6883 | PyObject *list; | ||||
6884 | #ifdef __APPLE__ | ||||
6885 | int *groups, basegid; | ||||
6886 | #else | ||||
6887 | gid_t *groups, basegid; | ||||
6888 | #endif | ||||
Miss Islington (bot) | c80183e | 2019-06-13 00:27:23 -0700 | [diff] [blame] | 6889 | |
6890 | /* | ||||
6891 | * NGROUPS_MAX is defined by POSIX.1 as the maximum | ||||
6892 | * number of supplimental groups a users can belong to. | ||||
6893 | * We have to increment it by one because | ||||
6894 | * getgrouplist() returns both the supplemental groups | ||||
6895 | * and the primary group, i.e. all of the groups the | ||||
6896 | * user belongs to. | ||||
6897 | */ | ||||
6898 | ngroups = 1 + MAX_GROUPS; | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6899 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6900 | #ifdef __APPLE__ |
6901 | if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid)) | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6902 | return NULL; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6903 | #else |
6904 | if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user, | ||||
6905 | _Py_Gid_Converter, &basegid)) | ||||
6906 | return NULL; | ||||
6907 | #endif | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6908 | |
Miss Islington (bot) | af6fd1f | 2020-03-24 10:40:31 -0700 | [diff] [blame] | 6909 | while (1) { |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6910 | #ifdef __APPLE__ |
Miss Islington (bot) | 21bee0b | 2020-03-23 12:18:41 -0700 | [diff] [blame] | 6911 | groups = PyMem_New(int, ngroups); |
Miss Islington (bot) | af6fd1f | 2020-03-24 10:40:31 -0700 | [diff] [blame] | 6912 | #else |
6913 | groups = PyMem_New(gid_t, ngroups); | ||||
6914 | #endif | ||||
Miss Islington (bot) | 21bee0b | 2020-03-23 12:18:41 -0700 | [diff] [blame] | 6915 | if (groups == NULL) { |
6916 | return PyErr_NoMemory(); | ||||
6917 | } | ||||
Miss Islington (bot) | af6fd1f | 2020-03-24 10:40:31 -0700 | [diff] [blame] | 6918 | |
6919 | int old_ngroups = ngroups; | ||||
6920 | if (getgrouplist(user, basegid, groups, &ngroups) != -1) { | ||||
6921 | /* Success */ | ||||
6922 | break; | ||||
6923 | } | ||||
6924 | |||||
6925 | /* getgrouplist() fails if the group list is too small */ | ||||
6926 | PyMem_Free(groups); | ||||
6927 | |||||
6928 | if (ngroups > old_ngroups) { | ||||
6929 | /* If the group list is too small, the glibc implementation of | ||||
6930 | getgrouplist() sets ngroups to the total number of groups and | ||||
6931 | returns -1. */ | ||||
6932 | } | ||||
6933 | else { | ||||
6934 | /* Double the group list size */ | ||||
6935 | if (ngroups > INT_MAX / 2) { | ||||
6936 | return PyErr_NoMemory(); | ||||
6937 | } | ||||
6938 | ngroups *= 2; | ||||
6939 | } | ||||
6940 | |||||
6941 | /* Retry getgrouplist() with a larger group list */ | ||||
Miss Islington (bot) | 21bee0b | 2020-03-23 12:18:41 -0700 | [diff] [blame] | 6942 | } |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6943 | |
Gregory P. Smith | 1d300ce | 2018-12-30 21:13:02 -0800 | [diff] [blame] | 6944 | #ifdef _Py_MEMORY_SANITIZER |
6945 | /* Clang memory sanitizer libc intercepts don't know getgrouplist. */ | ||||
6946 | __msan_unpoison(&ngroups, sizeof(ngroups)); | ||||
6947 | __msan_unpoison(groups, ngroups*sizeof(*groups)); | ||||
6948 | #endif | ||||
6949 | |||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6950 | list = PyList_New(ngroups); |
6951 | if (list == NULL) { | ||||
6952 | PyMem_Del(groups); | ||||
6953 | return NULL; | ||||
6954 | } | ||||
6955 | |||||
6956 | for (i = 0; i < ngroups; i++) { | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6957 | #ifdef __APPLE__ |
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6958 | PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]); |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 6959 | #else |
6960 | PyObject *o = _PyLong_FromGid(groups[i]); | ||||
6961 | #endif | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 6962 | if (o == NULL) { |
6963 | Py_DECREF(list); | ||||
6964 | PyMem_Del(groups); | ||||
6965 | return NULL; | ||||
6966 | } | ||||
6967 | PyList_SET_ITEM(list, i, o); | ||||
6968 | } | ||||
6969 | |||||
6970 | PyMem_Del(groups); | ||||
6971 | |||||
6972 | return list; | ||||
6973 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6974 | #endif /* HAVE_GETGROUPLIST */ |
6975 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 6976 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6977 | #ifdef HAVE_GETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6978 | /*[clinic input] |
6979 | os.getgroups | ||||
6980 | |||||
6981 | Return list of supplemental group IDs for the process. | ||||
6982 | [clinic start generated code]*/ | ||||
6983 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 6984 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 6985 | os_getgroups_impl(PyObject *module) |
6986 | /*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/ | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 6987 | { |
6988 | PyObject *result = NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 6989 | gid_t grouplist[MAX_GROUPS]; |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6990 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 6991 | /* On MacOSX getgroups(2) can return more than MAX_GROUPS results |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 6992 | * This is a helper variable to store the intermediate result when |
6993 | * that happens. | ||||
6994 | * | ||||
6995 | * To keep the code readable the OSX behaviour is unconditional, | ||||
6996 | * according to the POSIX spec this should be safe on all unix-y | ||||
6997 | * systems. | ||||
6998 | */ | ||||
6999 | gid_t* alt_grouplist = grouplist; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7000 | int n; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7001 | |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7002 | #ifdef __APPLE__ |
7003 | /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if | ||||
7004 | * there are more groups than can fit in grouplist. Therefore, on OS X | ||||
7005 | * always first call getgroups with length 0 to get the actual number | ||||
7006 | * of groups. | ||||
7007 | */ | ||||
7008 | n = getgroups(0, NULL); | ||||
7009 | if (n < 0) { | ||||
7010 | return posix_error(); | ||||
7011 | } else if (n <= MAX_GROUPS) { | ||||
7012 | /* groups will fit in existing array */ | ||||
7013 | alt_grouplist = grouplist; | ||||
7014 | } else { | ||||
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7015 | alt_grouplist = PyMem_New(gid_t, n); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7016 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7017 | return PyErr_NoMemory(); |
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7018 | } |
7019 | } | ||||
7020 | |||||
7021 | n = getgroups(n, alt_grouplist); | ||||
7022 | if (n == -1) { | ||||
7023 | if (alt_grouplist != grouplist) { | ||||
7024 | PyMem_Free(alt_grouplist); | ||||
7025 | } | ||||
7026 | return posix_error(); | ||||
7027 | } | ||||
7028 | #else | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7029 | n = getgroups(MAX_GROUPS, grouplist); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7030 | if (n < 0) { |
7031 | if (errno == EINVAL) { | ||||
7032 | n = getgroups(0, NULL); | ||||
7033 | if (n == -1) { | ||||
7034 | return posix_error(); | ||||
7035 | } | ||||
7036 | if (n == 0) { | ||||
7037 | /* Avoid malloc(0) */ | ||||
7038 | alt_grouplist = grouplist; | ||||
7039 | } else { | ||||
Serhiy Storchaka | 1a1ff29 | 2015-02-16 13:28:22 +0200 | [diff] [blame] | 7040 | alt_grouplist = PyMem_New(gid_t, n); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7041 | if (alt_grouplist == NULL) { |
Zackery Spytz | 4c49da0 | 2018-12-07 03:11:30 -0700 | [diff] [blame] | 7042 | return PyErr_NoMemory(); |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7043 | } |
7044 | n = getgroups(n, alt_grouplist); | ||||
7045 | if (n == -1) { | ||||
7046 | PyMem_Free(alt_grouplist); | ||||
7047 | return posix_error(); | ||||
7048 | } | ||||
7049 | } | ||||
7050 | } else { | ||||
7051 | return posix_error(); | ||||
7052 | } | ||||
7053 | } | ||||
Ned Deily | b5dd6d2 | 2013-08-01 21:21:15 -0700 | [diff] [blame] | 7054 | #endif |
7055 | |||||
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7056 | result = PyList_New(n); |
7057 | if (result != NULL) { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7058 | int i; |
7059 | for (i = 0; i < n; ++i) { | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7060 | PyObject *o = _PyLong_FromGid(alt_grouplist[i]); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7061 | if (o == NULL) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 7062 | Py_DECREF(result); |
7063 | result = NULL; | ||||
7064 | break; | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7065 | } |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7066 | PyList_SET_ITEM(result, i, o); |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7067 | } |
Ronald Oussoren | b6ee4f5 | 2010-07-23 13:53:51 +0000 | [diff] [blame] | 7068 | } |
7069 | |||||
7070 | if (alt_grouplist != grouplist) { | ||||
7071 | PyMem_Free(alt_grouplist); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7072 | } |
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7073 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7074 | return result; |
7075 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7076 | #endif /* HAVE_GETGROUPS */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 7077 | |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7078 | #ifdef HAVE_INITGROUPS |
7079 | PyDoc_STRVAR(posix_initgroups__doc__, | ||||
7080 | "initgroups(username, gid) -> None\n\n\ | ||||
7081 | Call the system initgroups() to initialize the group access list with all of\n\ | ||||
7082 | the groups of which the specified username is a member, plus the specified\n\ | ||||
7083 | group id."); | ||||
7084 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7085 | /* AC 3.5: funny apple logic */ |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7086 | static PyObject * |
7087 | posix_initgroups(PyObject *self, PyObject *args) | ||||
7088 | { | ||||
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7089 | PyObject *oname; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 7090 | const char *username; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7091 | int res; |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7092 | #ifdef __APPLE__ |
7093 | int gid; | ||||
7094 | #else | ||||
7095 | gid_t gid; | ||||
7096 | #endif | ||||
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7097 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7098 | #ifdef __APPLE__ |
7099 | if (!PyArg_ParseTuple(args, "O&i:initgroups", | ||||
7100 | PyUnicode_FSConverter, &oname, | ||||
7101 | &gid)) | ||||
7102 | #else | ||||
7103 | if (!PyArg_ParseTuple(args, "O&O&:initgroups", | ||||
7104 | PyUnicode_FSConverter, &oname, | ||||
7105 | _Py_Gid_Converter, &gid)) | ||||
7106 | #endif | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7107 | return NULL; |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7108 | username = PyBytes_AS_STRING(oname); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7109 | |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7110 | res = initgroups(username, gid); |
Victor Stinner | 61ec5dc | 2010-08-15 09:22:44 +0000 | [diff] [blame] | 7111 | Py_DECREF(oname); |
7112 | if (res == -1) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7113 | return PyErr_SetFromErrno(PyExc_OSError); |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7114 | |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7115 | Py_RETURN_NONE; |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7116 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7117 | #endif /* HAVE_INITGROUPS */ |
7118 | |||||
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 7119 | |
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7120 | #ifdef HAVE_GETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7121 | /*[clinic input] |
7122 | os.getpgid | ||||
7123 | |||||
7124 | pid: pid_t | ||||
7125 | |||||
7126 | Call the system call getpgid(), and return the result. | ||||
7127 | [clinic start generated code]*/ | ||||
7128 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7129 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7130 | os_getpgid_impl(PyObject *module, pid_t pid) |
7131 | /*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7132 | { |
7133 | pid_t pgid = getpgid(pid); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7134 | if (pgid < 0) |
7135 | return posix_error(); | ||||
7136 | return PyLong_FromPid(pgid); | ||||
Martin v. Löwis | 606edc1 | 2002-06-13 21:09:11 +0000 | [diff] [blame] | 7137 | } |
7138 | #endif /* HAVE_GETPGID */ | ||||
7139 | |||||
7140 | |||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7141 | #ifdef HAVE_GETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7142 | /*[clinic input] |
7143 | os.getpgrp | ||||
7144 | |||||
7145 | Return the current process group id. | ||||
7146 | [clinic start generated code]*/ | ||||
7147 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7148 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7149 | os_getpgrp_impl(PyObject *module) |
7150 | /*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/ | ||||
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7151 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7152 | #ifdef GETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7153 | return PyLong_FromPid(getpgrp(0)); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7154 | #else /* GETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7155 | return PyLong_FromPid(getpgrp()); |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7156 | #endif /* GETPGRP_HAVE_ARG */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7157 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7158 | #endif /* HAVE_GETPGRP */ |
Guido van Rossum | 0481447 | 1991-06-04 20:23:49 +0000 | [diff] [blame] | 7159 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7160 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7161 | #ifdef HAVE_SETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7162 | /*[clinic input] |
7163 | os.setpgrp | ||||
7164 | |||||
7165 | Make the current process the leader of its process group. | ||||
7166 | [clinic start generated code]*/ | ||||
7167 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7168 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7169 | os_setpgrp_impl(PyObject *module) |
7170 | /*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/ | ||||
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7171 | { |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7172 | #ifdef SETPGRP_HAVE_ARG |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7173 | if (setpgrp(0, 0) < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7174 | #else /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7175 | if (setpgrp() < 0) |
Guido van Rossum | 6493389 | 1994-10-20 21:56:42 +0000 | [diff] [blame] | 7176 | #endif /* SETPGRP_HAVE_ARG */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7177 | return posix_error(); |
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7178 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 7179 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7180 | #endif /* HAVE_SETPGRP */ |
7181 | |||||
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7182 | #ifdef HAVE_GETPPID |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7183 | |
7184 | #ifdef MS_WINDOWS | ||||
7185 | #include <tlhelp32.h> | ||||
7186 | |||||
7187 | static PyObject* | ||||
7188 | win32_getppid() | ||||
7189 | { | ||||
7190 | HANDLE snapshot; | ||||
7191 | pid_t mypid; | ||||
7192 | PyObject* result = NULL; | ||||
7193 | BOOL have_record; | ||||
7194 | PROCESSENTRY32 pe; | ||||
7195 | |||||
7196 | mypid = getpid(); /* This function never fails */ | ||||
7197 | |||||
7198 | snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); | ||||
7199 | if (snapshot == INVALID_HANDLE_VALUE) | ||||
7200 | return PyErr_SetFromWindowsErr(GetLastError()); | ||||
7201 | |||||
7202 | pe.dwSize = sizeof(pe); | ||||
7203 | have_record = Process32First(snapshot, &pe); | ||||
7204 | while (have_record) { | ||||
7205 | if (mypid == (pid_t)pe.th32ProcessID) { | ||||
7206 | /* We could cache the ulong value in a static variable. */ | ||||
7207 | result = PyLong_FromPid((pid_t)pe.th32ParentProcessID); | ||||
7208 | break; | ||||
7209 | } | ||||
7210 | |||||
7211 | have_record = Process32Next(snapshot, &pe); | ||||
7212 | } | ||||
7213 | |||||
7214 | /* If our loop exits and our pid was not found (result will be NULL) | ||||
7215 | * then GetLastError will return ERROR_NO_MORE_FILES. This is an | ||||
7216 | * error anyway, so let's raise it. */ | ||||
7217 | if (!result) | ||||
7218 | result = PyErr_SetFromWindowsErr(GetLastError()); | ||||
7219 | |||||
7220 | CloseHandle(snapshot); | ||||
7221 | |||||
7222 | return result; | ||||
7223 | } | ||||
7224 | #endif /*MS_WINDOWS*/ | ||||
7225 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7226 | |
7227 | /*[clinic input] | ||||
7228 | os.getppid | ||||
7229 | |||||
7230 | Return the parent's process id. | ||||
7231 | |||||
7232 | If the parent process has already exited, Windows machines will still | ||||
7233 | return its id; others systems will return the id of the 'init' process (1). | ||||
7234 | [clinic start generated code]*/ | ||||
7235 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7236 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7237 | os_getppid_impl(PyObject *module) |
7238 | /*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/ | ||||
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7239 | { |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7240 | #ifdef MS_WINDOWS |
7241 | return win32_getppid(); | ||||
7242 | #else | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7243 | return PyLong_FromPid(getppid()); |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7244 | #endif |
Amaury Forgeot d'Arc | 4b6fdf3 | 2010-09-07 21:31:17 +0000 | [diff] [blame] | 7245 | } |
7246 | #endif /* HAVE_GETPPID */ | ||||
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7247 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7248 | |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7249 | #ifdef HAVE_GETLOGIN |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7250 | /*[clinic input] |
7251 | os.getlogin | ||||
7252 | |||||
7253 | Return the actual login name. | ||||
7254 | [clinic start generated code]*/ | ||||
7255 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7256 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7257 | os_getlogin_impl(PyObject *module) |
7258 | /*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/ | ||||
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7259 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7260 | PyObject *result = NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7261 | #ifdef MS_WINDOWS |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7262 | wchar_t user_name[UNLEN + 1]; |
Victor Stinner | 6394188 | 2011-09-29 00:42:28 +0200 | [diff] [blame] | 7263 | DWORD num_chars = Py_ARRAY_LENGTH(user_name); |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7264 | |
7265 | if (GetUserNameW(user_name, &num_chars)) { | ||||
7266 | /* num_chars is the number of unicode chars plus null terminator */ | ||||
7267 | result = PyUnicode_FromWideChar(user_name, num_chars - 1); | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 7268 | } |
7269 | else | ||||
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7270 | result = PyErr_SetFromWindowsErr(GetLastError()); |
7271 | #else | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7272 | char *name; |
7273 | int old_errno = errno; | ||||
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7274 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7275 | errno = 0; |
7276 | name = getlogin(); | ||||
7277 | if (name == NULL) { | ||||
7278 | if (errno) | ||||
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7279 | posix_error(); |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7280 | else |
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7281 | PyErr_SetString(PyExc_OSError, "unable to determine login name"); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7282 | } |
7283 | else | ||||
Victor Stinner | e039ffe | 2010-08-15 09:33:08 +0000 | [diff] [blame] | 7284 | result = PyUnicode_DecodeFSDefault(name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7285 | errno = old_errno; |
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7286 | #endif |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7287 | return result; |
7288 | } | ||||
Brian Curtin | e8e4b3b | 2010-09-23 20:04:14 +0000 | [diff] [blame] | 7289 | #endif /* HAVE_GETLOGIN */ |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 7290 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7291 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7292 | #ifdef HAVE_GETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7293 | /*[clinic input] |
7294 | os.getuid | ||||
7295 | |||||
7296 | Return the current process's user id. | ||||
7297 | [clinic start generated code]*/ | ||||
7298 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7299 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7300 | os_getuid_impl(PyObject *module) |
7301 | /*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/ | ||||
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7302 | { |
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7303 | return _PyLong_FromUid(getuid()); |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7304 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7305 | #endif /* HAVE_GETUID */ |
Guido van Rossum | 46003ff | 1992-05-15 11:05:24 +0000 | [diff] [blame] | 7306 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7307 | |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7308 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7309 | #define HAVE_KILL |
7310 | #endif /* MS_WINDOWS */ | ||||
7311 | |||||
7312 | #ifdef HAVE_KILL | ||||
7313 | /*[clinic input] | ||||
7314 | os.kill | ||||
7315 | |||||
7316 | pid: pid_t | ||||
7317 | signal: Py_ssize_t | ||||
7318 | / | ||||
7319 | |||||
7320 | Kill a process with a signal. | ||||
7321 | [clinic start generated code]*/ | ||||
7322 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7323 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7324 | os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal) |
7325 | /*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7326 | { |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 7327 | if (PySys_Audit("os.kill", "in", pid, signal) < 0) { |
7328 | return NULL; | ||||
7329 | } | ||||
7330 | #ifndef MS_WINDOWS | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7331 | if (kill(pid, (int)signal) == -1) |
7332 | return posix_error(); | ||||
7333 | Py_RETURN_NONE; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7334 | #else /* !MS_WINDOWS */ |
Amaury Forgeot d'Arc | 0a589c9 | 2010-05-15 20:35:12 +0000 | [diff] [blame] | 7335 | PyObject *result; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7336 | DWORD sig = (DWORD)signal; |
7337 | DWORD err; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7338 | HANDLE handle; |
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7339 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7340 | /* Console processes which share a common console can be sent CTRL+C or |
7341 | CTRL+BREAK events, provided they handle said events. */ | ||||
7342 | if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) { | ||||
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7343 | if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7344 | err = GetLastError(); |
7345 | PyErr_SetFromWindowsErr(err); | ||||
7346 | } | ||||
7347 | else | ||||
7348 | Py_RETURN_NONE; | ||||
7349 | } | ||||
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7350 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7351 | /* If the signal is outside of what GenerateConsoleCtrlEvent can use, |
7352 | attempt to open and terminate the process. */ | ||||
Richard Oudkerk | ac0ad88 | 2013-06-05 23:29:30 +0100 | [diff] [blame] | 7353 | handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7354 | if (handle == NULL) { |
7355 | err = GetLastError(); | ||||
7356 | return PyErr_SetFromWindowsErr(err); | ||||
7357 | } | ||||
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7358 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7359 | if (TerminateProcess(handle, sig) == 0) { |
7360 | err = GetLastError(); | ||||
7361 | result = PyErr_SetFromWindowsErr(err); | ||||
7362 | } else { | ||||
7363 | Py_INCREF(Py_None); | ||||
7364 | result = Py_None; | ||||
7365 | } | ||||
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7366 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7367 | CloseHandle(handle); |
7368 | return result; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7369 | #endif /* !MS_WINDOWS */ |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 7370 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7371 | #endif /* HAVE_KILL */ |
7372 | |||||
7373 | |||||
7374 | #ifdef HAVE_KILLPG | ||||
7375 | /*[clinic input] | ||||
7376 | os.killpg | ||||
7377 | |||||
7378 | pgid: pid_t | ||||
7379 | signal: int | ||||
7380 | / | ||||
7381 | |||||
7382 | Kill a process group with a signal. | ||||
7383 | [clinic start generated code]*/ | ||||
7384 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7385 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7386 | os_killpg_impl(PyObject *module, pid_t pgid, int signal) |
7387 | /*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7388 | { |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 7389 | if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) { |
7390 | return NULL; | ||||
7391 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7392 | /* XXX some man pages make the `pgid` parameter an int, others |
7393 | a pid_t. Since getpgrp() returns a pid_t, we assume killpg should | ||||
7394 | take the same type. Moreover, pid_t is always at least as wide as | ||||
7395 | int (else compilation of this module fails), which is safe. */ | ||||
7396 | if (killpg(pgid, signal) == -1) | ||||
7397 | return posix_error(); | ||||
7398 | Py_RETURN_NONE; | ||||
7399 | } | ||||
7400 | #endif /* HAVE_KILLPG */ | ||||
7401 | |||||
Brian Curtin | eb24d74 | 2010-04-12 17:16:38 +0000 | [diff] [blame] | 7402 | |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7403 | #ifdef HAVE_PLOCK |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7404 | #ifdef HAVE_SYS_LOCK_H |
7405 | #include <sys/lock.h> | ||||
7406 | #endif | ||||
7407 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7408 | /*[clinic input] |
7409 | os.plock | ||||
7410 | op: int | ||||
7411 | / | ||||
7412 | |||||
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 7413 | Lock program segments into memory."); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7414 | [clinic start generated code]*/ |
7415 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7416 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7417 | os_plock_impl(PyObject *module, int op) |
7418 | /*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7419 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7420 | if (plock(op) == -1) |
7421 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7422 | Py_RETURN_NONE; |
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7423 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7424 | #endif /* HAVE_PLOCK */ |
7425 | |||||
Guido van Rossum | c012547 | 1996-06-28 18:55:32 +0000 | [diff] [blame] | 7426 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7427 | #ifdef HAVE_SETUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7428 | /*[clinic input] |
7429 | os.setuid | ||||
7430 | |||||
7431 | uid: uid_t | ||||
7432 | / | ||||
7433 | |||||
7434 | Set the current process's user id. | ||||
7435 | [clinic start generated code]*/ | ||||
7436 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7437 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7438 | os_setuid_impl(PyObject *module, uid_t uid) |
7439 | /*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7440 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7441 | if (setuid(uid) < 0) |
7442 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7443 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7444 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7445 | #endif /* HAVE_SETUID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7446 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7447 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7448 | #ifdef HAVE_SETEUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7449 | /*[clinic input] |
7450 | os.seteuid | ||||
7451 | |||||
7452 | euid: uid_t | ||||
7453 | / | ||||
7454 | |||||
7455 | Set the current process's effective user id. | ||||
7456 | [clinic start generated code]*/ | ||||
7457 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7458 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7459 | os_seteuid_impl(PyObject *module, uid_t euid) |
7460 | /*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7461 | { |
7462 | if (seteuid(euid) < 0) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7463 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7464 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7465 | } |
7466 | #endif /* HAVE_SETEUID */ | ||||
7467 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7468 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7469 | #ifdef HAVE_SETEGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7470 | /*[clinic input] |
7471 | os.setegid | ||||
7472 | |||||
7473 | egid: gid_t | ||||
7474 | / | ||||
7475 | |||||
7476 | Set the current process's effective group id. | ||||
7477 | [clinic start generated code]*/ | ||||
7478 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7479 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7480 | os_setegid_impl(PyObject *module, gid_t egid) |
7481 | /*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7482 | { |
7483 | if (setegid(egid) < 0) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7484 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7485 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7486 | } |
7487 | #endif /* HAVE_SETEGID */ | ||||
7488 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7489 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7490 | #ifdef HAVE_SETREUID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7491 | /*[clinic input] |
7492 | os.setreuid | ||||
7493 | |||||
7494 | ruid: uid_t | ||||
7495 | euid: uid_t | ||||
7496 | / | ||||
7497 | |||||
7498 | Set the current process's real and effective user ids. | ||||
7499 | [clinic start generated code]*/ | ||||
7500 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7501 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7502 | os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid) |
7503 | /*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7504 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7505 | if (setreuid(ruid, euid) < 0) { |
7506 | return posix_error(); | ||||
7507 | } else { | ||||
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7508 | Py_RETURN_NONE; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7509 | } |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7510 | } |
7511 | #endif /* HAVE_SETREUID */ | ||||
7512 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7513 | |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7514 | #ifdef HAVE_SETREGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7515 | /*[clinic input] |
7516 | os.setregid | ||||
7517 | |||||
7518 | rgid: gid_t | ||||
7519 | egid: gid_t | ||||
7520 | / | ||||
7521 | |||||
7522 | Set the current process's real and effective group ids. | ||||
7523 | [clinic start generated code]*/ | ||||
7524 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7525 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7526 | os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid) |
7527 | /*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7528 | { |
7529 | if (setregid(rgid, egid) < 0) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7530 | return posix_error(); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7531 | Py_RETURN_NONE; |
Andrew M. Kuchling | 8d2f2b2d | 2000-07-13 01:26:58 +0000 | [diff] [blame] | 7532 | } |
7533 | #endif /* HAVE_SETREGID */ | ||||
7534 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7535 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 7536 | #ifdef HAVE_SETGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7537 | /*[clinic input] |
7538 | os.setgid | ||||
7539 | gid: gid_t | ||||
7540 | / | ||||
7541 | |||||
7542 | Set the current process's group id. | ||||
7543 | [clinic start generated code]*/ | ||||
7544 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7545 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7546 | os_setgid_impl(PyObject *module, gid_t gid) |
7547 | /*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7548 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7549 | if (setgid(gid) < 0) |
7550 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7551 | Py_RETURN_NONE; |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7552 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 7553 | #endif /* HAVE_SETGID */ |
Guido van Rossum | a3d78fb | 1993-11-10 09:23:53 +0000 | [diff] [blame] | 7554 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7555 | |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7556 | #ifdef HAVE_SETGROUPS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7557 | /*[clinic input] |
7558 | os.setgroups | ||||
7559 | |||||
7560 | groups: object | ||||
7561 | / | ||||
7562 | |||||
7563 | Set the groups of the current process to list. | ||||
7564 | [clinic start generated code]*/ | ||||
7565 | |||||
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7566 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7567 | os_setgroups(PyObject *module, PyObject *groups) |
7568 | /*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/ | ||||
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7569 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7570 | Py_ssize_t i, len; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7571 | gid_t grouplist[MAX_GROUPS]; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 7572 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7573 | if (!PySequence_Check(groups)) { |
7574 | PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence"); | ||||
7575 | return NULL; | ||||
7576 | } | ||||
7577 | len = PySequence_Size(groups); | ||||
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 7578 | if (len < 0) { |
7579 | return NULL; | ||||
7580 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7581 | if (len > MAX_GROUPS) { |
7582 | PyErr_SetString(PyExc_ValueError, "too many groups"); | ||||
7583 | return NULL; | ||||
7584 | } | ||||
7585 | for(i = 0; i < len; i++) { | ||||
7586 | PyObject *elem; | ||||
7587 | elem = PySequence_GetItem(groups, i); | ||||
7588 | if (!elem) | ||||
7589 | return NULL; | ||||
7590 | if (!PyLong_Check(elem)) { | ||||
7591 | PyErr_SetString(PyExc_TypeError, | ||||
7592 | "groups must be integers"); | ||||
7593 | Py_DECREF(elem); | ||||
7594 | return NULL; | ||||
7595 | } else { | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7596 | if (!_Py_Gid_Converter(elem, &grouplist[i])) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7597 | Py_DECREF(elem); |
7598 | return NULL; | ||||
7599 | } | ||||
7600 | } | ||||
7601 | Py_DECREF(elem); | ||||
7602 | } | ||||
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7603 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7604 | if (setgroups(len, grouplist) < 0) |
7605 | return posix_error(); | ||||
Serhiy Storchaka | 228b12e | 2017-01-23 09:47:21 +0200 | [diff] [blame] | 7606 | Py_RETURN_NONE; |
Martin v. Löwis | 61c5edf | 2001-10-18 04:06:00 +0000 | [diff] [blame] | 7607 | } |
7608 | #endif /* HAVE_SETGROUPS */ | ||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7609 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7610 | #if defined(HAVE_WAIT3) || defined(HAVE_WAIT4) |
7611 | static PyObject * | ||||
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7612 | wait_helper(pid_t pid, int status, struct rusage *ru) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7613 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7614 | PyObject *result; |
7615 | static PyObject *struct_rusage; | ||||
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 7616 | _Py_IDENTIFIER(struct_rusage); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7617 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7618 | if (pid == -1) |
7619 | return posix_error(); | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7620 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7621 | if (struct_rusage == NULL) { |
7622 | PyObject *m = PyImport_ImportModuleNoBlock("resource"); | ||||
7623 | if (m == NULL) | ||||
7624 | return NULL; | ||||
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 7625 | struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7626 | Py_DECREF(m); |
7627 | if (struct_rusage == NULL) | ||||
7628 | return NULL; | ||||
7629 | } | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7630 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7631 | /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */ |
7632 | result = PyStructSequence_New((PyTypeObject*) struct_rusage); | ||||
7633 | if (!result) | ||||
7634 | return NULL; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7635 | |
7636 | #ifndef doubletime | ||||
7637 | #define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001) | ||||
7638 | #endif | ||||
7639 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7640 | PyStructSequence_SET_ITEM(result, 0, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7641 | PyFloat_FromDouble(doubletime(ru->ru_utime))); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7642 | PyStructSequence_SET_ITEM(result, 1, |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7643 | PyFloat_FromDouble(doubletime(ru->ru_stime))); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7644 | #define SET_INT(result, index, value)\ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7645 | PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value)) |
7646 | SET_INT(result, 2, ru->ru_maxrss); | ||||
7647 | SET_INT(result, 3, ru->ru_ixrss); | ||||
7648 | SET_INT(result, 4, ru->ru_idrss); | ||||
7649 | SET_INT(result, 5, ru->ru_isrss); | ||||
7650 | SET_INT(result, 6, ru->ru_minflt); | ||||
7651 | SET_INT(result, 7, ru->ru_majflt); | ||||
7652 | SET_INT(result, 8, ru->ru_nswap); | ||||
7653 | SET_INT(result, 9, ru->ru_inblock); | ||||
7654 | SET_INT(result, 10, ru->ru_oublock); | ||||
7655 | SET_INT(result, 11, ru->ru_msgsnd); | ||||
7656 | SET_INT(result, 12, ru->ru_msgrcv); | ||||
7657 | SET_INT(result, 13, ru->ru_nsignals); | ||||
7658 | SET_INT(result, 14, ru->ru_nvcsw); | ||||
7659 | SET_INT(result, 15, ru->ru_nivcsw); | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7660 | #undef SET_INT |
7661 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7662 | if (PyErr_Occurred()) { |
7663 | Py_DECREF(result); | ||||
7664 | return NULL; | ||||
7665 | } | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7666 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7667 | return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7668 | } |
7669 | #endif /* HAVE_WAIT3 || HAVE_WAIT4 */ | ||||
7670 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7671 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7672 | #ifdef HAVE_WAIT3 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7673 | /*[clinic input] |
7674 | os.wait3 | ||||
7675 | |||||
7676 | options: int | ||||
7677 | Wait for completion of a child process. | ||||
7678 | |||||
7679 | Returns a tuple of information about the child process: | ||||
7680 | (pid, status, rusage) | ||||
7681 | [clinic start generated code]*/ | ||||
7682 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7683 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7684 | os_wait3_impl(PyObject *module, int options) |
7685 | /*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/ | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7686 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7687 | pid_t pid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7688 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7689 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7690 | WAIT_TYPE status; |
7691 | WAIT_STATUS_INT(status) = 0; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7692 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7693 | do { |
7694 | Py_BEGIN_ALLOW_THREADS | ||||
7695 | pid = wait3(&status, options, &ru); | ||||
7696 | Py_END_ALLOW_THREADS | ||||
7697 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
7698 | if (pid < 0) | ||||
7699 | return (!async_err) ? posix_error() : NULL; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7700 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 7701 | return wait_helper(pid, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7702 | } |
7703 | #endif /* HAVE_WAIT3 */ | ||||
7704 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7705 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7706 | #ifdef HAVE_WAIT4 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7707 | /*[clinic input] |
7708 | |||||
7709 | os.wait4 | ||||
7710 | |||||
7711 | pid: pid_t | ||||
7712 | options: int | ||||
7713 | |||||
7714 | Wait for completion of a specific child process. | ||||
7715 | |||||
7716 | Returns a tuple of information about the child process: | ||||
7717 | (pid, status, rusage) | ||||
7718 | [clinic start generated code]*/ | ||||
7719 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7720 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7721 | os_wait4_impl(PyObject *module, pid_t pid, int options) |
7722 | /*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7723 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7724 | pid_t res; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7725 | struct rusage ru; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7726 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7727 | WAIT_TYPE status; |
7728 | WAIT_STATUS_INT(status) = 0; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7729 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7730 | do { |
7731 | Py_BEGIN_ALLOW_THREADS | ||||
7732 | res = wait4(pid, &status, options, &ru); | ||||
7733 | Py_END_ALLOW_THREADS | ||||
7734 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
7735 | if (res < 0) | ||||
7736 | return (!async_err) ? posix_error() : NULL; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7737 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7738 | return wait_helper(res, WAIT_STATUS_INT(status), &ru); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7739 | } |
7740 | #endif /* HAVE_WAIT4 */ | ||||
7741 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7742 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7743 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7744 | /*[clinic input] |
7745 | os.waitid | ||||
7746 | |||||
7747 | idtype: idtype_t | ||||
7748 | Must be one of be P_PID, P_PGID or P_ALL. | ||||
7749 | id: id_t | ||||
7750 | The id to wait on. | ||||
7751 | options: int | ||||
7752 | Constructed from the ORing of one or more of WEXITED, WSTOPPED | ||||
7753 | or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT. | ||||
7754 | / | ||||
7755 | |||||
7756 | Returns the result of waiting for a process or processes. | ||||
7757 | |||||
7758 | Returns either waitid_result or None if WNOHANG is specified and there are | ||||
7759 | no children in a waitable state. | ||||
7760 | [clinic start generated code]*/ | ||||
7761 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7762 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7763 | os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options) |
7764 | /*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7765 | { |
7766 | PyObject *result; | ||||
7767 | int res; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7768 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7769 | siginfo_t si; |
7770 | si.si_pid = 0; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7771 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7772 | do { |
7773 | Py_BEGIN_ALLOW_THREADS | ||||
7774 | res = waitid(idtype, id, &si, options); | ||||
7775 | Py_END_ALLOW_THREADS | ||||
7776 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
7777 | if (res < 0) | ||||
7778 | return (!async_err) ? posix_error() : NULL; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7779 | |
7780 | if (si.si_pid == 0) | ||||
7781 | Py_RETURN_NONE; | ||||
7782 | |||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 7783 | result = PyStructSequence_New(WaitidResultType); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7784 | if (!result) |
7785 | return NULL; | ||||
7786 | |||||
7787 | PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid)); | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 7788 | PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid)); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7789 | PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo))); |
7790 | PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status))); | ||||
7791 | PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code))); | ||||
7792 | if (PyErr_Occurred()) { | ||||
7793 | Py_DECREF(result); | ||||
7794 | return NULL; | ||||
7795 | } | ||||
7796 | |||||
7797 | return result; | ||||
7798 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7799 | #endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 7800 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7801 | |
7802 | #if defined(HAVE_WAITPID) | ||||
7803 | /*[clinic input] | ||||
7804 | os.waitpid | ||||
7805 | pid: pid_t | ||||
7806 | options: int | ||||
7807 | / | ||||
7808 | |||||
7809 | Wait for completion of a given child process. | ||||
7810 | |||||
7811 | Returns a tuple of information regarding the child process: | ||||
7812 | (pid, status) | ||||
7813 | |||||
7814 | The options argument is ignored on Windows. | ||||
7815 | [clinic start generated code]*/ | ||||
7816 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7817 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7818 | os_waitpid_impl(PyObject *module, pid_t pid, int options) |
7819 | /*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7820 | { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7821 | pid_t res; |
7822 | int async_err = 0; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7823 | WAIT_TYPE status; |
7824 | WAIT_STATUS_INT(status) = 0; | ||||
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 7825 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7826 | do { |
7827 | Py_BEGIN_ALLOW_THREADS | ||||
7828 | res = waitpid(pid, &status, options); | ||||
7829 | Py_END_ALLOW_THREADS | ||||
7830 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
7831 | if (res < 0) | ||||
7832 | return (!async_err) ? posix_error() : NULL; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7833 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7834 | return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status)); |
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7835 | } |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7836 | #elif defined(HAVE_CWAIT) |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7837 | /* 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] | 7838 | /*[clinic input] |
7839 | os.waitpid | ||||
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7840 | pid: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7841 | options: int |
7842 | / | ||||
7843 | |||||
7844 | Wait for completion of a given process. | ||||
7845 | |||||
7846 | Returns a tuple of information regarding the process: | ||||
7847 | (pid, status << 8) | ||||
7848 | |||||
7849 | The options argument is ignored on Windows. | ||||
7850 | [clinic start generated code]*/ | ||||
7851 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7852 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7853 | os_waitpid_impl(PyObject *module, intptr_t pid, int options) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 7854 | /*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7855 | { |
7856 | int status; | ||||
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 7857 | intptr_t res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7858 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7859 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7860 | do { |
7861 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7862 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7863 | res = _cwait(&status, pid, options); |
Steve Dower | 11f4326 | 2016-11-19 18:33:39 -0800 | [diff] [blame] | 7864 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7865 | Py_END_ALLOW_THREADS |
7866 | } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 7867 | if (res < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7868 | return (!async_err) ? posix_error() : NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7869 | |
Victor Stinner | b073509 | 2020-04-22 17:57:59 +0200 | [diff] [blame^] | 7870 | unsigned long long ustatus = (unsigned int)status; |
7871 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7872 | /* shift the status left a byte so this is more like the POSIX waitpid */ |
Victor Stinner | b073509 | 2020-04-22 17:57:59 +0200 | [diff] [blame^] | 7873 | return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8); |
Tim Peters | ab034fa | 2002-02-01 11:27:43 +0000 | [diff] [blame] | 7874 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7875 | #endif |
7876 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7877 | |
Guido van Rossum | ad0ee83 | 1995-03-01 10:34:45 +0000 | [diff] [blame] | 7878 | #ifdef HAVE_WAIT |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7879 | /*[clinic input] |
7880 | os.wait | ||||
7881 | |||||
7882 | Wait for completion of a child process. | ||||
7883 | |||||
7884 | Returns a tuple of information about the child process: | ||||
7885 | (pid, status) | ||||
7886 | [clinic start generated code]*/ | ||||
7887 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7888 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7889 | os_wait_impl(PyObject *module) |
7890 | /*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/ | ||||
Guido van Rossum | 21803b8 | 1992-08-09 12:55:27 +0000 | [diff] [blame] | 7891 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7892 | pid_t pid; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7893 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7894 | WAIT_TYPE status; |
7895 | WAIT_STATUS_INT(status) = 0; | ||||
Neal Norwitz | e241ce8 | 2003-02-17 18:17:05 +0000 | [diff] [blame] | 7896 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 7897 | do { |
7898 | Py_BEGIN_ALLOW_THREADS | ||||
7899 | pid = wait(&status); | ||||
7900 | Py_END_ALLOW_THREADS | ||||
7901 | } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
7902 | if (pid < 0) | ||||
7903 | return (!async_err) ? posix_error() : NULL; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7904 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 7905 | return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status)); |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7906 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7907 | #endif /* HAVE_WAIT */ |
Guido van Rossum | 85e3b01 | 1991-06-03 12:42:10 +0000 | [diff] [blame] | 7908 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7909 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7910 | #if defined(HAVE_READLINK) || defined(MS_WINDOWS) |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7911 | /*[clinic input] |
7912 | os.readlink | ||||
7913 | |||||
7914 | path: path_t | ||||
7915 | * | ||||
7916 | dir_fd: dir_fd(requires='readlinkat') = None | ||||
7917 | |||||
7918 | Return a string representing the path to which the symbolic link points. | ||||
7919 | |||||
7920 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
7921 | and path should be relative; path will then be relative to that directory. | ||||
7922 | |||||
7923 | dir_fd may not be implemented on your platform. If it is unavailable, | ||||
7924 | using it will raise a NotImplementedError. | ||||
7925 | [clinic start generated code]*/ | ||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 7926 | |
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 7927 | static PyObject * |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7928 | os_readlink_impl(PyObject *module, path_t *path, int dir_fd) |
7929 | /*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/ | ||||
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 7930 | { |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7931 | #if defined(HAVE_READLINK) |
Christian Heimes | 3cb091e | 2016-09-23 20:24:28 +0200 | [diff] [blame] | 7932 | char buffer[MAXPATHLEN+1]; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 7933 | ssize_t length; |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7934 | |
7935 | Py_BEGIN_ALLOW_THREADS | ||||
7936 | #ifdef HAVE_READLINKAT | ||||
7937 | if (dir_fd != DEFAULT_DIR_FD) | ||||
7938 | length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN); | ||||
7939 | else | ||||
7940 | #endif | ||||
7941 | length = readlink(path->narrow, buffer, MAXPATHLEN); | ||||
7942 | Py_END_ALLOW_THREADS | ||||
7943 | |||||
7944 | if (length < 0) { | ||||
7945 | return path_error(path); | ||||
7946 | } | ||||
7947 | buffer[length] = '\0'; | ||||
7948 | |||||
7949 | if (PyUnicode_Check(path->object)) | ||||
7950 | return PyUnicode_DecodeFSDefaultAndSize(buffer, length); | ||||
7951 | else | ||||
7952 | return PyBytes_FromStringAndSize(buffer, length); | ||||
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7953 | #elif defined(MS_WINDOWS) |
7954 | DWORD n_bytes_returned; | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 7955 | DWORD io_result = 0; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7956 | HANDLE reparse_point_handle; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7957 | char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE]; |
7958 | _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer; | ||||
Miss Islington (bot) | 54dac6c | 2019-09-03 13:13:41 -0700 | [diff] [blame] | 7959 | PyObject *result = NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7960 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7961 | /* First get a handle to the reparse point */ |
7962 | Py_BEGIN_ALLOW_THREADS | ||||
7963 | reparse_point_handle = CreateFileW( | ||||
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7964 | path->wide, |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7965 | 0, |
7966 | 0, | ||||
7967 | 0, | ||||
7968 | OPEN_EXISTING, | ||||
7969 | FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, | ||||
7970 | 0); | ||||
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 7971 | if (reparse_point_handle != INVALID_HANDLE_VALUE) { |
7972 | /* New call DeviceIoControl to read the reparse point */ | ||||
7973 | io_result = DeviceIoControl( | ||||
7974 | reparse_point_handle, | ||||
7975 | FSCTL_GET_REPARSE_POINT, | ||||
7976 | 0, 0, /* in buffer */ | ||||
7977 | target_buffer, sizeof(target_buffer), | ||||
7978 | &n_bytes_returned, | ||||
7979 | 0 /* we're not using OVERLAPPED_IO */ | ||||
7980 | ); | ||||
7981 | CloseHandle(reparse_point_handle); | ||||
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7982 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7983 | Py_END_ALLOW_THREADS |
7984 | |||||
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7985 | if (io_result == 0) { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 7986 | return path_error(path); |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 7987 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7988 | |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 7989 | wchar_t *name = NULL; |
7990 | Py_ssize_t nameLen = 0; | ||||
7991 | if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7992 | { |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 7993 | name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer + |
7994 | rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset); | ||||
7995 | nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 7996 | } |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 7997 | else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) |
7998 | { | ||||
7999 | name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer + | ||||
8000 | rdb->MountPointReparseBuffer.SubstituteNameOffset); | ||||
8001 | nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t); | ||||
8002 | } | ||||
8003 | else | ||||
8004 | { | ||||
8005 | PyErr_SetString(PyExc_ValueError, "not a symbolic link"); | ||||
8006 | } | ||||
8007 | if (name) { | ||||
8008 | if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) { | ||||
8009 | /* Our buffer is mutable, so this is okay */ | ||||
8010 | name[1] = L'\\'; | ||||
8011 | } | ||||
8012 | result = PyUnicode_FromWideChar(name, nameLen); | ||||
Miss Islington (bot) | 54dac6c | 2019-09-03 13:13:41 -0700 | [diff] [blame] | 8013 | if (result && path->narrow) { |
Steve Dower | 9eb3d54 | 2019-08-21 15:52:42 -0700 | [diff] [blame] | 8014 | Py_SETREF(result, PyUnicode_EncodeFSDefault(result)); |
8015 | } | ||||
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8016 | } |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 8017 | return result; |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8018 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8019 | } |
Berker Peksag | e0b5b20 | 2018-08-15 13:03:41 +0300 | [diff] [blame] | 8020 | #endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8021 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8022 | #ifdef HAVE_SYMLINK |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8023 | |
8024 | #if defined(MS_WINDOWS) | ||||
8025 | |||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8026 | /* Remove the last portion of the path - return 0 on success */ |
8027 | static int | ||||
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8028 | _dirnameW(WCHAR *path) |
8029 | { | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8030 | WCHAR *ptr; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8031 | size_t length = wcsnlen_s(path, MAX_PATH); |
8032 | if (length == MAX_PATH) { | ||||
8033 | return -1; | ||||
8034 | } | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8035 | |
8036 | /* walk the path from the end until a backslash is encountered */ | ||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8037 | for(ptr = path + length; ptr != path; ptr--) { |
8038 | if (*ptr == L'\\' || *ptr == L'/') { | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8039 | break; |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8040 | } |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8041 | } |
8042 | *ptr = 0; | ||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8043 | return 0; |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8044 | } |
8045 | |||||
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8046 | /* Is this path absolute? */ |
8047 | static int | ||||
8048 | _is_absW(const WCHAR *path) | ||||
8049 | { | ||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8050 | return path[0] == L'\\' || path[0] == L'/' || |
8051 | (path[0] && path[1] == L':'); | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8052 | } |
8053 | |||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8054 | /* join root and rest with a backslash - return 0 on success */ |
8055 | static int | ||||
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8056 | _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) |
8057 | { | ||||
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8058 | if (_is_absW(rest)) { |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8059 | return wcscpy_s(dest_path, MAX_PATH, rest); |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8060 | } |
8061 | |||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8062 | if (wcscpy_s(dest_path, MAX_PATH, root)) { |
8063 | return -1; | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8064 | } |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8065 | |
8066 | if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) { | ||||
8067 | return -1; | ||||
8068 | } | ||||
8069 | |||||
8070 | return wcscat_s(dest_path, MAX_PATH, rest); | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8071 | } |
8072 | |||||
Victor Stinner | 31b3b92 | 2013-06-05 01:49:17 +0200 | [diff] [blame] | 8073 | /* Return True if the path at src relative to dest is a directory */ |
8074 | static int | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 8075 | _check_dirW(LPCWSTR src, LPCWSTR dest) |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8076 | { |
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8077 | WIN32_FILE_ATTRIBUTE_DATA src_info; |
8078 | WCHAR dest_parent[MAX_PATH]; | ||||
8079 | WCHAR src_resolved[MAX_PATH] = L""; | ||||
8080 | |||||
8081 | /* dest_parent = os.path.dirname(dest) */ | ||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8082 | if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
8083 | _dirnameW(dest_parent)) { | ||||
8084 | return 0; | ||||
8085 | } | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8086 | /* src_resolved = os.path.join(dest_parent, src) */ |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8087 | if (_joinW(src_resolved, dest_parent, src)) { |
8088 | return 0; | ||||
8089 | } | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8090 | return ( |
8091 | GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info) | ||||
8092 | && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY | ||||
8093 | ); | ||||
8094 | } | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8095 | #endif |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8096 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8097 | |
8098 | /*[clinic input] | ||||
8099 | os.symlink | ||||
8100 | src: path_t | ||||
8101 | dst: path_t | ||||
8102 | target_is_directory: bool = False | ||||
8103 | * | ||||
8104 | dir_fd: dir_fd(requires='symlinkat')=None | ||||
8105 | |||||
8106 | # "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\ | ||||
8107 | |||||
8108 | Create a symbolic link pointing to src named dst. | ||||
8109 | |||||
8110 | target_is_directory is required on Windows if the target is to be | ||||
8111 | interpreted as a directory. (On Windows, symlink requires | ||||
8112 | Windows 6.0 or greater, and raises a NotImplementedError otherwise.) | ||||
8113 | target_is_directory is ignored on non-Windows platforms. | ||||
8114 | |||||
8115 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
8116 | and path should be relative; path will then be relative to that directory. | ||||
8117 | dir_fd may not be implemented on your platform. | ||||
8118 | If it is unavailable, using it will raise a NotImplementedError. | ||||
8119 | |||||
8120 | [clinic start generated code]*/ | ||||
8121 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8122 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8123 | os_symlink_impl(PyObject *module, path_t *src, path_t *dst, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 8124 | int target_is_directory, int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8125 | /*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8126 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8127 | #ifdef MS_WINDOWS |
8128 | DWORD result; | ||||
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8129 | DWORD flags = 0; |
8130 | |||||
8131 | /* Assumed true, set to false if detected to not be available. */ | ||||
8132 | static int windows_has_symlink_unprivileged_flag = TRUE; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8133 | #else |
8134 | int result; | ||||
8135 | #endif | ||||
8136 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 8137 | if (PySys_Audit("os.symlink", "OOi", src->object, dst->object, |
8138 | dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) { | ||||
8139 | return NULL; | ||||
8140 | } | ||||
8141 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8142 | #ifdef MS_WINDOWS |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8143 | |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8144 | if (windows_has_symlink_unprivileged_flag) { |
8145 | /* Allow non-admin symlinks if system allows it. */ | ||||
8146 | flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; | ||||
8147 | } | ||||
Jason R. Coombs | 3a09286 | 2013-05-27 23:21:28 -0400 | [diff] [blame] | 8148 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8149 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8150 | _Py_BEGIN_SUPPRESS_IPH |
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8151 | /* if src is a directory, ensure flags==1 (target_is_directory bit) */ |
8152 | if (target_is_directory || _check_dirW(src->wide, dst->wide)) { | ||||
8153 | flags |= SYMBOLIC_LINK_FLAG_DIRECTORY; | ||||
8154 | } | ||||
8155 | |||||
8156 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); | ||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8157 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8158 | Py_END_ALLOW_THREADS |
8159 | |||||
Vidar Tonaas Fauske | 0e10766 | 2019-04-09 20:19:46 +0200 | [diff] [blame] | 8160 | if (windows_has_symlink_unprivileged_flag && !result && |
8161 | ERROR_INVALID_PARAMETER == GetLastError()) { | ||||
8162 | |||||
8163 | Py_BEGIN_ALLOW_THREADS | ||||
8164 | _Py_BEGIN_SUPPRESS_IPH | ||||
8165 | /* This error might be caused by | ||||
8166 | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported. | ||||
8167 | Try again, and update windows_has_symlink_unprivileged_flag if we | ||||
8168 | are successful this time. | ||||
8169 | |||||
8170 | NOTE: There is a risk of a race condition here if there are other | ||||
8171 | conditions than the flag causing ERROR_INVALID_PARAMETER, and | ||||
8172 | another process (or thread) changes that condition in between our | ||||
8173 | calls to CreateSymbolicLink. | ||||
8174 | */ | ||||
8175 | flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE); | ||||
8176 | result = CreateSymbolicLinkW(dst->wide, src->wide, flags); | ||||
8177 | _Py_END_SUPPRESS_IPH | ||||
8178 | Py_END_ALLOW_THREADS | ||||
8179 | |||||
8180 | if (result || ERROR_INVALID_PARAMETER != GetLastError()) { | ||||
8181 | windows_has_symlink_unprivileged_flag = FALSE; | ||||
8182 | } | ||||
8183 | } | ||||
8184 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8185 | if (!result) |
8186 | return path_error2(src, dst); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8187 | |
8188 | #else | ||||
8189 | |||||
Steve Dower | 6921e73 | 2018-03-05 14:26:08 -0800 | [diff] [blame] | 8190 | if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) { |
8191 | PyErr_SetString(PyExc_ValueError, | ||||
8192 | "symlink: src and dst must be the same type"); | ||||
8193 | return NULL; | ||||
8194 | } | ||||
8195 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8196 | Py_BEGIN_ALLOW_THREADS |
8197 | #if HAVE_SYMLINKAT | ||||
8198 | if (dir_fd != DEFAULT_DIR_FD) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8199 | result = symlinkat(src->narrow, dir_fd, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8200 | else |
8201 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8202 | result = symlink(src->narrow, dst->narrow); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8203 | Py_END_ALLOW_THREADS |
8204 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8205 | if (result) |
8206 | return path_error2(src, dst); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8207 | #endif |
8208 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8209 | Py_RETURN_NONE; |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8210 | } |
8211 | #endif /* HAVE_SYMLINK */ | ||||
8212 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8213 | |
Brian Curtin | d40e6f7 | 2010-07-08 21:39:08 +0000 | [diff] [blame] | 8214 | |
Guido van Rossum | bfaf3d6 | 1997-12-29 20:02:27 +0000 | [diff] [blame] | 8215 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8216 | static PyStructSequence_Field times_result_fields[] = { |
8217 | {"user", "user time"}, | ||||
8218 | {"system", "system time"}, | ||||
8219 | {"children_user", "user time of children"}, | ||||
8220 | {"children_system", "system time of children"}, | ||||
8221 | {"elapsed", "elapsed time since an arbitrary point in the past"}, | ||||
8222 | {NULL} | ||||
8223 | }; | ||||
8224 | |||||
8225 | PyDoc_STRVAR(times_result__doc__, | ||||
8226 | "times_result: Result from os.times().\n\n\ | ||||
8227 | This object may be accessed either as a tuple of\n\ | ||||
8228 | (user, system, children_user, children_system, elapsed),\n\ | ||||
8229 | or via the attributes user, system, children_user, children_system,\n\ | ||||
8230 | and elapsed.\n\ | ||||
8231 | \n\ | ||||
8232 | See os.times for more information."); | ||||
8233 | |||||
8234 | static PyStructSequence_Desc times_result_desc = { | ||||
8235 | "times_result", /* name */ | ||||
8236 | times_result__doc__, /* doc */ | ||||
8237 | times_result_fields, | ||||
8238 | 5 | ||||
8239 | }; | ||||
8240 | |||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8241 | static PyTypeObject* TimesResultType; |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8242 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8243 | #ifdef MS_WINDOWS |
8244 | #define HAVE_TIMES /* mandatory, for the method table */ | ||||
8245 | #endif | ||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8246 | |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8247 | #ifdef HAVE_TIMES |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8248 | |
8249 | static PyObject * | ||||
8250 | build_times_result(double user, double system, | ||||
8251 | double children_user, double children_system, | ||||
8252 | double elapsed) | ||||
8253 | { | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 8254 | PyObject *value = PyStructSequence_New(TimesResultType); |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8255 | if (value == NULL) |
8256 | return NULL; | ||||
8257 | |||||
8258 | #define SET(i, field) \ | ||||
8259 | { \ | ||||
8260 | PyObject *o = PyFloat_FromDouble(field); \ | ||||
8261 | if (!o) { \ | ||||
8262 | Py_DECREF(value); \ | ||||
8263 | return NULL; \ | ||||
8264 | } \ | ||||
8265 | PyStructSequence_SET_ITEM(value, i, o); \ | ||||
8266 | } \ | ||||
8267 | |||||
8268 | SET(0, user); | ||||
8269 | SET(1, system); | ||||
8270 | SET(2, children_user); | ||||
8271 | SET(3, children_system); | ||||
8272 | SET(4, elapsed); | ||||
8273 | |||||
8274 | #undef SET | ||||
8275 | |||||
8276 | return value; | ||||
8277 | } | ||||
8278 | |||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8279 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8280 | #ifndef MS_WINDOWS |
8281 | #define NEED_TICKS_PER_SECOND | ||||
8282 | static long ticks_per_second = -1; | ||||
8283 | #endif /* MS_WINDOWS */ | ||||
8284 | |||||
8285 | /*[clinic input] | ||||
8286 | os.times | ||||
8287 | |||||
8288 | Return a collection containing process timing information. | ||||
8289 | |||||
8290 | The object returned behaves like a named tuple with these fields: | ||||
8291 | (utime, stime, cutime, cstime, elapsed_time) | ||||
8292 | All fields are floating point numbers. | ||||
8293 | [clinic start generated code]*/ | ||||
8294 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8295 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8296 | os_times_impl(PyObject *module) |
8297 | /*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8298 | #ifdef MS_WINDOWS |
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8299 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8300 | FILETIME create, exit, kernel, user; |
8301 | HANDLE hProc; | ||||
8302 | hProc = GetCurrentProcess(); | ||||
8303 | GetProcessTimes(hProc, &create, &exit, &kernel, &user); | ||||
8304 | /* The fields of a FILETIME structure are the hi and lo part | ||||
8305 | of a 64-bit value expressed in 100 nanosecond units. | ||||
8306 | 1e7 is one second in such units; 1e-7 the inverse. | ||||
8307 | 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7. | ||||
8308 | */ | ||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 8309 | return build_times_result( |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8310 | (double)(user.dwHighDateTime*429.4967296 + |
8311 | user.dwLowDateTime*1e-7), | ||||
8312 | (double)(kernel.dwHighDateTime*429.4967296 + | ||||
8313 | kernel.dwLowDateTime*1e-7), | ||||
8314 | (double)0, | ||||
8315 | (double)0, | ||||
8316 | (double)0); | ||||
Guido van Rossum | 14ed0b2 | 1994-09-29 09:50:09 +0000 | [diff] [blame] | 8317 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8318 | #else /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8319 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8320 | |
8321 | |||||
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8322 | struct tms t; |
8323 | clock_t c; | ||||
8324 | errno = 0; | ||||
8325 | c = times(&t); | ||||
8326 | if (c == (clock_t) -1) | ||||
8327 | return posix_error(); | ||||
8328 | return build_times_result( | ||||
8329 | (double)t.tms_utime / ticks_per_second, | ||||
8330 | (double)t.tms_stime / ticks_per_second, | ||||
8331 | (double)t.tms_cutime / ticks_per_second, | ||||
8332 | (double)t.tms_cstime / ticks_per_second, | ||||
8333 | (double)c / ticks_per_second); | ||||
8334 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8335 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | f3923e9 | 2012-07-24 21:23:53 +0200 | [diff] [blame] | 8336 | #endif /* HAVE_TIMES */ |
8337 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8338 | |
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8339 | #ifdef HAVE_GETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8340 | /*[clinic input] |
8341 | os.getsid | ||||
8342 | |||||
8343 | pid: pid_t | ||||
8344 | / | ||||
8345 | |||||
8346 | Call the system call getsid(pid) and return the result. | ||||
8347 | [clinic start generated code]*/ | ||||
8348 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8349 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8350 | os_getsid_impl(PyObject *module, pid_t pid) |
8351 | /*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8352 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8353 | int sid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8354 | sid = getsid(pid); |
8355 | if (sid < 0) | ||||
8356 | return posix_error(); | ||||
8357 | return PyLong_FromLong((long)sid); | ||||
Martin v. Löwis | 49ee14d | 2003-11-10 06:35:36 +0000 | [diff] [blame] | 8358 | } |
8359 | #endif /* HAVE_GETSID */ | ||||
8360 | |||||
8361 | |||||
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8362 | #ifdef HAVE_SETSID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8363 | /*[clinic input] |
8364 | os.setsid | ||||
8365 | |||||
8366 | Call the system call setsid(). | ||||
8367 | [clinic start generated code]*/ | ||||
8368 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8369 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8370 | os_setsid_impl(PyObject *module) |
8371 | /*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/ | ||||
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8372 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8373 | if (setsid() < 0) |
8374 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8375 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8376 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8377 | #endif /* HAVE_SETSID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8378 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8379 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8380 | #ifdef HAVE_SETPGID |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8381 | /*[clinic input] |
8382 | os.setpgid | ||||
8383 | |||||
8384 | pid: pid_t | ||||
8385 | pgrp: pid_t | ||||
8386 | / | ||||
8387 | |||||
8388 | Call the system call setpgid(pid, pgrp). | ||||
8389 | [clinic start generated code]*/ | ||||
8390 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8391 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8392 | os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp) |
8393 | /*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8394 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8395 | if (setpgid(pid, pgrp) < 0) |
8396 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8397 | Py_RETURN_NONE; |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8398 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8399 | #endif /* HAVE_SETPGID */ |
Guido van Rossum | c2670a0 | 1992-09-13 20:07:29 +0000 | [diff] [blame] | 8400 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8401 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8402 | #ifdef HAVE_TCGETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8403 | /*[clinic input] |
8404 | os.tcgetpgrp | ||||
8405 | |||||
8406 | fd: int | ||||
8407 | / | ||||
8408 | |||||
8409 | Return the process group associated with the terminal specified by fd. | ||||
8410 | [clinic start generated code]*/ | ||||
8411 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8412 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8413 | os_tcgetpgrp_impl(PyObject *module, int fd) |
8414 | /*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8415 | { |
8416 | pid_t pgid = tcgetpgrp(fd); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8417 | if (pgid < 0) |
8418 | return posix_error(); | ||||
8419 | return PyLong_FromPid(pgid); | ||||
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8420 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8421 | #endif /* HAVE_TCGETPGRP */ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8422 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8423 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8424 | #ifdef HAVE_TCSETPGRP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8425 | /*[clinic input] |
8426 | os.tcsetpgrp | ||||
8427 | |||||
8428 | fd: int | ||||
8429 | pgid: pid_t | ||||
8430 | / | ||||
8431 | |||||
8432 | Set the process group associated with the terminal specified by fd. | ||||
8433 | [clinic start generated code]*/ | ||||
8434 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8435 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8436 | os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid) |
8437 | /*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8438 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8439 | if (tcsetpgrp(fd, pgid) < 0) |
8440 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8441 | Py_RETURN_NONE; |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 8442 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8443 | #endif /* HAVE_TCSETPGRP */ |
Guido van Rossum | 22db57e | 1992-04-05 14:25:30 +0000 | [diff] [blame] | 8444 | |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8445 | /* Functions acting on file descriptors */ |
8446 | |||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8447 | #ifdef O_CLOEXEC |
8448 | extern int _Py_open_cloexec_works; | ||||
8449 | #endif | ||||
8450 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8451 | |
8452 | /*[clinic input] | ||||
8453 | os.open -> int | ||||
8454 | path: path_t | ||||
8455 | flags: int | ||||
8456 | mode: int = 0o777 | ||||
8457 | * | ||||
8458 | dir_fd: dir_fd(requires='openat') = None | ||||
8459 | |||||
8460 | # "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\ | ||||
8461 | |||||
8462 | Open a file for low level IO. Returns a file descriptor (integer). | ||||
8463 | |||||
8464 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
8465 | and path should be relative; path will then be relative to that directory. | ||||
8466 | dir_fd may not be implemented on your platform. | ||||
8467 | If it is unavailable, using it will raise a NotImplementedError. | ||||
8468 | [clinic start generated code]*/ | ||||
8469 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8470 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8471 | os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd) |
8472 | /*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8473 | { |
8474 | int fd; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8475 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8476 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8477 | #ifdef O_CLOEXEC |
8478 | int *atomic_flag_works = &_Py_open_cloexec_works; | ||||
8479 | #elif !defined(MS_WINDOWS) | ||||
8480 | int *atomic_flag_works = NULL; | ||||
8481 | #endif | ||||
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 8482 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8483 | #ifdef MS_WINDOWS |
8484 | flags |= O_NOINHERIT; | ||||
8485 | #elif defined(O_CLOEXEC) | ||||
8486 | flags |= O_CLOEXEC; | ||||
8487 | #endif | ||||
8488 | |||||
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 8489 | if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) { |
8490 | return -1; | ||||
8491 | } | ||||
8492 | |||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8493 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8494 | do { |
8495 | Py_BEGIN_ALLOW_THREADS | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8496 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 8497 | fd = _wopen(path->wide, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8498 | #else |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8499 | #ifdef HAVE_OPENAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8500 | if (dir_fd != DEFAULT_DIR_FD) |
8501 | fd = openat(dir_fd, path->narrow, flags, mode); | ||||
8502 | else | ||||
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8503 | #endif /* HAVE_OPENAT */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8504 | fd = open(path->narrow, flags, mode); |
Steve Dower | 6230aaf | 2016-09-09 09:03:15 -0700 | [diff] [blame] | 8505 | #endif /* !MS_WINDOWS */ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8506 | Py_END_ALLOW_THREADS |
8507 | } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8508 | _Py_END_SUPPRESS_IPH |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8509 | |
Victor Stinner | d3ffd32 | 2015-09-15 10:11:03 +0200 | [diff] [blame] | 8510 | if (fd < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8511 | if (!async_err) |
8512 | PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8513 | return -1; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 8514 | } |
8515 | |||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8516 | #ifndef MS_WINDOWS |
8517 | if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) { | ||||
8518 | close(fd); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8519 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8520 | } |
8521 | #endif | ||||
8522 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8523 | return fd; |
8524 | } | ||||
8525 | |||||
8526 | |||||
8527 | /*[clinic input] | ||||
8528 | os.close | ||||
8529 | |||||
8530 | fd: int | ||||
8531 | |||||
8532 | Close a file descriptor. | ||||
8533 | [clinic start generated code]*/ | ||||
8534 | |||||
Barry Warsaw | 53699e9 | 1996-12-10 23:23:01 +0000 | [diff] [blame] | 8535 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8536 | os_close_impl(PyObject *module, int fd) |
8537 | /*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/ | ||||
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8538 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8539 | int res; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8540 | /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/ |
8541 | * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html | ||||
8542 | * for more details. | ||||
8543 | */ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8544 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8545 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8546 | res = close(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8547 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8548 | Py_END_ALLOW_THREADS |
8549 | if (res < 0) | ||||
8550 | return posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8551 | Py_RETURN_NONE; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8552 | } |
8553 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8554 | |
Miss Islington (bot) | 84eb42e | 2019-09-12 04:19:21 -0700 | [diff] [blame] | 8555 | #ifdef HAVE_FDWALK |
8556 | static int | ||||
8557 | _fdwalk_close_func(void *lohi, int fd) | ||||
8558 | { | ||||
8559 | int lo = ((int *)lohi)[0]; | ||||
8560 | int hi = ((int *)lohi)[1]; | ||||
8561 | |||||
8562 | if (fd >= hi) | ||||
8563 | return 1; | ||||
8564 | else if (fd >= lo) | ||||
8565 | close(fd); | ||||
8566 | return 0; | ||||
8567 | } | ||||
8568 | #endif /* HAVE_FDWALK */ | ||||
8569 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8570 | /*[clinic input] |
8571 | os.closerange | ||||
8572 | |||||
8573 | fd_low: int | ||||
8574 | fd_high: int | ||||
8575 | / | ||||
8576 | |||||
8577 | Closes all file descriptors in [fd_low, fd_high), ignoring errors. | ||||
8578 | [clinic start generated code]*/ | ||||
8579 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8580 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8581 | os_closerange_impl(PyObject *module, int fd_low, int fd_high) |
8582 | /*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8583 | { |
Miss Islington (bot) | 84eb42e | 2019-09-12 04:19:21 -0700 | [diff] [blame] | 8584 | #ifdef HAVE_FDWALK |
8585 | int lohi[2]; | ||||
8586 | #else | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8587 | int i; |
Miss Islington (bot) | 84eb42e | 2019-09-12 04:19:21 -0700 | [diff] [blame] | 8588 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8589 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8590 | _Py_BEGIN_SUPPRESS_IPH |
Miss Islington (bot) | 84eb42e | 2019-09-12 04:19:21 -0700 | [diff] [blame] | 8591 | #ifdef HAVE_FDWALK |
8592 | lohi[0] = Py_MAX(fd_low, 0); | ||||
8593 | lohi[1] = fd_high; | ||||
8594 | fdwalk(_fdwalk_close_func, lohi); | ||||
8595 | #else | ||||
Benjamin Peterson | 207116b | 2016-09-08 11:28:06 -0700 | [diff] [blame] | 8596 | for (i = Py_MAX(fd_low, 0); i < fd_high; i++) |
Steve Dower | 940f33a | 2016-09-08 11:21:54 -0700 | [diff] [blame] | 8597 | close(i); |
Miss Islington (bot) | 84eb42e | 2019-09-12 04:19:21 -0700 | [diff] [blame] | 8598 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8599 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8600 | Py_END_ALLOW_THREADS |
8601 | Py_RETURN_NONE; | ||||
Christian Heimes | fdab48e | 2008-01-20 09:06:41 +0000 | [diff] [blame] | 8602 | } |
8603 | |||||
8604 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8605 | /*[clinic input] |
8606 | os.dup -> int | ||||
8607 | |||||
8608 | fd: int | ||||
8609 | / | ||||
8610 | |||||
8611 | Return a duplicate of a file descriptor. | ||||
8612 | [clinic start generated code]*/ | ||||
8613 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8614 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8615 | os_dup_impl(PyObject *module, int fd) |
8616 | /*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8617 | { |
8618 | return _Py_dup(fd); | ||||
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8619 | } |
8620 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8621 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8622 | /*[clinic input] |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8623 | os.dup2 -> int |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8624 | fd: int |
8625 | fd2: int | ||||
8626 | inheritable: bool=True | ||||
8627 | |||||
8628 | Duplicate file descriptor. | ||||
8629 | [clinic start generated code]*/ | ||||
8630 | |||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8631 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8632 | os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable) |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8633 | /*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8634 | { |
Stéphane Wirtel | 3d86e48 | 2018-01-30 07:04:36 +0100 | [diff] [blame] | 8635 | int res = 0; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8636 | #if defined(HAVE_DUP3) && \ |
8637 | !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)) | ||||
8638 | /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */ | ||||
Alexey Izbyshev | b3caf38 | 2018-02-20 10:25:46 +0300 | [diff] [blame] | 8639 | static int dup3_works = -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8640 | #endif |
8641 | |||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8642 | if (fd < 0 || fd2 < 0) { |
8643 | posix_error(); | ||||
8644 | return -1; | ||||
8645 | } | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8646 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8647 | /* dup2() can fail with EINTR if the target FD is already open, because it |
8648 | * then has to be closed. See os_close_impl() for why we don't handle EINTR | ||||
8649 | * upon close(), and therefore below. | ||||
8650 | */ | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8651 | #ifdef MS_WINDOWS |
8652 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8653 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8654 | res = dup2(fd, fd2); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8655 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8656 | Py_END_ALLOW_THREADS |
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8657 | if (res < 0) { |
8658 | posix_error(); | ||||
8659 | return -1; | ||||
8660 | } | ||||
8661 | res = fd2; // msvcrt dup2 returns 0 on success. | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8662 | |
8663 | /* Character files like console cannot be make non-inheritable */ | ||||
8664 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { | ||||
8665 | close(fd2); | ||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8666 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8667 | } |
8668 | |||||
8669 | #elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC) | ||||
8670 | Py_BEGIN_ALLOW_THREADS | ||||
8671 | if (!inheritable) | ||||
8672 | res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2); | ||||
8673 | else | ||||
8674 | res = dup2(fd, fd2); | ||||
8675 | Py_END_ALLOW_THREADS | ||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8676 | if (res < 0) { |
8677 | posix_error(); | ||||
8678 | return -1; | ||||
8679 | } | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8680 | |
8681 | #else | ||||
8682 | |||||
8683 | #ifdef HAVE_DUP3 | ||||
8684 | if (!inheritable && dup3_works != 0) { | ||||
8685 | Py_BEGIN_ALLOW_THREADS | ||||
8686 | res = dup3(fd, fd2, O_CLOEXEC); | ||||
8687 | Py_END_ALLOW_THREADS | ||||
8688 | if (res < 0) { | ||||
8689 | if (dup3_works == -1) | ||||
8690 | dup3_works = (errno != ENOSYS); | ||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8691 | if (dup3_works) { |
8692 | posix_error(); | ||||
8693 | return -1; | ||||
8694 | } | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8695 | } |
8696 | } | ||||
8697 | |||||
8698 | if (inheritable || dup3_works == 0) | ||||
8699 | { | ||||
8700 | #endif | ||||
8701 | Py_BEGIN_ALLOW_THREADS | ||||
8702 | res = dup2(fd, fd2); | ||||
8703 | Py_END_ALLOW_THREADS | ||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8704 | if (res < 0) { |
8705 | posix_error(); | ||||
8706 | return -1; | ||||
8707 | } | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8708 | |
8709 | if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) { | ||||
8710 | close(fd2); | ||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8711 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 8712 | } |
8713 | #ifdef HAVE_DUP3 | ||||
8714 | } | ||||
8715 | #endif | ||||
8716 | |||||
8717 | #endif | ||||
8718 | |||||
Benjamin Peterson | bbdb17d | 2017-12-29 13:13:06 -0800 | [diff] [blame] | 8719 | return res; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8720 | } |
8721 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8722 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8723 | #ifdef HAVE_LOCKF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8724 | /*[clinic input] |
8725 | os.lockf | ||||
8726 | |||||
8727 | fd: int | ||||
8728 | An open file descriptor. | ||||
8729 | command: int | ||||
8730 | One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST. | ||||
8731 | length: Py_off_t | ||||
8732 | The number of bytes to lock, starting at the current position. | ||||
8733 | / | ||||
8734 | |||||
8735 | Apply, test or remove a POSIX lock on an open file descriptor. | ||||
8736 | |||||
8737 | [clinic start generated code]*/ | ||||
8738 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8739 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8740 | os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length) |
8741 | /*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8742 | { |
8743 | int res; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8744 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 8745 | if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) { |
8746 | return NULL; | ||||
8747 | } | ||||
8748 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8749 | Py_BEGIN_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8750 | res = lockf(fd, command, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8751 | Py_END_ALLOW_THREADS |
8752 | |||||
8753 | if (res < 0) | ||||
8754 | return posix_error(); | ||||
8755 | |||||
8756 | Py_RETURN_NONE; | ||||
8757 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8758 | #endif /* HAVE_LOCKF */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8759 | |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8760 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8761 | /*[clinic input] |
8762 | os.lseek -> Py_off_t | ||||
8763 | |||||
8764 | fd: int | ||||
8765 | position: Py_off_t | ||||
8766 | how: int | ||||
8767 | / | ||||
8768 | |||||
8769 | Set the position of a file descriptor. Return the new position. | ||||
8770 | |||||
8771 | Return the new cursor position in number of bytes | ||||
8772 | relative to the beginning of the file. | ||||
8773 | [clinic start generated code]*/ | ||||
8774 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8775 | static Py_off_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8776 | os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how) |
8777 | /*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8778 | { |
8779 | Py_off_t result; | ||||
8780 | |||||
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8781 | #ifdef SEEK_SET |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8782 | /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
8783 | switch (how) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8784 | case 0: how = SEEK_SET; break; |
8785 | case 1: how = SEEK_CUR; break; | ||||
8786 | case 2: how = SEEK_END; break; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8787 | } |
Guido van Rossum | 6a3eb5f | 1994-08-18 15:42:46 +0000 | [diff] [blame] | 8788 | #endif /* SEEK_END */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8789 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8790 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8791 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 14b9b11 | 2013-06-25 00:37:25 +0200 | [diff] [blame] | 8792 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8793 | result = _lseeki64(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8794 | #else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8795 | result = lseek(fd, position, how); |
Fred Drake | 699f352 | 2000-06-29 21:12:41 +0000 | [diff] [blame] | 8796 | #endif |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8797 | _Py_END_SUPPRESS_IPH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8798 | Py_END_ALLOW_THREADS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8799 | if (result < 0) |
8800 | posix_error(); | ||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 8801 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8802 | return result; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8803 | } |
8804 | |||||
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 8805 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8806 | /*[clinic input] |
8807 | os.read | ||||
8808 | fd: int | ||||
8809 | length: Py_ssize_t | ||||
8810 | / | ||||
8811 | |||||
8812 | Read from a file descriptor. Returns a bytes object. | ||||
8813 | [clinic start generated code]*/ | ||||
8814 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8815 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8816 | os_read_impl(PyObject *module, int fd, Py_ssize_t length) |
8817 | /*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8818 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8819 | Py_ssize_t n; |
8820 | PyObject *buffer; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8821 | |
8822 | if (length < 0) { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8823 | errno = EINVAL; |
8824 | return posix_error(); | ||||
8825 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8826 | |
Victor Stinner | 9a0d7a7 | 2018-11-22 15:03:40 +0100 | [diff] [blame] | 8827 | length = Py_MIN(length, _PY_READ_MAX); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8828 | |
8829 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8830 | if (buffer == NULL) |
8831 | return NULL; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8832 | |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8833 | n = _Py_read(fd, PyBytes_AS_STRING(buffer), length); |
8834 | if (n == -1) { | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8835 | Py_DECREF(buffer); |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 8836 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8837 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8838 | |
8839 | if (n != length) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8840 | _PyBytes_Resize(&buffer, n); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8841 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 8842 | return buffer; |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 8843 | } |
8844 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8845 | #if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8846 | || defined(__APPLE__))) \ |
8847 | || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \ | ||||
8848 | || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) | ||||
8849 | static int | ||||
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8850 | iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type) |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8851 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8852 | Py_ssize_t i, j; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8853 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8854 | *iov = PyMem_New(struct iovec, cnt); |
8855 | if (*iov == NULL) { | ||||
8856 | PyErr_NoMemory(); | ||||
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8857 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8858 | } |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 8859 | |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8860 | *buf = PyMem_New(Py_buffer, cnt); |
8861 | if (*buf == NULL) { | ||||
8862 | PyMem_Del(*iov); | ||||
8863 | PyErr_NoMemory(); | ||||
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8864 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8865 | } |
8866 | |||||
8867 | for (i = 0; i < cnt; i++) { | ||||
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8868 | PyObject *item = PySequence_GetItem(seq, i); |
8869 | if (item == NULL) | ||||
8870 | goto fail; | ||||
8871 | if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) { | ||||
8872 | Py_DECREF(item); | ||||
8873 | goto fail; | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8874 | } |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8875 | Py_DECREF(item); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8876 | (*iov)[i].iov_base = (*buf)[i].buf; |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8877 | (*iov)[i].iov_len = (*buf)[i].len; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8878 | } |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 8879 | return 0; |
Ross Lagerwall | 9ad63e0 | 2011-03-19 09:11:14 +0200 | [diff] [blame] | 8880 | |
8881 | fail: | ||||
8882 | PyMem_Del(*iov); | ||||
8883 | for (j = 0; j < i; j++) { | ||||
8884 | PyBuffer_Release(&(*buf)[j]); | ||||
8885 | } | ||||
8886 | PyMem_Del(*buf); | ||||
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8887 | return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 8888 | } |
8889 | |||||
8890 | static void | ||||
8891 | iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt) | ||||
8892 | { | ||||
8893 | int i; | ||||
8894 | PyMem_Del(iov); | ||||
8895 | for (i = 0; i < cnt; i++) { | ||||
8896 | PyBuffer_Release(&buf[i]); | ||||
8897 | } | ||||
8898 | PyMem_Del(buf); | ||||
8899 | } | ||||
8900 | #endif | ||||
8901 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8902 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8903 | #ifdef HAVE_READV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8904 | /*[clinic input] |
8905 | os.readv -> Py_ssize_t | ||||
8906 | |||||
8907 | fd: int | ||||
8908 | buffers: object | ||||
8909 | / | ||||
8910 | |||||
8911 | Read from a file descriptor fd into an iterable of buffers. | ||||
8912 | |||||
8913 | The buffers should be mutable buffers accepting bytes. | ||||
8914 | readv will transfer data into each buffer until it is full | ||||
8915 | and then move on to the next buffer in the sequence to hold | ||||
8916 | the rest of the data. | ||||
8917 | |||||
8918 | readv returns the total number of bytes read, | ||||
8919 | which may be less than the total capacity of all the buffers. | ||||
8920 | [clinic start generated code]*/ | ||||
8921 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8922 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8923 | os_readv_impl(PyObject *module, int fd, PyObject *buffers) |
8924 | /*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8925 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8926 | Py_ssize_t cnt, n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8927 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8928 | struct iovec *iov; |
8929 | Py_buffer *buf; | ||||
8930 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8931 | if (!PySequence_Check(buffers)) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8932 | PyErr_SetString(PyExc_TypeError, |
8933 | "readv() arg 2 must be a sequence"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8934 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8935 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8936 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8937 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 8938 | if (cnt < 0) |
8939 | return -1; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8940 | |
8941 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) | ||||
8942 | return -1; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8943 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8944 | do { |
8945 | Py_BEGIN_ALLOW_THREADS | ||||
8946 | n = readv(fd, iov, cnt); | ||||
8947 | Py_END_ALLOW_THREADS | ||||
8948 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8949 | |
8950 | iov_cleanup(iov, buf, cnt); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8951 | if (n < 0) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8952 | if (!async_err) |
8953 | posix_error(); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8954 | return -1; |
8955 | } | ||||
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 8956 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8957 | return n; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8958 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8959 | #endif /* HAVE_READV */ |
8960 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8961 | |
8962 | #ifdef HAVE_PREAD | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8963 | /*[clinic input] |
8964 | # TODO length should be size_t! but Python doesn't support parsing size_t yet. | ||||
8965 | os.pread | ||||
8966 | |||||
8967 | fd: int | ||||
8968 | length: int | ||||
8969 | offset: Py_off_t | ||||
8970 | / | ||||
8971 | |||||
8972 | Read a number of bytes from a file descriptor starting at a particular offset. | ||||
8973 | |||||
8974 | Read length bytes from file descriptor fd, starting at offset bytes from | ||||
8975 | the beginning of the file. The file offset remains unchanged. | ||||
8976 | [clinic start generated code]*/ | ||||
8977 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8978 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 8979 | os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset) |
8980 | /*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8981 | { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8982 | Py_ssize_t n; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8983 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8984 | PyObject *buffer; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8985 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8986 | if (length < 0) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8987 | errno = EINVAL; |
8988 | return posix_error(); | ||||
8989 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 8990 | buffer = PyBytes_FromStringAndSize((char *)NULL, length); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 8991 | if (buffer == NULL) |
8992 | return NULL; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8993 | |
8994 | do { | ||||
8995 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8996 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8997 | n = pread(fd, PyBytes_AS_STRING(buffer), length, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 8998 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 8999 | Py_END_ALLOW_THREADS |
9000 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
9001 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9002 | if (n < 0) { |
9003 | Py_DECREF(buffer); | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9004 | return (!async_err) ? posix_error() : NULL; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9005 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9006 | if (n != length) |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9007 | _PyBytes_Resize(&buffer, n); |
9008 | return buffer; | ||||
9009 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9010 | #endif /* HAVE_PREAD */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9011 | |
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9012 | #if defined(HAVE_PREADV) || defined (HAVE_PREADV2) |
9013 | /*[clinic input] | ||||
9014 | os.preadv -> Py_ssize_t | ||||
9015 | |||||
9016 | fd: int | ||||
9017 | buffers: object | ||||
9018 | offset: Py_off_t | ||||
9019 | flags: int = 0 | ||||
9020 | / | ||||
9021 | |||||
9022 | Reads from a file descriptor into a number of mutable bytes-like objects. | ||||
9023 | |||||
9024 | Combines the functionality of readv() and pread(). As readv(), it will | ||||
9025 | transfer data into each buffer until it is full and then move on to the next | ||||
9026 | buffer in the sequence to hold the rest of the data. Its fourth argument, | ||||
9027 | specifies the file offset at which the input operation is to be performed. It | ||||
9028 | will return the total number of bytes read (which can be less than the total | ||||
9029 | capacity of all the objects). | ||||
9030 | |||||
9031 | The flags argument contains a bitwise OR of zero or more of the following flags: | ||||
9032 | |||||
9033 | - RWF_HIPRI | ||||
9034 | - RWF_NOWAIT | ||||
9035 | |||||
9036 | Using non-zero flags requires Linux 4.6 or newer. | ||||
9037 | [clinic start generated code]*/ | ||||
9038 | |||||
9039 | static Py_ssize_t | ||||
9040 | os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, | ||||
9041 | int flags) | ||||
9042 | /*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/ | ||||
9043 | { | ||||
9044 | Py_ssize_t cnt, n; | ||||
9045 | int async_err = 0; | ||||
9046 | struct iovec *iov; | ||||
9047 | Py_buffer *buf; | ||||
9048 | |||||
9049 | if (!PySequence_Check(buffers)) { | ||||
9050 | PyErr_SetString(PyExc_TypeError, | ||||
9051 | "preadv2() arg 2 must be a sequence"); | ||||
9052 | return -1; | ||||
9053 | } | ||||
9054 | |||||
9055 | cnt = PySequence_Size(buffers); | ||||
9056 | if (cnt < 0) { | ||||
9057 | return -1; | ||||
9058 | } | ||||
9059 | |||||
9060 | #ifndef HAVE_PREADV2 | ||||
9061 | if(flags != 0) { | ||||
9062 | argument_unavailable_error("preadv2", "flags"); | ||||
9063 | return -1; | ||||
9064 | } | ||||
9065 | #endif | ||||
9066 | |||||
9067 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) { | ||||
9068 | return -1; | ||||
9069 | } | ||||
9070 | #ifdef HAVE_PREADV2 | ||||
9071 | do { | ||||
9072 | Py_BEGIN_ALLOW_THREADS | ||||
9073 | _Py_BEGIN_SUPPRESS_IPH | ||||
9074 | n = preadv2(fd, iov, cnt, offset, flags); | ||||
9075 | _Py_END_SUPPRESS_IPH | ||||
9076 | Py_END_ALLOW_THREADS | ||||
9077 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
9078 | #else | ||||
9079 | do { | ||||
9080 | Py_BEGIN_ALLOW_THREADS | ||||
9081 | _Py_BEGIN_SUPPRESS_IPH | ||||
9082 | n = preadv(fd, iov, cnt, offset); | ||||
9083 | _Py_END_SUPPRESS_IPH | ||||
9084 | Py_END_ALLOW_THREADS | ||||
9085 | } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
9086 | #endif | ||||
9087 | |||||
9088 | iov_cleanup(iov, buf, cnt); | ||||
9089 | if (n < 0) { | ||||
9090 | if (!async_err) { | ||||
9091 | posix_error(); | ||||
9092 | } | ||||
9093 | return -1; | ||||
9094 | } | ||||
9095 | |||||
9096 | return n; | ||||
9097 | } | ||||
9098 | #endif /* HAVE_PREADV */ | ||||
9099 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9100 | |
9101 | /*[clinic input] | ||||
9102 | os.write -> Py_ssize_t | ||||
9103 | |||||
9104 | fd: int | ||||
9105 | data: Py_buffer | ||||
9106 | / | ||||
9107 | |||||
9108 | Write a bytes object to a file descriptor. | ||||
9109 | [clinic start generated code]*/ | ||||
9110 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9111 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9112 | os_write_impl(PyObject *module, int fd, Py_buffer *data) |
9113 | /*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9114 | { |
Victor Stinner | 66aab0c | 2015-03-19 22:53:20 +0100 | [diff] [blame] | 9115 | return _Py_write(fd, data->buf, data->len); |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9116 | } |
9117 | |||||
9118 | #ifdef HAVE_SENDFILE | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9119 | PyDoc_STRVAR(posix_sendfile__doc__, |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 9120 | "sendfile(out, in, offset, count) -> byteswritten\n\ |
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9121 | sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9122 | -> byteswritten\n\ |
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 9123 | Copy count bytes from file descriptor in to file descriptor out."); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9124 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9125 | /* AC 3.5: don't bother converting, has optional group*/ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9126 | static PyObject * |
9127 | posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict) | ||||
9128 | { | ||||
9129 | int in, out; | ||||
9130 | Py_ssize_t ret; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9131 | int async_err = 0; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9132 | off_t offset; |
9133 | |||||
9134 | #if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__) | ||||
9135 | #ifndef __APPLE__ | ||||
9136 | Py_ssize_t len; | ||||
9137 | #endif | ||||
9138 | PyObject *headers = NULL, *trailers = NULL; | ||||
9139 | Py_buffer *hbuf, *tbuf; | ||||
9140 | off_t sbytes; | ||||
9141 | struct sf_hdtr sf; | ||||
9142 | int flags = 0; | ||||
Martin Panter | bf19d16 | 2015-09-09 01:01:13 +0000 | [diff] [blame] | 9143 | /* Beware that "in" clashes with Python's own "in" operator keyword */ |
Benjamin Peterson | d8a43b4 | 2011-02-26 21:35:16 +0000 | [diff] [blame] | 9144 | static char *keywords[] = {"out", "in", |
9145 | "offset", "count", | ||||
9146 | "headers", "trailers", "flags", NULL}; | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9147 | |
Victor Stinner | 6ce0dbf | 2013-07-07 16:32:36 +0200 | [diff] [blame] | 9148 | sf.headers = NULL; |
9149 | sf.trailers = NULL; | ||||
9150 | |||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9151 | #ifdef __APPLE__ |
9152 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile", | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9153 | 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] | 9154 | #else |
9155 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile", | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9156 | keywords, &out, &in, Py_off_t_converter, &offset, &len, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9157 | #endif |
9158 | &headers, &trailers, &flags)) | ||||
9159 | return NULL; | ||||
9160 | if (headers != NULL) { | ||||
9161 | if (!PySequence_Check(headers)) { | ||||
9162 | PyErr_SetString(PyExc_TypeError, | ||||
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9163 | "sendfile() headers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9164 | return NULL; |
9165 | } else { | ||||
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9166 | Py_ssize_t i = PySequence_Size(headers); |
9167 | if (i < 0) | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9168 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9169 | if (i > INT_MAX) { |
9170 | PyErr_SetString(PyExc_OverflowError, | ||||
9171 | "sendfile() header is too large"); | ||||
9172 | return NULL; | ||||
9173 | } | ||||
9174 | if (i > 0) { | ||||
9175 | sf.hdr_cnt = (int)i; | ||||
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9176 | if (iov_setup(&(sf.headers), &hbuf, |
9177 | headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0) | ||||
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9178 | return NULL; |
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9179 | #ifdef __APPLE__ |
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9180 | for (i = 0; i < sf.hdr_cnt; i++) { |
9181 | Py_ssize_t blen = sf.headers[i].iov_len; | ||||
9182 | # define OFF_T_MAX 0x7fffffffffffffff | ||||
9183 | if (sbytes >= OFF_T_MAX - blen) { | ||||
9184 | PyErr_SetString(PyExc_OverflowError, | ||||
9185 | "sendfile() header is too large"); | ||||
9186 | return NULL; | ||||
9187 | } | ||||
9188 | sbytes += blen; | ||||
9189 | } | ||||
Giampaolo Rodolà | acdad9a | 2011-03-03 16:10:51 +0000 | [diff] [blame] | 9190 | #endif |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9191 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9192 | } |
9193 | } | ||||
9194 | if (trailers != NULL) { | ||||
9195 | if (!PySequence_Check(trailers)) { | ||||
9196 | PyErr_SetString(PyExc_TypeError, | ||||
Martin Panter | 9499413 | 2015-09-09 05:29:24 +0000 | [diff] [blame] | 9197 | "sendfile() trailers must be a sequence"); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9198 | return NULL; |
9199 | } else { | ||||
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9200 | Py_ssize_t i = PySequence_Size(trailers); |
9201 | if (i < 0) | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9202 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9203 | if (i > INT_MAX) { |
9204 | PyErr_SetString(PyExc_OverflowError, | ||||
9205 | "sendfile() trailer is too large"); | ||||
9206 | return NULL; | ||||
9207 | } | ||||
9208 | if (i > 0) { | ||||
9209 | sf.trl_cnt = (int)i; | ||||
Serhiy Storchaka | 9d57273 | 2018-07-31 10:24:54 +0300 | [diff] [blame] | 9210 | if (iov_setup(&(sf.trailers), &tbuf, |
9211 | trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0) | ||||
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9212 | return NULL; |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9213 | } |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9214 | } |
9215 | } | ||||
9216 | |||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9217 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9218 | do { |
9219 | Py_BEGIN_ALLOW_THREADS | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9220 | #ifdef __APPLE__ |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9221 | ret = sendfile(in, out, offset, &sbytes, &sf, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9222 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9223 | ret = sendfile(in, out, offset, len, &sf, &sbytes, flags); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9224 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9225 | Py_END_ALLOW_THREADS |
9226 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9227 | _Py_END_SUPPRESS_IPH |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9228 | |
9229 | if (sf.headers != NULL) | ||||
9230 | iov_cleanup(sf.headers, hbuf, sf.hdr_cnt); | ||||
9231 | if (sf.trailers != NULL) | ||||
9232 | iov_cleanup(sf.trailers, tbuf, sf.trl_cnt); | ||||
9233 | |||||
9234 | if (ret < 0) { | ||||
9235 | if ((errno == EAGAIN) || (errno == EBUSY)) { | ||||
9236 | if (sbytes != 0) { | ||||
9237 | // some data has been sent | ||||
9238 | goto done; | ||||
9239 | } | ||||
9240 | else { | ||||
9241 | // no data has been sent; upper application is supposed | ||||
9242 | // to retry on EAGAIN or EBUSY | ||||
9243 | return posix_error(); | ||||
9244 | } | ||||
9245 | } | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9246 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9247 | } |
9248 | goto done; | ||||
9249 | |||||
9250 | done: | ||||
9251 | #if !defined(HAVE_LARGEFILE_SUPPORT) | ||||
9252 | return Py_BuildValue("l", sbytes); | ||||
9253 | #else | ||||
9254 | return Py_BuildValue("L", sbytes); | ||||
9255 | #endif | ||||
9256 | |||||
9257 | #else | ||||
9258 | Py_ssize_t count; | ||||
9259 | PyObject *offobj; | ||||
9260 | static char *keywords[] = {"out", "in", | ||||
9261 | "offset", "count", NULL}; | ||||
9262 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile", | ||||
9263 | keywords, &out, &in, &offobj, &count)) | ||||
9264 | return NULL; | ||||
Benjamin Peterson | 840ef8f | 2016-09-07 14:45:10 -0700 | [diff] [blame] | 9265 | #ifdef __linux__ |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9266 | if (offobj == Py_None) { |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9267 | do { |
9268 | Py_BEGIN_ALLOW_THREADS | ||||
9269 | ret = sendfile(out, in, NULL, count); | ||||
9270 | Py_END_ALLOW_THREADS | ||||
9271 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9272 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9273 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodola' | ff1a735 | 2011-04-19 09:47:16 +0200 | [diff] [blame] | 9274 | return Py_BuildValue("n", ret); |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9275 | } |
9276 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9277 | if (!Py_off_t_converter(offobj, &offset)) |
Antoine Pitrou | dcc20b8 | 2011-02-26 13:38:35 +0000 | [diff] [blame] | 9278 | return NULL; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9279 | |
9280 | do { | ||||
9281 | Py_BEGIN_ALLOW_THREADS | ||||
9282 | ret = sendfile(out, in, &offset, count); | ||||
9283 | Py_END_ALLOW_THREADS | ||||
9284 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9285 | if (ret < 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9286 | return (!async_err) ? posix_error() : NULL; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 9287 | return Py_BuildValue("n", ret); |
9288 | #endif | ||||
9289 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9290 | #endif /* HAVE_SENDFILE */ |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 9291 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9292 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9293 | #if defined(__APPLE__) |
9294 | /*[clinic input] | ||||
9295 | os._fcopyfile | ||||
9296 | |||||
9297 | infd: int | ||||
9298 | outfd: int | ||||
9299 | flags: int | ||||
9300 | / | ||||
9301 | |||||
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9302 | Efficiently copy content or metadata of 2 regular file descriptors (macOS). |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9303 | [clinic start generated code]*/ |
9304 | |||||
9305 | static PyObject * | ||||
9306 | os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags) | ||||
Giampaolo Rodola | c7f02a9 | 2018-06-19 08:27:29 -0700 | [diff] [blame] | 9307 | /*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/ |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 9308 | { |
9309 | int ret; | ||||
9310 | |||||
9311 | Py_BEGIN_ALLOW_THREADS | ||||
9312 | ret = fcopyfile(infd, outfd, NULL, flags); | ||||
9313 | Py_END_ALLOW_THREADS | ||||
9314 | if (ret < 0) | ||||
9315 | return posix_error(); | ||||
9316 | Py_RETURN_NONE; | ||||
9317 | } | ||||
9318 | #endif | ||||
9319 | |||||
9320 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9321 | /*[clinic input] |
9322 | os.fstat | ||||
9323 | |||||
9324 | fd : int | ||||
9325 | |||||
9326 | Perform a stat system call on the given file descriptor. | ||||
9327 | |||||
9328 | Like stat(), but for an open file descriptor. | ||||
9329 | Equivalent to os.stat(fd). | ||||
9330 | [clinic start generated code]*/ | ||||
9331 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9332 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9333 | os_fstat_impl(PyObject *module, int fd) |
9334 | /*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9335 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9336 | STRUCT_STAT st; |
9337 | int res; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9338 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9339 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9340 | do { |
9341 | Py_BEGIN_ALLOW_THREADS | ||||
9342 | res = FSTAT(fd, &st); | ||||
9343 | Py_END_ALLOW_THREADS | ||||
9344 | } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9345 | if (res != 0) { |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9346 | #ifdef MS_WINDOWS |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9347 | return PyErr_SetFromWindowsErr(0); |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9348 | #else |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9349 | return (!async_err) ? posix_error() : NULL; |
Martin v. Löwis | 1469466 | 2006-02-03 12:54:16 +0000 | [diff] [blame] | 9350 | #endif |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9351 | } |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 9352 | |
Victor Stinner | 4195b5c | 2012-02-08 23:03:19 +0100 | [diff] [blame] | 9353 | return _pystat_fromstructstat(&st); |
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9354 | } |
9355 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9356 | |
9357 | /*[clinic input] | ||||
9358 | os.isatty -> bool | ||||
9359 | fd: int | ||||
9360 | / | ||||
9361 | |||||
9362 | Return True if the fd is connected to a terminal. | ||||
9363 | |||||
9364 | Return True if the file descriptor is an open file descriptor | ||||
9365 | connected to the slave end of a terminal. | ||||
9366 | [clinic start generated code]*/ | ||||
9367 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9368 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9369 | os_isatty_impl(PyObject *module, int fd) |
9370 | /*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9371 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9372 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9373 | _Py_BEGIN_SUPPRESS_IPH |
9374 | return_value = isatty(fd); | ||||
9375 | _Py_END_SUPPRESS_IPH | ||||
9376 | return return_value; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9377 | } |
9378 | |||||
9379 | |||||
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9380 | #ifdef HAVE_PIPE |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9381 | /*[clinic input] |
9382 | os.pipe | ||||
9383 | |||||
9384 | Create a pipe. | ||||
9385 | |||||
9386 | Returns a tuple of two file descriptors: | ||||
9387 | (read_fd, write_fd) | ||||
9388 | [clinic start generated code]*/ | ||||
9389 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9390 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9391 | os_pipe_impl(PyObject *module) |
9392 | /*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/ | ||||
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9393 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9394 | int fds[2]; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9395 | #ifdef MS_WINDOWS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9396 | HANDLE read, write; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9397 | SECURITY_ATTRIBUTES attr; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9398 | BOOL ok; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9399 | #else |
9400 | int res; | ||||
9401 | #endif | ||||
9402 | |||||
9403 | #ifdef MS_WINDOWS | ||||
9404 | attr.nLength = sizeof(attr); | ||||
9405 | attr.lpSecurityDescriptor = NULL; | ||||
9406 | attr.bInheritHandle = FALSE; | ||||
9407 | |||||
9408 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9409 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9410 | ok = CreatePipe(&read, &write, &attr, 0); |
9411 | if (ok) { | ||||
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 9412 | fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY); |
9413 | fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY); | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9414 | if (fds[0] == -1 || fds[1] == -1) { |
9415 | CloseHandle(read); | ||||
9416 | CloseHandle(write); | ||||
9417 | ok = 0; | ||||
9418 | } | ||||
9419 | } | ||||
Steve Dower | c363061 | 2016-11-19 18:41:16 -0800 | [diff] [blame] | 9420 | _Py_END_SUPPRESS_IPH |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9421 | Py_END_ALLOW_THREADS |
9422 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 9423 | if (!ok) |
Victor Stinner | b024e84 | 2012-10-31 22:24:06 +0100 | [diff] [blame] | 9424 | return PyErr_SetFromWindowsErr(0); |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 9425 | #else |
9426 | |||||
9427 | #ifdef HAVE_PIPE2 | ||||
9428 | Py_BEGIN_ALLOW_THREADS | ||||
9429 | res = pipe2(fds, O_CLOEXEC); | ||||
9430 | Py_END_ALLOW_THREADS | ||||
9431 | |||||
9432 | if (res != 0 && errno == ENOSYS) | ||||
9433 | { | ||||
9434 | #endif | ||||
9435 | Py_BEGIN_ALLOW_THREADS | ||||
9436 | res = pipe(fds); | ||||
9437 | Py_END_ALLOW_THREADS | ||||
9438 | |||||
9439 | if (res == 0) { | ||||
9440 | if (_Py_set_inheritable(fds[0], 0, NULL) < 0) { | ||||
9441 | close(fds[0]); | ||||
9442 | close(fds[1]); | ||||
9443 | return NULL; | ||||
9444 | } | ||||
9445 | if (_Py_set_inheritable(fds[1], 0, NULL) < 0) { | ||||
9446 | close(fds[0]); | ||||
9447 | close(fds[1]); | ||||
9448 | return NULL; | ||||
9449 | } | ||||
9450 | } | ||||
9451 | #ifdef HAVE_PIPE2 | ||||
9452 | } | ||||
9453 | #endif | ||||
9454 | |||||
9455 | if (res != 0) | ||||
9456 | return PyErr_SetFromErrno(PyExc_OSError); | ||||
9457 | #endif /* !MS_WINDOWS */ | ||||
9458 | return Py_BuildValue("(ii)", fds[0], fds[1]); | ||||
Guido van Rossum | 687dd13 | 1993-05-17 08:34:16 +0000 | [diff] [blame] | 9459 | } |
Guido van Rossum | a4916fa | 1996-05-23 22:58:55 +0000 | [diff] [blame] | 9460 | #endif /* HAVE_PIPE */ |
9461 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9462 | |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9463 | #ifdef HAVE_PIPE2 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9464 | /*[clinic input] |
9465 | os.pipe2 | ||||
9466 | |||||
9467 | flags: int | ||||
9468 | / | ||||
9469 | |||||
9470 | Create a pipe with flags set atomically. | ||||
9471 | |||||
9472 | Returns a tuple of two file descriptors: | ||||
9473 | (read_fd, write_fd) | ||||
9474 | |||||
9475 | flags can be constructed by ORing together one or more of these values: | ||||
9476 | O_NONBLOCK, O_CLOEXEC. | ||||
9477 | [clinic start generated code]*/ | ||||
9478 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9479 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9480 | os_pipe2_impl(PyObject *module, int flags) |
9481 | /*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9482 | { |
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9483 | int fds[2]; |
9484 | int res; | ||||
9485 | |||||
Charles-François Natali | daafdd5 | 2011-05-29 20:07:40 +0200 | [diff] [blame] | 9486 | res = pipe2(fds, flags); |
9487 | if (res != 0) | ||||
9488 | return posix_error(); | ||||
9489 | return Py_BuildValue("(ii)", fds[0], fds[1]); | ||||
9490 | } | ||||
9491 | #endif /* HAVE_PIPE2 */ | ||||
9492 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9493 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9494 | #ifdef HAVE_WRITEV |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9495 | /*[clinic input] |
9496 | os.writev -> Py_ssize_t | ||||
9497 | fd: int | ||||
9498 | buffers: object | ||||
9499 | / | ||||
9500 | |||||
9501 | Iterate over buffers, and write the contents of each to a file descriptor. | ||||
9502 | |||||
9503 | Returns the total number of bytes written. | ||||
9504 | buffers must be a sequence of bytes-like objects. | ||||
9505 | [clinic start generated code]*/ | ||||
9506 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9507 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9508 | os_writev_impl(PyObject *module, int fd, PyObject *buffers) |
9509 | /*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9510 | { |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9511 | Py_ssize_t cnt; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9512 | Py_ssize_t result; |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9513 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9514 | struct iovec *iov; |
9515 | Py_buffer *buf; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9516 | |
9517 | if (!PySequence_Check(buffers)) { | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9518 | PyErr_SetString(PyExc_TypeError, |
9519 | "writev() arg 2 must be a sequence"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9520 | return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9521 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9522 | cnt = PySequence_Size(buffers); |
Serhiy Storchaka | bf623ae | 2017-04-19 20:03:52 +0300 | [diff] [blame] | 9523 | if (cnt < 0) |
9524 | return -1; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9525 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9526 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { |
9527 | return -1; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9528 | } |
9529 | |||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9530 | do { |
9531 | Py_BEGIN_ALLOW_THREADS | ||||
9532 | result = writev(fd, iov, cnt); | ||||
9533 | Py_END_ALLOW_THREADS | ||||
9534 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9535 | |
9536 | iov_cleanup(iov, buf, cnt); | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9537 | if (result < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9538 | posix_error(); |
Victor Stinner | 57ddf78 | 2014-01-08 15:21:28 +0100 | [diff] [blame] | 9539 | |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 9540 | return result; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9541 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9542 | #endif /* HAVE_WRITEV */ |
9543 | |||||
9544 | |||||
9545 | #ifdef HAVE_PWRITE | ||||
9546 | /*[clinic input] | ||||
9547 | os.pwrite -> Py_ssize_t | ||||
9548 | |||||
9549 | fd: int | ||||
9550 | buffer: Py_buffer | ||||
9551 | offset: Py_off_t | ||||
9552 | / | ||||
9553 | |||||
9554 | Write bytes to a file descriptor starting at a particular offset. | ||||
9555 | |||||
9556 | Write buffer to fd, starting at offset bytes from the beginning of | ||||
9557 | the file. Returns the number of bytes writte. Does not change the | ||||
9558 | current file offset. | ||||
9559 | [clinic start generated code]*/ | ||||
9560 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9561 | static Py_ssize_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9562 | os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset) |
9563 | /*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9564 | { |
9565 | Py_ssize_t size; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9566 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9567 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9568 | do { |
9569 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9570 | _Py_BEGIN_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9571 | size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 9572 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9573 | Py_END_ALLOW_THREADS |
9574 | } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9575 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9576 | if (size < 0 && !async_err) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9577 | posix_error(); |
9578 | return size; | ||||
9579 | } | ||||
9580 | #endif /* HAVE_PWRITE */ | ||||
9581 | |||||
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9582 | #if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2) |
9583 | /*[clinic input] | ||||
9584 | os.pwritev -> Py_ssize_t | ||||
9585 | |||||
9586 | fd: int | ||||
9587 | buffers: object | ||||
9588 | offset: Py_off_t | ||||
9589 | flags: int = 0 | ||||
9590 | / | ||||
9591 | |||||
9592 | Writes the contents of bytes-like objects to a file descriptor at a given offset. | ||||
9593 | |||||
9594 | Combines the functionality of writev() and pwrite(). All buffers must be a sequence | ||||
9595 | of bytes-like objects. Buffers are processed in array order. Entire contents of first | ||||
9596 | buffer is written before proceeding to second, and so on. The operating system may | ||||
9597 | set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used. | ||||
9598 | This function writes the contents of each object to the file descriptor and returns | ||||
9599 | the total number of bytes written. | ||||
9600 | |||||
9601 | The flags argument contains a bitwise OR of zero or more of the following flags: | ||||
9602 | |||||
9603 | - RWF_DSYNC | ||||
9604 | - RWF_SYNC | ||||
9605 | |||||
9606 | Using non-zero flags requires Linux 4.7 or newer. | ||||
9607 | [clinic start generated code]*/ | ||||
9608 | |||||
9609 | static Py_ssize_t | ||||
9610 | os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset, | ||||
9611 | int flags) | ||||
9612 | /*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/ | ||||
9613 | { | ||||
9614 | Py_ssize_t cnt; | ||||
9615 | Py_ssize_t result; | ||||
9616 | int async_err = 0; | ||||
9617 | struct iovec *iov; | ||||
9618 | Py_buffer *buf; | ||||
9619 | |||||
9620 | if (!PySequence_Check(buffers)) { | ||||
9621 | PyErr_SetString(PyExc_TypeError, | ||||
9622 | "pwritev() arg 2 must be a sequence"); | ||||
9623 | return -1; | ||||
9624 | } | ||||
9625 | |||||
9626 | cnt = PySequence_Size(buffers); | ||||
9627 | if (cnt < 0) { | ||||
9628 | return -1; | ||||
9629 | } | ||||
9630 | |||||
9631 | #ifndef HAVE_PWRITEV2 | ||||
9632 | if(flags != 0) { | ||||
9633 | argument_unavailable_error("pwritev2", "flags"); | ||||
9634 | return -1; | ||||
9635 | } | ||||
9636 | #endif | ||||
9637 | |||||
9638 | if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) { | ||||
9639 | return -1; | ||||
9640 | } | ||||
9641 | #ifdef HAVE_PWRITEV2 | ||||
9642 | do { | ||||
9643 | Py_BEGIN_ALLOW_THREADS | ||||
9644 | _Py_BEGIN_SUPPRESS_IPH | ||||
9645 | result = pwritev2(fd, iov, cnt, offset, flags); | ||||
9646 | _Py_END_SUPPRESS_IPH | ||||
9647 | Py_END_ALLOW_THREADS | ||||
9648 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
9649 | #else | ||||
9650 | do { | ||||
9651 | Py_BEGIN_ALLOW_THREADS | ||||
9652 | _Py_BEGIN_SUPPRESS_IPH | ||||
9653 | result = pwritev(fd, iov, cnt, offset); | ||||
9654 | _Py_END_SUPPRESS_IPH | ||||
9655 | Py_END_ALLOW_THREADS | ||||
9656 | } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
9657 | #endif | ||||
9658 | |||||
9659 | iov_cleanup(iov, buf, cnt); | ||||
9660 | if (result < 0) { | ||||
9661 | if (!async_err) { | ||||
9662 | posix_error(); | ||||
9663 | } | ||||
9664 | return -1; | ||||
9665 | } | ||||
9666 | |||||
9667 | return result; | ||||
9668 | } | ||||
9669 | #endif /* HAVE_PWRITEV */ | ||||
9670 | |||||
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9671 | #ifdef HAVE_COPY_FILE_RANGE |
9672 | /*[clinic input] | ||||
9673 | |||||
9674 | os.copy_file_range | ||||
9675 | src: int | ||||
9676 | Source file descriptor. | ||||
9677 | dst: int | ||||
9678 | Destination file descriptor. | ||||
9679 | count: Py_ssize_t | ||||
9680 | Number of bytes to copy. | ||||
9681 | offset_src: object = None | ||||
9682 | Starting offset in src. | ||||
9683 | offset_dst: object = None | ||||
9684 | Starting offset in dst. | ||||
9685 | |||||
9686 | Copy count bytes from one file descriptor to another. | ||||
9687 | |||||
9688 | If offset_src is None, then src is read from the current position; | ||||
9689 | respectively for offset_dst. | ||||
9690 | [clinic start generated code]*/ | ||||
9691 | |||||
9692 | static PyObject * | ||||
9693 | os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count, | ||||
9694 | PyObject *offset_src, PyObject *offset_dst) | ||||
9695 | /*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/ | ||||
9696 | { | ||||
9697 | off_t offset_src_val, offset_dst_val; | ||||
9698 | off_t *p_offset_src = NULL; | ||||
9699 | off_t *p_offset_dst = NULL; | ||||
9700 | Py_ssize_t ret; | ||||
9701 | int async_err = 0; | ||||
9702 | /* The flags argument is provided to allow | ||||
9703 | * for future extensions and currently must be to 0. */ | ||||
9704 | int flags = 0; | ||||
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 9705 | |
9706 | |||||
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 9707 | if (count < 0) { |
9708 | PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed"); | ||||
9709 | return NULL; | ||||
9710 | } | ||||
9711 | |||||
9712 | if (offset_src != Py_None) { | ||||
9713 | if (!Py_off_t_converter(offset_src, &offset_src_val)) { | ||||
9714 | return NULL; | ||||
9715 | } | ||||
9716 | p_offset_src = &offset_src_val; | ||||
9717 | } | ||||
9718 | |||||
9719 | if (offset_dst != Py_None) { | ||||
9720 | if (!Py_off_t_converter(offset_dst, &offset_dst_val)) { | ||||
9721 | return NULL; | ||||
9722 | } | ||||
9723 | p_offset_dst = &offset_dst_val; | ||||
9724 | } | ||||
9725 | |||||
9726 | do { | ||||
9727 | Py_BEGIN_ALLOW_THREADS | ||||
9728 | ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags); | ||||
9729 | Py_END_ALLOW_THREADS | ||||
9730 | } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); | ||||
9731 | |||||
9732 | if (ret < 0) { | ||||
9733 | return (!async_err) ? posix_error() : NULL; | ||||
9734 | } | ||||
9735 | |||||
9736 | return PyLong_FromSsize_t(ret); | ||||
9737 | } | ||||
9738 | #endif /* HAVE_COPY_FILE_RANGE*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9739 | |
9740 | #ifdef HAVE_MKFIFO | ||||
9741 | /*[clinic input] | ||||
9742 | os.mkfifo | ||||
9743 | |||||
9744 | path: path_t | ||||
9745 | mode: int=0o666 | ||||
9746 | * | ||||
9747 | dir_fd: dir_fd(requires='mkfifoat')=None | ||||
9748 | |||||
9749 | Create a "fifo" (a POSIX named pipe). | ||||
9750 | |||||
9751 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
9752 | and path should be relative; path will then be relative to that directory. | ||||
9753 | dir_fd may not be implemented on your platform. | ||||
9754 | If it is unavailable, using it will raise a NotImplementedError. | ||||
9755 | [clinic start generated code]*/ | ||||
9756 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9757 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9758 | os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd) |
9759 | /*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9760 | { |
9761 | int result; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9762 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9763 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9764 | do { |
9765 | Py_BEGIN_ALLOW_THREADS | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9766 | #ifdef HAVE_MKFIFOAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9767 | if (dir_fd != DEFAULT_DIR_FD) |
9768 | result = mkfifoat(dir_fd, path->narrow, mode); | ||||
9769 | else | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9770 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9771 | result = mkfifo(path->narrow, mode); |
9772 | Py_END_ALLOW_THREADS | ||||
9773 | } while (result != 0 && errno == EINTR && | ||||
9774 | !(async_err = PyErr_CheckSignals())); | ||||
9775 | if (result != 0) | ||||
9776 | return (!async_err) ? posix_error() : NULL; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9777 | |
9778 | Py_RETURN_NONE; | ||||
9779 | } | ||||
9780 | #endif /* HAVE_MKFIFO */ | ||||
9781 | |||||
9782 | |||||
9783 | #if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) | ||||
9784 | /*[clinic input] | ||||
9785 | os.mknod | ||||
9786 | |||||
9787 | path: path_t | ||||
9788 | mode: int=0o600 | ||||
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9789 | device: dev_t=0 |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9790 | * |
9791 | dir_fd: dir_fd(requires='mknodat')=None | ||||
9792 | |||||
9793 | Create a node in the file system. | ||||
9794 | |||||
9795 | Create a node in the file system (file, device special file or named pipe) | ||||
9796 | at path. mode specifies both the permissions to use and the | ||||
9797 | type of node to be created, being combined (bitwise OR) with one of | ||||
9798 | S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode, | ||||
9799 | device defines the newly created device special file (probably using | ||||
9800 | os.makedev()). Otherwise device is ignored. | ||||
9801 | |||||
9802 | If dir_fd is not None, it should be a file descriptor open to a directory, | ||||
9803 | and path should be relative; path will then be relative to that directory. | ||||
9804 | dir_fd may not be implemented on your platform. | ||||
9805 | If it is unavailable, using it will raise a NotImplementedError. | ||||
9806 | [clinic start generated code]*/ | ||||
9807 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9808 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9809 | os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 9810 | int dir_fd) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9811 | /*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9812 | { |
9813 | int result; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9814 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9815 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9816 | do { |
9817 | Py_BEGIN_ALLOW_THREADS | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9818 | #ifdef HAVE_MKNODAT |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9819 | if (dir_fd != DEFAULT_DIR_FD) |
9820 | result = mknodat(dir_fd, path->narrow, mode, device); | ||||
9821 | else | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9822 | #endif |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9823 | result = mknod(path->narrow, mode, device); |
9824 | Py_END_ALLOW_THREADS | ||||
9825 | } while (result != 0 && errno == EINTR && | ||||
9826 | !(async_err = PyErr_CheckSignals())); | ||||
9827 | if (result != 0) | ||||
9828 | return (!async_err) ? posix_error() : NULL; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9829 | |
9830 | Py_RETURN_NONE; | ||||
9831 | } | ||||
9832 | #endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */ | ||||
9833 | |||||
9834 | |||||
9835 | #ifdef HAVE_DEVICE_MACROS | ||||
9836 | /*[clinic input] | ||||
9837 | os.major -> unsigned_int | ||||
9838 | |||||
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9839 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9840 | / |
9841 | |||||
9842 | Extracts a device major number from a raw device number. | ||||
9843 | [clinic start generated code]*/ | ||||
9844 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9845 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9846 | os_major_impl(PyObject *module, dev_t device) |
9847 | /*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9848 | { |
9849 | return major(device); | ||||
9850 | } | ||||
9851 | |||||
9852 | |||||
9853 | /*[clinic input] | ||||
9854 | os.minor -> unsigned_int | ||||
9855 | |||||
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9856 | device: dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9857 | / |
9858 | |||||
9859 | Extracts a device minor number from a raw device number. | ||||
9860 | [clinic start generated code]*/ | ||||
9861 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9862 | static unsigned int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9863 | os_minor_impl(PyObject *module, dev_t device) |
9864 | /*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9865 | { |
9866 | return minor(device); | ||||
9867 | } | ||||
9868 | |||||
9869 | |||||
9870 | /*[clinic input] | ||||
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9871 | os.makedev -> dev_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9872 | |
9873 | major: int | ||||
9874 | minor: int | ||||
9875 | / | ||||
9876 | |||||
9877 | Composes a raw device number from the major and minor device numbers. | ||||
9878 | [clinic start generated code]*/ | ||||
9879 | |||||
Serhiy Storchaka | acdb7c1 | 2015-01-18 11:17:39 +0200 | [diff] [blame] | 9880 | static dev_t |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9881 | os_makedev_impl(PyObject *module, int major, int minor) |
9882 | /*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9883 | { |
9884 | return makedev(major, minor); | ||||
9885 | } | ||||
9886 | #endif /* HAVE_DEVICE_MACROS */ | ||||
9887 | |||||
9888 | |||||
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9889 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9890 | /*[clinic input] |
9891 | os.ftruncate | ||||
9892 | |||||
9893 | fd: int | ||||
9894 | length: Py_off_t | ||||
9895 | / | ||||
9896 | |||||
9897 | Truncate a file, specified by file descriptor, to a specific length. | ||||
9898 | [clinic start generated code]*/ | ||||
9899 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9900 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9901 | os_ftruncate_impl(PyObject *module, int fd, Py_off_t length) |
9902 | /*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9903 | { |
9904 | int result; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9905 | int async_err = 0; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9906 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9907 | if (PySys_Audit("os.truncate", "in", fd, length) < 0) { |
9908 | return NULL; | ||||
9909 | } | ||||
9910 | |||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9911 | do { |
9912 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9913 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9914 | #ifdef MS_WINDOWS |
9915 | result = _chsize_s(fd, length); | ||||
9916 | #else | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9917 | result = ftruncate(fd, length); |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9918 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9919 | _Py_END_SUPPRESS_IPH |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 9920 | Py_END_ALLOW_THREADS |
9921 | } while (result != 0 && errno == EINTR && | ||||
9922 | !(async_err = PyErr_CheckSignals())); | ||||
9923 | if (result != 0) | ||||
9924 | return (!async_err) ? posix_error() : NULL; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9925 | Py_RETURN_NONE; |
9926 | } | ||||
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9927 | #endif /* HAVE_FTRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9928 | |
9929 | |||||
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9930 | #if defined HAVE_TRUNCATE || defined MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9931 | /*[clinic input] |
9932 | os.truncate | ||||
9933 | path: path_t(allow_fd='PATH_HAVE_FTRUNCATE') | ||||
9934 | length: Py_off_t | ||||
9935 | |||||
9936 | Truncate a file, specified by path, to a specific length. | ||||
9937 | |||||
9938 | On some platforms, path may also be specified as an open file descriptor. | ||||
9939 | If this functionality is unavailable, using it raises an exception. | ||||
9940 | [clinic start generated code]*/ | ||||
9941 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9942 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 9943 | os_truncate_impl(PyObject *module, path_t *path, Py_off_t length) |
9944 | /*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9945 | { |
9946 | int result; | ||||
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9947 | #ifdef MS_WINDOWS |
9948 | int fd; | ||||
9949 | #endif | ||||
9950 | |||||
9951 | if (path->fd != -1) | ||||
9952 | return os_ftruncate_impl(module, path->fd, length); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9953 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 9954 | if (PySys_Audit("os.truncate", "On", path->object, length) < 0) { |
9955 | return NULL; | ||||
9956 | } | ||||
9957 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9958 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9959 | _Py_BEGIN_SUPPRESS_IPH |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9960 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 9961 | fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT); |
Victor Stinner | cc0bbbc | 2015-04-25 00:21:52 +0200 | [diff] [blame] | 9962 | if (fd < 0) |
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9963 | result = -1; |
9964 | else { | ||||
9965 | result = _chsize_s(fd, length); | ||||
9966 | close(fd); | ||||
9967 | if (result < 0) | ||||
9968 | errno = result; | ||||
9969 | } | ||||
9970 | #else | ||||
9971 | result = truncate(path->narrow, length); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9972 | #endif |
Steve Dower | a1c7e72 | 2015-04-12 00:26:43 -0400 | [diff] [blame] | 9973 | _Py_END_SUPPRESS_IPH |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9974 | Py_END_ALLOW_THREADS |
9975 | if (result < 0) | ||||
Alexey Izbyshev | 8346031 | 2018-10-20 03:28:22 +0300 | [diff] [blame] | 9976 | return posix_path_error(path); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9977 | |
9978 | Py_RETURN_NONE; | ||||
9979 | } | ||||
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 9980 | #endif /* HAVE_TRUNCATE || MS_WINDOWS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9981 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 9982 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9983 | /* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise() |
9984 | and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is | ||||
9985 | defined, which is the case in Python on AIX. AIX bug report: | ||||
9986 | http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */ | ||||
9987 | #if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__) | ||||
9988 | # define POSIX_FADVISE_AIX_BUG | ||||
9989 | #endif | ||||
9990 | |||||
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 9991 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 9992 | #if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 9993 | /*[clinic input] |
9994 | os.posix_fallocate | ||||
9995 | |||||
9996 | fd: int | ||||
9997 | offset: Py_off_t | ||||
9998 | length: Py_off_t | ||||
9999 | / | ||||
10000 | |||||
10001 | Ensure a file has allocated at least a particular number of bytes on disk. | ||||
10002 | |||||
10003 | Ensure that the file specified by fd encompasses a range of bytes | ||||
10004 | starting at offset bytes from the beginning and continuing for length bytes. | ||||
10005 | [clinic start generated code]*/ | ||||
10006 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10007 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10008 | os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10009 | Py_off_t length) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10010 | /*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10011 | { |
10012 | int result; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10013 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10014 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10015 | do { |
10016 | Py_BEGIN_ALLOW_THREADS | ||||
10017 | result = posix_fallocate(fd, offset, length); | ||||
10018 | Py_END_ALLOW_THREADS | ||||
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10019 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
10020 | |||||
10021 | if (result == 0) | ||||
10022 | Py_RETURN_NONE; | ||||
10023 | |||||
10024 | if (async_err) | ||||
10025 | return NULL; | ||||
10026 | |||||
10027 | errno = result; | ||||
10028 | return posix_error(); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10029 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10030 | #endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10031 | |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10032 | |
Victor Stinner | d6b1769 | 2014-09-30 12:20:05 +0200 | [diff] [blame] | 10033 | #if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10034 | /*[clinic input] |
10035 | os.posix_fadvise | ||||
10036 | |||||
10037 | fd: int | ||||
10038 | offset: Py_off_t | ||||
10039 | length: Py_off_t | ||||
10040 | advice: int | ||||
10041 | / | ||||
10042 | |||||
10043 | Announce an intention to access data in a specific pattern. | ||||
10044 | |||||
10045 | Announce an intention to access data in a specific pattern, thus allowing | ||||
10046 | the kernel to make optimizations. | ||||
10047 | The advice applies to the region of the file specified by fd starting at | ||||
10048 | offset and continuing for length bytes. | ||||
10049 | advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL, | ||||
10050 | POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or | ||||
10051 | POSIX_FADV_DONTNEED. | ||||
10052 | [clinic start generated code]*/ | ||||
10053 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10054 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10055 | os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 10056 | Py_off_t length, int advice) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10057 | /*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10058 | { |
10059 | int result; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10060 | int async_err = 0; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10061 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10062 | do { |
10063 | Py_BEGIN_ALLOW_THREADS | ||||
10064 | result = posix_fadvise(fd, offset, length, advice); | ||||
10065 | Py_END_ALLOW_THREADS | ||||
Коренберг Марк | d4b93e2 | 2017-08-14 18:55:16 +0500 | [diff] [blame] | 10066 | } while (result == EINTR && !(async_err = PyErr_CheckSignals())); |
10067 | |||||
10068 | if (result == 0) | ||||
10069 | Py_RETURN_NONE; | ||||
10070 | |||||
10071 | if (async_err) | ||||
10072 | return NULL; | ||||
10073 | |||||
10074 | errno = result; | ||||
10075 | return posix_error(); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10076 | } |
Victor Stinner | ec39e26 | 2014-09-30 12:35:58 +0200 | [diff] [blame] | 10077 | #endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */ |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 10078 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10079 | #ifdef HAVE_PUTENV |
Guido van Rossum | ec4f4ac | 1997-06-02 22:20:51 +0000 | [diff] [blame] | 10080 | |
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 10081 | /* Save putenv() parameters as values here, so we can collect them when they |
10082 | * get re-set with another call for the same key. */ | ||||
10083 | static PyObject *posix_putenv_garbage; | ||||
10084 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10085 | static void |
10086 | posix_putenv_garbage_setitem(PyObject *name, PyObject *value) | ||||
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10087 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10088 | /* Install the first arg and newstr in posix_putenv_garbage; |
10089 | * this will cause previous value to be collected. This has to | ||||
10090 | * happen after the real putenv() call because the old value | ||||
10091 | * was still accessible until then. */ | ||||
10092 | if (PyDict_SetItem(posix_putenv_garbage, name, value)) | ||||
10093 | /* really not much we can do; just leak */ | ||||
10094 | PyErr_Clear(); | ||||
10095 | else | ||||
10096 | Py_DECREF(value); | ||||
10097 | } | ||||
10098 | |||||
10099 | |||||
Thomas Heller | f78f12a | 2007-11-08 19:33:05 +0000 | [diff] [blame] | 10100 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10101 | /*[clinic input] |
10102 | os.putenv | ||||
10103 | |||||
10104 | name: unicode | ||||
10105 | value: unicode | ||||
10106 | / | ||||
10107 | |||||
10108 | Change or add an environment variable. | ||||
10109 | [clinic start generated code]*/ | ||||
10110 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10111 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10112 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
10113 | /*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10114 | { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 10115 | const wchar_t *env; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10116 | Py_ssize_t size; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10117 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10118 | /* Search from index 1 because on Windows starting '=' is allowed for |
10119 | defining hidden environment variables. */ | ||||
10120 | if (PyUnicode_GET_LENGTH(name) == 0 || | ||||
10121 | PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1) | ||||
10122 | { | ||||
10123 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); | ||||
10124 | return NULL; | ||||
10125 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10126 | PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value); |
10127 | if (unicode == NULL) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10128 | return NULL; |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10129 | } |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10130 | |
10131 | env = PyUnicode_AsUnicodeAndSize(unicode, &size); | ||||
10132 | if (env == NULL) | ||||
10133 | goto error; | ||||
10134 | if (size > _MAX_ENV) { | ||||
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10135 | PyErr_Format(PyExc_ValueError, |
10136 | "the environment variable is longer than %u characters", | ||||
10137 | _MAX_ENV); | ||||
10138 | goto error; | ||||
10139 | } | ||||
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10140 | if (wcslen(env) != (size_t)size) { |
10141 | PyErr_SetString(PyExc_ValueError, "embedded null character"); | ||||
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 10142 | goto error; |
Serhiy Storchaka | f7eae0a | 2017-06-28 08:30:06 +0300 | [diff] [blame] | 10143 | } |
10144 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10145 | if (_wputenv(env)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10146 | posix_error(); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10147 | goto error; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10148 | } |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10149 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10150 | posix_putenv_garbage_setitem(name, unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10151 | Py_RETURN_NONE; |
10152 | |||||
10153 | error: | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10154 | Py_DECREF(unicode); |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10155 | return NULL; |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10156 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10157 | #else /* MS_WINDOWS */ |
10158 | /*[clinic input] | ||||
10159 | os.putenv | ||||
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10160 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10161 | name: FSConverter |
10162 | value: FSConverter | ||||
10163 | / | ||||
10164 | |||||
10165 | Change or add an environment variable. | ||||
10166 | [clinic start generated code]*/ | ||||
10167 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10168 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10169 | os_putenv_impl(PyObject *module, PyObject *name, PyObject *value) |
10170 | /*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10171 | { |
10172 | PyObject *bytes = NULL; | ||||
10173 | char *env; | ||||
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10174 | const char *name_string = PyBytes_AS_STRING(name); |
10175 | const char *value_string = PyBytes_AS_STRING(value); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10176 | |
Serhiy Storchaka | 7770394 | 2017-06-25 07:33:01 +0300 | [diff] [blame] | 10177 | if (strchr(name_string, '=') != NULL) { |
10178 | PyErr_SetString(PyExc_ValueError, "illegal environment variable name"); | ||||
10179 | return NULL; | ||||
10180 | } | ||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 10181 | if (PySys_Audit("os.putenv", "OO", name, value) < 0) { |
10182 | return NULL; | ||||
10183 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10184 | bytes = PyBytes_FromFormat("%s=%s", name_string, value_string); |
10185 | if (bytes == NULL) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10186 | return NULL; |
10187 | } | ||||
10188 | |||||
10189 | env = PyBytes_AS_STRING(bytes); | ||||
10190 | if (putenv(env)) { | ||||
10191 | Py_DECREF(bytes); | ||||
10192 | return posix_error(); | ||||
10193 | } | ||||
10194 | |||||
10195 | posix_putenv_garbage_setitem(name, bytes); | ||||
10196 | Py_RETURN_NONE; | ||||
10197 | } | ||||
10198 | #endif /* MS_WINDOWS */ | ||||
10199 | #endif /* HAVE_PUTENV */ | ||||
10200 | |||||
10201 | |||||
10202 | #ifdef HAVE_UNSETENV | ||||
10203 | /*[clinic input] | ||||
10204 | os.unsetenv | ||||
10205 | name: FSConverter | ||||
10206 | / | ||||
10207 | |||||
10208 | Delete an environment variable. | ||||
10209 | [clinic start generated code]*/ | ||||
10210 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10211 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10212 | os_unsetenv_impl(PyObject *module, PyObject *name) |
10213 | /*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10214 | { |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10215 | #ifndef HAVE_BROKEN_UNSETENV |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10216 | int err; |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10217 | #endif |
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10218 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 10219 | if (PySys_Audit("os.unsetenv", "(O)", name) < 0) { |
10220 | return NULL; | ||||
10221 | } | ||||
10222 | |||||
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10223 | #ifdef HAVE_BROKEN_UNSETENV |
10224 | unsetenv(PyBytes_AS_STRING(name)); | ||||
10225 | #else | ||||
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10226 | err = unsetenv(PyBytes_AS_STRING(name)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10227 | if (err) |
Victor Stinner | 60b385e | 2011-11-22 22:01:28 +0100 | [diff] [blame] | 10228 | return posix_error(); |
Victor Stinner | 984890f | 2011-11-24 13:53:38 +0100 | [diff] [blame] | 10229 | #endif |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10230 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10231 | /* Remove the key from posix_putenv_garbage; |
10232 | * this will cause it to be collected. This has to | ||||
10233 | * happen after the real unsetenv() call because the | ||||
10234 | * old value was still accessible until then. | ||||
10235 | */ | ||||
Victor Stinner | 6517095 | 2011-11-22 22:16:17 +0100 | [diff] [blame] | 10236 | if (PyDict_DelItem(posix_putenv_garbage, name)) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10237 | /* really not much we can do; just leak */ |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 10238 | if (!PyErr_ExceptionMatches(PyExc_KeyError)) { |
10239 | return NULL; | ||||
10240 | } | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10241 | PyErr_Clear(); |
10242 | } | ||||
Victor Stinner | 84ae118 | 2010-05-06 22:05:07 +0000 | [diff] [blame] | 10243 | Py_RETURN_NONE; |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10244 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10245 | #endif /* HAVE_UNSETENV */ |
Guido van Rossum | c524d95 | 2001-10-19 01:31:59 +0000 | [diff] [blame] | 10246 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10247 | |
10248 | /*[clinic input] | ||||
10249 | os.strerror | ||||
10250 | |||||
10251 | code: int | ||||
10252 | / | ||||
10253 | |||||
10254 | Translate an error code to a message string. | ||||
10255 | [clinic start generated code]*/ | ||||
10256 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10257 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10258 | os_strerror_impl(PyObject *module, int code) |
10259 | /*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10260 | { |
10261 | char *message = strerror(code); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10262 | if (message == NULL) { |
10263 | PyErr_SetString(PyExc_ValueError, | ||||
10264 | "strerror() argument out of range"); | ||||
10265 | return NULL; | ||||
10266 | } | ||||
Victor Stinner | 1b57967 | 2011-12-17 05:47:23 +0100 | [diff] [blame] | 10267 | return PyUnicode_DecodeLocale(message, "surrogateescape"); |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10268 | } |
Guido van Rossum | b6a4716 | 1997-09-15 22:54:34 +0000 | [diff] [blame] | 10269 | |
Guido van Rossum | f1af3fe | 1996-07-23 19:18:10 +0000 | [diff] [blame] | 10270 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10271 | #ifdef HAVE_SYS_WAIT_H |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10272 | #ifdef WCOREDUMP |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10273 | /*[clinic input] |
10274 | os.WCOREDUMP -> bool | ||||
10275 | |||||
10276 | status: int | ||||
10277 | / | ||||
10278 | |||||
10279 | Return True if the process returning status was dumped to a core file. | ||||
10280 | [clinic start generated code]*/ | ||||
10281 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10282 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10283 | os_WCOREDUMP_impl(PyObject *module, int status) |
10284 | /*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10285 | { |
10286 | WAIT_TYPE wait_status; | ||||
10287 | WAIT_STATUS_INT(wait_status) = status; | ||||
10288 | return WCOREDUMP(wait_status); | ||||
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10289 | } |
10290 | #endif /* WCOREDUMP */ | ||||
10291 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10292 | |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10293 | #ifdef WIFCONTINUED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10294 | /*[clinic input] |
10295 | os.WIFCONTINUED -> bool | ||||
10296 | |||||
10297 | status: int | ||||
10298 | |||||
10299 | Return True if a particular process was continued from a job control stop. | ||||
10300 | |||||
10301 | Return True if the process returning status was continued from a | ||||
10302 | job control stop. | ||||
10303 | [clinic start generated code]*/ | ||||
10304 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10305 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10306 | os_WIFCONTINUED_impl(PyObject *module, int status) |
10307 | /*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10308 | { |
10309 | WAIT_TYPE wait_status; | ||||
10310 | WAIT_STATUS_INT(wait_status) = status; | ||||
10311 | return WIFCONTINUED(wait_status); | ||||
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 10312 | } |
10313 | #endif /* WIFCONTINUED */ | ||||
10314 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10315 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10316 | #ifdef WIFSTOPPED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10317 | /*[clinic input] |
10318 | os.WIFSTOPPED -> bool | ||||
10319 | |||||
10320 | status: int | ||||
10321 | |||||
10322 | Return True if the process returning status was stopped. | ||||
10323 | [clinic start generated code]*/ | ||||
10324 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10325 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10326 | os_WIFSTOPPED_impl(PyObject *module, int status) |
10327 | /*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10328 | { |
10329 | WAIT_TYPE wait_status; | ||||
10330 | WAIT_STATUS_INT(wait_status) = status; | ||||
10331 | return WIFSTOPPED(wait_status); | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10332 | } |
10333 | #endif /* WIFSTOPPED */ | ||||
10334 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10335 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10336 | #ifdef WIFSIGNALED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10337 | /*[clinic input] |
10338 | os.WIFSIGNALED -> bool | ||||
10339 | |||||
10340 | status: int | ||||
10341 | |||||
10342 | Return True if the process returning status was terminated by a signal. | ||||
10343 | [clinic start generated code]*/ | ||||
10344 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10345 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10346 | os_WIFSIGNALED_impl(PyObject *module, int status) |
10347 | /*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10348 | { |
10349 | WAIT_TYPE wait_status; | ||||
10350 | WAIT_STATUS_INT(wait_status) = status; | ||||
10351 | return WIFSIGNALED(wait_status); | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10352 | } |
10353 | #endif /* WIFSIGNALED */ | ||||
10354 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10355 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10356 | #ifdef WIFEXITED |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10357 | /*[clinic input] |
10358 | os.WIFEXITED -> bool | ||||
10359 | |||||
10360 | status: int | ||||
10361 | |||||
10362 | Return True if the process returning status exited via the exit() system call. | ||||
10363 | [clinic start generated code]*/ | ||||
10364 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10365 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10366 | os_WIFEXITED_impl(PyObject *module, int status) |
10367 | /*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10368 | { |
10369 | WAIT_TYPE wait_status; | ||||
10370 | WAIT_STATUS_INT(wait_status) = status; | ||||
10371 | return WIFEXITED(wait_status); | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10372 | } |
10373 | #endif /* WIFEXITED */ | ||||
10374 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10375 | |
Guido van Rossum | 54ecc3d | 1999-01-27 17:53:11 +0000 | [diff] [blame] | 10376 | #ifdef WEXITSTATUS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10377 | /*[clinic input] |
10378 | os.WEXITSTATUS -> int | ||||
10379 | |||||
10380 | status: int | ||||
10381 | |||||
10382 | Return the process return code from status. | ||||
10383 | [clinic start generated code]*/ | ||||
10384 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10385 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10386 | os_WEXITSTATUS_impl(PyObject *module, int status) |
10387 | /*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10388 | { |
10389 | WAIT_TYPE wait_status; | ||||
10390 | WAIT_STATUS_INT(wait_status) = status; | ||||
10391 | return WEXITSTATUS(wait_status); | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10392 | } |
10393 | #endif /* WEXITSTATUS */ | ||||
10394 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10395 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10396 | #ifdef WTERMSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10397 | /*[clinic input] |
10398 | os.WTERMSIG -> int | ||||
10399 | |||||
10400 | status: int | ||||
10401 | |||||
10402 | Return the signal that terminated the process that provided the status value. | ||||
10403 | [clinic start generated code]*/ | ||||
10404 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10405 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10406 | os_WTERMSIG_impl(PyObject *module, int status) |
10407 | /*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10408 | { |
10409 | WAIT_TYPE wait_status; | ||||
10410 | WAIT_STATUS_INT(wait_status) = status; | ||||
10411 | return WTERMSIG(wait_status); | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10412 | } |
10413 | #endif /* WTERMSIG */ | ||||
10414 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10415 | |
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10416 | #ifdef WSTOPSIG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10417 | /*[clinic input] |
10418 | os.WSTOPSIG -> int | ||||
10419 | |||||
10420 | status: int | ||||
10421 | |||||
10422 | Return the signal that stopped the process that provided the status value. | ||||
10423 | [clinic start generated code]*/ | ||||
10424 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10425 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10426 | os_WSTOPSIG_impl(PyObject *module, int status) |
10427 | /*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10428 | { |
10429 | WAIT_TYPE wait_status; | ||||
10430 | WAIT_STATUS_INT(wait_status) = status; | ||||
10431 | return WSTOPSIG(wait_status); | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10432 | } |
10433 | #endif /* WSTOPSIG */ | ||||
Guido van Rossum | c964179 | 1998-08-04 15:26:23 +0000 | [diff] [blame] | 10434 | #endif /* HAVE_SYS_WAIT_H */ |
10435 | |||||
10436 | |||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10437 | #if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | d5753e1 | 1999-10-19 13:29:23 +0000 | [diff] [blame] | 10438 | #ifdef _SCO_DS |
10439 | /* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the | ||||
10440 | needed definitions in sys/statvfs.h */ | ||||
10441 | #define _SVID3 | ||||
10442 | #endif | ||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10443 | #include <sys/statvfs.h> |
10444 | |||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10445 | static PyObject* |
10446 | _pystatvfs_fromstructstatvfs(struct statvfs st) { | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 10447 | PyObject *v = PyStructSequence_New(StatVFSResultType); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10448 | if (v == NULL) |
10449 | return NULL; | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10450 | |
10451 | #if !defined(HAVE_LARGEFILE_SUPPORT) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10452 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
10453 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); | ||||
10454 | PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks)); | ||||
10455 | PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree)); | ||||
10456 | PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail)); | ||||
10457 | PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files)); | ||||
10458 | PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree)); | ||||
10459 | PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail)); | ||||
10460 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); | ||||
10461 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10462 | #else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10463 | PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize)); |
10464 | PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize)); | ||||
10465 | PyStructSequence_SET_ITEM(v, 2, | ||||
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10466 | PyLong_FromLongLong((long long) st.f_blocks)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10467 | PyStructSequence_SET_ITEM(v, 3, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10468 | PyLong_FromLongLong((long long) st.f_bfree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10469 | PyStructSequence_SET_ITEM(v, 4, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10470 | PyLong_FromLongLong((long long) st.f_bavail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10471 | PyStructSequence_SET_ITEM(v, 5, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10472 | PyLong_FromLongLong((long long) st.f_files)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10473 | PyStructSequence_SET_ITEM(v, 6, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10474 | PyLong_FromLongLong((long long) st.f_ffree)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10475 | PyStructSequence_SET_ITEM(v, 7, |
Benjamin Peterson | af580df | 2016-09-06 10:46:49 -0700 | [diff] [blame] | 10476 | PyLong_FromLongLong((long long) st.f_favail)); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10477 | PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag)); |
10478 | PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax)); | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10479 | #endif |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10480 | /* The _ALL_SOURCE feature test macro defines f_fsid as a structure |
10481 | * (issue #32390). */ | ||||
10482 | #if defined(_AIX) && defined(_ALL_SOURCE) | ||||
10483 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0])); | ||||
10484 | #else | ||||
Giuseppe Scrivano | 96a5e50 | 2017-12-14 23:46:46 +0100 | [diff] [blame] | 10485 | PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid)); |
Michael Felt | 502d551 | 2018-01-05 13:01:58 +0100 | [diff] [blame] | 10486 | #endif |
Victor Stinner | f0a7bac | 2013-10-30 18:55:24 +0100 | [diff] [blame] | 10487 | if (PyErr_Occurred()) { |
10488 | Py_DECREF(v); | ||||
10489 | return NULL; | ||||
10490 | } | ||||
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10491 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10492 | return v; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10493 | } |
10494 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10495 | |
10496 | /*[clinic input] | ||||
10497 | os.fstatvfs | ||||
10498 | fd: int | ||||
10499 | / | ||||
10500 | |||||
10501 | Perform an fstatvfs system call on the given fd. | ||||
10502 | |||||
10503 | Equivalent to statvfs(fd). | ||||
10504 | [clinic start generated code]*/ | ||||
10505 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10506 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10507 | os_fstatvfs_impl(PyObject *module, int fd) |
10508 | /*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10509 | { |
10510 | int result; | ||||
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10511 | int async_err = 0; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10512 | struct statvfs st; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10513 | |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10514 | do { |
10515 | Py_BEGIN_ALLOW_THREADS | ||||
10516 | result = fstatvfs(fd, &st); | ||||
10517 | Py_END_ALLOW_THREADS | ||||
10518 | } while (result != 0 && errno == EINTR && | ||||
10519 | !(async_err = PyErr_CheckSignals())); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10520 | if (result != 0) |
Charles-François Natali | 6e6c59b | 2015-02-07 13:27:50 +0000 | [diff] [blame] | 10521 | return (!async_err) ? posix_error() : NULL; |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 10522 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10523 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10524 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10525 | #endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10526 | |
10527 | |||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 10528 | #if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10529 | #include <sys/statvfs.h> |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10530 | /*[clinic input] |
10531 | os.statvfs | ||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10532 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10533 | path: path_t(allow_fd='PATH_HAVE_FSTATVFS') |
10534 | |||||
10535 | Perform a statvfs system call on the given path. | ||||
10536 | |||||
10537 | path may always be specified as a string. | ||||
10538 | On some platforms, path may also be specified as an open file descriptor. | ||||
10539 | If this functionality is unavailable, using it raises an exception. | ||||
10540 | [clinic start generated code]*/ | ||||
10541 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10542 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10543 | os_statvfs_impl(PyObject *module, path_t *path) |
10544 | /*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10545 | { |
10546 | int result; | ||||
10547 | struct statvfs st; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10548 | |
10549 | Py_BEGIN_ALLOW_THREADS | ||||
10550 | #ifdef HAVE_FSTATVFS | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10551 | if (path->fd != -1) { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10552 | #ifdef __APPLE__ |
10553 | /* handle weak-linking on Mac OS X 10.3 */ | ||||
10554 | if (fstatvfs == NULL) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10555 | fd_specified("statvfs", path->fd); |
10556 | return NULL; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10557 | } |
10558 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10559 | result = fstatvfs(path->fd, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10560 | } |
10561 | else | ||||
10562 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10563 | result = statvfs(path->narrow, &st); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10564 | Py_END_ALLOW_THREADS |
10565 | |||||
10566 | if (result) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10567 | return path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 10568 | } |
10569 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10570 | return _pystatvfs_fromstructstatvfs(st); |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10571 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10572 | #endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */ |
10573 | |||||
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 10574 | |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10575 | #ifdef MS_WINDOWS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10576 | /*[clinic input] |
10577 | os._getdiskusage | ||||
10578 | |||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10579 | path: path_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10580 | |
10581 | Return disk usage statistics about the given path as a (total, free) tuple. | ||||
10582 | [clinic start generated code]*/ | ||||
10583 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10584 | static PyObject * |
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10585 | os__getdiskusage_impl(PyObject *module, path_t *path) |
10586 | /*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/ | ||||
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10587 | { |
10588 | BOOL retval; | ||||
10589 | ULARGE_INTEGER _, total, free; | ||||
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10590 | DWORD err = 0; |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10591 | |
10592 | Py_BEGIN_ALLOW_THREADS | ||||
Steve Dower | 23ad6d0 | 2018-02-22 10:39:10 -0800 | [diff] [blame] | 10593 | retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free); |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10594 | Py_END_ALLOW_THREADS |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10595 | if (retval == 0) { |
10596 | if (GetLastError() == ERROR_DIRECTORY) { | ||||
10597 | wchar_t *dir_path = NULL; | ||||
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10598 | |
Joe Pamer | c8c0249 | 2018-09-25 10:57:36 -0400 | [diff] [blame] | 10599 | dir_path = PyMem_New(wchar_t, path->length + 1); |
10600 | if (dir_path == NULL) { | ||||
10601 | return PyErr_NoMemory(); | ||||
10602 | } | ||||
10603 | |||||
10604 | wcscpy_s(dir_path, path->length + 1, path->wide); | ||||
10605 | |||||
10606 | if (_dirnameW(dir_path) != -1) { | ||||
10607 | Py_BEGIN_ALLOW_THREADS | ||||
10608 | retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free); | ||||
10609 | Py_END_ALLOW_THREADS | ||||
10610 | } | ||||
10611 | /* Record the last error in case it's modified by PyMem_Free. */ | ||||
10612 | err = GetLastError(); | ||||
10613 | PyMem_Free(dir_path); | ||||
10614 | if (retval) { | ||||
10615 | goto success; | ||||
10616 | } | ||||
10617 | } | ||||
10618 | return PyErr_SetFromWindowsErr(err); | ||||
10619 | } | ||||
10620 | |||||
10621 | success: | ||||
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10622 | return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart); |
10623 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10624 | #endif /* MS_WINDOWS */ |
Giampaolo Rodola' | 210e7ca | 2011-07-01 13:55:36 +0200 | [diff] [blame] | 10625 | |
10626 | |||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10627 | /* This is used for fpathconf(), pathconf(), confstr() and sysconf(). |
10628 | * It maps strings representing configuration variable names to | ||||
10629 | * integer values, allowing those functions to be called with the | ||||
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 10630 | * magic names instead of polluting the module's namespace with tons of |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10631 | * rarely-used constants. There are three separate tables that use |
10632 | * these definitions. | ||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 10633 | * |
10634 | * This code is always included, even if none of the interfaces that | ||||
10635 | * need it are included. The #if hackery needed to avoid it would be | ||||
10636 | * sufficiently pervasive that it's not worth the loss of readability. | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10637 | */ |
10638 | struct constdef { | ||||
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 10639 | const char *name; |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10640 | int value; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10641 | }; |
10642 | |||||
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10643 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10644 | conv_confname(PyObject *arg, int *valuep, struct constdef *table, |
Guido van Rossum | 7d5baac | 2007-08-27 23:24:46 +0000 | [diff] [blame] | 10645 | size_t tablesize) |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10646 | { |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 10647 | if (PyLong_Check(arg)) { |
Serhiy Storchaka | 56f6e76 | 2015-09-06 21:25:30 +0300 | [diff] [blame] | 10648 | int value = _PyLong_AsInt(arg); |
10649 | if (value == -1 && PyErr_Occurred()) | ||||
10650 | return 0; | ||||
10651 | *valuep = value; | ||||
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10652 | return 1; |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10653 | } |
Guido van Rossum | bce56a6 | 2007-05-10 18:04:33 +0000 | [diff] [blame] | 10654 | else { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10655 | /* look up the value in the table using a binary search */ |
10656 | size_t lo = 0; | ||||
10657 | size_t mid; | ||||
10658 | size_t hi = tablesize; | ||||
10659 | int cmp; | ||||
10660 | const char *confname; | ||||
10661 | if (!PyUnicode_Check(arg)) { | ||||
10662 | PyErr_SetString(PyExc_TypeError, | ||||
10663 | "configuration names must be strings or integers"); | ||||
10664 | return 0; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10665 | } |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 10666 | confname = PyUnicode_AsUTF8(arg); |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 10667 | if (confname == NULL) |
10668 | return 0; | ||||
10669 | while (lo < hi) { | ||||
10670 | mid = (lo + hi) / 2; | ||||
10671 | cmp = strcmp(confname, table[mid].name); | ||||
10672 | if (cmp < 0) | ||||
10673 | hi = mid; | ||||
10674 | else if (cmp > 0) | ||||
10675 | lo = mid + 1; | ||||
10676 | else { | ||||
10677 | *valuep = table[mid].value; | ||||
10678 | return 1; | ||||
10679 | } | ||||
10680 | } | ||||
10681 | PyErr_SetString(PyExc_ValueError, "unrecognized configuration name"); | ||||
10682 | return 0; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10683 | } |
Fred Drake | 12c6e2d | 1999-12-14 21:25:03 +0000 | [diff] [blame] | 10684 | } |
10685 | |||||
10686 | |||||
10687 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) | ||||
10688 | static struct constdef posix_constants_pathconf[] = { | ||||
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10689 | #ifdef _PC_ABI_AIO_XFER_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10690 | {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10691 | #endif |
10692 | #ifdef _PC_ABI_ASYNC_IO | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10693 | {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10694 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10695 | #ifdef _PC_ASYNC_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10696 | {"PC_ASYNC_IO", _PC_ASYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10697 | #endif |
10698 | #ifdef _PC_CHOWN_RESTRICTED | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10699 | {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10700 | #endif |
10701 | #ifdef _PC_FILESIZEBITS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10702 | {"PC_FILESIZEBITS", _PC_FILESIZEBITS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10703 | #endif |
10704 | #ifdef _PC_LAST | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10705 | {"PC_LAST", _PC_LAST}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10706 | #endif |
10707 | #ifdef _PC_LINK_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10708 | {"PC_LINK_MAX", _PC_LINK_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10709 | #endif |
10710 | #ifdef _PC_MAX_CANON | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10711 | {"PC_MAX_CANON", _PC_MAX_CANON}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10712 | #endif |
10713 | #ifdef _PC_MAX_INPUT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10714 | {"PC_MAX_INPUT", _PC_MAX_INPUT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10715 | #endif |
10716 | #ifdef _PC_NAME_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10717 | {"PC_NAME_MAX", _PC_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10718 | #endif |
10719 | #ifdef _PC_NO_TRUNC | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10720 | {"PC_NO_TRUNC", _PC_NO_TRUNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10721 | #endif |
10722 | #ifdef _PC_PATH_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10723 | {"PC_PATH_MAX", _PC_PATH_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10724 | #endif |
10725 | #ifdef _PC_PIPE_BUF | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10726 | {"PC_PIPE_BUF", _PC_PIPE_BUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10727 | #endif |
10728 | #ifdef _PC_PRIO_IO | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10729 | {"PC_PRIO_IO", _PC_PRIO_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10730 | #endif |
10731 | #ifdef _PC_SOCK_MAXBUF | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10732 | {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10733 | #endif |
10734 | #ifdef _PC_SYNC_IO | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10735 | {"PC_SYNC_IO", _PC_SYNC_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10736 | #endif |
10737 | #ifdef _PC_VDISABLE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10738 | {"PC_VDISABLE", _PC_VDISABLE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10739 | #endif |
Jesus Cea | 7e9065c | 2010-10-25 13:02:04 +0000 | [diff] [blame] | 10740 | #ifdef _PC_ACL_ENABLED |
10741 | {"PC_ACL_ENABLED", _PC_ACL_ENABLED}, | ||||
10742 | #endif | ||||
10743 | #ifdef _PC_MIN_HOLE_SIZE | ||||
10744 | {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE}, | ||||
10745 | #endif | ||||
10746 | #ifdef _PC_ALLOC_SIZE_MIN | ||||
10747 | {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN}, | ||||
10748 | #endif | ||||
10749 | #ifdef _PC_REC_INCR_XFER_SIZE | ||||
10750 | {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE}, | ||||
10751 | #endif | ||||
10752 | #ifdef _PC_REC_MAX_XFER_SIZE | ||||
10753 | {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE}, | ||||
10754 | #endif | ||||
10755 | #ifdef _PC_REC_MIN_XFER_SIZE | ||||
10756 | {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE}, | ||||
10757 | #endif | ||||
10758 | #ifdef _PC_REC_XFER_ALIGN | ||||
10759 | {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN}, | ||||
10760 | #endif | ||||
10761 | #ifdef _PC_SYMLINK_MAX | ||||
10762 | {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX}, | ||||
10763 | #endif | ||||
10764 | #ifdef _PC_XATTR_ENABLED | ||||
10765 | {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED}, | ||||
10766 | #endif | ||||
10767 | #ifdef _PC_XATTR_EXISTS | ||||
10768 | {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS}, | ||||
10769 | #endif | ||||
10770 | #ifdef _PC_TIMESTAMP_RESOLUTION | ||||
10771 | {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION}, | ||||
10772 | #endif | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10773 | }; |
10774 | |||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10775 | static int |
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 10776 | conv_path_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10777 | { |
10778 | return conv_confname(arg, valuep, posix_constants_pathconf, | ||||
10779 | sizeof(posix_constants_pathconf) | ||||
10780 | / sizeof(struct constdef)); | ||||
10781 | } | ||||
10782 | #endif | ||||
10783 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10784 | |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10785 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10786 | /*[clinic input] |
10787 | os.fpathconf -> long | ||||
10788 | |||||
10789 | fd: int | ||||
10790 | name: path_confname | ||||
10791 | / | ||||
10792 | |||||
10793 | Return the configuration limit name for the file descriptor fd. | ||||
10794 | |||||
10795 | If there is no limit, return -1. | ||||
10796 | [clinic start generated code]*/ | ||||
10797 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10798 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10799 | os_fpathconf_impl(PyObject *module, int fd, int name) |
10800 | /*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10801 | { |
10802 | long limit; | ||||
10803 | |||||
10804 | errno = 0; | ||||
10805 | limit = fpathconf(fd, name); | ||||
10806 | if (limit == -1 && errno != 0) | ||||
10807 | posix_error(); | ||||
10808 | |||||
10809 | return limit; | ||||
10810 | } | ||||
10811 | #endif /* HAVE_FPATHCONF */ | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10812 | |
10813 | |||||
10814 | #ifdef HAVE_PATHCONF | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10815 | /*[clinic input] |
10816 | os.pathconf -> long | ||||
10817 | path: path_t(allow_fd='PATH_HAVE_FPATHCONF') | ||||
10818 | name: path_confname | ||||
10819 | |||||
10820 | Return the configuration limit name for the file or directory path. | ||||
10821 | |||||
10822 | If there is no limit, return -1. | ||||
10823 | On some platforms, path may also be specified as an open file descriptor. | ||||
10824 | If this functionality is unavailable, using it raises an exception. | ||||
10825 | [clinic start generated code]*/ | ||||
10826 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10827 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 10828 | os_pathconf_impl(PyObject *module, path_t *path, int name) |
10829 | /*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10830 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10831 | long limit; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10832 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10833 | errno = 0; |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10834 | #ifdef HAVE_FPATHCONF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10835 | if (path->fd != -1) |
10836 | limit = fpathconf(path->fd, name); | ||||
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 10837 | else |
10838 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10839 | limit = pathconf(path->narrow, name); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10840 | if (limit == -1 && errno != 0) { |
10841 | if (errno == EINVAL) | ||||
Stefan Krah | 9943926 | 2010-11-26 12:58:05 +0000 | [diff] [blame] | 10842 | /* could be a path or name problem */ |
10843 | posix_error(); | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10844 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10845 | path_error(path); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10846 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10847 | |
10848 | return limit; | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10849 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 10850 | #endif /* HAVE_PATHCONF */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10851 | |
10852 | #ifdef HAVE_CONFSTR | ||||
10853 | static struct constdef posix_constants_confstr[] = { | ||||
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10854 | #ifdef _CS_ARCHITECTURE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10855 | {"CS_ARCHITECTURE", _CS_ARCHITECTURE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10856 | #endif |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10857 | #ifdef _CS_GNU_LIBC_VERSION |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10858 | {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10859 | #endif |
10860 | #ifdef _CS_GNU_LIBPTHREAD_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10861 | {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION}, |
Mark Dickinson | 876d7c8 | 2010-04-16 12:47:52 +0000 | [diff] [blame] | 10862 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10863 | #ifdef _CS_HOSTNAME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10864 | {"CS_HOSTNAME", _CS_HOSTNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10865 | #endif |
10866 | #ifdef _CS_HW_PROVIDER | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10867 | {"CS_HW_PROVIDER", _CS_HW_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10868 | #endif |
10869 | #ifdef _CS_HW_SERIAL | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10870 | {"CS_HW_SERIAL", _CS_HW_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10871 | #endif |
10872 | #ifdef _CS_INITTAB_NAME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10873 | {"CS_INITTAB_NAME", _CS_INITTAB_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10874 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10875 | #ifdef _CS_LFS64_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10876 | {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10877 | #endif |
10878 | #ifdef _CS_LFS64_LDFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10879 | {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10880 | #endif |
10881 | #ifdef _CS_LFS64_LIBS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10882 | {"CS_LFS64_LIBS", _CS_LFS64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10883 | #endif |
10884 | #ifdef _CS_LFS64_LINTFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10885 | {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10886 | #endif |
10887 | #ifdef _CS_LFS_CFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10888 | {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10889 | #endif |
10890 | #ifdef _CS_LFS_LDFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10891 | {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10892 | #endif |
10893 | #ifdef _CS_LFS_LIBS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10894 | {"CS_LFS_LIBS", _CS_LFS_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10895 | #endif |
10896 | #ifdef _CS_LFS_LINTFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10897 | {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10898 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10899 | #ifdef _CS_MACHINE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10900 | {"CS_MACHINE", _CS_MACHINE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10901 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10902 | #ifdef _CS_PATH |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10903 | {"CS_PATH", _CS_PATH}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10904 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10905 | #ifdef _CS_RELEASE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10906 | {"CS_RELEASE", _CS_RELEASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10907 | #endif |
10908 | #ifdef _CS_SRPC_DOMAIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10909 | {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10910 | #endif |
10911 | #ifdef _CS_SYSNAME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10912 | {"CS_SYSNAME", _CS_SYSNAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10913 | #endif |
10914 | #ifdef _CS_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10915 | {"CS_VERSION", _CS_VERSION}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10916 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10917 | #ifdef _CS_XBS5_ILP32_OFF32_CFLAGS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10918 | {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10919 | #endif |
10920 | #ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10921 | {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10922 | #endif |
10923 | #ifdef _CS_XBS5_ILP32_OFF32_LIBS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10924 | {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10925 | #endif |
10926 | #ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10927 | {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10928 | #endif |
10929 | #ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10930 | {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10931 | #endif |
10932 | #ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10933 | {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10934 | #endif |
10935 | #ifdef _CS_XBS5_ILP32_OFFBIG_LIBS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10936 | {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10937 | #endif |
10938 | #ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10939 | {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10940 | #endif |
10941 | #ifdef _CS_XBS5_LP64_OFF64_CFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10942 | {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10943 | #endif |
10944 | #ifdef _CS_XBS5_LP64_OFF64_LDFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10945 | {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10946 | #endif |
10947 | #ifdef _CS_XBS5_LP64_OFF64_LIBS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10948 | {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10949 | #endif |
10950 | #ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10951 | {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10952 | #endif |
10953 | #ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10954 | {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10955 | #endif |
10956 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10957 | {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10958 | #endif |
10959 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10960 | {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10961 | #endif |
10962 | #ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10963 | {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 10964 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10965 | #ifdef _MIPS_CS_AVAIL_PROCESSORS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10966 | {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10967 | #endif |
10968 | #ifdef _MIPS_CS_BASE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10969 | {"MIPS_CS_BASE", _MIPS_CS_BASE}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10970 | #endif |
10971 | #ifdef _MIPS_CS_HOSTID | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10972 | {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10973 | #endif |
10974 | #ifdef _MIPS_CS_HW_NAME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10975 | {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10976 | #endif |
10977 | #ifdef _MIPS_CS_NUM_PROCESSORS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10978 | {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10979 | #endif |
10980 | #ifdef _MIPS_CS_OSREL_MAJ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10981 | {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10982 | #endif |
10983 | #ifdef _MIPS_CS_OSREL_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10984 | {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10985 | #endif |
10986 | #ifdef _MIPS_CS_OSREL_PATCH | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10987 | {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10988 | #endif |
10989 | #ifdef _MIPS_CS_OS_NAME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10990 | {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10991 | #endif |
10992 | #ifdef _MIPS_CS_OS_PROVIDER | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10993 | {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10994 | #endif |
10995 | #ifdef _MIPS_CS_PROCESSORS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10996 | {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 10997 | #endif |
10998 | #ifdef _MIPS_CS_SERIAL | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 10999 | {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11000 | #endif |
11001 | #ifdef _MIPS_CS_VENDOR | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11002 | {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11003 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11004 | }; |
11005 | |||||
11006 | static int | ||||
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11007 | conv_confstr_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11008 | { |
11009 | return conv_confname(arg, valuep, posix_constants_confstr, | ||||
11010 | sizeof(posix_constants_confstr) | ||||
11011 | / sizeof(struct constdef)); | ||||
11012 | } | ||||
11013 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11014 | |
11015 | /*[clinic input] | ||||
11016 | os.confstr | ||||
11017 | |||||
11018 | name: confstr_confname | ||||
11019 | / | ||||
11020 | |||||
11021 | Return a string-valued system configuration variable. | ||||
11022 | [clinic start generated code]*/ | ||||
11023 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11024 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11025 | os_confstr_impl(PyObject *module, int name) |
11026 | /*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/ | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11027 | { |
11028 | PyObject *result = NULL; | ||||
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11029 | char buffer[255]; |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11030 | size_t len; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11031 | |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11032 | errno = 0; |
11033 | len = confstr(name, buffer, sizeof(buffer)); | ||||
11034 | if (len == 0) { | ||||
11035 | if (errno) { | ||||
11036 | posix_error(); | ||||
11037 | return NULL; | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11038 | } |
11039 | else { | ||||
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11040 | Py_RETURN_NONE; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11041 | } |
11042 | } | ||||
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11043 | |
Victor Stinner | dd3a6a5 | 2013-06-25 23:13:47 +0200 | [diff] [blame] | 11044 | if (len >= sizeof(buffer)) { |
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11045 | size_t len2; |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11046 | char *buf = PyMem_Malloc(len); |
11047 | if (buf == NULL) | ||||
11048 | return PyErr_NoMemory(); | ||||
Victor Stinner | cbc18f3 | 2014-12-05 22:51:51 +0100 | [diff] [blame] | 11049 | len2 = confstr(name, buf, len); |
11050 | assert(len == len2); | ||||
Christian Heimes | 8714cfd | 2015-04-21 10:57:41 +0200 | [diff] [blame] | 11051 | result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1); |
Victor Stinner | cb04352 | 2010-09-10 23:49:04 +0000 | [diff] [blame] | 11052 | PyMem_Free(buf); |
11053 | } | ||||
11054 | else | ||||
11055 | result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1); | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11056 | return result; |
11057 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11058 | #endif /* HAVE_CONFSTR */ |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11059 | |
11060 | |||||
11061 | #ifdef HAVE_SYSCONF | ||||
11062 | static struct constdef posix_constants_sysconf[] = { | ||||
11063 | #ifdef _SC_2_CHAR_TERM | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11064 | {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11065 | #endif |
11066 | #ifdef _SC_2_C_BIND | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11067 | {"SC_2_C_BIND", _SC_2_C_BIND}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11068 | #endif |
11069 | #ifdef _SC_2_C_DEV | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11070 | {"SC_2_C_DEV", _SC_2_C_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11071 | #endif |
11072 | #ifdef _SC_2_C_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11073 | {"SC_2_C_VERSION", _SC_2_C_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11074 | #endif |
11075 | #ifdef _SC_2_FORT_DEV | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11076 | {"SC_2_FORT_DEV", _SC_2_FORT_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11077 | #endif |
11078 | #ifdef _SC_2_FORT_RUN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11079 | {"SC_2_FORT_RUN", _SC_2_FORT_RUN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11080 | #endif |
11081 | #ifdef _SC_2_LOCALEDEF | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11082 | {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11083 | #endif |
11084 | #ifdef _SC_2_SW_DEV | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11085 | {"SC_2_SW_DEV", _SC_2_SW_DEV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11086 | #endif |
11087 | #ifdef _SC_2_UPE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11088 | {"SC_2_UPE", _SC_2_UPE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11089 | #endif |
11090 | #ifdef _SC_2_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11091 | {"SC_2_VERSION", _SC_2_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11092 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11093 | #ifdef _SC_ABI_ASYNCHRONOUS_IO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11094 | {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11095 | #endif |
11096 | #ifdef _SC_ACL | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11097 | {"SC_ACL", _SC_ACL}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11098 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11099 | #ifdef _SC_AIO_LISTIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11100 | {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11101 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11102 | #ifdef _SC_AIO_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11103 | {"SC_AIO_MAX", _SC_AIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11104 | #endif |
11105 | #ifdef _SC_AIO_PRIO_DELTA_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11106 | {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11107 | #endif |
11108 | #ifdef _SC_ARG_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11109 | {"SC_ARG_MAX", _SC_ARG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11110 | #endif |
11111 | #ifdef _SC_ASYNCHRONOUS_IO | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11112 | {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11113 | #endif |
11114 | #ifdef _SC_ATEXIT_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11115 | {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11116 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11117 | #ifdef _SC_AUDIT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11118 | {"SC_AUDIT", _SC_AUDIT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11119 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11120 | #ifdef _SC_AVPHYS_PAGES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11121 | {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11122 | #endif |
11123 | #ifdef _SC_BC_BASE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11124 | {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11125 | #endif |
11126 | #ifdef _SC_BC_DIM_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11127 | {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11128 | #endif |
11129 | #ifdef _SC_BC_SCALE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11130 | {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11131 | #endif |
11132 | #ifdef _SC_BC_STRING_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11133 | {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11134 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11135 | #ifdef _SC_CAP |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11136 | {"SC_CAP", _SC_CAP}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11137 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11138 | #ifdef _SC_CHARCLASS_NAME_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11139 | {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11140 | #endif |
11141 | #ifdef _SC_CHAR_BIT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11142 | {"SC_CHAR_BIT", _SC_CHAR_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11143 | #endif |
11144 | #ifdef _SC_CHAR_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11145 | {"SC_CHAR_MAX", _SC_CHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11146 | #endif |
11147 | #ifdef _SC_CHAR_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11148 | {"SC_CHAR_MIN", _SC_CHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11149 | #endif |
11150 | #ifdef _SC_CHILD_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11151 | {"SC_CHILD_MAX", _SC_CHILD_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11152 | #endif |
11153 | #ifdef _SC_CLK_TCK | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11154 | {"SC_CLK_TCK", _SC_CLK_TCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11155 | #endif |
11156 | #ifdef _SC_COHER_BLKSZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11157 | {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11158 | #endif |
11159 | #ifdef _SC_COLL_WEIGHTS_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11160 | {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11161 | #endif |
11162 | #ifdef _SC_DCACHE_ASSOC | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11163 | {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11164 | #endif |
11165 | #ifdef _SC_DCACHE_BLKSZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11166 | {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11167 | #endif |
11168 | #ifdef _SC_DCACHE_LINESZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11169 | {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11170 | #endif |
11171 | #ifdef _SC_DCACHE_SZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11172 | {"SC_DCACHE_SZ", _SC_DCACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11173 | #endif |
11174 | #ifdef _SC_DCACHE_TBLKSZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11175 | {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11176 | #endif |
11177 | #ifdef _SC_DELAYTIMER_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11178 | {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11179 | #endif |
11180 | #ifdef _SC_EQUIV_CLASS_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11181 | {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11182 | #endif |
11183 | #ifdef _SC_EXPR_NEST_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11184 | {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11185 | #endif |
11186 | #ifdef _SC_FSYNC | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11187 | {"SC_FSYNC", _SC_FSYNC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11188 | #endif |
11189 | #ifdef _SC_GETGR_R_SIZE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11190 | {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11191 | #endif |
11192 | #ifdef _SC_GETPW_R_SIZE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11193 | {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11194 | #endif |
11195 | #ifdef _SC_ICACHE_ASSOC | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11196 | {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11197 | #endif |
11198 | #ifdef _SC_ICACHE_BLKSZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11199 | {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11200 | #endif |
11201 | #ifdef _SC_ICACHE_LINESZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11202 | {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11203 | #endif |
11204 | #ifdef _SC_ICACHE_SZ | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11205 | {"SC_ICACHE_SZ", _SC_ICACHE_SZ}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11206 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11207 | #ifdef _SC_INF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11208 | {"SC_INF", _SC_INF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11209 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11210 | #ifdef _SC_INT_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11211 | {"SC_INT_MAX", _SC_INT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11212 | #endif |
11213 | #ifdef _SC_INT_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11214 | {"SC_INT_MIN", _SC_INT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11215 | #endif |
11216 | #ifdef _SC_IOV_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11217 | {"SC_IOV_MAX", _SC_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11218 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11219 | #ifdef _SC_IP_SECOPTS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11220 | {"SC_IP_SECOPTS", _SC_IP_SECOPTS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11221 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11222 | #ifdef _SC_JOB_CONTROL |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11223 | {"SC_JOB_CONTROL", _SC_JOB_CONTROL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11224 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11225 | #ifdef _SC_KERN_POINTERS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11226 | {"SC_KERN_POINTERS", _SC_KERN_POINTERS}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11227 | #endif |
11228 | #ifdef _SC_KERN_SIM | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11229 | {"SC_KERN_SIM", _SC_KERN_SIM}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11230 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11231 | #ifdef _SC_LINE_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11232 | {"SC_LINE_MAX", _SC_LINE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11233 | #endif |
11234 | #ifdef _SC_LOGIN_NAME_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11235 | {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11236 | #endif |
11237 | #ifdef _SC_LOGNAME_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11238 | {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11239 | #endif |
11240 | #ifdef _SC_LONG_BIT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11241 | {"SC_LONG_BIT", _SC_LONG_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11242 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11243 | #ifdef _SC_MAC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11244 | {"SC_MAC", _SC_MAC}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11245 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11246 | #ifdef _SC_MAPPED_FILES |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11247 | {"SC_MAPPED_FILES", _SC_MAPPED_FILES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11248 | #endif |
11249 | #ifdef _SC_MAXPID | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11250 | {"SC_MAXPID", _SC_MAXPID}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11251 | #endif |
11252 | #ifdef _SC_MB_LEN_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11253 | {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11254 | #endif |
11255 | #ifdef _SC_MEMLOCK | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11256 | {"SC_MEMLOCK", _SC_MEMLOCK}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11257 | #endif |
11258 | #ifdef _SC_MEMLOCK_RANGE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11259 | {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11260 | #endif |
11261 | #ifdef _SC_MEMORY_PROTECTION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11262 | {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11263 | #endif |
11264 | #ifdef _SC_MESSAGE_PASSING | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11265 | {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11266 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11267 | #ifdef _SC_MMAP_FIXED_ALIGNMENT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11268 | {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11269 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11270 | #ifdef _SC_MQ_OPEN_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11271 | {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11272 | #endif |
11273 | #ifdef _SC_MQ_PRIO_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11274 | {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11275 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11276 | #ifdef _SC_NACLS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11277 | {"SC_NACLS_MAX", _SC_NACLS_MAX}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11278 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11279 | #ifdef _SC_NGROUPS_MAX |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11280 | {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11281 | #endif |
11282 | #ifdef _SC_NL_ARGMAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11283 | {"SC_NL_ARGMAX", _SC_NL_ARGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11284 | #endif |
11285 | #ifdef _SC_NL_LANGMAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11286 | {"SC_NL_LANGMAX", _SC_NL_LANGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11287 | #endif |
11288 | #ifdef _SC_NL_MSGMAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11289 | {"SC_NL_MSGMAX", _SC_NL_MSGMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11290 | #endif |
11291 | #ifdef _SC_NL_NMAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11292 | {"SC_NL_NMAX", _SC_NL_NMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11293 | #endif |
11294 | #ifdef _SC_NL_SETMAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11295 | {"SC_NL_SETMAX", _SC_NL_SETMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11296 | #endif |
11297 | #ifdef _SC_NL_TEXTMAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11298 | {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11299 | #endif |
11300 | #ifdef _SC_NPROCESSORS_CONF | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11301 | {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11302 | #endif |
11303 | #ifdef _SC_NPROCESSORS_ONLN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11304 | {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11305 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11306 | #ifdef _SC_NPROC_CONF |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11307 | {"SC_NPROC_CONF", _SC_NPROC_CONF}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11308 | #endif |
11309 | #ifdef _SC_NPROC_ONLN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11310 | {"SC_NPROC_ONLN", _SC_NPROC_ONLN}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11311 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11312 | #ifdef _SC_NZERO |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11313 | {"SC_NZERO", _SC_NZERO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11314 | #endif |
11315 | #ifdef _SC_OPEN_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11316 | {"SC_OPEN_MAX", _SC_OPEN_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11317 | #endif |
11318 | #ifdef _SC_PAGESIZE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11319 | {"SC_PAGESIZE", _SC_PAGESIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11320 | #endif |
11321 | #ifdef _SC_PAGE_SIZE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11322 | {"SC_PAGE_SIZE", _SC_PAGE_SIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11323 | #endif |
11324 | #ifdef _SC_PASS_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11325 | {"SC_PASS_MAX", _SC_PASS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11326 | #endif |
11327 | #ifdef _SC_PHYS_PAGES | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11328 | {"SC_PHYS_PAGES", _SC_PHYS_PAGES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11329 | #endif |
11330 | #ifdef _SC_PII | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11331 | {"SC_PII", _SC_PII}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11332 | #endif |
11333 | #ifdef _SC_PII_INTERNET | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11334 | {"SC_PII_INTERNET", _SC_PII_INTERNET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11335 | #endif |
11336 | #ifdef _SC_PII_INTERNET_DGRAM | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11337 | {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11338 | #endif |
11339 | #ifdef _SC_PII_INTERNET_STREAM | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11340 | {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11341 | #endif |
11342 | #ifdef _SC_PII_OSI | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11343 | {"SC_PII_OSI", _SC_PII_OSI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11344 | #endif |
11345 | #ifdef _SC_PII_OSI_CLTS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11346 | {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11347 | #endif |
11348 | #ifdef _SC_PII_OSI_COTS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11349 | {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11350 | #endif |
11351 | #ifdef _SC_PII_OSI_M | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11352 | {"SC_PII_OSI_M", _SC_PII_OSI_M}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11353 | #endif |
11354 | #ifdef _SC_PII_SOCKET | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11355 | {"SC_PII_SOCKET", _SC_PII_SOCKET}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11356 | #endif |
11357 | #ifdef _SC_PII_XTI | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11358 | {"SC_PII_XTI", _SC_PII_XTI}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11359 | #endif |
11360 | #ifdef _SC_POLL | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11361 | {"SC_POLL", _SC_POLL}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11362 | #endif |
11363 | #ifdef _SC_PRIORITIZED_IO | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11364 | {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11365 | #endif |
11366 | #ifdef _SC_PRIORITY_SCHEDULING | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11367 | {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11368 | #endif |
11369 | #ifdef _SC_REALTIME_SIGNALS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11370 | {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11371 | #endif |
11372 | #ifdef _SC_RE_DUP_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11373 | {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11374 | #endif |
11375 | #ifdef _SC_RTSIG_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11376 | {"SC_RTSIG_MAX", _SC_RTSIG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11377 | #endif |
11378 | #ifdef _SC_SAVED_IDS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11379 | {"SC_SAVED_IDS", _SC_SAVED_IDS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11380 | #endif |
11381 | #ifdef _SC_SCHAR_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11382 | {"SC_SCHAR_MAX", _SC_SCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11383 | #endif |
11384 | #ifdef _SC_SCHAR_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11385 | {"SC_SCHAR_MIN", _SC_SCHAR_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11386 | #endif |
11387 | #ifdef _SC_SELECT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11388 | {"SC_SELECT", _SC_SELECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11389 | #endif |
11390 | #ifdef _SC_SEMAPHORES | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11391 | {"SC_SEMAPHORES", _SC_SEMAPHORES}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11392 | #endif |
11393 | #ifdef _SC_SEM_NSEMS_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11394 | {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11395 | #endif |
11396 | #ifdef _SC_SEM_VALUE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11397 | {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11398 | #endif |
11399 | #ifdef _SC_SHARED_MEMORY_OBJECTS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11400 | {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11401 | #endif |
11402 | #ifdef _SC_SHRT_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11403 | {"SC_SHRT_MAX", _SC_SHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11404 | #endif |
11405 | #ifdef _SC_SHRT_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11406 | {"SC_SHRT_MIN", _SC_SHRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11407 | #endif |
11408 | #ifdef _SC_SIGQUEUE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11409 | {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11410 | #endif |
11411 | #ifdef _SC_SIGRT_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11412 | {"SC_SIGRT_MAX", _SC_SIGRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11413 | #endif |
11414 | #ifdef _SC_SIGRT_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11415 | {"SC_SIGRT_MIN", _SC_SIGRT_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11416 | #endif |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11417 | #ifdef _SC_SOFTPOWER |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11418 | {"SC_SOFTPOWER", _SC_SOFTPOWER}, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11419 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11420 | #ifdef _SC_SPLIT_CACHE |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11421 | {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11422 | #endif |
11423 | #ifdef _SC_SSIZE_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11424 | {"SC_SSIZE_MAX", _SC_SSIZE_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11425 | #endif |
11426 | #ifdef _SC_STACK_PROT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11427 | {"SC_STACK_PROT", _SC_STACK_PROT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11428 | #endif |
11429 | #ifdef _SC_STREAM_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11430 | {"SC_STREAM_MAX", _SC_STREAM_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11431 | #endif |
11432 | #ifdef _SC_SYNCHRONIZED_IO | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11433 | {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11434 | #endif |
11435 | #ifdef _SC_THREADS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11436 | {"SC_THREADS", _SC_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11437 | #endif |
11438 | #ifdef _SC_THREAD_ATTR_STACKADDR | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11439 | {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11440 | #endif |
11441 | #ifdef _SC_THREAD_ATTR_STACKSIZE | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11442 | {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11443 | #endif |
11444 | #ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11445 | {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11446 | #endif |
11447 | #ifdef _SC_THREAD_KEYS_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11448 | {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11449 | #endif |
11450 | #ifdef _SC_THREAD_PRIORITY_SCHEDULING | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11451 | {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11452 | #endif |
11453 | #ifdef _SC_THREAD_PRIO_INHERIT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11454 | {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11455 | #endif |
11456 | #ifdef _SC_THREAD_PRIO_PROTECT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11457 | {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11458 | #endif |
11459 | #ifdef _SC_THREAD_PROCESS_SHARED | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11460 | {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11461 | #endif |
11462 | #ifdef _SC_THREAD_SAFE_FUNCTIONS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11463 | {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11464 | #endif |
11465 | #ifdef _SC_THREAD_STACK_MIN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11466 | {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11467 | #endif |
11468 | #ifdef _SC_THREAD_THREADS_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11469 | {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11470 | #endif |
11471 | #ifdef _SC_TIMERS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11472 | {"SC_TIMERS", _SC_TIMERS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11473 | #endif |
11474 | #ifdef _SC_TIMER_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11475 | {"SC_TIMER_MAX", _SC_TIMER_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11476 | #endif |
11477 | #ifdef _SC_TTY_NAME_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11478 | {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11479 | #endif |
11480 | #ifdef _SC_TZNAME_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11481 | {"SC_TZNAME_MAX", _SC_TZNAME_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11482 | #endif |
11483 | #ifdef _SC_T_IOV_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11484 | {"SC_T_IOV_MAX", _SC_T_IOV_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11485 | #endif |
11486 | #ifdef _SC_UCHAR_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11487 | {"SC_UCHAR_MAX", _SC_UCHAR_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11488 | #endif |
11489 | #ifdef _SC_UINT_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11490 | {"SC_UINT_MAX", _SC_UINT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11491 | #endif |
11492 | #ifdef _SC_UIO_MAXIOV | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11493 | {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11494 | #endif |
11495 | #ifdef _SC_ULONG_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11496 | {"SC_ULONG_MAX", _SC_ULONG_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11497 | #endif |
11498 | #ifdef _SC_USHRT_MAX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11499 | {"SC_USHRT_MAX", _SC_USHRT_MAX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11500 | #endif |
11501 | #ifdef _SC_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11502 | {"SC_VERSION", _SC_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11503 | #endif |
11504 | #ifdef _SC_WORD_BIT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11505 | {"SC_WORD_BIT", _SC_WORD_BIT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11506 | #endif |
11507 | #ifdef _SC_XBS5_ILP32_OFF32 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11508 | {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11509 | #endif |
11510 | #ifdef _SC_XBS5_ILP32_OFFBIG | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11511 | {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11512 | #endif |
11513 | #ifdef _SC_XBS5_LP64_OFF64 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11514 | {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11515 | #endif |
11516 | #ifdef _SC_XBS5_LPBIG_OFFBIG | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11517 | {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11518 | #endif |
11519 | #ifdef _SC_XOPEN_CRYPT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11520 | {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11521 | #endif |
11522 | #ifdef _SC_XOPEN_ENH_I18N | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11523 | {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11524 | #endif |
11525 | #ifdef _SC_XOPEN_LEGACY | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11526 | {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11527 | #endif |
11528 | #ifdef _SC_XOPEN_REALTIME | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11529 | {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11530 | #endif |
11531 | #ifdef _SC_XOPEN_REALTIME_THREADS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11532 | {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11533 | #endif |
11534 | #ifdef _SC_XOPEN_SHM | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11535 | {"SC_XOPEN_SHM", _SC_XOPEN_SHM}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11536 | #endif |
11537 | #ifdef _SC_XOPEN_UNIX | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11538 | {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11539 | #endif |
11540 | #ifdef _SC_XOPEN_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11541 | {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11542 | #endif |
11543 | #ifdef _SC_XOPEN_XCU_VERSION | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11544 | {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11545 | #endif |
11546 | #ifdef _SC_XOPEN_XPG2 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11547 | {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11548 | #endif |
11549 | #ifdef _SC_XOPEN_XPG3 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11550 | {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11551 | #endif |
11552 | #ifdef _SC_XOPEN_XPG4 | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11553 | {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4}, |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11554 | #endif |
11555 | }; | ||||
11556 | |||||
11557 | static int | ||||
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11558 | conv_sysconf_confname(PyObject *arg, int *valuep) |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11559 | { |
11560 | return conv_confname(arg, valuep, posix_constants_sysconf, | ||||
11561 | sizeof(posix_constants_sysconf) | ||||
11562 | / sizeof(struct constdef)); | ||||
11563 | } | ||||
11564 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11565 | |
11566 | /*[clinic input] | ||||
11567 | os.sysconf -> long | ||||
11568 | name: sysconf_confname | ||||
11569 | / | ||||
11570 | |||||
11571 | Return an integer-valued system configuration variable. | ||||
11572 | [clinic start generated code]*/ | ||||
11573 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11574 | static long |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11575 | os_sysconf_impl(PyObject *module, int name) |
11576 | /*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11577 | { |
11578 | long value; | ||||
11579 | |||||
11580 | errno = 0; | ||||
11581 | value = sysconf(name); | ||||
11582 | if (value == -1 && errno != 0) | ||||
11583 | posix_error(); | ||||
11584 | return value; | ||||
11585 | } | ||||
11586 | #endif /* HAVE_SYSCONF */ | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 11587 | |
11588 | |||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11589 | /* 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] | 11590 | * 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] | 11591 | * the exported dictionaries that are used to publish information about the |
11592 | * names available on the host platform. | ||||
11593 | * | ||||
11594 | * Sorting the table at runtime ensures that the table is properly ordered | ||||
11595 | * when used, even for platforms we're not able to test on. It also makes | ||||
11596 | * it easier to add additional entries to the tables. | ||||
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11597 | */ |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11598 | |
11599 | static int | ||||
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11600 | cmp_constdefs(const void *v1, const void *v2) |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11601 | { |
11602 | const struct constdef *c1 = | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11603 | (const struct constdef *) v1; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11604 | const struct constdef *c2 = |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11605 | (const struct constdef *) v2; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11606 | |
11607 | return strcmp(c1->name, c2->name); | ||||
11608 | } | ||||
11609 | |||||
11610 | static int | ||||
Fredrik Lundh | ff7df9d | 2000-07-08 22:48:53 +0000 | [diff] [blame] | 11611 | setup_confname_table(struct constdef *table, size_t tablesize, |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 11612 | const char *tablename, PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11613 | { |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11614 | PyObject *d = NULL; |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11615 | size_t i; |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11616 | |
11617 | qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs); | ||||
11618 | d = PyDict_New(); | ||||
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11619 | if (d == NULL) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11620 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11621 | |
Barry Warsaw | 3155db3 | 2000-04-13 15:20:40 +0000 | [diff] [blame] | 11622 | for (i=0; i < tablesize; ++i) { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11623 | PyObject *o = PyLong_FromLong(table[i].value); |
11624 | if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) { | ||||
11625 | Py_XDECREF(o); | ||||
11626 | Py_DECREF(d); | ||||
11627 | return -1; | ||||
11628 | } | ||||
11629 | Py_DECREF(o); | ||||
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11630 | } |
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11631 | return PyModule_AddObject(module, tablename, d); |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11632 | } |
11633 | |||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11634 | /* Return -1 on failure, 0 on success. */ |
11635 | static int | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11636 | setup_confname_tables(PyObject *module) |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11637 | { |
11638 | #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF) | ||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11639 | if (setup_confname_table(posix_constants_pathconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11640 | sizeof(posix_constants_pathconf) |
11641 | / sizeof(struct constdef), | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11642 | "pathconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11643 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11644 | #endif |
11645 | #ifdef HAVE_CONFSTR | ||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11646 | if (setup_confname_table(posix_constants_confstr, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11647 | sizeof(posix_constants_confstr) |
11648 | / sizeof(struct constdef), | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11649 | "confstr_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11650 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11651 | #endif |
11652 | #ifdef HAVE_SYSCONF | ||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11653 | if (setup_confname_table(posix_constants_sysconf, |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11654 | sizeof(posix_constants_sysconf) |
11655 | / sizeof(struct constdef), | ||||
Fred Drake | 4d1e64b | 2002-04-15 19:40:07 +0000 | [diff] [blame] | 11656 | "sysconf_names", module)) |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11657 | return -1; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11658 | #endif |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11659 | return 0; |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11660 | } |
Fred Drake | d86ed29 | 1999-12-15 15:34:33 +0000 | [diff] [blame] | 11661 | |
11662 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11663 | /*[clinic input] |
11664 | os.abort | ||||
11665 | |||||
11666 | Abort the interpreter immediately. | ||||
11667 | |||||
11668 | This function 'dumps core' or otherwise fails in the hardest way possible | ||||
11669 | on the hosting operating system. This function never returns. | ||||
11670 | [clinic start generated code]*/ | ||||
11671 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11672 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11673 | os_abort_impl(PyObject *module) |
11674 | /*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/ | ||||
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11675 | { |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11676 | abort(); |
11677 | /*NOTREACHED*/ | ||||
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11678 | #ifndef __clang__ |
11679 | /* Issue #28152: abort() is declared with __attribute__((__noreturn__)). | ||||
11680 | GCC emits a warning without "return NULL;" (compiler bug?), but Clang | ||||
11681 | is smarter and emits a warning on the return. */ | ||||
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11682 | Py_FatalError("abort() called from Python code didn't abort!"); |
11683 | return NULL; | ||||
Victor Stinner | 9a2329f | 2016-12-05 17:56:36 +0100 | [diff] [blame] | 11684 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11685 | } |
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 11686 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11687 | #ifdef MS_WINDOWS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11688 | /* Grab ShellExecute dynamically from shell32 */ |
11689 | static int has_ShellExecute = -1; | ||||
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11690 | static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR, |
11691 | LPCWSTR, INT); | ||||
11692 | static int | ||||
11693 | check_ShellExecute() | ||||
11694 | { | ||||
11695 | HINSTANCE hShell32; | ||||
11696 | |||||
11697 | /* only recheck */ | ||||
11698 | if (-1 == has_ShellExecute) { | ||||
11699 | Py_BEGIN_ALLOW_THREADS | ||||
Victor Stinner | a991215 | 2017-10-13 13:46:57 -0700 | [diff] [blame] | 11700 | /* Security note: this call is not vulnerable to "DLL hijacking". |
11701 | SHELL32 is part of "KnownDLLs" and so Windows always load | ||||
11702 | the system SHELL32.DLL, even if there is another SHELL32.DLL | ||||
11703 | in the DLL search path. */ | ||||
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11704 | hShell32 = LoadLibraryW(L"SHELL32"); |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11705 | if (hShell32) { |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11706 | *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32, |
11707 | "ShellExecuteW"); | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11708 | has_ShellExecute = Py_ShellExecuteW != NULL; |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11709 | } else { |
11710 | has_ShellExecute = 0; | ||||
11711 | } | ||||
Tony Roberts | 4860f01 | 2019-02-02 18:16:42 +0100 | [diff] [blame] | 11712 | Py_END_ALLOW_THREADS |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11713 | } |
11714 | return has_ShellExecute; | ||||
11715 | } | ||||
11716 | |||||
11717 | |||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11718 | /*[clinic input] |
11719 | os.startfile | ||||
11720 | filepath: path_t | ||||
11721 | operation: Py_UNICODE = NULL | ||||
Hirokazu Yamamoto | 8223c24 | 2009-05-17 04:21:53 +0000 | [diff] [blame] | 11722 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11723 | Start a file with its associated application. |
11724 | |||||
11725 | When "operation" is not specified or "open", this acts like | ||||
11726 | double-clicking the file in Explorer, or giving the file name as an | ||||
11727 | argument to the DOS "start" command: the file is opened with whatever | ||||
11728 | application (if any) its extension is associated. | ||||
11729 | When another "operation" is given, it specifies what should be done with | ||||
11730 | the file. A typical operation is "print". | ||||
11731 | |||||
11732 | startfile returns as soon as the associated application is launched. | ||||
11733 | There is no option to wait for the application to close, and no way | ||||
11734 | to retrieve the application's exit status. | ||||
11735 | |||||
11736 | The filepath is relative to the current directory. If you want to use | ||||
11737 | an absolute path, make sure the first character is not a slash ("/"); | ||||
11738 | the underlying Win32 ShellExecute function doesn't work if it is. | ||||
11739 | [clinic start generated code]*/ | ||||
11740 | |||||
11741 | static PyObject * | ||||
Serhiy Storchaka | afb3e71 | 2018-12-14 11:19:51 +0200 | [diff] [blame] | 11742 | os_startfile_impl(PyObject *module, path_t *filepath, |
11743 | const Py_UNICODE *operation) | ||||
Serhiy Storchaka | d322abb | 2019-09-14 13:31:50 +0300 | [diff] [blame] | 11744 | /*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/ |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11745 | { |
11746 | HINSTANCE rc; | ||||
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11747 | |
11748 | if(!check_ShellExecute()) { | ||||
11749 | /* If the OS doesn't have ShellExecute, return a | ||||
11750 | NotImplementedError. */ | ||||
11751 | return PyErr_Format(PyExc_NotImplementedError, | ||||
11752 | "startfile not available on this platform"); | ||||
11753 | } | ||||
11754 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 11755 | if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) { |
Miss Islington (bot) | 3498ac5 | 2020-02-04 16:32:32 -0800 | [diff] [blame] | 11756 | return NULL; |
11757 | } | ||||
11758 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11759 | Py_BEGIN_ALLOW_THREADS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11760 | rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide, |
Steve Dower | 7d0e0c9 | 2015-01-24 08:18:24 -0800 | [diff] [blame] | 11761 | NULL, NULL, SW_SHOWNORMAL); |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11762 | Py_END_ALLOW_THREADS |
11763 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11764 | if (rc <= (HINSTANCE)32) { |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11765 | win32_error_object("startfile", filepath->object); |
Victor Stinner | eb5657a | 2011-09-30 01:44:27 +0200 | [diff] [blame] | 11766 | return NULL; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11767 | } |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 11768 | Py_RETURN_NONE; |
Tim Peters | f58a7aa | 2000-09-22 10:05:54 +0000 | [diff] [blame] | 11769 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11770 | #endif /* MS_WINDOWS */ |
11771 | |||||
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 11772 | |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11773 | #ifdef HAVE_GETLOADAVG |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11774 | /*[clinic input] |
11775 | os.getloadavg | ||||
11776 | |||||
11777 | Return average recent system load information. | ||||
11778 | |||||
11779 | Return the number of processes in the system run queue averaged over | ||||
11780 | the last 1, 5, and 15 minutes as a tuple of three floats. | ||||
11781 | Raises OSError if the load average was unobtainable. | ||||
11782 | [clinic start generated code]*/ | ||||
11783 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11784 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11785 | os_getloadavg_impl(PyObject *module) |
11786 | /*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/ | ||||
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11787 | { |
11788 | double loadavg[3]; | ||||
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11789 | if (getloadavg(loadavg, 3)!=3) { |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11790 | PyErr_SetString(PyExc_OSError, "Load averages are unobtainable"); |
11791 | return NULL; | ||||
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11792 | } else |
Stefan Krah | 0e803b3 | 2010-11-26 16:16:47 +0000 | [diff] [blame] | 11793 | return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]); |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11794 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11795 | #endif /* HAVE_GETLOADAVG */ |
Martin v. Löwis | 438b534 | 2002-12-27 10:16:42 +0000 | [diff] [blame] | 11796 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11797 | |
11798 | /*[clinic input] | ||||
11799 | os.device_encoding | ||||
11800 | fd: int | ||||
11801 | |||||
11802 | Return a string describing the encoding of a terminal's file descriptor. | ||||
11803 | |||||
11804 | The file descriptor must be attached to a terminal. | ||||
11805 | If the device is not a terminal, return None. | ||||
11806 | [clinic start generated code]*/ | ||||
11807 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11808 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11809 | os_device_encoding_impl(PyObject *module, int fd) |
11810 | /*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11811 | { |
Brett Cannon | efb00c0 | 2012-02-29 18:31:31 -0500 | [diff] [blame] | 11812 | return _Py_device_encoding(fd); |
Martin v. Löwis | d1cd4d4 | 2007-08-11 14:02:14 +0000 | [diff] [blame] | 11813 | } |
11814 | |||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11815 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11816 | #ifdef HAVE_SETRESUID |
11817 | /*[clinic input] | ||||
11818 | os.setresuid | ||||
11819 | |||||
11820 | ruid: uid_t | ||||
11821 | euid: uid_t | ||||
11822 | suid: uid_t | ||||
11823 | / | ||||
11824 | |||||
11825 | Set the current process's real, effective, and saved user ids. | ||||
11826 | [clinic start generated code]*/ | ||||
11827 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11828 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11829 | os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid) |
11830 | /*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11831 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11832 | if (setresuid(ruid, euid, suid) < 0) |
11833 | return posix_error(); | ||||
11834 | Py_RETURN_NONE; | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11835 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11836 | #endif /* HAVE_SETRESUID */ |
11837 | |||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11838 | |
11839 | #ifdef HAVE_SETRESGID | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11840 | /*[clinic input] |
11841 | os.setresgid | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11842 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11843 | rgid: gid_t |
11844 | egid: gid_t | ||||
11845 | sgid: gid_t | ||||
11846 | / | ||||
11847 | |||||
11848 | Set the current process's real, effective, and saved group ids. | ||||
11849 | [clinic start generated code]*/ | ||||
11850 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11851 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11852 | os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid) |
11853 | /*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11854 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11855 | if (setresgid(rgid, egid, sgid) < 0) |
11856 | return posix_error(); | ||||
11857 | Py_RETURN_NONE; | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11858 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11859 | #endif /* HAVE_SETRESGID */ |
11860 | |||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11861 | |
11862 | #ifdef HAVE_GETRESUID | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11863 | /*[clinic input] |
11864 | os.getresuid | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11865 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11866 | Return a tuple of the current process's real, effective, and saved user ids. |
11867 | [clinic start generated code]*/ | ||||
11868 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11869 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11870 | os_getresuid_impl(PyObject *module) |
11871 | /*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/ | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11872 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11873 | uid_t ruid, euid, suid; |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11874 | if (getresuid(&ruid, &euid, &suid) < 0) |
11875 | return posix_error(); | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11876 | return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid), |
11877 | _PyLong_FromUid(euid), | ||||
11878 | _PyLong_FromUid(suid)); | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11879 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11880 | #endif /* HAVE_GETRESUID */ |
11881 | |||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11882 | |
11883 | #ifdef HAVE_GETRESGID | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11884 | /*[clinic input] |
11885 | os.getresgid | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11886 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11887 | Return a tuple of the current process's real, effective, and saved group ids. |
11888 | [clinic start generated code]*/ | ||||
11889 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11890 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11891 | os_getresgid_impl(PyObject *module) |
11892 | /*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11893 | { |
11894 | gid_t rgid, egid, sgid; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 11895 | if (getresgid(&rgid, &egid, &sgid) < 0) |
11896 | return posix_error(); | ||||
Serhiy Storchaka | 7cf5599 | 2013-02-10 21:56:49 +0200 | [diff] [blame] | 11897 | return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid), |
11898 | _PyLong_FromGid(egid), | ||||
11899 | _PyLong_FromGid(sgid)); | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11900 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11901 | #endif /* HAVE_GETRESGID */ |
11902 | |||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 11903 | |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 11904 | #ifdef USE_XATTRS |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11905 | /*[clinic input] |
11906 | os.getxattr | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11907 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11908 | path: path_t(allow_fd=True) |
11909 | attribute: path_t | ||||
11910 | * | ||||
11911 | follow_symlinks: bool = True | ||||
11912 | |||||
11913 | Return the value of extended attribute attribute on path. | ||||
11914 | |||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11915 | path may be either a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11916 | If follow_symlinks is False, and the last element of the path is a symbolic |
11917 | link, getxattr will examine the symbolic link itself instead of the file | ||||
11918 | the link points to. | ||||
11919 | |||||
11920 | [clinic start generated code]*/ | ||||
11921 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11922 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11923 | os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 11924 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11925 | /*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11926 | { |
11927 | Py_ssize_t i; | ||||
11928 | PyObject *buffer = NULL; | ||||
11929 | |||||
11930 | if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks)) | ||||
11931 | return NULL; | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11932 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 11933 | if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) { |
11934 | return NULL; | ||||
11935 | } | ||||
11936 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11937 | for (i = 0; ; i++) { |
11938 | void *ptr; | ||||
11939 | ssize_t result; | ||||
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 11940 | static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0}; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11941 | Py_ssize_t buffer_size = buffer_sizes[i]; |
11942 | if (!buffer_size) { | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11943 | path_error(path); |
11944 | return NULL; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11945 | } |
11946 | buffer = PyBytes_FromStringAndSize(NULL, buffer_size); | ||||
11947 | if (!buffer) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11948 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11949 | ptr = PyBytes_AS_STRING(buffer); |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11950 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11951 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11952 | if (path->fd >= 0) |
11953 | result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11954 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11955 | result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11956 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11957 | result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11958 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11959 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11960 | if (result < 0) { |
11961 | Py_DECREF(buffer); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11962 | if (errno == ERANGE) |
11963 | continue; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11964 | path_error(path); |
11965 | return NULL; | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11966 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11967 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11968 | if (result != buffer_size) { |
11969 | /* Can only shrink. */ | ||||
11970 | _PyBytes_Resize(&buffer, result); | ||||
11971 | } | ||||
11972 | break; | ||||
11973 | } | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11974 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 11975 | return buffer; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11976 | } |
11977 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11978 | |
11979 | /*[clinic input] | ||||
11980 | os.setxattr | ||||
11981 | |||||
11982 | path: path_t(allow_fd=True) | ||||
11983 | attribute: path_t | ||||
11984 | value: Py_buffer | ||||
11985 | flags: int = 0 | ||||
11986 | * | ||||
11987 | follow_symlinks: bool = True | ||||
11988 | |||||
11989 | Set extended attribute attribute on path to value. | ||||
11990 | |||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 11991 | path may be either a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 11992 | If follow_symlinks is False, and the last element of the path is a symbolic |
11993 | link, setxattr will modify the symbolic link itself instead of the file | ||||
11994 | the link points to. | ||||
11995 | |||||
11996 | [clinic start generated code]*/ | ||||
11997 | |||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 11998 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 11999 | os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12000 | Py_buffer *value, int flags, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12001 | /*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12002 | { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12003 | ssize_t result; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12004 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12005 | if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks)) |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12006 | return NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12007 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 12008 | if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object, |
12009 | value->buf, value->len, flags) < 0) { | ||||
12010 | return NULL; | ||||
12011 | } | ||||
12012 | |||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12013 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12014 | if (path->fd > -1) |
12015 | result = fsetxattr(path->fd, attribute->narrow, | ||||
12016 | value->buf, value->len, flags); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12017 | else if (follow_symlinks) |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12018 | result = setxattr(path->narrow, attribute->narrow, |
12019 | value->buf, value->len, flags); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12020 | else |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12021 | result = lsetxattr(path->narrow, attribute->narrow, |
12022 | value->buf, value->len, flags); | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12023 | Py_END_ALLOW_THREADS; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12024 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12025 | if (result) { |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12026 | path_error(path); |
12027 | return NULL; | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12028 | } |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12029 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12030 | Py_RETURN_NONE; |
12031 | } | ||||
12032 | |||||
12033 | |||||
12034 | /*[clinic input] | ||||
12035 | os.removexattr | ||||
12036 | |||||
12037 | path: path_t(allow_fd=True) | ||||
12038 | attribute: path_t | ||||
12039 | * | ||||
12040 | follow_symlinks: bool = True | ||||
12041 | |||||
12042 | Remove extended attribute attribute on path. | ||||
12043 | |||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12044 | path may be either a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12045 | If follow_symlinks is False, and the last element of the path is a symbolic |
12046 | link, removexattr will modify the symbolic link itself instead of the file | ||||
12047 | the link points to. | ||||
12048 | |||||
12049 | [clinic start generated code]*/ | ||||
12050 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12051 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12052 | os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12053 | int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12054 | /*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12055 | { |
12056 | ssize_t result; | ||||
12057 | |||||
12058 | if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks)) | ||||
12059 | return NULL; | ||||
12060 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 12061 | if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) { |
12062 | return NULL; | ||||
12063 | } | ||||
12064 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12065 | Py_BEGIN_ALLOW_THREADS; |
12066 | if (path->fd > -1) | ||||
12067 | result = fremovexattr(path->fd, attribute->narrow); | ||||
12068 | else if (follow_symlinks) | ||||
12069 | result = removexattr(path->narrow, attribute->narrow); | ||||
12070 | else | ||||
12071 | result = lremovexattr(path->narrow, attribute->narrow); | ||||
12072 | Py_END_ALLOW_THREADS; | ||||
12073 | |||||
12074 | if (result) { | ||||
12075 | return path_error(path); | ||||
12076 | } | ||||
12077 | |||||
12078 | Py_RETURN_NONE; | ||||
12079 | } | ||||
12080 | |||||
12081 | |||||
12082 | /*[clinic input] | ||||
12083 | os.listxattr | ||||
12084 | |||||
12085 | path: path_t(allow_fd=True, nullable=True) = None | ||||
12086 | * | ||||
12087 | follow_symlinks: bool = True | ||||
12088 | |||||
12089 | Return a list of extended attributes on path. | ||||
12090 | |||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12091 | path may be either None, a string, a path-like object, or an open file descriptor. |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12092 | if path is None, listxattr will examine the current directory. |
12093 | If follow_symlinks is False, and the last element of the path is a symbolic | ||||
12094 | link, listxattr will examine the symbolic link itself instead of the file | ||||
12095 | the link points to. | ||||
12096 | [clinic start generated code]*/ | ||||
12097 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12098 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12099 | os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 12100 | /*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12101 | { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12102 | Py_ssize_t i; |
12103 | PyObject *result = NULL; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12104 | const char *name; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12105 | char *buffer = NULL; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12106 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12107 | if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks)) |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12108 | goto exit; |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12109 | |
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 12110 | if (PySys_Audit("os.listxattr", "(O)", |
12111 | path->object ? path->object : Py_None) < 0) { | ||||
12112 | return NULL; | ||||
12113 | } | ||||
12114 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12115 | name = path->narrow ? path->narrow : "."; |
12116 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12117 | for (i = 0; ; i++) { |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12118 | const char *start, *trace, *end; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12119 | ssize_t length; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 12120 | static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 }; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12121 | Py_ssize_t buffer_size = buffer_sizes[i]; |
12122 | if (!buffer_size) { | ||||
Christian Heimes | 3b9493b | 2012-09-23 16:11:15 +0200 | [diff] [blame] | 12123 | /* ERANGE */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12124 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12125 | break; |
12126 | } | ||||
12127 | buffer = PyMem_MALLOC(buffer_size); | ||||
12128 | if (!buffer) { | ||||
12129 | PyErr_NoMemory(); | ||||
12130 | break; | ||||
12131 | } | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12132 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12133 | Py_BEGIN_ALLOW_THREADS; |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12134 | if (path->fd > -1) |
12135 | length = flistxattr(path->fd, buffer, buffer_size); | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12136 | else if (follow_symlinks) |
12137 | length = listxattr(name, buffer, buffer_size); | ||||
12138 | else | ||||
12139 | length = llistxattr(name, buffer, buffer_size); | ||||
12140 | Py_END_ALLOW_THREADS; | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12141 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12142 | if (length < 0) { |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12143 | if (errno == ERANGE) { |
12144 | PyMem_FREE(buffer); | ||||
Benjamin Peterson | dedac52 | 2013-05-13 19:55:40 -0500 | [diff] [blame] | 12145 | buffer = NULL; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12146 | continue; |
Antoine Pitrou | 7f98739 | 2013-05-13 19:46:29 +0200 | [diff] [blame] | 12147 | } |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12148 | path_error(path); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12149 | break; |
12150 | } | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12151 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12152 | result = PyList_New(0); |
12153 | if (!result) { | ||||
12154 | goto exit; | ||||
12155 | } | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12156 | |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12157 | end = buffer + length; |
12158 | for (trace = start = buffer; trace != end; trace++) { | ||||
12159 | if (!*trace) { | ||||
12160 | int error; | ||||
12161 | PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start, | ||||
12162 | trace - start); | ||||
12163 | if (!attribute) { | ||||
12164 | Py_DECREF(result); | ||||
12165 | result = NULL; | ||||
12166 | goto exit; | ||||
12167 | } | ||||
12168 | error = PyList_Append(result, attribute); | ||||
12169 | Py_DECREF(attribute); | ||||
12170 | if (error) { | ||||
12171 | Py_DECREF(result); | ||||
12172 | result = NULL; | ||||
12173 | goto exit; | ||||
12174 | } | ||||
12175 | start = trace + 1; | ||||
12176 | } | ||||
12177 | } | ||||
12178 | break; | ||||
12179 | } | ||||
12180 | exit: | ||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 12181 | if (buffer) |
12182 | PyMem_FREE(buffer); | ||||
12183 | return result; | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12184 | } |
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 12185 | #endif /* USE_XATTRS */ |
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 12186 | |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12187 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12188 | /*[clinic input] |
12189 | os.urandom | ||||
12190 | |||||
12191 | size: Py_ssize_t | ||||
12192 | / | ||||
12193 | |||||
12194 | Return a bytes object containing random bytes suitable for cryptographic use. | ||||
12195 | [clinic start generated code]*/ | ||||
12196 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12197 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12198 | os_urandom_impl(PyObject *module, Py_ssize_t size) |
12199 | /*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12200 | { |
12201 | PyObject *bytes; | ||||
12202 | int result; | ||||
12203 | |||||
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12204 | if (size < 0) |
12205 | return PyErr_Format(PyExc_ValueError, | ||||
12206 | "negative argument not allowed"); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12207 | bytes = PyBytes_FromStringAndSize(NULL, size); |
12208 | if (bytes == NULL) | ||||
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12209 | return NULL; |
12210 | |||||
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 12211 | result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes)); |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12212 | if (result == -1) { |
12213 | Py_DECREF(bytes); | ||||
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12214 | return NULL; |
12215 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12216 | return bytes; |
Georg Brandl | 2fb477c | 2012-02-21 00:33:36 +0100 | [diff] [blame] | 12217 | } |
12218 | |||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 12219 | #ifdef HAVE_MEMFD_CREATE |
12220 | /*[clinic input] | ||||
12221 | os.memfd_create | ||||
12222 | |||||
12223 | name: FSConverter | ||||
12224 | flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC | ||||
12225 | |||||
12226 | [clinic start generated code]*/ | ||||
12227 | |||||
12228 | static PyObject * | ||||
12229 | os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags) | ||||
12230 | /*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/ | ||||
12231 | { | ||||
12232 | int fd; | ||||
12233 | const char *bytes = PyBytes_AS_STRING(name); | ||||
12234 | Py_BEGIN_ALLOW_THREADS | ||||
12235 | fd = memfd_create(bytes, flags); | ||||
12236 | Py_END_ALLOW_THREADS | ||||
12237 | if (fd == -1) { | ||||
12238 | return PyErr_SetFromErrno(PyExc_OSError); | ||||
12239 | } | ||||
12240 | return PyLong_FromLong(fd); | ||||
12241 | } | ||||
12242 | #endif | ||||
12243 | |||||
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12244 | /* Terminal size querying */ |
12245 | |||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 12246 | static PyTypeObject* TerminalSizeType; |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12247 | |
12248 | PyDoc_STRVAR(TerminalSize_docstring, | ||||
12249 | "A tuple of (columns, lines) for holding terminal window size"); | ||||
12250 | |||||
12251 | static PyStructSequence_Field TerminalSize_fields[] = { | ||||
12252 | {"columns", "width of the terminal window in characters"}, | ||||
12253 | {"lines", "height of the terminal window in characters"}, | ||||
12254 | {NULL, NULL} | ||||
12255 | }; | ||||
12256 | |||||
12257 | static PyStructSequence_Desc TerminalSize_desc = { | ||||
12258 | "os.terminal_size", | ||||
12259 | TerminalSize_docstring, | ||||
12260 | TerminalSize_fields, | ||||
12261 | 2, | ||||
12262 | }; | ||||
12263 | |||||
12264 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12265 | /* AC 3.5: fd should accept None */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12266 | PyDoc_STRVAR(termsize__doc__, |
12267 | "Return the size of the terminal window as (columns, lines).\n" \ | ||||
12268 | "\n" \ | ||||
12269 | "The optional argument fd (default standard output) specifies\n" \ | ||||
12270 | "which file descriptor should be queried.\n" \ | ||||
12271 | "\n" \ | ||||
12272 | "If the file descriptor is not connected to a terminal, an OSError\n" \ | ||||
12273 | "is thrown.\n" \ | ||||
12274 | "\n" \ | ||||
12275 | "This function will only be defined if an implementation is\n" \ | ||||
12276 | "available for this system.\n" \ | ||||
12277 | "\n" \ | ||||
oldk | aa0735f | 2018-02-02 16:52:55 +0800 | [diff] [blame] | 12278 | "shutil.get_terminal_size is the high-level function which should\n" \ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12279 | "normally be used, os.get_terminal_size is the low-level implementation."); |
12280 | |||||
12281 | static PyObject* | ||||
12282 | get_terminal_size(PyObject *self, PyObject *args) | ||||
12283 | { | ||||
12284 | int columns, lines; | ||||
12285 | PyObject *termsize; | ||||
12286 | |||||
12287 | int fd = fileno(stdout); | ||||
12288 | /* Under some conditions stdout may not be connected and | ||||
12289 | * fileno(stdout) may point to an invalid file descriptor. For example | ||||
12290 | * GUI apps don't have valid standard streams by default. | ||||
12291 | * | ||||
12292 | * If this happens, and the optional fd argument is not present, | ||||
12293 | * the ioctl below will fail returning EBADF. This is what we want. | ||||
12294 | */ | ||||
12295 | |||||
12296 | if (!PyArg_ParseTuple(args, "|i", &fd)) | ||||
12297 | return NULL; | ||||
12298 | |||||
12299 | #ifdef TERMSIZE_USE_IOCTL | ||||
12300 | { | ||||
12301 | struct winsize w; | ||||
12302 | if (ioctl(fd, TIOCGWINSZ, &w)) | ||||
12303 | return PyErr_SetFromErrno(PyExc_OSError); | ||||
12304 | columns = w.ws_col; | ||||
12305 | lines = w.ws_row; | ||||
12306 | } | ||||
12307 | #endif /* TERMSIZE_USE_IOCTL */ | ||||
12308 | |||||
12309 | #ifdef TERMSIZE_USE_CONIO | ||||
12310 | { | ||||
12311 | DWORD nhandle; | ||||
12312 | HANDLE handle; | ||||
12313 | CONSOLE_SCREEN_BUFFER_INFO csbi; | ||||
12314 | switch (fd) { | ||||
12315 | case 0: nhandle = STD_INPUT_HANDLE; | ||||
12316 | break; | ||||
12317 | case 1: nhandle = STD_OUTPUT_HANDLE; | ||||
12318 | break; | ||||
12319 | case 2: nhandle = STD_ERROR_HANDLE; | ||||
12320 | break; | ||||
12321 | default: | ||||
12322 | return PyErr_Format(PyExc_ValueError, "bad file descriptor"); | ||||
12323 | } | ||||
12324 | handle = GetStdHandle(nhandle); | ||||
12325 | if (handle == NULL) | ||||
12326 | return PyErr_Format(PyExc_OSError, "handle cannot be retrieved"); | ||||
12327 | if (handle == INVALID_HANDLE_VALUE) | ||||
12328 | return PyErr_SetFromWindowsErr(0); | ||||
12329 | |||||
12330 | if (!GetConsoleScreenBufferInfo(handle, &csbi)) | ||||
12331 | return PyErr_SetFromWindowsErr(0); | ||||
12332 | |||||
12333 | columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; | ||||
12334 | lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; | ||||
12335 | } | ||||
12336 | #endif /* TERMSIZE_USE_CONIO */ | ||||
12337 | |||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 12338 | termsize = PyStructSequence_New(TerminalSizeType); |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12339 | if (termsize == NULL) |
12340 | return NULL; | ||||
12341 | PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns)); | ||||
12342 | PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines)); | ||||
12343 | if (PyErr_Occurred()) { | ||||
12344 | Py_DECREF(termsize); | ||||
12345 | return NULL; | ||||
12346 | } | ||||
12347 | return termsize; | ||||
12348 | } | ||||
12349 | #endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */ | ||||
12350 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12351 | |
12352 | /*[clinic input] | ||||
12353 | os.cpu_count | ||||
12354 | |||||
Charles-François Natali | 80d62e6 | 2015-08-13 20:37:08 +0100 | [diff] [blame] | 12355 | Return the number of CPUs in the system; return None if indeterminable. |
12356 | |||||
12357 | This number is not equivalent to the number of CPUs the current process can | ||||
12358 | use. The number of usable CPUs can be obtained with | ||||
12359 | ``len(os.sched_getaffinity(0))`` | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12360 | [clinic start generated code]*/ |
12361 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12362 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12363 | os_cpu_count_impl(PyObject *module) |
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 12364 | /*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/ |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12365 | { |
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12366 | int ncpu = 0; |
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12367 | #ifdef MS_WINDOWS |
Miss Islington (bot) | 43ee0e2 | 2019-09-11 08:56:13 -0700 | [diff] [blame] | 12368 | /* Declare prototype here to avoid pulling in all of the Win7 APIs in 3.8 */ |
12369 | DWORD WINAPI GetActiveProcessorCount(WORD group); | ||||
12370 | ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS); | ||||
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12371 | #elif defined(__hpux) |
12372 | ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL); | ||||
12373 | #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN) | ||||
12374 | ncpu = sysconf(_SC_NPROCESSORS_ONLN); | ||||
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12375 | #elif defined(__DragonFly__) || \ |
12376 | defined(__OpenBSD__) || \ | ||||
12377 | defined(__FreeBSD__) || \ | ||||
Charles-Francois Natali | d59087d | 2013-05-20 17:31:06 +0200 | [diff] [blame] | 12378 | defined(__NetBSD__) || \ |
12379 | defined(__APPLE__) | ||||
Charles-Francois Natali | 7c4f8da | 2013-05-20 17:40:32 +0200 | [diff] [blame] | 12380 | int mib[2]; |
12381 | size_t len = sizeof(ncpu); | ||||
12382 | mib[0] = CTL_HW; | ||||
12383 | mib[1] = HW_NCPU; | ||||
12384 | if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0) | ||||
12385 | ncpu = 0; | ||||
Charles-Francois Natali | 44feda3 | 2013-05-20 14:40:46 +0200 | [diff] [blame] | 12386 | #endif |
12387 | if (ncpu >= 1) | ||||
12388 | return PyLong_FromLong(ncpu); | ||||
12389 | else | ||||
12390 | Py_RETURN_NONE; | ||||
12391 | } | ||||
12392 | |||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12393 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12394 | /*[clinic input] |
12395 | os.get_inheritable -> bool | ||||
12396 | |||||
12397 | fd: int | ||||
12398 | / | ||||
12399 | |||||
12400 | Get the close-on-exe flag of the specified file descriptor. | ||||
12401 | [clinic start generated code]*/ | ||||
12402 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12403 | static int |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12404 | os_get_inheritable_impl(PyObject *module, int fd) |
12405 | /*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/ | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12406 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12407 | int return_value; |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12408 | _Py_BEGIN_SUPPRESS_IPH |
12409 | return_value = _Py_get_inheritable(fd); | ||||
12410 | _Py_END_SUPPRESS_IPH | ||||
12411 | return return_value; | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12412 | } |
12413 | |||||
12414 | |||||
12415 | /*[clinic input] | ||||
12416 | os.set_inheritable | ||||
12417 | fd: int | ||||
12418 | inheritable: int | ||||
12419 | / | ||||
12420 | |||||
12421 | Set the inheritable flag of the specified file descriptor. | ||||
12422 | [clinic start generated code]*/ | ||||
12423 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12424 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 12425 | os_set_inheritable_impl(PyObject *module, int fd, int inheritable) |
12426 | /*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/ | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12427 | { |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12428 | int result; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12429 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12430 | _Py_BEGIN_SUPPRESS_IPH |
12431 | result = _Py_set_inheritable(fd, inheritable, NULL); | ||||
12432 | _Py_END_SUPPRESS_IPH | ||||
12433 | if (result < 0) | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12434 | return NULL; |
12435 | Py_RETURN_NONE; | ||||
12436 | } | ||||
12437 | |||||
12438 | |||||
12439 | #ifdef MS_WINDOWS | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12440 | /*[clinic input] |
12441 | os.get_handle_inheritable -> bool | ||||
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12442 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12443 | / |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12444 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12445 | Get the close-on-exe flag of the specified file descriptor. |
12446 | [clinic start generated code]*/ | ||||
12447 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12448 | static int |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12449 | os_get_handle_inheritable_impl(PyObject *module, intptr_t handle) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12450 | /*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12451 | { |
12452 | DWORD flags; | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12453 | |
12454 | if (!GetHandleInformation((HANDLE)handle, &flags)) { | ||||
12455 | PyErr_SetFromWindowsErr(0); | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12456 | return -1; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12457 | } |
12458 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12459 | return flags & HANDLE_FLAG_INHERIT; |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12460 | } |
12461 | |||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12462 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12463 | /*[clinic input] |
12464 | os.set_handle_inheritable | ||||
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12465 | handle: intptr_t |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12466 | inheritable: bool |
12467 | / | ||||
12468 | |||||
12469 | Set the inheritable flag of the specified handle. | ||||
12470 | [clinic start generated code]*/ | ||||
12471 | |||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12472 | static PyObject * |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 12473 | os_set_handle_inheritable_impl(PyObject *module, intptr_t handle, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 12474 | int inheritable) |
Victor Stinner | 581139c | 2016-09-06 15:54:20 -0700 | [diff] [blame] | 12475 | /*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12476 | { |
12477 | DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0; | ||||
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 12478 | if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) { |
12479 | PyErr_SetFromWindowsErr(0); | ||||
12480 | return NULL; | ||||
12481 | } | ||||
12482 | Py_RETURN_NONE; | ||||
12483 | } | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 12484 | #endif /* MS_WINDOWS */ |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 12485 | |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12486 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12487 | /*[clinic input] |
12488 | os.get_blocking -> bool | ||||
12489 | fd: int | ||||
12490 | / | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12491 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12492 | Get the blocking mode of the file descriptor. |
12493 | |||||
12494 | Return False if the O_NONBLOCK flag is set, True if the flag is cleared. | ||||
12495 | [clinic start generated code]*/ | ||||
12496 | |||||
12497 | static int | ||||
12498 | os_get_blocking_impl(PyObject *module, int fd) | ||||
12499 | /*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/ | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12500 | { |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12501 | int blocking; |
12502 | |||||
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12503 | _Py_BEGIN_SUPPRESS_IPH |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12504 | blocking = _Py_get_blocking(fd); |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12505 | _Py_END_SUPPRESS_IPH |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12506 | return blocking; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12507 | } |
12508 | |||||
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12509 | /*[clinic input] |
12510 | os.set_blocking | ||||
12511 | fd: int | ||||
12512 | blocking: bool(accept={int}) | ||||
12513 | / | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12514 | |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12515 | Set the blocking mode of the specified file descriptor. |
12516 | |||||
12517 | Set the O_NONBLOCK flag if blocking is False, | ||||
12518 | clear the O_NONBLOCK flag otherwise. | ||||
12519 | [clinic start generated code]*/ | ||||
12520 | |||||
12521 | static PyObject * | ||||
12522 | os_set_blocking_impl(PyObject *module, int fd, int blocking) | ||||
12523 | /*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/ | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12524 | { |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 12525 | int result; |
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12526 | |
Steve Dower | 8fc8980 | 2015-04-12 00:26:27 -0400 | [diff] [blame] | 12527 | _Py_BEGIN_SUPPRESS_IPH |
12528 | result = _Py_set_blocking(fd, blocking); | ||||
12529 | _Py_END_SUPPRESS_IPH | ||||
12530 | if (result < 0) | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 12531 | return NULL; |
12532 | Py_RETURN_NONE; | ||||
12533 | } | ||||
12534 | #endif /* !MS_WINDOWS */ | ||||
12535 | |||||
12536 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12537 | /*[clinic input] |
12538 | class os.DirEntry "DirEntry *" "&DirEntryType" | ||||
12539 | [clinic start generated code]*/ | ||||
12540 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12541 | |
12542 | typedef struct { | ||||
12543 | PyObject_HEAD | ||||
12544 | PyObject *name; | ||||
12545 | PyObject *path; | ||||
12546 | PyObject *stat; | ||||
12547 | PyObject *lstat; | ||||
12548 | #ifdef MS_WINDOWS | ||||
12549 | struct _Py_stat_struct win32_lstat; | ||||
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12550 | uint64_t win32_file_index; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12551 | int got_file_index; |
12552 | #else /* POSIX */ | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12553 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12554 | unsigned char d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12555 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12556 | ino_t d_ino; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12557 | int dir_fd; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12558 | #endif |
12559 | } DirEntry; | ||||
12560 | |||||
12561 | static void | ||||
12562 | DirEntry_dealloc(DirEntry *entry) | ||||
12563 | { | ||||
12564 | Py_XDECREF(entry->name); | ||||
12565 | Py_XDECREF(entry->path); | ||||
12566 | Py_XDECREF(entry->stat); | ||||
12567 | Py_XDECREF(entry->lstat); | ||||
12568 | Py_TYPE(entry)->tp_free((PyObject *)entry); | ||||
12569 | } | ||||
12570 | |||||
12571 | /* Forward reference */ | ||||
12572 | static int | ||||
12573 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits); | ||||
12574 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12575 | /*[clinic input] |
12576 | os.DirEntry.is_symlink -> bool | ||||
12577 | |||||
12578 | Return True if the entry is a symbolic link; cached per entry. | ||||
12579 | [clinic start generated code]*/ | ||||
12580 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12581 | static int |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12582 | os_DirEntry_is_symlink_impl(DirEntry *self) |
12583 | /*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12584 | { |
12585 | #ifdef MS_WINDOWS | ||||
12586 | return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12587 | #elif defined(HAVE_DIRENT_D_TYPE) |
12588 | /* POSIX */ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12589 | if (self->d_type != DT_UNKNOWN) |
12590 | return self->d_type == DT_LNK; | ||||
12591 | else | ||||
12592 | return DirEntry_test_mode(self, 0, S_IFLNK); | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12593 | #else |
12594 | /* POSIX without d_type */ | ||||
12595 | return DirEntry_test_mode(self, 0, S_IFLNK); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12596 | #endif |
12597 | } | ||||
12598 | |||||
12599 | static PyObject * | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12600 | DirEntry_fetch_stat(DirEntry *self, int follow_symlinks) |
12601 | { | ||||
12602 | int result; | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12603 | STRUCT_STAT st; |
12604 | PyObject *ub; | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12605 | |
12606 | #ifdef MS_WINDOWS | ||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12607 | if (!PyUnicode_FSDecoder(self->path, &ub)) |
12608 | return NULL; | ||||
12609 | const wchar_t *path = PyUnicode_AsUnicode(ub); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12610 | #else /* POSIX */ |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12611 | if (!PyUnicode_FSConverter(self->path, &ub)) |
12612 | return NULL; | ||||
12613 | const char *path = PyBytes_AS_STRING(ub); | ||||
12614 | if (self->dir_fd != DEFAULT_DIR_FD) { | ||||
12615 | #ifdef HAVE_FSTATAT | ||||
12616 | result = fstatat(self->dir_fd, path, &st, | ||||
12617 | follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW); | ||||
12618 | #else | ||||
12619 | PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat"); | ||||
12620 | return NULL; | ||||
12621 | #endif /* HAVE_FSTATAT */ | ||||
12622 | } | ||||
12623 | else | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12624 | #endif |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12625 | { |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12626 | if (follow_symlinks) |
12627 | result = STAT(path, &st); | ||||
12628 | else | ||||
12629 | result = LSTAT(path, &st); | ||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 12630 | } |
12631 | Py_DECREF(ub); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12632 | |
12633 | if (result != 0) | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12634 | return path_object_error(self->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12635 | |
12636 | return _pystat_fromstructstat(&st); | ||||
12637 | } | ||||
12638 | |||||
12639 | static PyObject * | ||||
12640 | DirEntry_get_lstat(DirEntry *self) | ||||
12641 | { | ||||
12642 | if (!self->lstat) { | ||||
12643 | #ifdef MS_WINDOWS | ||||
12644 | self->lstat = _pystat_fromstructstat(&self->win32_lstat); | ||||
12645 | #else /* POSIX */ | ||||
12646 | self->lstat = DirEntry_fetch_stat(self, 0); | ||||
12647 | #endif | ||||
12648 | } | ||||
12649 | Py_XINCREF(self->lstat); | ||||
12650 | return self->lstat; | ||||
12651 | } | ||||
12652 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12653 | /*[clinic input] |
12654 | os.DirEntry.stat | ||||
12655 | * | ||||
12656 | follow_symlinks: bool = True | ||||
12657 | |||||
12658 | Return stat_result object for the entry; cached per entry. | ||||
12659 | [clinic start generated code]*/ | ||||
12660 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12661 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12662 | os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks) |
12663 | /*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12664 | { |
12665 | if (!follow_symlinks) | ||||
12666 | return DirEntry_get_lstat(self); | ||||
12667 | |||||
12668 | if (!self->stat) { | ||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12669 | int result = os_DirEntry_is_symlink_impl(self); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12670 | if (result == -1) |
12671 | return NULL; | ||||
12672 | else if (result) | ||||
12673 | self->stat = DirEntry_fetch_stat(self, 1); | ||||
12674 | else | ||||
12675 | self->stat = DirEntry_get_lstat(self); | ||||
12676 | } | ||||
12677 | |||||
12678 | Py_XINCREF(self->stat); | ||||
12679 | return self->stat; | ||||
12680 | } | ||||
12681 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12682 | /* Set exception and return -1 on error, 0 for False, 1 for True */ |
12683 | static int | ||||
12684 | DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits) | ||||
12685 | { | ||||
12686 | PyObject *stat = NULL; | ||||
12687 | PyObject *st_mode = NULL; | ||||
12688 | long mode; | ||||
12689 | int result; | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12690 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12691 | int is_symlink; |
12692 | int need_stat; | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12693 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12694 | #ifdef MS_WINDOWS |
12695 | unsigned long dir_bits; | ||||
12696 | #endif | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12697 | _Py_IDENTIFIER(st_mode); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12698 | |
12699 | #ifdef MS_WINDOWS | ||||
12700 | is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK; | ||||
12701 | need_stat = follow_symlinks && is_symlink; | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12702 | #elif defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12703 | is_symlink = self->d_type == DT_LNK; |
12704 | need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink); | ||||
12705 | #endif | ||||
12706 | |||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12707 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12708 | if (need_stat) { |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12709 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12710 | stat = os_DirEntry_stat_impl(self, follow_symlinks); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12711 | if (!stat) { |
12712 | if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) { | ||||
12713 | /* If file doesn't exist (anymore), then return False | ||||
12714 | (i.e., say it's not a file/directory) */ | ||||
12715 | PyErr_Clear(); | ||||
12716 | return 0; | ||||
12717 | } | ||||
12718 | goto error; | ||||
12719 | } | ||||
12720 | st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode); | ||||
12721 | if (!st_mode) | ||||
12722 | goto error; | ||||
12723 | |||||
12724 | mode = PyLong_AsLong(st_mode); | ||||
12725 | if (mode == -1 && PyErr_Occurred()) | ||||
12726 | goto error; | ||||
12727 | Py_CLEAR(st_mode); | ||||
12728 | Py_CLEAR(stat); | ||||
12729 | result = (mode & S_IFMT) == mode_bits; | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12730 | #if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12731 | } |
12732 | else if (is_symlink) { | ||||
12733 | assert(mode_bits != S_IFLNK); | ||||
12734 | result = 0; | ||||
12735 | } | ||||
12736 | else { | ||||
12737 | assert(mode_bits == S_IFDIR || mode_bits == S_IFREG); | ||||
12738 | #ifdef MS_WINDOWS | ||||
12739 | dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY; | ||||
12740 | if (mode_bits == S_IFDIR) | ||||
12741 | result = dir_bits != 0; | ||||
12742 | else | ||||
12743 | result = dir_bits == 0; | ||||
12744 | #else /* POSIX */ | ||||
12745 | if (mode_bits == S_IFDIR) | ||||
12746 | result = self->d_type == DT_DIR; | ||||
12747 | else | ||||
12748 | result = self->d_type == DT_REG; | ||||
12749 | #endif | ||||
12750 | } | ||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 12751 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12752 | |
12753 | return result; | ||||
12754 | |||||
12755 | error: | ||||
12756 | Py_XDECREF(st_mode); | ||||
12757 | Py_XDECREF(stat); | ||||
12758 | return -1; | ||||
12759 | } | ||||
12760 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12761 | /*[clinic input] |
12762 | os.DirEntry.is_dir -> bool | ||||
12763 | * | ||||
12764 | follow_symlinks: bool = True | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12765 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12766 | Return True if the entry is a directory; cached per entry. |
12767 | [clinic start generated code]*/ | ||||
12768 | |||||
12769 | static int | ||||
12770 | os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks) | ||||
12771 | /*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/ | ||||
12772 | { | ||||
12773 | return DirEntry_test_mode(self, follow_symlinks, S_IFDIR); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12774 | } |
12775 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12776 | /*[clinic input] |
12777 | os.DirEntry.is_file -> bool | ||||
12778 | * | ||||
12779 | follow_symlinks: bool = True | ||||
12780 | |||||
12781 | Return True if the entry is a file; cached per entry. | ||||
12782 | [clinic start generated code]*/ | ||||
12783 | |||||
12784 | static int | ||||
12785 | os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks) | ||||
12786 | /*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12787 | { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12788 | return DirEntry_test_mode(self, follow_symlinks, S_IFREG); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12789 | } |
12790 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12791 | /*[clinic input] |
12792 | os.DirEntry.inode | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12793 | |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12794 | Return inode of the entry; cached per entry. |
12795 | [clinic start generated code]*/ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12796 | |
12797 | static PyObject * | ||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12798 | os_DirEntry_inode_impl(DirEntry *self) |
12799 | /*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12800 | { |
12801 | #ifdef MS_WINDOWS | ||||
12802 | if (!self->got_file_index) { | ||||
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12803 | PyObject *unicode; |
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12804 | const wchar_t *path; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12805 | STRUCT_STAT stat; |
12806 | int result; | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12807 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12808 | if (!PyUnicode_FSDecoder(self->path, &unicode)) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12809 | return NULL; |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12810 | path = PyUnicode_AsUnicode(unicode); |
12811 | result = LSTAT(path, &stat); | ||||
12812 | Py_DECREF(unicode); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12813 | |
Serhiy Storchaka | 2674bc7 | 2016-10-08 20:16:57 +0300 | [diff] [blame] | 12814 | if (result != 0) |
12815 | return path_object_error(self->path); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12816 | |
12817 | self->win32_file_index = stat.st_ino; | ||||
12818 | self->got_file_index = 1; | ||||
12819 | } | ||||
Victor Stinner | 0f6d733 | 2017-03-09 17:34:28 +0100 | [diff] [blame] | 12820 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index)); |
12821 | return PyLong_FromUnsignedLongLong(self->win32_file_index); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12822 | #else /* POSIX */ |
xdegaye | 50e8603 | 2017-05-22 11:15:08 +0200 | [diff] [blame] | 12823 | Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino)); |
12824 | return PyLong_FromUnsignedLongLong(self->d_ino); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12825 | #endif |
12826 | } | ||||
12827 | |||||
12828 | static PyObject * | ||||
12829 | DirEntry_repr(DirEntry *self) | ||||
12830 | { | ||||
12831 | return PyUnicode_FromFormat("<DirEntry %R>", self->name); | ||||
12832 | } | ||||
12833 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12834 | /*[clinic input] |
12835 | os.DirEntry.__fspath__ | ||||
12836 | |||||
12837 | Returns the path for the entry. | ||||
12838 | [clinic start generated code]*/ | ||||
12839 | |||||
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12840 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12841 | os_DirEntry___fspath___impl(DirEntry *self) |
12842 | /*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/ | ||||
Brett Cannon | 96881cd | 2016-06-10 14:37:21 -0700 | [diff] [blame] | 12843 | { |
12844 | Py_INCREF(self->path); | ||||
12845 | return self->path; | ||||
12846 | } | ||||
12847 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12848 | static PyMemberDef DirEntry_members[] = { |
12849 | {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY, | ||||
12850 | "the entry's base filename, relative to scandir() \"path\" argument"}, | ||||
12851 | {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY, | ||||
12852 | "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"}, | ||||
12853 | {NULL} | ||||
12854 | }; | ||||
12855 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12856 | #include "clinic/posixmodule.c.h" |
12857 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12858 | static PyMethodDef DirEntry_methods[] = { |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 12859 | OS_DIRENTRY_IS_DIR_METHODDEF |
12860 | OS_DIRENTRY_IS_FILE_METHODDEF | ||||
12861 | OS_DIRENTRY_IS_SYMLINK_METHODDEF | ||||
12862 | OS_DIRENTRY_STAT_METHODDEF | ||||
12863 | OS_DIRENTRY_INODE_METHODDEF | ||||
12864 | OS_DIRENTRY___FSPATH___METHODDEF | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12865 | {NULL} |
12866 | }; | ||||
12867 | |||||
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 12868 | static PyTypeObject DirEntryType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12869 | PyVarObject_HEAD_INIT(NULL, 0) |
12870 | MODNAME ".DirEntry", /* tp_name */ | ||||
12871 | sizeof(DirEntry), /* tp_basicsize */ | ||||
12872 | 0, /* tp_itemsize */ | ||||
12873 | /* methods */ | ||||
12874 | (destructor)DirEntry_dealloc, /* tp_dealloc */ | ||||
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 12875 | 0, /* tp_vectorcall_offset */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12876 | 0, /* tp_getattr */ |
12877 | 0, /* tp_setattr */ | ||||
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 12878 | 0, /* tp_as_async */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12879 | (reprfunc)DirEntry_repr, /* tp_repr */ |
12880 | 0, /* tp_as_number */ | ||||
12881 | 0, /* tp_as_sequence */ | ||||
12882 | 0, /* tp_as_mapping */ | ||||
12883 | 0, /* tp_hash */ | ||||
12884 | 0, /* tp_call */ | ||||
12885 | 0, /* tp_str */ | ||||
12886 | 0, /* tp_getattro */ | ||||
12887 | 0, /* tp_setattro */ | ||||
12888 | 0, /* tp_as_buffer */ | ||||
12889 | Py_TPFLAGS_DEFAULT, /* tp_flags */ | ||||
12890 | 0, /* tp_doc */ | ||||
12891 | 0, /* tp_traverse */ | ||||
12892 | 0, /* tp_clear */ | ||||
12893 | 0, /* tp_richcompare */ | ||||
12894 | 0, /* tp_weaklistoffset */ | ||||
12895 | 0, /* tp_iter */ | ||||
12896 | 0, /* tp_iternext */ | ||||
12897 | DirEntry_methods, /* tp_methods */ | ||||
12898 | DirEntry_members, /* tp_members */ | ||||
12899 | }; | ||||
12900 | |||||
12901 | #ifdef MS_WINDOWS | ||||
12902 | |||||
12903 | static wchar_t * | ||||
Serhiy Storchaka | deab18d | 2016-05-07 16:45:18 +0300 | [diff] [blame] | 12904 | join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12905 | { |
12906 | Py_ssize_t path_len; | ||||
12907 | Py_ssize_t size; | ||||
12908 | wchar_t *result; | ||||
12909 | wchar_t ch; | ||||
12910 | |||||
12911 | if (!path_wide) { /* Default arg: "." */ | ||||
12912 | path_wide = L"."; | ||||
12913 | path_len = 1; | ||||
12914 | } | ||||
12915 | else { | ||||
12916 | path_len = wcslen(path_wide); | ||||
12917 | } | ||||
12918 | |||||
12919 | /* The +1's are for the path separator and the NUL */ | ||||
12920 | size = path_len + 1 + wcslen(filename) + 1; | ||||
12921 | result = PyMem_New(wchar_t, size); | ||||
12922 | if (!result) { | ||||
12923 | PyErr_NoMemory(); | ||||
12924 | return NULL; | ||||
12925 | } | ||||
12926 | wcscpy(result, path_wide); | ||||
12927 | if (path_len > 0) { | ||||
12928 | ch = result[path_len - 1]; | ||||
12929 | if (ch != SEP && ch != ALTSEP && ch != L':') | ||||
12930 | result[path_len++] = SEP; | ||||
12931 | wcscpy(result + path_len, filename); | ||||
12932 | } | ||||
12933 | return result; | ||||
12934 | } | ||||
12935 | |||||
12936 | static PyObject * | ||||
12937 | DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW) | ||||
12938 | { | ||||
12939 | DirEntry *entry; | ||||
12940 | BY_HANDLE_FILE_INFORMATION file_info; | ||||
12941 | ULONG reparse_tag; | ||||
12942 | wchar_t *joined_path; | ||||
12943 | |||||
12944 | entry = PyObject_New(DirEntry, &DirEntryType); | ||||
12945 | if (!entry) | ||||
12946 | return NULL; | ||||
12947 | entry->name = NULL; | ||||
12948 | entry->path = NULL; | ||||
12949 | entry->stat = NULL; | ||||
12950 | entry->lstat = NULL; | ||||
12951 | entry->got_file_index = 0; | ||||
12952 | |||||
12953 | entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1); | ||||
12954 | if (!entry->name) | ||||
12955 | goto error; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12956 | if (path->narrow) { |
12957 | Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name)); | ||||
12958 | if (!entry->name) | ||||
12959 | goto error; | ||||
12960 | } | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12961 | |
12962 | joined_path = join_path_filenameW(path->wide, dataW->cFileName); | ||||
12963 | if (!joined_path) | ||||
12964 | goto error; | ||||
12965 | |||||
12966 | entry->path = PyUnicode_FromWideChar(joined_path, -1); | ||||
12967 | PyMem_Free(joined_path); | ||||
12968 | if (!entry->path) | ||||
12969 | goto error; | ||||
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12970 | if (path->narrow) { |
12971 | Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path)); | ||||
12972 | if (!entry->path) | ||||
12973 | goto error; | ||||
12974 | } | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12975 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 12976 | find_data_to_file_info(dataW, &file_info, &reparse_tag); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12977 | _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat); |
12978 | |||||
12979 | return (PyObject *)entry; | ||||
12980 | |||||
12981 | error: | ||||
12982 | Py_DECREF(entry); | ||||
12983 | return NULL; | ||||
12984 | } | ||||
12985 | |||||
12986 | #else /* POSIX */ | ||||
12987 | |||||
12988 | static char * | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 12989 | join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 12990 | { |
12991 | Py_ssize_t path_len; | ||||
12992 | Py_ssize_t size; | ||||
12993 | char *result; | ||||
12994 | |||||
12995 | if (!path_narrow) { /* Default arg: "." */ | ||||
12996 | path_narrow = "."; | ||||
12997 | path_len = 1; | ||||
12998 | } | ||||
12999 | else { | ||||
13000 | path_len = strlen(path_narrow); | ||||
13001 | } | ||||
13002 | |||||
13003 | if (filename_len == -1) | ||||
13004 | filename_len = strlen(filename); | ||||
13005 | |||||
13006 | /* The +1's are for the path separator and the NUL */ | ||||
13007 | size = path_len + 1 + filename_len + 1; | ||||
13008 | result = PyMem_New(char, size); | ||||
13009 | if (!result) { | ||||
13010 | PyErr_NoMemory(); | ||||
13011 | return NULL; | ||||
13012 | } | ||||
13013 | strcpy(result, path_narrow); | ||||
13014 | if (path_len > 0 && result[path_len - 1] != '/') | ||||
13015 | result[path_len++] = '/'; | ||||
13016 | strcpy(result + path_len, filename); | ||||
13017 | return result; | ||||
13018 | } | ||||
13019 | |||||
13020 | static PyObject * | ||||
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 13021 | DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13022 | ino_t d_ino |
13023 | #ifdef HAVE_DIRENT_D_TYPE | ||||
13024 | , unsigned char d_type | ||||
13025 | #endif | ||||
13026 | ) | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13027 | { |
13028 | DirEntry *entry; | ||||
13029 | char *joined_path; | ||||
13030 | |||||
13031 | entry = PyObject_New(DirEntry, &DirEntryType); | ||||
13032 | if (!entry) | ||||
13033 | return NULL; | ||||
13034 | entry->name = NULL; | ||||
13035 | entry->path = NULL; | ||||
13036 | entry->stat = NULL; | ||||
13037 | entry->lstat = NULL; | ||||
13038 | |||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13039 | if (path->fd != -1) { |
13040 | entry->dir_fd = path->fd; | ||||
13041 | joined_path = NULL; | ||||
13042 | } | ||||
13043 | else { | ||||
13044 | entry->dir_fd = DEFAULT_DIR_FD; | ||||
13045 | joined_path = join_path_filename(path->narrow, name, name_len); | ||||
13046 | if (!joined_path) | ||||
13047 | goto error; | ||||
13048 | } | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13049 | |
Serhiy Storchaka | 1180e5a | 2017-07-11 06:36:46 +0300 | [diff] [blame] | 13050 | if (!path->narrow || !PyObject_CheckBuffer(path->object)) { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13051 | entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len); |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13052 | if (joined_path) |
13053 | entry->path = PyUnicode_DecodeFSDefault(joined_path); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13054 | } |
13055 | else { | ||||
13056 | entry->name = PyBytes_FromStringAndSize(name, name_len); | ||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13057 | if (joined_path) |
13058 | entry->path = PyBytes_FromString(joined_path); | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13059 | } |
13060 | PyMem_Free(joined_path); | ||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13061 | if (!entry->name) |
13062 | goto error; | ||||
13063 | |||||
13064 | if (path->fd != -1) { | ||||
13065 | entry->path = entry->name; | ||||
13066 | Py_INCREF(entry->path); | ||||
13067 | } | ||||
13068 | else if (!entry->path) | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13069 | goto error; |
13070 | |||||
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13071 | #ifdef HAVE_DIRENT_D_TYPE |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13072 | entry->d_type = d_type; |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13073 | #endif |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13074 | entry->d_ino = d_ino; |
13075 | |||||
13076 | return (PyObject *)entry; | ||||
13077 | |||||
13078 | error: | ||||
13079 | Py_XDECREF(entry); | ||||
13080 | return NULL; | ||||
13081 | } | ||||
13082 | |||||
13083 | #endif | ||||
13084 | |||||
13085 | |||||
13086 | typedef struct { | ||||
13087 | PyObject_HEAD | ||||
13088 | path_t path; | ||||
13089 | #ifdef MS_WINDOWS | ||||
13090 | HANDLE handle; | ||||
13091 | WIN32_FIND_DATAW file_data; | ||||
13092 | int first_time; | ||||
13093 | #else /* POSIX */ | ||||
13094 | DIR *dirp; | ||||
13095 | #endif | ||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13096 | #ifdef HAVE_FDOPENDIR |
13097 | int fd; | ||||
13098 | #endif | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13099 | } ScandirIterator; |
13100 | |||||
13101 | #ifdef MS_WINDOWS | ||||
13102 | |||||
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13103 | static int |
13104 | ScandirIterator_is_closed(ScandirIterator *iterator) | ||||
13105 | { | ||||
13106 | return iterator->handle == INVALID_HANDLE_VALUE; | ||||
13107 | } | ||||
13108 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13109 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13110 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13111 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13112 | HANDLE handle = iterator->handle; |
13113 | |||||
13114 | if (handle == INVALID_HANDLE_VALUE) | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13115 | return; |
13116 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13117 | iterator->handle = INVALID_HANDLE_VALUE; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13118 | Py_BEGIN_ALLOW_THREADS |
13119 | FindClose(handle); | ||||
13120 | Py_END_ALLOW_THREADS | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13121 | } |
13122 | |||||
13123 | static PyObject * | ||||
13124 | ScandirIterator_iternext(ScandirIterator *iterator) | ||||
13125 | { | ||||
13126 | WIN32_FIND_DATAW *file_data = &iterator->file_data; | ||||
13127 | BOOL success; | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13128 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13129 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13130 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13131 | if (iterator->handle == INVALID_HANDLE_VALUE) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13132 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13133 | |
13134 | while (1) { | ||||
13135 | if (!iterator->first_time) { | ||||
13136 | Py_BEGIN_ALLOW_THREADS | ||||
13137 | success = FindNextFileW(iterator->handle, file_data); | ||||
13138 | Py_END_ALLOW_THREADS | ||||
13139 | if (!success) { | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13140 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13141 | if (GetLastError() != ERROR_NO_MORE_FILES) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13142 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13143 | break; |
13144 | } | ||||
13145 | } | ||||
13146 | iterator->first_time = 0; | ||||
13147 | |||||
13148 | /* Skip over . and .. */ | ||||
13149 | if (wcscmp(file_data->cFileName, L".") != 0 && | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13150 | wcscmp(file_data->cFileName, L"..") != 0) { |
13151 | entry = DirEntry_from_find_data(&iterator->path, file_data); | ||||
13152 | if (!entry) | ||||
13153 | break; | ||||
13154 | return entry; | ||||
13155 | } | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13156 | |
13157 | /* Loop till we get a non-dot directory or finish iterating */ | ||||
13158 | } | ||||
13159 | |||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13160 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13161 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13162 | return NULL; |
13163 | } | ||||
13164 | |||||
13165 | #else /* POSIX */ | ||||
13166 | |||||
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13167 | static int |
13168 | ScandirIterator_is_closed(ScandirIterator *iterator) | ||||
13169 | { | ||||
13170 | return !iterator->dirp; | ||||
13171 | } | ||||
13172 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13173 | static void |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13174 | ScandirIterator_closedir(ScandirIterator *iterator) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13175 | { |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13176 | DIR *dirp = iterator->dirp; |
13177 | |||||
13178 | if (!dirp) | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13179 | return; |
13180 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13181 | iterator->dirp = NULL; |
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13182 | Py_BEGIN_ALLOW_THREADS |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13183 | #ifdef HAVE_FDOPENDIR |
13184 | if (iterator->path.fd != -1) | ||||
13185 | rewinddir(dirp); | ||||
13186 | #endif | ||||
Serhiy Storchaka | fbb1c5e | 2016-03-30 20:40:02 +0300 | [diff] [blame] | 13187 | closedir(dirp); |
13188 | Py_END_ALLOW_THREADS | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13189 | return; |
13190 | } | ||||
13191 | |||||
13192 | static PyObject * | ||||
13193 | ScandirIterator_iternext(ScandirIterator *iterator) | ||||
13194 | { | ||||
13195 | struct dirent *direntp; | ||||
13196 | Py_ssize_t name_len; | ||||
13197 | int is_dot; | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13198 | PyObject *entry; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13199 | |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13200 | /* Happens if the iterator is iterated twice, or closed explicitly */ |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13201 | if (!iterator->dirp) |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13202 | return NULL; |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13203 | |
13204 | while (1) { | ||||
13205 | errno = 0; | ||||
13206 | Py_BEGIN_ALLOW_THREADS | ||||
13207 | direntp = readdir(iterator->dirp); | ||||
13208 | Py_END_ALLOW_THREADS | ||||
13209 | |||||
13210 | if (!direntp) { | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13211 | /* Error or no more files */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13212 | if (errno != 0) |
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13213 | path_error(&iterator->path); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13214 | break; |
13215 | } | ||||
13216 | |||||
13217 | /* Skip over . and .. */ | ||||
13218 | name_len = NAMLEN(direntp); | ||||
13219 | is_dot = direntp->d_name[0] == '.' && | ||||
13220 | (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2)); | ||||
13221 | if (!is_dot) { | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13222 | entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name, |
Victor Stinner | 35a97c0 | 2015-03-08 02:59:09 +0100 | [diff] [blame] | 13223 | name_len, direntp->d_ino |
13224 | #ifdef HAVE_DIRENT_D_TYPE | ||||
13225 | , direntp->d_type | ||||
13226 | #endif | ||||
13227 | ); | ||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13228 | if (!entry) |
13229 | break; | ||||
13230 | return entry; | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13231 | } |
13232 | |||||
13233 | /* Loop till we get a non-dot directory or finish iterating */ | ||||
13234 | } | ||||
13235 | |||||
Serhiy Storchaka | 988b9bc | 2016-02-08 17:56:36 +0200 | [diff] [blame] | 13236 | /* Error or no more files */ |
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13237 | ScandirIterator_closedir(iterator); |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13238 | return NULL; |
13239 | } | ||||
13240 | |||||
13241 | #endif | ||||
13242 | |||||
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13243 | static PyObject * |
13244 | ScandirIterator_close(ScandirIterator *self, PyObject *args) | ||||
13245 | { | ||||
13246 | ScandirIterator_closedir(self); | ||||
13247 | Py_RETURN_NONE; | ||||
13248 | } | ||||
13249 | |||||
13250 | static PyObject * | ||||
13251 | ScandirIterator_enter(PyObject *self, PyObject *args) | ||||
13252 | { | ||||
13253 | Py_INCREF(self); | ||||
13254 | return self; | ||||
13255 | } | ||||
13256 | |||||
13257 | static PyObject * | ||||
13258 | ScandirIterator_exit(ScandirIterator *self, PyObject *args) | ||||
13259 | { | ||||
13260 | ScandirIterator_closedir(self); | ||||
13261 | Py_RETURN_NONE; | ||||
13262 | } | ||||
13263 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13264 | static void |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13265 | ScandirIterator_finalize(ScandirIterator *iterator) |
13266 | { | ||||
13267 | PyObject *error_type, *error_value, *error_traceback; | ||||
13268 | |||||
13269 | /* Save the current exception, if any. */ | ||||
13270 | PyErr_Fetch(&error_type, &error_value, &error_traceback); | ||||
13271 | |||||
13272 | if (!ScandirIterator_is_closed(iterator)) { | ||||
13273 | ScandirIterator_closedir(iterator); | ||||
13274 | |||||
13275 | if (PyErr_ResourceWarning((PyObject *)iterator, 1, | ||||
13276 | "unclosed scandir iterator %R", iterator)) { | ||||
13277 | /* Spurious errors can appear at shutdown */ | ||||
13278 | if (PyErr_ExceptionMatches(PyExc_Warning)) { | ||||
13279 | PyErr_WriteUnraisable((PyObject *) iterator); | ||||
13280 | } | ||||
13281 | } | ||||
13282 | } | ||||
13283 | |||||
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13284 | path_cleanup(&iterator->path); |
13285 | |||||
13286 | /* Restore the saved exception. */ | ||||
13287 | PyErr_Restore(error_type, error_value, error_traceback); | ||||
13288 | } | ||||
13289 | |||||
13290 | static void | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13291 | ScandirIterator_dealloc(ScandirIterator *iterator) |
13292 | { | ||||
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13293 | if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0) |
13294 | return; | ||||
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13295 | |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13296 | Py_TYPE(iterator)->tp_free((PyObject *)iterator); |
13297 | } | ||||
13298 | |||||
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13299 | static PyMethodDef ScandirIterator_methods[] = { |
13300 | {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS}, | ||||
13301 | {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS}, | ||||
13302 | {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS}, | ||||
13303 | {NULL} | ||||
13304 | }; | ||||
13305 | |||||
Benjamin Peterson | 5646de4 | 2015-04-12 17:56:34 -0400 | [diff] [blame] | 13306 | static PyTypeObject ScandirIteratorType = { |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13307 | PyVarObject_HEAD_INIT(NULL, 0) |
13308 | MODNAME ".ScandirIterator", /* tp_name */ | ||||
13309 | sizeof(ScandirIterator), /* tp_basicsize */ | ||||
13310 | 0, /* tp_itemsize */ | ||||
13311 | /* methods */ | ||||
13312 | (destructor)ScandirIterator_dealloc, /* tp_dealloc */ | ||||
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 13313 | 0, /* tp_vectorcall_offset */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13314 | 0, /* tp_getattr */ |
13315 | 0, /* tp_setattr */ | ||||
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 13316 | 0, /* tp_as_async */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13317 | 0, /* tp_repr */ |
13318 | 0, /* tp_as_number */ | ||||
13319 | 0, /* tp_as_sequence */ | ||||
13320 | 0, /* tp_as_mapping */ | ||||
13321 | 0, /* tp_hash */ | ||||
13322 | 0, /* tp_call */ | ||||
13323 | 0, /* tp_str */ | ||||
13324 | 0, /* tp_getattro */ | ||||
13325 | 0, /* tp_setattro */ | ||||
13326 | 0, /* tp_as_buffer */ | ||||
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 13327 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13328 | 0, /* tp_doc */ |
13329 | 0, /* tp_traverse */ | ||||
13330 | 0, /* tp_clear */ | ||||
13331 | 0, /* tp_richcompare */ | ||||
13332 | 0, /* tp_weaklistoffset */ | ||||
13333 | PyObject_SelfIter, /* tp_iter */ | ||||
13334 | (iternextfunc)ScandirIterator_iternext, /* tp_iternext */ | ||||
Serhiy Storchaka | ffe96ae | 2016-02-11 13:21:30 +0200 | [diff] [blame] | 13335 | ScandirIterator_methods, /* tp_methods */ |
Victor Stinner | 7bfa409 | 2016-03-23 00:43:54 +0100 | [diff] [blame] | 13336 | 0, /* tp_members */ |
13337 | 0, /* tp_getset */ | ||||
13338 | 0, /* tp_base */ | ||||
13339 | 0, /* tp_dict */ | ||||
13340 | 0, /* tp_descr_get */ | ||||
13341 | 0, /* tp_descr_set */ | ||||
13342 | 0, /* tp_dictoffset */ | ||||
13343 | 0, /* tp_init */ | ||||
13344 | 0, /* tp_alloc */ | ||||
13345 | 0, /* tp_new */ | ||||
13346 | 0, /* tp_free */ | ||||
13347 | 0, /* tp_is_gc */ | ||||
13348 | 0, /* tp_bases */ | ||||
13349 | 0, /* tp_mro */ | ||||
13350 | 0, /* tp_cache */ | ||||
13351 | 0, /* tp_subclasses */ | ||||
13352 | 0, /* tp_weaklist */ | ||||
13353 | 0, /* tp_del */ | ||||
13354 | 0, /* tp_version_tag */ | ||||
13355 | (destructor)ScandirIterator_finalize, /* tp_finalize */ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13356 | }; |
13357 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13358 | /*[clinic input] |
13359 | os.scandir | ||||
13360 | |||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13361 | path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13362 | |
13363 | Return an iterator of DirEntry objects for given path. | ||||
13364 | |||||
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13365 | path can be specified as either str, bytes, or a path-like object. If path |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13366 | is bytes, the names of yielded DirEntry objects will also be bytes; in |
13367 | all other circumstances they will be str. | ||||
13368 | |||||
13369 | If path is None, uses the path='.'. | ||||
13370 | [clinic start generated code]*/ | ||||
13371 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13372 | static PyObject * |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13373 | os_scandir_impl(PyObject *module, path_t *path) |
BNMetrics | b942707 | 2018-11-02 15:20:19 +0000 | [diff] [blame] | 13374 | /*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/ |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13375 | { |
13376 | ScandirIterator *iterator; | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13377 | #ifdef MS_WINDOWS |
13378 | wchar_t *path_strW; | ||||
13379 | #else | ||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13380 | const char *path_str; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13381 | #ifdef HAVE_FDOPENDIR |
13382 | int fd = -1; | ||||
13383 | #endif | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13384 | #endif |
13385 | |||||
Miss Islington (bot) | 8763d43 | 2019-06-24 09:09:47 -0700 | [diff] [blame] | 13386 | if (PySys_Audit("os.scandir", "O", |
13387 | path->object ? path->object : Py_None) < 0) { | ||||
13388 | return NULL; | ||||
13389 | } | ||||
13390 | |||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13391 | iterator = PyObject_New(ScandirIterator, &ScandirIteratorType); |
13392 | if (!iterator) | ||||
13393 | return NULL; | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13394 | |
13395 | #ifdef MS_WINDOWS | ||||
13396 | iterator->handle = INVALID_HANDLE_VALUE; | ||||
13397 | #else | ||||
13398 | iterator->dirp = NULL; | ||||
13399 | #endif | ||||
13400 | |||||
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13401 | memcpy(&iterator->path, path, sizeof(path_t)); |
Serhiy Storchaka | 095ef73 | 2017-02-09 20:05:51 +0200 | [diff] [blame] | 13402 | /* Move the ownership to iterator->path */ |
13403 | path->object = NULL; | ||||
13404 | path->cleanup = NULL; | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13405 | |
13406 | #ifdef MS_WINDOWS | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13407 | iterator->first_time = 1; |
13408 | |||||
13409 | path_strW = join_path_filenameW(iterator->path.wide, L"*.*"); | ||||
13410 | if (!path_strW) | ||||
13411 | goto error; | ||||
13412 | |||||
13413 | Py_BEGIN_ALLOW_THREADS | ||||
13414 | iterator->handle = FindFirstFileW(path_strW, &iterator->file_data); | ||||
13415 | Py_END_ALLOW_THREADS | ||||
13416 | |||||
13417 | PyMem_Free(path_strW); | ||||
13418 | |||||
13419 | if (iterator->handle == INVALID_HANDLE_VALUE) { | ||||
13420 | path_error(&iterator->path); | ||||
13421 | goto error; | ||||
13422 | } | ||||
13423 | #else /* POSIX */ | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13424 | errno = 0; |
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13425 | #ifdef HAVE_FDOPENDIR |
13426 | if (path->fd != -1) { | ||||
13427 | /* closedir() closes the FD, so we duplicate it */ | ||||
13428 | fd = _Py_dup(path->fd); | ||||
13429 | if (fd == -1) | ||||
13430 | goto error; | ||||
13431 | |||||
13432 | Py_BEGIN_ALLOW_THREADS | ||||
13433 | iterator->dirp = fdopendir(fd); | ||||
13434 | Py_END_ALLOW_THREADS | ||||
13435 | } | ||||
13436 | else | ||||
13437 | #endif | ||||
13438 | { | ||||
13439 | if (iterator->path.narrow) | ||||
13440 | path_str = iterator->path.narrow; | ||||
13441 | else | ||||
13442 | path_str = "."; | ||||
13443 | |||||
13444 | Py_BEGIN_ALLOW_THREADS | ||||
13445 | iterator->dirp = opendir(path_str); | ||||
13446 | Py_END_ALLOW_THREADS | ||||
13447 | } | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13448 | |
13449 | if (!iterator->dirp) { | ||||
13450 | path_error(&iterator->path); | ||||
Serhiy Storchaka | ea720fe | 2017-03-30 09:12:31 +0300 | [diff] [blame] | 13451 | #ifdef HAVE_FDOPENDIR |
13452 | if (fd != -1) { | ||||
13453 | Py_BEGIN_ALLOW_THREADS | ||||
13454 | close(fd); | ||||
13455 | Py_END_ALLOW_THREADS | ||||
13456 | } | ||||
13457 | #endif | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13458 | goto error; |
13459 | } | ||||
13460 | #endif | ||||
13461 | |||||
13462 | return (PyObject *)iterator; | ||||
13463 | |||||
13464 | error: | ||||
13465 | Py_DECREF(iterator); | ||||
13466 | return NULL; | ||||
13467 | } | ||||
13468 | |||||
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13469 | /* |
13470 | Return the file system path representation of the object. | ||||
13471 | |||||
13472 | If the object is str or bytes, then allow it to pass through with | ||||
13473 | an incremented refcount. If the object defines __fspath__(), then | ||||
13474 | return the result of that method. All other types raise a TypeError. | ||||
13475 | */ | ||||
13476 | PyObject * | ||||
13477 | PyOS_FSPath(PyObject *path) | ||||
13478 | { | ||||
Brett Cannon | 3f9183b | 2016-08-26 14:44:48 -0700 | [diff] [blame] | 13479 | /* For error message reasons, this function is manually inlined in |
13480 | path_converter(). */ | ||||
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13481 | _Py_IDENTIFIER(__fspath__); |
13482 | PyObject *func = NULL; | ||||
13483 | PyObject *path_repr = NULL; | ||||
13484 | |||||
13485 | if (PyUnicode_Check(path) || PyBytes_Check(path)) { | ||||
13486 | Py_INCREF(path); | ||||
13487 | return path; | ||||
13488 | } | ||||
13489 | |||||
13490 | func = _PyObject_LookupSpecial(path, &PyId___fspath__); | ||||
13491 | if (NULL == func) { | ||||
13492 | return PyErr_Format(PyExc_TypeError, | ||||
13493 | "expected str, bytes or os.PathLike object, " | ||||
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13494 | "not %.200s", |
13495 | Py_TYPE(path)->tp_name); | ||||
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13496 | } |
13497 | |||||
Victor Stinner | f17c3de | 2016-12-06 18:46:19 +0100 | [diff] [blame] | 13498 | path_repr = _PyObject_CallNoArg(func); |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13499 | Py_DECREF(func); |
Brett Cannon | 044283a | 2016-07-15 10:41:49 -0700 | [diff] [blame] | 13500 | if (NULL == path_repr) { |
13501 | return NULL; | ||||
13502 | } | ||||
13503 | |||||
Brett Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13504 | if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) { |
13505 | PyErr_Format(PyExc_TypeError, | ||||
13506 | "expected %.200s.__fspath__() to return str or bytes, " | ||||
13507 | "not %.200s", Py_TYPE(path)->tp_name, | ||||
13508 | Py_TYPE(path_repr)->tp_name); | ||||
13509 | Py_DECREF(path_repr); | ||||
13510 | return NULL; | ||||
13511 | } | ||||
13512 | |||||
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13513 | return path_repr; |
13514 | } | ||||
13515 | |||||
13516 | /*[clinic input] | ||||
13517 | os.fspath | ||||
13518 | |||||
13519 | path: object | ||||
13520 | |||||
13521 | Return the file system path representation of the object. | ||||
13522 | |||||
Brett Cannon | b4f43e9 | 2016-06-09 14:32:08 -0700 | [diff] [blame] | 13523 | If the object is str or bytes, then allow it to pass through as-is. If the |
13524 | object defines __fspath__(), then return the result of that method. All other | ||||
13525 | types raise a TypeError. | ||||
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13526 | [clinic start generated code]*/ |
13527 | |||||
13528 | static PyObject * | ||||
Serhiy Storchaka | 2954f83 | 2016-07-07 18:20:03 +0300 | [diff] [blame] | 13529 | os_fspath_impl(PyObject *module, PyObject *path) |
13530 | /*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/ | ||||
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13531 | { |
13532 | return PyOS_FSPath(path); | ||||
13533 | } | ||||
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 13534 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13535 | #ifdef HAVE_GETRANDOM_SYSCALL |
13536 | /*[clinic input] | ||||
13537 | os.getrandom | ||||
13538 | |||||
13539 | size: Py_ssize_t | ||||
13540 | flags: int=0 | ||||
13541 | |||||
13542 | Obtain a series of random bytes. | ||||
13543 | [clinic start generated code]*/ | ||||
13544 | |||||
13545 | static PyObject * | ||||
13546 | os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags) | ||||
13547 | /*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/ | ||||
13548 | { | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13549 | PyObject *bytes; |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13550 | Py_ssize_t n; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13551 | |
13552 | if (size < 0) { | ||||
13553 | errno = EINVAL; | ||||
13554 | return posix_error(); | ||||
13555 | } | ||||
13556 | |||||
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13557 | bytes = PyBytes_FromStringAndSize(NULL, size); |
13558 | if (bytes == NULL) { | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13559 | PyErr_NoMemory(); |
13560 | return NULL; | ||||
13561 | } | ||||
13562 | |||||
13563 | while (1) { | ||||
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13564 | n = syscall(SYS_getrandom, |
13565 | PyBytes_AS_STRING(bytes), | ||||
13566 | PyBytes_GET_SIZE(bytes), | ||||
13567 | flags); | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13568 | if (n < 0 && errno == EINTR) { |
13569 | if (PyErr_CheckSignals() < 0) { | ||||
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13570 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13571 | } |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13572 | |
13573 | /* getrandom() was interrupted by a signal: retry */ | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13574 | continue; |
13575 | } | ||||
13576 | break; | ||||
13577 | } | ||||
13578 | |||||
13579 | if (n < 0) { | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13580 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13581 | goto error; |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13582 | } |
13583 | |||||
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13584 | if (n != size) { |
13585 | _PyBytes_Resize(&bytes, n); | ||||
13586 | } | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13587 | |
13588 | return bytes; | ||||
Victor Stinner | ec2319c | 2016-09-20 23:00:59 +0200 | [diff] [blame] | 13589 | |
13590 | error: | ||||
13591 | Py_DECREF(bytes); | ||||
13592 | return NULL; | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13593 | } |
13594 | #endif /* HAVE_GETRANDOM_SYSCALL */ | ||||
13595 | |||||
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13596 | #ifdef MS_WINDOWS |
13597 | /* bpo-36085: Helper functions for managing DLL search directories | ||||
13598 | * on win32 | ||||
13599 | */ | ||||
13600 | |||||
13601 | typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory); | ||||
13602 | typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie); | ||||
13603 | |||||
13604 | /*[clinic input] | ||||
13605 | os._add_dll_directory | ||||
13606 | |||||
13607 | path: path_t | ||||
13608 | |||||
13609 | Add a path to the DLL search path. | ||||
13610 | |||||
13611 | This search path is used when resolving dependencies for imported | ||||
13612 | extension modules (the module itself is resolved through sys.path), | ||||
13613 | and also by ctypes. | ||||
13614 | |||||
13615 | Returns an opaque value that may be passed to os.remove_dll_directory | ||||
13616 | to remove this directory from the search path. | ||||
13617 | [clinic start generated code]*/ | ||||
13618 | |||||
13619 | static PyObject * | ||||
13620 | os__add_dll_directory_impl(PyObject *module, path_t *path) | ||||
13621 | /*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/ | ||||
13622 | { | ||||
13623 | HMODULE hKernel32; | ||||
13624 | PAddDllDirectory AddDllDirectory; | ||||
13625 | DLL_DIRECTORY_COOKIE cookie = 0; | ||||
13626 | DWORD err = 0; | ||||
13627 | |||||
Steve Dower | a00b5be | 2020-02-13 08:30:27 +0000 | [diff] [blame] | 13628 | if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) { |
13629 | return NULL; | ||||
13630 | } | ||||
13631 | |||||
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13632 | /* For Windows 7, we have to load this. As this will be a fairly |
13633 | infrequent operation, just do it each time. Kernel32 is always | ||||
13634 | loaded. */ | ||||
13635 | Py_BEGIN_ALLOW_THREADS | ||||
13636 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || | ||||
13637 | !(AddDllDirectory = (PAddDllDirectory)GetProcAddress( | ||||
13638 | hKernel32, "AddDllDirectory")) || | ||||
13639 | !(cookie = (*AddDllDirectory)(path->wide))) { | ||||
13640 | err = GetLastError(); | ||||
13641 | } | ||||
13642 | Py_END_ALLOW_THREADS | ||||
13643 | |||||
13644 | if (err) { | ||||
13645 | return win32_error_object_err("add_dll_directory", | ||||
13646 | path->object, err); | ||||
13647 | } | ||||
13648 | |||||
13649 | return PyCapsule_New(cookie, "DLL directory cookie", NULL); | ||||
13650 | } | ||||
13651 | |||||
13652 | /*[clinic input] | ||||
13653 | os._remove_dll_directory | ||||
13654 | |||||
13655 | cookie: object | ||||
13656 | |||||
13657 | Removes a path from the DLL search path. | ||||
13658 | |||||
13659 | The parameter is an opaque value that was returned from | ||||
13660 | os.add_dll_directory. You can only remove directories that you added | ||||
13661 | yourself. | ||||
13662 | [clinic start generated code]*/ | ||||
13663 | |||||
13664 | static PyObject * | ||||
13665 | os__remove_dll_directory_impl(PyObject *module, PyObject *cookie) | ||||
13666 | /*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/ | ||||
13667 | { | ||||
13668 | HMODULE hKernel32; | ||||
13669 | PRemoveDllDirectory RemoveDllDirectory; | ||||
13670 | DLL_DIRECTORY_COOKIE cookieValue; | ||||
13671 | DWORD err = 0; | ||||
13672 | |||||
13673 | if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) { | ||||
13674 | PyErr_SetString(PyExc_TypeError, | ||||
13675 | "Provided cookie was not returned from os.add_dll_directory"); | ||||
13676 | return NULL; | ||||
13677 | } | ||||
13678 | |||||
13679 | cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer( | ||||
13680 | cookie, "DLL directory cookie"); | ||||
13681 | |||||
13682 | /* For Windows 7, we have to load this. As this will be a fairly | ||||
13683 | infrequent operation, just do it each time. Kernel32 is always | ||||
13684 | loaded. */ | ||||
13685 | Py_BEGIN_ALLOW_THREADS | ||||
13686 | if (!(hKernel32 = GetModuleHandleW(L"kernel32")) || | ||||
13687 | !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress( | ||||
13688 | hKernel32, "RemoveDllDirectory")) || | ||||
13689 | !(*RemoveDllDirectory)(cookieValue)) { | ||||
13690 | err = GetLastError(); | ||||
13691 | } | ||||
13692 | Py_END_ALLOW_THREADS | ||||
13693 | |||||
13694 | if (err) { | ||||
13695 | return win32_error_object_err("remove_dll_directory", | ||||
13696 | NULL, err); | ||||
13697 | } | ||||
13698 | |||||
13699 | if (PyCapsule_SetName(cookie, NULL)) { | ||||
13700 | return NULL; | ||||
13701 | } | ||||
13702 | |||||
13703 | Py_RETURN_NONE; | ||||
13704 | } | ||||
13705 | |||||
13706 | #endif | ||||
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13707 | |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13708 | static PyMethodDef posix_methods[] = { |
Larry Hastings | 3182680 | 2013-10-19 00:09:25 -0700 | [diff] [blame] | 13709 | |
13710 | OS_STAT_METHODDEF | ||||
13711 | OS_ACCESS_METHODDEF | ||||
13712 | OS_TTYNAME_METHODDEF | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13713 | OS_CHDIR_METHODDEF |
13714 | OS_CHFLAGS_METHODDEF | ||||
13715 | OS_CHMOD_METHODDEF | ||||
13716 | OS_FCHMOD_METHODDEF | ||||
13717 | OS_LCHMOD_METHODDEF | ||||
13718 | OS_CHOWN_METHODDEF | ||||
13719 | OS_FCHOWN_METHODDEF | ||||
13720 | OS_LCHOWN_METHODDEF | ||||
13721 | OS_LCHFLAGS_METHODDEF | ||||
13722 | OS_CHROOT_METHODDEF | ||||
13723 | OS_CTERMID_METHODDEF | ||||
13724 | OS_GETCWD_METHODDEF | ||||
13725 | OS_GETCWDB_METHODDEF | ||||
13726 | OS_LINK_METHODDEF | ||||
13727 | OS_LISTDIR_METHODDEF | ||||
13728 | OS_LSTAT_METHODDEF | ||||
13729 | OS_MKDIR_METHODDEF | ||||
13730 | OS_NICE_METHODDEF | ||||
13731 | OS_GETPRIORITY_METHODDEF | ||||
13732 | OS_SETPRIORITY_METHODDEF | ||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 13733 | OS_POSIX_SPAWN_METHODDEF |
Joannah Nanjekye | 92b8322 | 2019-01-16 16:29:26 +0300 | [diff] [blame] | 13734 | OS_POSIX_SPAWNP_METHODDEF |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13735 | OS_READLINK_METHODDEF |
Pablo Galindo | aac4d03 | 2019-05-31 19:39:47 +0100 | [diff] [blame] | 13736 | OS_COPY_FILE_RANGE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13737 | OS_RENAME_METHODDEF |
13738 | OS_REPLACE_METHODDEF | ||||
13739 | OS_RMDIR_METHODDEF | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13740 | OS_SYMLINK_METHODDEF |
13741 | OS_SYSTEM_METHODDEF | ||||
13742 | OS_UMASK_METHODDEF | ||||
13743 | OS_UNAME_METHODDEF | ||||
13744 | OS_UNLINK_METHODDEF | ||||
13745 | OS_REMOVE_METHODDEF | ||||
13746 | OS_UTIME_METHODDEF | ||||
13747 | OS_TIMES_METHODDEF | ||||
13748 | OS__EXIT_METHODDEF | ||||
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 13749 | OS__FCOPYFILE_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13750 | OS_EXECV_METHODDEF |
13751 | OS_EXECVE_METHODDEF | ||||
13752 | OS_SPAWNV_METHODDEF | ||||
13753 | OS_SPAWNVE_METHODDEF | ||||
13754 | OS_FORK1_METHODDEF | ||||
13755 | OS_FORK_METHODDEF | ||||
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame] | 13756 | OS_REGISTER_AT_FORK_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13757 | OS_SCHED_GET_PRIORITY_MAX_METHODDEF |
13758 | OS_SCHED_GET_PRIORITY_MIN_METHODDEF | ||||
13759 | OS_SCHED_GETPARAM_METHODDEF | ||||
13760 | OS_SCHED_GETSCHEDULER_METHODDEF | ||||
13761 | OS_SCHED_RR_GET_INTERVAL_METHODDEF | ||||
13762 | OS_SCHED_SETPARAM_METHODDEF | ||||
13763 | OS_SCHED_SETSCHEDULER_METHODDEF | ||||
13764 | OS_SCHED_YIELD_METHODDEF | ||||
13765 | OS_SCHED_SETAFFINITY_METHODDEF | ||||
13766 | OS_SCHED_GETAFFINITY_METHODDEF | ||||
13767 | OS_OPENPTY_METHODDEF | ||||
13768 | OS_FORKPTY_METHODDEF | ||||
13769 | OS_GETEGID_METHODDEF | ||||
13770 | OS_GETEUID_METHODDEF | ||||
13771 | OS_GETGID_METHODDEF | ||||
Ross Lagerwall | b0ae53d | 2011-06-10 07:30:30 +0200 | [diff] [blame] | 13772 | #ifdef HAVE_GETGROUPLIST |
13773 | {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__}, | ||||
13774 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13775 | OS_GETGROUPS_METHODDEF |
13776 | OS_GETPID_METHODDEF | ||||
13777 | OS_GETPGRP_METHODDEF | ||||
13778 | OS_GETPPID_METHODDEF | ||||
13779 | OS_GETUID_METHODDEF | ||||
13780 | OS_GETLOGIN_METHODDEF | ||||
13781 | OS_KILL_METHODDEF | ||||
13782 | OS_KILLPG_METHODDEF | ||||
13783 | OS_PLOCK_METHODDEF | ||||
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13784 | #ifdef MS_WINDOWS |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 13785 | OS_STARTFILE_METHODDEF |
Thomas Heller | 8b7a957 | 2007-08-31 06:44:36 +0000 | [diff] [blame] | 13786 | #endif |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13787 | OS_SETUID_METHODDEF |
13788 | OS_SETEUID_METHODDEF | ||||
13789 | OS_SETREUID_METHODDEF | ||||
13790 | OS_SETGID_METHODDEF | ||||
13791 | OS_SETEGID_METHODDEF | ||||
13792 | OS_SETREGID_METHODDEF | ||||
13793 | OS_SETGROUPS_METHODDEF | ||||
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13794 | #ifdef HAVE_INITGROUPS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13795 | {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__}, |
Antoine Pitrou | b7572f0 | 2009-12-02 20:46:48 +0000 | [diff] [blame] | 13796 | #endif /* HAVE_INITGROUPS */ |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13797 | OS_GETPGID_METHODDEF |
13798 | OS_SETPGRP_METHODDEF | ||||
13799 | OS_WAIT_METHODDEF | ||||
13800 | OS_WAIT3_METHODDEF | ||||
13801 | OS_WAIT4_METHODDEF | ||||
13802 | OS_WAITID_METHODDEF | ||||
13803 | OS_WAITPID_METHODDEF | ||||
13804 | OS_GETSID_METHODDEF | ||||
13805 | OS_SETSID_METHODDEF | ||||
13806 | OS_SETPGID_METHODDEF | ||||
13807 | OS_TCGETPGRP_METHODDEF | ||||
13808 | OS_TCSETPGRP_METHODDEF | ||||
13809 | OS_OPEN_METHODDEF | ||||
13810 | OS_CLOSE_METHODDEF | ||||
13811 | OS_CLOSERANGE_METHODDEF | ||||
13812 | OS_DEVICE_ENCODING_METHODDEF | ||||
13813 | OS_DUP_METHODDEF | ||||
13814 | OS_DUP2_METHODDEF | ||||
13815 | OS_LOCKF_METHODDEF | ||||
13816 | OS_LSEEK_METHODDEF | ||||
13817 | OS_READ_METHODDEF | ||||
13818 | OS_READV_METHODDEF | ||||
13819 | OS_PREAD_METHODDEF | ||||
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13820 | OS_PREADV_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13821 | OS_WRITE_METHODDEF |
13822 | OS_WRITEV_METHODDEF | ||||
13823 | OS_PWRITE_METHODDEF | ||||
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 13824 | OS_PWRITEV_METHODDEF |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13825 | #ifdef HAVE_SENDFILE |
Serhiy Storchaka | 62be742 | 2018-11-27 13:27:31 +0200 | [diff] [blame] | 13826 | {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS, |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 13827 | posix_sendfile__doc__}, |
13828 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13829 | OS_FSTAT_METHODDEF |
13830 | OS_ISATTY_METHODDEF | ||||
13831 | OS_PIPE_METHODDEF | ||||
13832 | OS_PIPE2_METHODDEF | ||||
13833 | OS_MKFIFO_METHODDEF | ||||
13834 | OS_MKNOD_METHODDEF | ||||
13835 | OS_MAJOR_METHODDEF | ||||
13836 | OS_MINOR_METHODDEF | ||||
13837 | OS_MAKEDEV_METHODDEF | ||||
13838 | OS_FTRUNCATE_METHODDEF | ||||
13839 | OS_TRUNCATE_METHODDEF | ||||
13840 | OS_POSIX_FALLOCATE_METHODDEF | ||||
13841 | OS_POSIX_FADVISE_METHODDEF | ||||
13842 | OS_PUTENV_METHODDEF | ||||
13843 | OS_UNSETENV_METHODDEF | ||||
13844 | OS_STRERROR_METHODDEF | ||||
13845 | OS_FCHDIR_METHODDEF | ||||
13846 | OS_FSYNC_METHODDEF | ||||
13847 | OS_SYNC_METHODDEF | ||||
13848 | OS_FDATASYNC_METHODDEF | ||||
13849 | OS_WCOREDUMP_METHODDEF | ||||
13850 | OS_WIFCONTINUED_METHODDEF | ||||
13851 | OS_WIFSTOPPED_METHODDEF | ||||
13852 | OS_WIFSIGNALED_METHODDEF | ||||
13853 | OS_WIFEXITED_METHODDEF | ||||
13854 | OS_WEXITSTATUS_METHODDEF | ||||
13855 | OS_WTERMSIG_METHODDEF | ||||
13856 | OS_WSTOPSIG_METHODDEF | ||||
13857 | OS_FSTATVFS_METHODDEF | ||||
13858 | OS_STATVFS_METHODDEF | ||||
13859 | OS_CONFSTR_METHODDEF | ||||
13860 | OS_SYSCONF_METHODDEF | ||||
13861 | OS_FPATHCONF_METHODDEF | ||||
13862 | OS_PATHCONF_METHODDEF | ||||
13863 | OS_ABORT_METHODDEF | ||||
Serhiy Storchaka | f0b5015 | 2015-05-13 00:52:39 +0300 | [diff] [blame] | 13864 | OS__GETFULLPATHNAME_METHODDEF |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13865 | OS__GETDISKUSAGE_METHODDEF |
13866 | OS__GETFINALPATHNAME_METHODDEF | ||||
13867 | OS__GETVOLUMEPATHNAME_METHODDEF | ||||
13868 | OS_GETLOADAVG_METHODDEF | ||||
13869 | OS_URANDOM_METHODDEF | ||||
13870 | OS_SETRESUID_METHODDEF | ||||
13871 | OS_SETRESGID_METHODDEF | ||||
13872 | OS_GETRESUID_METHODDEF | ||||
13873 | OS_GETRESGID_METHODDEF | ||||
Martin v. Löwis | 7aed61a | 2009-11-27 14:09:49 +0000 | [diff] [blame] | 13874 | |
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13875 | OS_GETXATTR_METHODDEF |
13876 | OS_SETXATTR_METHODDEF | ||||
13877 | OS_REMOVEXATTR_METHODDEF | ||||
13878 | OS_LISTXATTR_METHODDEF | ||||
13879 | |||||
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 13880 | #if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) |
13881 | {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__}, | ||||
13882 | #endif | ||||
Larry Hastings | 2f93635 | 2014-08-05 14:04:04 +1000 | [diff] [blame] | 13883 | OS_CPU_COUNT_METHODDEF |
13884 | OS_GET_INHERITABLE_METHODDEF | ||||
13885 | OS_SET_INHERITABLE_METHODDEF | ||||
13886 | OS_GET_HANDLE_INHERITABLE_METHODDEF | ||||
13887 | OS_SET_HANDLE_INHERITABLE_METHODDEF | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13888 | #ifndef MS_WINDOWS |
Serhiy Storchaka | 12a69db | 2018-09-17 15:38:27 +0300 | [diff] [blame] | 13889 | OS_GET_BLOCKING_METHODDEF |
13890 | OS_SET_BLOCKING_METHODDEF | ||||
Victor Stinner | 1db9e7b | 2014-07-29 22:32:47 +0200 | [diff] [blame] | 13891 | #endif |
Serhiy Storchaka | 49d02d1 | 2016-11-06 13:45:33 +0200 | [diff] [blame] | 13892 | OS_SCANDIR_METHODDEF |
Ethan Furman | 410ef8e | 2016-06-04 12:06:26 -0700 | [diff] [blame] | 13893 | OS_FSPATH_METHODDEF |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 13894 | OS_GETRANDOM_METHODDEF |
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 13895 | OS_MEMFD_CREATE_METHODDEF |
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 13896 | #ifdef MS_WINDOWS |
13897 | OS__ADD_DLL_DIRECTORY_METHODDEF | ||||
13898 | OS__REMOVE_DLL_DIRECTORY_METHODDEF | ||||
13899 | #endif | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 13900 | {NULL, NULL} /* Sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13901 | }; |
13902 | |||||
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13903 | static int |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13904 | all_ins(PyObject *m) |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13905 | { |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13906 | #ifdef F_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13907 | if (PyModule_AddIntMacro(m, F_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13908 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13909 | #ifdef R_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13910 | if (PyModule_AddIntMacro(m, R_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13911 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13912 | #ifdef W_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13913 | if (PyModule_AddIntMacro(m, W_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13914 | #endif |
Guido van Rossum | 94f6f72 | 1999-01-06 18:42:14 +0000 | [diff] [blame] | 13915 | #ifdef X_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13916 | if (PyModule_AddIntMacro(m, X_OK)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13917 | #endif |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13918 | #ifdef NGROUPS_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13919 | if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1; |
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 13920 | #endif |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13921 | #ifdef TMP_MAX |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13922 | if (PyModule_AddIntMacro(m, TMP_MAX)) return -1; |
Fred Drake | 5ab8eaf | 1999-12-09 21:13:07 +0000 | [diff] [blame] | 13923 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13924 | #ifdef WCONTINUED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13925 | if (PyModule_AddIntMacro(m, WCONTINUED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13926 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13927 | #ifdef WNOHANG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13928 | if (PyModule_AddIntMacro(m, WNOHANG)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 13929 | #endif |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13930 | #ifdef WUNTRACED |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13931 | if (PyModule_AddIntMacro(m, WUNTRACED)) return -1; |
Fred Drake | 106c1a0 | 2002-04-23 15:58:02 +0000 | [diff] [blame] | 13932 | #endif |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13933 | #ifdef O_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13934 | if (PyModule_AddIntMacro(m, O_RDONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13935 | #endif |
13936 | #ifdef O_WRONLY | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13937 | if (PyModule_AddIntMacro(m, O_WRONLY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13938 | #endif |
13939 | #ifdef O_RDWR | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13940 | if (PyModule_AddIntMacro(m, O_RDWR)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13941 | #endif |
13942 | #ifdef O_NDELAY | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13943 | if (PyModule_AddIntMacro(m, O_NDELAY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13944 | #endif |
13945 | #ifdef O_NONBLOCK | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13946 | if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13947 | #endif |
13948 | #ifdef O_APPEND | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13949 | if (PyModule_AddIntMacro(m, O_APPEND)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13950 | #endif |
13951 | #ifdef O_DSYNC | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13952 | if (PyModule_AddIntMacro(m, O_DSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13953 | #endif |
13954 | #ifdef O_RSYNC | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13955 | if (PyModule_AddIntMacro(m, O_RSYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13956 | #endif |
13957 | #ifdef O_SYNC | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13958 | if (PyModule_AddIntMacro(m, O_SYNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13959 | #endif |
13960 | #ifdef O_NOCTTY | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13961 | if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13962 | #endif |
13963 | #ifdef O_CREAT | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13964 | if (PyModule_AddIntMacro(m, O_CREAT)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13965 | #endif |
13966 | #ifdef O_EXCL | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13967 | if (PyModule_AddIntMacro(m, O_EXCL)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13968 | #endif |
13969 | #ifdef O_TRUNC | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13970 | if (PyModule_AddIntMacro(m, O_TRUNC)) return -1; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 13971 | #endif |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13972 | #ifdef O_BINARY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13973 | if (PyModule_AddIntMacro(m, O_BINARY)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13974 | #endif |
13975 | #ifdef O_TEXT | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13976 | if (PyModule_AddIntMacro(m, O_TEXT)) return -1; |
Guido van Rossum | 98d9d09 | 1997-08-08 21:48:51 +0000 | [diff] [blame] | 13977 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13978 | #ifdef O_XATTR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13979 | if (PyModule_AddIntMacro(m, O_XATTR)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 13980 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13981 | #ifdef O_LARGEFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13982 | if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 13983 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13984 | #ifndef __GNU__ |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13985 | #ifdef O_SHLOCK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13986 | if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13987 | #endif |
13988 | #ifdef O_EXLOCK | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13989 | if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1; |
Skip Montanaro | 5ff1492 | 2005-05-16 02:42:22 +0000 | [diff] [blame] | 13990 | #endif |
doko@ubuntu.com | fcff437 | 2016-06-13 16:33:04 +0200 | [diff] [blame] | 13991 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13992 | #ifdef O_EXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13993 | if (PyModule_AddIntMacro(m, O_EXEC)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13994 | #endif |
13995 | #ifdef O_SEARCH | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13996 | if (PyModule_AddIntMacro(m, O_SEARCH)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 13997 | #endif |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 13998 | #ifdef O_PATH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 13999 | if (PyModule_AddIntMacro(m, O_PATH)) return -1; |
Benjamin Peterson | 3b965a2 | 2013-03-13 10:27:41 -0500 | [diff] [blame] | 14000 | #endif |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14001 | #ifdef O_TTY_INIT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14002 | if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1; |
Jesus Cea | cf38120 | 2012-04-24 20:44:40 +0200 | [diff] [blame] | 14003 | #endif |
Christian Heimes | 177b3f9 | 2013-08-16 14:35:09 +0200 | [diff] [blame] | 14004 | #ifdef O_TMPFILE |
14005 | if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1; | ||||
14006 | #endif | ||||
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14007 | #ifdef PRIO_PROCESS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14008 | if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14009 | #endif |
14010 | #ifdef PRIO_PGRP | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14011 | if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14012 | #endif |
14013 | #ifdef PRIO_USER | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14014 | if (PyModule_AddIntMacro(m, PRIO_USER)) return -1; |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14015 | #endif |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14016 | #ifdef O_CLOEXEC |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14017 | if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1; |
Charles-François Natali | 1e045b1 | 2011-05-22 20:42:32 +0200 | [diff] [blame] | 14018 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14019 | #ifdef O_ACCMODE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14020 | if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14021 | #endif |
Giampaolo Rodolà | 18e8bcb | 2011-02-25 20:57:54 +0000 | [diff] [blame] | 14022 | |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14023 | |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14024 | #ifdef SEEK_HOLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14025 | if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14026 | #endif |
14027 | #ifdef SEEK_DATA | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14028 | if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1; |
Jesus Cea | 9436361 | 2012-06-22 18:32:07 +0200 | [diff] [blame] | 14029 | #endif |
14030 | |||||
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14031 | /* MS Windows */ |
14032 | #ifdef O_NOINHERIT | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14033 | /* Don't inherit in child processes. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14034 | if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14035 | #endif |
14036 | #ifdef _O_SHORT_LIVED | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14037 | /* Optimize for short life (keep in memory). */ |
14038 | /* 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] | 14039 | if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14040 | #endif |
14041 | #ifdef O_TEMPORARY | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14042 | /* Automatically delete when last handle is closed. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14043 | if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14044 | #endif |
14045 | #ifdef O_RANDOM | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14046 | /* Optimize for random access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14047 | if (PyModule_AddIntMacro(m, O_RANDOM)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14048 | #endif |
14049 | #ifdef O_SEQUENTIAL | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14050 | /* Optimize for sequential access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14051 | if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14052 | #endif |
14053 | |||||
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14054 | /* GNU extensions. */ |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14055 | #ifdef O_ASYNC |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14056 | /* Send a SIGIO signal whenever input or output |
14057 | becomes available on file descriptor */ | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14058 | if (PyModule_AddIntMacro(m, O_ASYNC)) return -1; |
Alexandre Vassalotti | bee3253 | 2008-05-16 18:15:12 +0000 | [diff] [blame] | 14059 | #endif |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14060 | #ifdef O_DIRECT |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14061 | /* Direct disk access. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14062 | if (PyModule_AddIntMacro(m, O_DIRECT)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14063 | #endif |
14064 | #ifdef O_DIRECTORY | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14065 | /* Must be a directory. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14066 | if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14067 | #endif |
14068 | #ifdef O_NOFOLLOW | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14069 | /* Do not follow links. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14070 | if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1; |
Martin v. Löwis | 4fe3c27 | 2001-10-18 22:05:36 +0000 | [diff] [blame] | 14071 | #endif |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14072 | #ifdef O_NOLINKS |
14073 | /* 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] | 14074 | if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1; |
Jesus Cea | 1d642d2 | 2012-04-24 20:59:17 +0200 | [diff] [blame] | 14075 | #endif |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14076 | #ifdef O_NOATIME |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14077 | /* Do not update the access time. */ |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14078 | if (PyModule_AddIntMacro(m, O_NOATIME)) return -1; |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 14079 | #endif |
Guido van Rossum | d48f252 | 1997-12-05 22:19:34 +0000 | [diff] [blame] | 14080 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14081 | /* These come from sysexits.h */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14082 | #ifdef EX_OK |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14083 | if (PyModule_AddIntMacro(m, EX_OK)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14084 | #endif /* EX_OK */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14085 | #ifdef EX_USAGE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14086 | if (PyModule_AddIntMacro(m, EX_USAGE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14087 | #endif /* EX_USAGE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14088 | #ifdef EX_DATAERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14089 | if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14090 | #endif /* EX_DATAERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14091 | #ifdef EX_NOINPUT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14092 | if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14093 | #endif /* EX_NOINPUT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14094 | #ifdef EX_NOUSER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14095 | if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14096 | #endif /* EX_NOUSER */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14097 | #ifdef EX_NOHOST |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14098 | if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14099 | #endif /* EX_NOHOST */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14100 | #ifdef EX_UNAVAILABLE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14101 | if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14102 | #endif /* EX_UNAVAILABLE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14103 | #ifdef EX_SOFTWARE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14104 | if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14105 | #endif /* EX_SOFTWARE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14106 | #ifdef EX_OSERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14107 | if (PyModule_AddIntMacro(m, EX_OSERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14108 | #endif /* EX_OSERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14109 | #ifdef EX_OSFILE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14110 | if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14111 | #endif /* EX_OSFILE */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14112 | #ifdef EX_CANTCREAT |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14113 | if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14114 | #endif /* EX_CANTCREAT */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14115 | #ifdef EX_IOERR |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14116 | if (PyModule_AddIntMacro(m, EX_IOERR)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14117 | #endif /* EX_IOERR */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14118 | #ifdef EX_TEMPFAIL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14119 | if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14120 | #endif /* EX_TEMPFAIL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14121 | #ifdef EX_PROTOCOL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14122 | if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14123 | #endif /* EX_PROTOCOL */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14124 | #ifdef EX_NOPERM |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14125 | if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14126 | #endif /* EX_NOPERM */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14127 | #ifdef EX_CONFIG |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14128 | if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14129 | #endif /* EX_CONFIG */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14130 | #ifdef EX_NOTFOUND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14131 | if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1; |
Neal Norwitz | 8e914d9 | 2003-01-10 15:29:16 +0000 | [diff] [blame] | 14132 | #endif /* EX_NOTFOUND */ |
Barry Warsaw | 5676bd1 | 2003-01-07 20:57:09 +0000 | [diff] [blame] | 14133 | |
Amaury Forgeot d'Arc | 66d00ad | 2010-09-10 18:11:45 +0000 | [diff] [blame] | 14134 | /* statvfs */ |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14135 | #ifdef ST_RDONLY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14136 | if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14137 | #endif /* ST_RDONLY */ |
14138 | #ifdef ST_NOSUID | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14139 | if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1; |
Andrew M. Kuchling | 4ea04a3 | 2010-08-18 22:30:34 +0000 | [diff] [blame] | 14140 | #endif /* ST_NOSUID */ |
14141 | |||||
doko@ubuntu.com | ca616a2 | 2013-12-08 15:23:07 +0100 | [diff] [blame] | 14142 | /* GNU extensions */ |
14143 | #ifdef ST_NODEV | ||||
14144 | if (PyModule_AddIntMacro(m, ST_NODEV)) return -1; | ||||
14145 | #endif /* ST_NODEV */ | ||||
14146 | #ifdef ST_NOEXEC | ||||
14147 | if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1; | ||||
14148 | #endif /* ST_NOEXEC */ | ||||
14149 | #ifdef ST_SYNCHRONOUS | ||||
14150 | if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1; | ||||
14151 | #endif /* ST_SYNCHRONOUS */ | ||||
14152 | #ifdef ST_MANDLOCK | ||||
14153 | if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1; | ||||
14154 | #endif /* ST_MANDLOCK */ | ||||
14155 | #ifdef ST_WRITE | ||||
14156 | if (PyModule_AddIntMacro(m, ST_WRITE)) return -1; | ||||
14157 | #endif /* ST_WRITE */ | ||||
14158 | #ifdef ST_APPEND | ||||
14159 | if (PyModule_AddIntMacro(m, ST_APPEND)) return -1; | ||||
14160 | #endif /* ST_APPEND */ | ||||
14161 | #ifdef ST_NOATIME | ||||
14162 | if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1; | ||||
14163 | #endif /* ST_NOATIME */ | ||||
14164 | #ifdef ST_NODIRATIME | ||||
14165 | if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1; | ||||
14166 | #endif /* ST_NODIRATIME */ | ||||
14167 | #ifdef ST_RELATIME | ||||
14168 | if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1; | ||||
14169 | #endif /* ST_RELATIME */ | ||||
14170 | |||||
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14171 | /* FreeBSD sendfile() constants */ |
14172 | #ifdef SF_NODISKIO | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14173 | if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14174 | #endif |
14175 | #ifdef SF_MNOWAIT | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14176 | if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14177 | #endif |
14178 | #ifdef SF_SYNC | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14179 | if (PyModule_AddIntMacro(m, SF_SYNC)) return -1; |
Giampaolo Rodolà | c9c2c8b | 2011-02-25 14:39:16 +0000 | [diff] [blame] | 14180 | #endif |
14181 | |||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14182 | /* constants for posix_fadvise */ |
14183 | #ifdef POSIX_FADV_NORMAL | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14184 | if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14185 | #endif |
14186 | #ifdef POSIX_FADV_SEQUENTIAL | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14187 | if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14188 | #endif |
14189 | #ifdef POSIX_FADV_RANDOM | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14190 | if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14191 | #endif |
14192 | #ifdef POSIX_FADV_NOREUSE | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14193 | if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14194 | #endif |
14195 | #ifdef POSIX_FADV_WILLNEED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14196 | if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14197 | #endif |
14198 | #ifdef POSIX_FADV_DONTNEED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14199 | if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14200 | #endif |
14201 | |||||
14202 | /* constants for waitid */ | ||||
14203 | #if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID) | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14204 | if (PyModule_AddIntMacro(m, P_PID)) return -1; |
14205 | if (PyModule_AddIntMacro(m, P_PGID)) return -1; | ||||
14206 | if (PyModule_AddIntMacro(m, P_ALL)) return -1; | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14207 | #endif |
14208 | #ifdef WEXITED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14209 | if (PyModule_AddIntMacro(m, WEXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14210 | #endif |
14211 | #ifdef WNOWAIT | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14212 | if (PyModule_AddIntMacro(m, WNOWAIT)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14213 | #endif |
14214 | #ifdef WSTOPPED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14215 | if (PyModule_AddIntMacro(m, WSTOPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14216 | #endif |
14217 | #ifdef CLD_EXITED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14218 | if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14219 | #endif |
14220 | #ifdef CLD_DUMPED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14221 | if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14222 | #endif |
14223 | #ifdef CLD_TRAPPED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14224 | if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14225 | #endif |
14226 | #ifdef CLD_CONTINUED | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14227 | if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14228 | #endif |
14229 | |||||
14230 | /* constants for lockf */ | ||||
14231 | #ifdef F_LOCK | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14232 | if (PyModule_AddIntMacro(m, F_LOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14233 | #endif |
14234 | #ifdef F_TLOCK | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14235 | if (PyModule_AddIntMacro(m, F_TLOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14236 | #endif |
14237 | #ifdef F_ULOCK | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14238 | if (PyModule_AddIntMacro(m, F_ULOCK)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14239 | #endif |
14240 | #ifdef F_TEST | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14241 | if (PyModule_AddIntMacro(m, F_TEST)) return -1; |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14242 | #endif |
14243 | |||||
Pablo Galindo | 4defba3 | 2018-01-27 16:16:37 +0000 | [diff] [blame] | 14244 | #ifdef RWF_DSYNC |
14245 | if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1; | ||||
14246 | #endif | ||||
14247 | #ifdef RWF_HIPRI | ||||
14248 | if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1; | ||||
14249 | #endif | ||||
14250 | #ifdef RWF_SYNC | ||||
14251 | if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1; | ||||
14252 | #endif | ||||
14253 | #ifdef RWF_NOWAIT | ||||
14254 | if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1; | ||||
14255 | #endif | ||||
14256 | |||||
Pablo Galindo | 6c6ddf9 | 2018-01-29 01:56:10 +0000 | [diff] [blame] | 14257 | /* constants for posix_spawn */ |
14258 | #ifdef HAVE_POSIX_SPAWN | ||||
14259 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1; | ||||
14260 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1; | ||||
14261 | if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1; | ||||
14262 | #endif | ||||
14263 | |||||
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14264 | #if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN) |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14265 | if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1; |
14266 | if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1; | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14267 | if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1; |
pxinwr | f2d7ac7 | 2019-05-21 18:46:37 +0800 | [diff] [blame] | 14268 | #endif |
14269 | #ifdef HAVE_SPAWNV | ||||
14270 | if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1; | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14271 | if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1; |
Guido van Rossum | 246bc17 | 1999-02-01 23:54:31 +0000 | [diff] [blame] | 14272 | #endif |
14273 | |||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14274 | #ifdef HAVE_SCHED_H |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14275 | #ifdef SCHED_OTHER |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14276 | if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14277 | #endif |
14278 | #ifdef SCHED_FIFO | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14279 | if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14280 | #endif |
14281 | #ifdef SCHED_RR | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14282 | if (PyModule_AddIntMacro(m, SCHED_RR)) return -1; |
Benjamin Peterson | dbaa559 | 2016-07-30 23:21:50 -0700 | [diff] [blame] | 14283 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14284 | #ifdef SCHED_SPORADIC |
messi Liao | 0d32218 | 2017-06-13 22:30:43 +0800 | [diff] [blame] | 14285 | if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14286 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14287 | #ifdef SCHED_BATCH |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14288 | if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14289 | #endif |
14290 | #ifdef SCHED_IDLE | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14291 | if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14292 | #endif |
14293 | #ifdef SCHED_RESET_ON_FORK | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14294 | if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1; |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14295 | #endif |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14296 | #ifdef SCHED_SYS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14297 | if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14298 | #endif |
14299 | #ifdef SCHED_IA | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14300 | if (PyModule_AddIntMacro(m, SCHED_IA)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14301 | #endif |
14302 | #ifdef SCHED_FSS | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14303 | if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14304 | #endif |
14305 | #ifdef SCHED_FX | ||||
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14306 | if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1; |
Jesus Cea | f2cb4e8 | 2011-09-09 23:55:42 +0200 | [diff] [blame] | 14307 | #endif |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14308 | #endif |
14309 | |||||
Benjamin Peterson | 9428d53 | 2011-09-14 11:45:52 -0400 | [diff] [blame] | 14310 | #ifdef USE_XATTRS |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14311 | if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1; |
14312 | if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1; | ||||
14313 | if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1; | ||||
Benjamin Peterson | 799bd80 | 2011-08-31 22:15:17 -0400 | [diff] [blame] | 14314 | #endif |
14315 | |||||
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14316 | #if HAVE_DECL_RTLD_LAZY |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14317 | if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14318 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14319 | #if HAVE_DECL_RTLD_NOW |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14320 | if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14321 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14322 | #if HAVE_DECL_RTLD_GLOBAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14323 | if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14324 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14325 | #if HAVE_DECL_RTLD_LOCAL |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14326 | if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14327 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14328 | #if HAVE_DECL_RTLD_NODELETE |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14329 | if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14330 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14331 | #if HAVE_DECL_RTLD_NOLOAD |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14332 | if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14333 | #endif |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 14334 | #if HAVE_DECL_RTLD_DEEPBIND |
Charles-Francois Natali | 74ca886 | 2013-05-20 19:13:19 +0200 | [diff] [blame] | 14335 | if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1; |
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14336 | #endif |
Michael Felt | c5ae169 | 2017-12-19 13:58:49 +0100 | [diff] [blame] | 14337 | #if HAVE_DECL_RTLD_MEMBER |
14338 | if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1; | ||||
14339 | #endif | ||||
Victor Stinner | 8b905bd | 2011-10-25 13:34:04 +0200 | [diff] [blame] | 14340 | |
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14341 | #ifdef HAVE_GETRANDOM_SYSCALL |
14342 | if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; | ||||
14343 | if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; | ||||
14344 | #endif | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14345 | #ifdef HAVE_MEMFD_CREATE |
14346 | if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1; | ||||
14347 | if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1; | ||||
14348 | #ifdef MFD_HUGETLB | ||||
14349 | if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1; | ||||
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14350 | #endif |
14351 | #ifdef MFD_HUGE_SHIFT | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14352 | if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14353 | #endif |
14354 | #ifdef MFD_HUGE_MASK | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14355 | if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14356 | #endif |
14357 | #ifdef MFD_HUGE_64KB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14358 | if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14359 | #endif |
14360 | #ifdef MFD_HUGE_512KB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14361 | if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14362 | #endif |
14363 | #ifdef MFD_HUGE_1MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14364 | if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14365 | #endif |
14366 | #ifdef MFD_HUGE_2MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14367 | if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14368 | #endif |
14369 | #ifdef MFD_HUGE_8MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14370 | if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14371 | #endif |
14372 | #ifdef MFD_HUGE_16MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14373 | if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14374 | #endif |
14375 | #ifdef MFD_HUGE_32MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14376 | if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14377 | #endif |
14378 | #ifdef MFD_HUGE_256MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14379 | if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14380 | #endif |
14381 | #ifdef MFD_HUGE_512MB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14382 | if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14383 | #endif |
14384 | #ifdef MFD_HUGE_1GB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14385 | if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14386 | #endif |
14387 | #ifdef MFD_HUGE_2GB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14388 | if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1; |
Zackery Spytz | e70bfa95 | 2019-05-29 14:43:50 -0600 | [diff] [blame] | 14389 | #endif |
14390 | #ifdef MFD_HUGE_16GB | ||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14391 | if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1; |
14392 | #endif | ||||
14393 | #endif | ||||
Victor Stinner | 9b1f474 | 2016-09-06 16:18:52 -0700 | [diff] [blame] | 14394 | |
Giampaolo Rodola | 4a172cc | 2018-06-12 23:04:50 +0200 | [diff] [blame] | 14395 | #if defined(__APPLE__) |
14396 | if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1; | ||||
14397 | #endif | ||||
14398 | |||||
Steve Dower | 2438cdf | 2019-03-29 16:37:16 -0700 | [diff] [blame] | 14399 | #ifdef MS_WINDOWS |
14400 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1; | ||||
14401 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1; | ||||
14402 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1; | ||||
14403 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1; | ||||
14404 | if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1; | ||||
14405 | #endif | ||||
14406 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14407 | return 0; |
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14408 | } |
14409 | |||||
14410 | |||||
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14411 | static struct PyModuleDef posixmodule = { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14412 | PyModuleDef_HEAD_INIT, |
14413 | MODNAME, | ||||
14414 | posix__doc__, | ||||
14415 | -1, | ||||
14416 | posix_methods, | ||||
14417 | NULL, | ||||
14418 | NULL, | ||||
14419 | NULL, | ||||
14420 | NULL | ||||
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 14421 | }; |
14422 | |||||
14423 | |||||
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14424 | static const char * const have_functions[] = { |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14425 | |
14426 | #ifdef HAVE_FACCESSAT | ||||
14427 | "HAVE_FACCESSAT", | ||||
14428 | #endif | ||||
14429 | |||||
14430 | #ifdef HAVE_FCHDIR | ||||
14431 | "HAVE_FCHDIR", | ||||
14432 | #endif | ||||
14433 | |||||
14434 | #ifdef HAVE_FCHMOD | ||||
14435 | "HAVE_FCHMOD", | ||||
14436 | #endif | ||||
14437 | |||||
14438 | #ifdef HAVE_FCHMODAT | ||||
14439 | "HAVE_FCHMODAT", | ||||
14440 | #endif | ||||
14441 | |||||
14442 | #ifdef HAVE_FCHOWN | ||||
14443 | "HAVE_FCHOWN", | ||||
14444 | #endif | ||||
14445 | |||||
Larry Hastings | 00964ed | 2013-08-12 13:49:30 -0400 | [diff] [blame] | 14446 | #ifdef HAVE_FCHOWNAT |
14447 | "HAVE_FCHOWNAT", | ||||
14448 | #endif | ||||
14449 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14450 | #ifdef HAVE_FEXECVE |
14451 | "HAVE_FEXECVE", | ||||
14452 | #endif | ||||
14453 | |||||
14454 | #ifdef HAVE_FDOPENDIR | ||||
14455 | "HAVE_FDOPENDIR", | ||||
14456 | #endif | ||||
14457 | |||||
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14458 | #ifdef HAVE_FPATHCONF |
14459 | "HAVE_FPATHCONF", | ||||
14460 | #endif | ||||
14461 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14462 | #ifdef HAVE_FSTATAT |
14463 | "HAVE_FSTATAT", | ||||
14464 | #endif | ||||
14465 | |||||
14466 | #ifdef HAVE_FSTATVFS | ||||
14467 | "HAVE_FSTATVFS", | ||||
14468 | #endif | ||||
14469 | |||||
Steve Dower | fe0a41a | 2015-03-20 19:50:46 -0700 | [diff] [blame] | 14470 | #if defined HAVE_FTRUNCATE || defined MS_WINDOWS |
Georg Brandl | 306336b | 2012-06-24 12:55:33 +0200 | [diff] [blame] | 14471 | "HAVE_FTRUNCATE", |
14472 | #endif | ||||
14473 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14474 | #ifdef HAVE_FUTIMENS |
14475 | "HAVE_FUTIMENS", | ||||
14476 | #endif | ||||
14477 | |||||
14478 | #ifdef HAVE_FUTIMES | ||||
14479 | "HAVE_FUTIMES", | ||||
14480 | #endif | ||||
14481 | |||||
14482 | #ifdef HAVE_FUTIMESAT | ||||
14483 | "HAVE_FUTIMESAT", | ||||
14484 | #endif | ||||
14485 | |||||
14486 | #ifdef HAVE_LINKAT | ||||
14487 | "HAVE_LINKAT", | ||||
14488 | #endif | ||||
14489 | |||||
14490 | #ifdef HAVE_LCHFLAGS | ||||
14491 | "HAVE_LCHFLAGS", | ||||
14492 | #endif | ||||
14493 | |||||
14494 | #ifdef HAVE_LCHMOD | ||||
14495 | "HAVE_LCHMOD", | ||||
14496 | #endif | ||||
14497 | |||||
14498 | #ifdef HAVE_LCHOWN | ||||
14499 | "HAVE_LCHOWN", | ||||
14500 | #endif | ||||
14501 | |||||
14502 | #ifdef HAVE_LSTAT | ||||
14503 | "HAVE_LSTAT", | ||||
14504 | #endif | ||||
14505 | |||||
14506 | #ifdef HAVE_LUTIMES | ||||
14507 | "HAVE_LUTIMES", | ||||
14508 | #endif | ||||
14509 | |||||
Zackery Spytz | 43fdbd2 | 2019-05-29 13:57:07 -0600 | [diff] [blame] | 14510 | #ifdef HAVE_MEMFD_CREATE |
14511 | "HAVE_MEMFD_CREATE", | ||||
14512 | #endif | ||||
14513 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14514 | #ifdef HAVE_MKDIRAT |
14515 | "HAVE_MKDIRAT", | ||||
14516 | #endif | ||||
14517 | |||||
14518 | #ifdef HAVE_MKFIFOAT | ||||
14519 | "HAVE_MKFIFOAT", | ||||
14520 | #endif | ||||
14521 | |||||
14522 | #ifdef HAVE_MKNODAT | ||||
14523 | "HAVE_MKNODAT", | ||||
14524 | #endif | ||||
14525 | |||||
14526 | #ifdef HAVE_OPENAT | ||||
14527 | "HAVE_OPENAT", | ||||
14528 | #endif | ||||
14529 | |||||
14530 | #ifdef HAVE_READLINKAT | ||||
14531 | "HAVE_READLINKAT", | ||||
14532 | #endif | ||||
14533 | |||||
14534 | #ifdef HAVE_RENAMEAT | ||||
14535 | "HAVE_RENAMEAT", | ||||
14536 | #endif | ||||
14537 | |||||
14538 | #ifdef HAVE_SYMLINKAT | ||||
14539 | "HAVE_SYMLINKAT", | ||||
14540 | #endif | ||||
14541 | |||||
14542 | #ifdef HAVE_UNLINKAT | ||||
14543 | "HAVE_UNLINKAT", | ||||
14544 | #endif | ||||
14545 | |||||
14546 | #ifdef HAVE_UTIMENSAT | ||||
14547 | "HAVE_UTIMENSAT", | ||||
14548 | #endif | ||||
14549 | |||||
14550 | #ifdef MS_WINDOWS | ||||
14551 | "MS_WINDOWS", | ||||
14552 | #endif | ||||
14553 | |||||
14554 | NULL | ||||
14555 | }; | ||||
14556 | |||||
14557 | |||||
Mark Hammond | fe51c6d | 2002-08-02 02:27:13 +0000 | [diff] [blame] | 14558 | PyMODINIT_FUNC |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 14559 | INITFUNC(void) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14560 | { |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14561 | PyObject *m, *v; |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14562 | PyObject *list; |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 14563 | const char * const *trace; |
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14564 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14565 | m = PyModule_Create(&posixmodule); |
14566 | if (m == NULL) | ||||
14567 | return NULL; | ||||
Tim Peters | 5aa9160 | 2002-01-30 05:46:57 +0000 | [diff] [blame] | 14568 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14569 | /* Initialize environ dictionary */ |
14570 | v = convertenviron(); | ||||
14571 | Py_XINCREF(v); | ||||
14572 | if (v == NULL || PyModule_AddObject(m, "environ", v) != 0) | ||||
14573 | return NULL; | ||||
14574 | Py_DECREF(v); | ||||
Fred Drake | c968092 | 1999-12-13 16:37:25 +0000 | [diff] [blame] | 14575 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14576 | if (all_ins(m)) |
14577 | return NULL; | ||||
Barry Warsaw | 4a34209 | 1996-12-19 23:50:02 +0000 | [diff] [blame] | 14578 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14579 | if (setup_confname_tables(m)) |
14580 | return NULL; | ||||
Fred Drake | bec628d | 1999-12-15 18:31:10 +0000 | [diff] [blame] | 14581 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14582 | Py_INCREF(PyExc_OSError); |
14583 | PyModule_AddObject(m, "error", PyExc_OSError); | ||||
Fred Drake | 762e206 | 1999-08-26 17:23:54 +0000 | [diff] [blame] | 14584 | |
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14585 | #ifdef HAVE_PUTENV |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14586 | if (posix_putenv_garbage == NULL) |
14587 | posix_putenv_garbage = PyDict_New(); | ||||
Guido van Rossum | b3d3956 | 2000-01-31 18:41:26 +0000 | [diff] [blame] | 14588 | #endif |
Guido van Rossum | 98bf58f | 2001-10-18 20:34:25 +0000 | [diff] [blame] | 14589 | |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14590 | if (!initialized) { |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14591 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
14592 | waitid_result_desc.name = MODNAME ".waitid_result"; | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14593 | WaitidResultType = PyStructSequence_NewType(&waitid_result_desc); |
14594 | if (WaitidResultType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14595 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14596 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14597 | #endif |
14598 | |||||
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14599 | stat_result_desc.name = "os.stat_result"; /* see issue #19209 */ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14600 | stat_result_desc.fields[7].name = PyStructSequence_UnnamedField; |
14601 | stat_result_desc.fields[8].name = PyStructSequence_UnnamedField; | ||||
14602 | stat_result_desc.fields[9].name = PyStructSequence_UnnamedField; | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14603 | StatResultType = PyStructSequence_NewType(&stat_result_desc); |
14604 | if (StatResultType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14605 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14606 | } |
14607 | structseq_new = StatResultType->tp_new; | ||||
14608 | StatResultType->tp_new = statresult_new; | ||||
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14609 | |
Christian Heimes | 2582762 | 2013-10-12 01:27:08 +0200 | [diff] [blame] | 14610 | statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */ |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14611 | StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc); |
14612 | if (StatVFSResultType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14613 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14614 | } |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14615 | #ifdef NEED_TICKS_PER_SECOND |
14616 | # if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK) | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14617 | ticks_per_second = sysconf(_SC_CLK_TCK); |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14618 | # elif defined(HZ) |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14619 | ticks_per_second = HZ; |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14620 | # else |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14621 | ticks_per_second = 60; /* magic fallback value; may be bogus */ |
Martin v. Löwis | 05bfe1f | 2008-12-29 18:21:47 +0000 | [diff] [blame] | 14622 | # endif |
14623 | #endif | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14624 | |
William Orr | 81574b8 | 2018-10-01 22:19:56 -0700 | [diff] [blame] | 14625 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) |
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14626 | sched_param_desc.name = MODNAME ".sched_param"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14627 | SchedParamType = PyStructSequence_NewType(&sched_param_desc); |
14628 | if (SchedParamType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14629 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14630 | } |
14631 | SchedParamType->tp_new = os_sched_param; | ||||
Benjamin Peterson | 94b580d | 2011-08-02 17:30:04 -0500 | [diff] [blame] | 14632 | #endif |
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14633 | |
14634 | /* initialize TerminalSize_info */ | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14635 | TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc); |
14636 | if (TerminalSizeType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14637 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14638 | } |
Victor Stinner | 6036e44 | 2015-03-08 01:58:04 +0100 | [diff] [blame] | 14639 | |
14640 | /* initialize scandir types */ | ||||
14641 | if (PyType_Ready(&ScandirIteratorType) < 0) | ||||
14642 | return NULL; | ||||
14643 | if (PyType_Ready(&DirEntryType) < 0) | ||||
14644 | return NULL; | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14645 | } |
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14646 | #if defined(HAVE_WAITID) && !defined(__APPLE__) |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14647 | Py_INCREF((PyObject*) WaitidResultType); |
14648 | PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType); | ||||
Ross Lagerwall | 7807c35 | 2011-03-17 20:20:30 +0200 | [diff] [blame] | 14649 | #endif |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14650 | Py_INCREF((PyObject*) StatResultType); |
14651 | PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType); | ||||
14652 | Py_INCREF((PyObject*) StatVFSResultType); | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14653 | PyModule_AddObject(m, "statvfs_result", |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14654 | (PyObject*) StatVFSResultType); |
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14655 | |
14656 | #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14657 | Py_INCREF(SchedParamType); |
14658 | PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType); | ||||
Benjamin Peterson | e3298dd | 2011-08-02 18:40:46 -0500 | [diff] [blame] | 14659 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14660 | |
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14661 | times_result_desc.name = MODNAME ".times_result"; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14662 | TimesResultType = PyStructSequence_NewType(×_result_desc); |
14663 | if (TimesResultType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14664 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14665 | } |
14666 | PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType); | ||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14667 | |
14668 | uname_result_desc.name = MODNAME ".uname_result"; | ||||
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14669 | UnameResultType = PyStructSequence_NewType(&uname_result_desc); |
14670 | if (UnameResultType == NULL) { | ||||
Victor Stinner | 1c8f059 | 2013-07-22 22:24:54 +0200 | [diff] [blame] | 14671 | return NULL; |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14672 | } |
14673 | PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType); | ||||
Larry Hastings | 605a62d | 2012-06-24 04:33:36 -0700 | [diff] [blame] | 14674 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14675 | #ifdef __APPLE__ |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14676 | /* |
14677 | * Step 2 of weak-linking support on Mac OS X. | ||||
14678 | * | ||||
14679 | * The code below removes functions that are not available on the | ||||
14680 | * currently active platform. | ||||
14681 | * | ||||
14682 | * 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] | 14683 | * 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] | 14684 | * OSX 10.4. |
14685 | */ | ||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14686 | #ifdef HAVE_FSTATVFS |
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14687 | if (fstatvfs == NULL) { |
14688 | if (PyObject_DelAttrString(m, "fstatvfs") == -1) { | ||||
14689 | return NULL; | ||||
14690 | } | ||||
14691 | } | ||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14692 | #endif /* HAVE_FSTATVFS */ |
14693 | |||||
14694 | #ifdef HAVE_STATVFS | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14695 | if (statvfs == NULL) { |
14696 | if (PyObject_DelAttrString(m, "statvfs") == -1) { | ||||
14697 | return NULL; | ||||
14698 | } | ||||
14699 | } | ||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14700 | #endif /* HAVE_STATVFS */ |
14701 | |||||
14702 | # ifdef HAVE_LCHOWN | ||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14703 | if (lchown == NULL) { |
14704 | if (PyObject_DelAttrString(m, "lchown") == -1) { | ||||
14705 | return NULL; | ||||
14706 | } | ||||
14707 | } | ||||
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 14708 | #endif /* HAVE_LCHOWN */ |
14709 | |||||
14710 | |||||
14711 | #endif /* __APPLE__ */ | ||||
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14712 | |
Eddie Elizondo | 474eedf | 2018-11-13 04:09:31 -0800 | [diff] [blame] | 14713 | Py_INCREF(TerminalSizeType); |
14714 | PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType); | ||||
Antoine Pitrou | bcf2b59 | 2012-02-08 23:28:36 +0100 | [diff] [blame] | 14715 | |
Larry Hastings | 6fe20b3 | 2012-04-19 15:07:49 -0700 | [diff] [blame] | 14716 | billion = PyLong_FromLong(1000000000); |
14717 | if (!billion) | ||||
14718 | return NULL; | ||||
14719 | |||||
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14720 | /* suppress "function not used" warnings */ |
14721 | { | ||||
14722 | int ignored; | ||||
14723 | fd_specified("", -1); | ||||
14724 | follow_symlinks_specified("", 1); | ||||
14725 | dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1); | ||||
14726 | dir_fd_converter(Py_None, &ignored); | ||||
14727 | dir_fd_unavailable(Py_None, &ignored); | ||||
14728 | } | ||||
14729 | |||||
14730 | /* | ||||
14731 | * provide list of locally available functions | ||||
14732 | * so os.py can populate support_* lists | ||||
14733 | */ | ||||
14734 | list = PyList_New(0); | ||||
14735 | if (!list) | ||||
14736 | return NULL; | ||||
14737 | for (trace = have_functions; *trace; trace++) { | ||||
14738 | PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL); | ||||
14739 | if (!unicode) | ||||
14740 | return NULL; | ||||
14741 | if (PyList_Append(list, unicode)) | ||||
14742 | return NULL; | ||||
14743 | Py_DECREF(unicode); | ||||
14744 | } | ||||
14745 | PyModule_AddObject(m, "_have_functions", list); | ||||
Ned Deily | eb3be66 | 2016-08-15 14:40:38 -0400 | [diff] [blame] | 14746 | |
14747 | Py_INCREF((PyObject *) &DirEntryType); | ||||
Brett Cannon | a32c4d0 | 2016-06-24 14:14:44 -0700 | [diff] [blame] | 14748 | PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType); |
Larry Hastings | 9cf065c | 2012-06-22 16:30:09 -0700 | [diff] [blame] | 14749 | |
14750 | initialized = 1; | ||||
14751 | |||||
Victor Stinner | 8c62be8 | 2010-05-06 00:08:46 +0000 | [diff] [blame] | 14752 | return m; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 14753 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 14754 | |
14755 | #ifdef __cplusplus | ||||
14756 | } | ||||
14757 | #endif |