blob: 3f760183575aacaaa189e4ad392f474f6854e106 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* POSIX module implementation */
3
Jesus Ceaab70e2a2012-10-05 01:48:08 +02004/* 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 MacIntyre6c73af22002-03-03 03:07:07 +00006 functions are either unimplemented or implemented differently. The source
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007 assumes that for Windows NT, the macro 'MS_WINDOWS' is defined independent
Guido van Rossumad0ee831995-03-01 10:34:45 +00008 of the compiler used. Different compilers define their own feature
Victor Stinnerf427a142014-10-22 12:33:23 +02009 test macro, e.g. '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Larry Hastings31826802013-10-19 00:09:25 -070011
12
Thomas Wouters477c8d52006-05-27 19:21:47 +000013#ifdef __APPLE__
14 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000015 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000016 * 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 Wouters68bc4f92006-03-01 01:05:10 +000025#define PY_SSIZE_T_CLEAN
26
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000027#include "Python.h"
Antoine Pitrou346cbd32017-05-27 17:50:54 +020028#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010029#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020030#ifndef MS_WINDOWS
31#include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010032#else
33#include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020034#endif
Victor Stinner621cebe2018-11-12 16:53:38 +010035#include "pycore_pystate.h"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000036
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020037/* On android API level 21, 'AT_EACCESS' is not declared although
38 * HAVE_FACCESSAT is defined. */
39#ifdef __ANDROID__
40#undef HAVE_FACCESSAT
41#endif
42
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000043#include <stdio.h> /* needed for ctermid() */
44
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000045#ifdef __cplusplus
46extern "C" {
47#endif
48
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000049PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000050"This module provides access to operating system functionality that is\n\
51standardized by the C Standard and the POSIX standard (a thinly\n\
52disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000053corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000055
Ross Lagerwall4d076da2011-03-18 06:56:53 +020056#ifdef HAVE_SYS_UIO_H
57#include <sys/uio.h>
58#endif
59
Christian Heimes75b96182017-09-05 15:53:09 +020060#ifdef HAVE_SYS_SYSMACROS_H
61/* GNU C Library: major(), minor(), makedev() */
62#include <sys/sysmacros.h>
63#endif
64
Thomas Wouters0e3f5912006-08-11 14:57:12 +000065#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000066#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000067#endif /* HAVE_SYS_TYPES_H */
68
69#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000070#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000072
Guido van Rossum36bc6801995-06-14 22:54:23 +000073#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000074#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000075#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000076
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000078#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000080
Guido van Rossumb6775db1994-08-01 11:34:53 +000081#ifdef HAVE_FCNTL_H
82#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000083#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000084
Guido van Rossuma6535fd2001-10-18 19:44:10 +000085#ifdef HAVE_GRP_H
86#include <grp.h>
87#endif
88
Barry Warsaw5676bd12003-01-07 20:57:09 +000089#ifdef HAVE_SYSEXITS_H
90#include <sysexits.h>
91#endif /* HAVE_SYSEXITS_H */
92
Anthony Baxter8a560de2004-10-13 15:30:56 +000093#ifdef HAVE_SYS_LOADAVG_H
94#include <sys/loadavg.h>
95#endif
96
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000097#ifdef HAVE_SYS_SENDFILE_H
98#include <sys/sendfile.h>
99#endif
100
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200101#if defined(__APPLE__)
102#include <copyfile.h>
103#endif
104
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500105#ifdef HAVE_SCHED_H
106#include <sched.h>
107#endif
108
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500109#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500110#undef HAVE_SCHED_SETAFFINITY
111#endif
112
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200113#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400114#define USE_XATTRS
115#endif
116
117#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400118#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400119#endif
120
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000121#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
122#ifdef HAVE_SYS_SOCKET_H
123#include <sys/socket.h>
124#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000125#endif
126
Victor Stinner8b905bd2011-10-25 13:34:04 +0200127#ifdef HAVE_DLFCN_H
128#include <dlfcn.h>
129#endif
130
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200131#ifdef __hpux
132#include <sys/mpctl.h>
133#endif
134
135#if defined(__DragonFly__) || \
136 defined(__OpenBSD__) || \
137 defined(__FreeBSD__) || \
138 defined(__NetBSD__) || \
139 defined(__APPLE__)
140#include <sys/sysctl.h>
141#endif
142
Victor Stinner9b1f4742016-09-06 16:18:52 -0700143#ifdef HAVE_LINUX_RANDOM_H
144# include <linux/random.h>
145#endif
146#ifdef HAVE_GETRANDOM_SYSCALL
147# include <sys/syscall.h>
148#endif
149
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100150#if defined(MS_WINDOWS)
151# define TERMSIZE_USE_CONIO
152#elif defined(HAVE_SYS_IOCTL_H)
153# include <sys/ioctl.h>
154# if defined(HAVE_TERMIOS_H)
155# include <termios.h>
156# endif
157# if defined(TIOCGWINSZ)
158# define TERMSIZE_USE_IOCTL
159# endif
160#endif /* MS_WINDOWS */
161
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000162/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000163/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000164#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000165#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000166#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000167#include <process.h>
168#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000169#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000170#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000171#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000172#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000173#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700174#define HAVE_WSPAWNV 1
175#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000176#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000177#define HAVE_SYSTEM 1
178#define HAVE_CWAIT 1
179#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000180#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000181#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000182/* Unix functions that the configure script doesn't check for */
183#define HAVE_EXECV 1
184#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000186#define HAVE_FORK1 1
187#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000188#define HAVE_GETEGID 1
189#define HAVE_GETEUID 1
190#define HAVE_GETGID 1
191#define HAVE_GETPPID 1
192#define HAVE_GETUID 1
193#define HAVE_KILL 1
194#define HAVE_OPENDIR 1
195#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000196#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000198#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000199#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000200#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000201
Victor Stinnera2f7c002012-02-08 03:36:25 +0100202
Larry Hastings61272b72014-01-07 12:41:53 -0800203/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000204# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800205module os
Larry Hastings61272b72014-01-07 12:41:53 -0800206[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000207/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100208
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000210
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000211#if defined(__sgi)&&_COMPILER_VERSION>=700
212/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
213 (default) */
214extern char *ctermid_r(char *);
215#endif
216
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000217#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000218
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000219#ifdef HAVE_POSIX_SPAWN
220#include <spawn.h>
221#endif
222
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223#ifdef HAVE_UTIME_H
224#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000225#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000226
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000227#ifdef HAVE_SYS_UTIME_H
228#include <sys/utime.h>
229#define HAVE_UTIME_H /* pretend we do for the rest of this file */
230#endif /* HAVE_SYS_UTIME_H */
231
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#ifdef HAVE_SYS_TIMES_H
233#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000234#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235
236#ifdef HAVE_SYS_PARAM_H
237#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000238#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000239
240#ifdef HAVE_SYS_UTSNAME_H
241#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000242#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000244#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000246#define NAMLEN(dirent) strlen((dirent)->d_name)
247#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000248#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000249#include <direct.h>
250#define NAMLEN(dirent) strlen((dirent)->d_name)
251#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000252#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000253#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000254#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000255#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000256#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000257#endif
258#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000260#endif
261#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#endif
264#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000265
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000266#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000269#endif
270#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000272#endif
273#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000275#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000276#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000277#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000278#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100279#ifndef IO_REPARSE_TAG_MOUNT_POINT
280#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
281#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000282#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000283#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000285#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000286#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000287#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
288#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000289static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000290#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000291#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292
Tim Petersbc2e10e2002-03-03 23:17:02 +0000293#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000294#if defined(PATH_MAX) && PATH_MAX > 1024
295#define MAXPATHLEN PATH_MAX
296#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000297#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000298#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000299#endif /* MAXPATHLEN */
300
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000301#ifdef UNION_WAIT
302/* Emulate some macros on systems that have a union instead of macros */
303
304#ifndef WIFEXITED
305#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
306#endif
307
308#ifndef WEXITSTATUS
309#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
310#endif
311
312#ifndef WTERMSIG
313#define WTERMSIG(u_wait) ((u_wait).w_termsig)
314#endif
315
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316#define WAIT_TYPE union wait
317#define WAIT_STATUS_INT(s) (s.w_status)
318
319#else /* !UNION_WAIT */
320#define WAIT_TYPE int
321#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000322#endif /* UNION_WAIT */
323
Greg Wardb48bc172000-03-01 21:51:56 +0000324/* Don't use the "_r" form if we don't need it (also, won't have a
325 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200326#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000327#define USE_CTERMID_R
328#endif
329
Fred Drake699f3522000-06-29 21:12:41 +0000330/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000331#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000332#undef FSTAT
333#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200334#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000335# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700336# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200337# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800338# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000339#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000340# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700341# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000342# define FSTAT fstat
343# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000344#endif
345
Tim Peters11b23062003-04-23 02:39:17 +0000346#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000347#include <sys/mkdev.h>
348#else
349#if defined(MAJOR_IN_SYSMACROS)
350#include <sys/sysmacros.h>
351#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000352#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
353#include <sys/mkdev.h>
354#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000355#endif
Fred Drake699f3522000-06-29 21:12:41 +0000356
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200357#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100358#define INITFUNC PyInit_nt
359#define MODNAME "nt"
360#else
361#define INITFUNC PyInit_posix
362#define MODNAME "posix"
363#endif
364
jcea6c51d512018-01-28 14:00:08 +0100365#if defined(__sun)
366/* Something to implement in autoconf, not present in autoconf 2.69 */
367#define HAVE_STRUCT_STAT_ST_FSTYPE 1
368#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200369
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800370#ifdef _Py_MEMORY_SANITIZER
371# include <sanitizer/msan_interface.h>
372#endif
373
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200374#ifdef HAVE_FORK
375static void
376run_at_forkers(PyObject *lst, int reverse)
377{
378 Py_ssize_t i;
379 PyObject *cpy;
380
381 if (lst != NULL) {
382 assert(PyList_CheckExact(lst));
383
384 /* Use a list copy in case register_at_fork() is called from
385 * one of the callbacks.
386 */
387 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
388 if (cpy == NULL)
389 PyErr_WriteUnraisable(lst);
390 else {
391 if (reverse)
392 PyList_Reverse(cpy);
393 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
394 PyObject *func, *res;
395 func = PyList_GET_ITEM(cpy, i);
396 res = PyObject_CallObject(func, NULL);
397 if (res == NULL)
398 PyErr_WriteUnraisable(func);
399 else
400 Py_DECREF(res);
401 }
402 Py_DECREF(cpy);
403 }
404 }
405}
406
407void
408PyOS_BeforeFork(void)
409{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200410 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200411
412 _PyImport_AcquireLock();
413}
414
415void
416PyOS_AfterFork_Parent(void)
417{
418 if (_PyImport_ReleaseLock() <= 0)
419 Py_FatalError("failed releasing import lock after fork");
420
Victor Stinnercaba55b2018-08-03 15:33:52 +0200421 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200422}
423
424void
425PyOS_AfterFork_Child(void)
426{
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200427 _PyGILState_Reinit();
Eric Snow59032962018-09-14 14:17:20 -0700428 _PyInterpreterState_DeleteExceptMain();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200429 PyEval_ReInitThreads();
430 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200431 _PySignal_AfterFork();
Eric Snow8479a342019-03-08 23:44:33 -0700432 _PyRuntimeState_ReInitThreads();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200433
Victor Stinnercaba55b2018-08-03 15:33:52 +0200434 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200435}
436
437static int
438register_at_forker(PyObject **lst, PyObject *func)
439{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700440 if (func == NULL) /* nothing to register? do nothing. */
441 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200442 if (*lst == NULL) {
443 *lst = PyList_New(0);
444 if (*lst == NULL)
445 return -1;
446 }
447 return PyList_Append(*lst, func);
448}
449#endif
450
451/* Legacy wrapper */
452void
453PyOS_AfterFork(void)
454{
455#ifdef HAVE_FORK
456 PyOS_AfterFork_Child();
457#endif
458}
459
460
Victor Stinner6036e442015-03-08 01:58:04 +0100461#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200462/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700463void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
464void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200465 ULONG, struct _Py_stat_struct *);
466#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700467
468#ifdef MS_WINDOWS
469static int
470win32_warn_bytes_api()
471{
472 return PyErr_WarnEx(PyExc_DeprecationWarning,
473 "The Windows bytes API has been deprecated, "
474 "use Unicode filenames instead",
475 1);
476}
477#endif
478
479
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200480#ifndef MS_WINDOWS
481PyObject *
482_PyLong_FromUid(uid_t uid)
483{
484 if (uid == (uid_t)-1)
485 return PyLong_FromLong(-1);
486 return PyLong_FromUnsignedLong(uid);
487}
488
489PyObject *
490_PyLong_FromGid(gid_t gid)
491{
492 if (gid == (gid_t)-1)
493 return PyLong_FromLong(-1);
494 return PyLong_FromUnsignedLong(gid);
495}
496
497int
498_Py_Uid_Converter(PyObject *obj, void *p)
499{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700500 uid_t uid;
501 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200502 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200503 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700504 unsigned long uresult;
505
506 index = PyNumber_Index(obj);
507 if (index == NULL) {
508 PyErr_Format(PyExc_TypeError,
509 "uid should be integer, not %.200s",
510 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200511 return 0;
512 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700513
514 /*
515 * Handling uid_t is complicated for two reasons:
516 * * Although uid_t is (always?) unsigned, it still
517 * accepts -1.
518 * * We don't know its size in advance--it may be
519 * bigger than an int, or it may be smaller than
520 * a long.
521 *
522 * So a bit of defensive programming is in order.
523 * Start with interpreting the value passed
524 * in as a signed long and see if it works.
525 */
526
527 result = PyLong_AsLongAndOverflow(index, &overflow);
528
529 if (!overflow) {
530 uid = (uid_t)result;
531
532 if (result == -1) {
533 if (PyErr_Occurred())
534 goto fail;
535 /* It's a legitimate -1, we're done. */
536 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200537 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700538
539 /* Any other negative number is disallowed. */
540 if (result < 0)
541 goto underflow;
542
543 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200544 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700545 (long)uid != result)
546 goto underflow;
547 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200548 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700549
550 if (overflow < 0)
551 goto underflow;
552
553 /*
554 * Okay, the value overflowed a signed long. If it
555 * fits in an *unsigned* long, it may still be okay,
556 * as uid_t may be unsigned long on this platform.
557 */
558 uresult = PyLong_AsUnsignedLong(index);
559 if (PyErr_Occurred()) {
560 if (PyErr_ExceptionMatches(PyExc_OverflowError))
561 goto overflow;
562 goto fail;
563 }
564
565 uid = (uid_t)uresult;
566
567 /*
568 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
569 * but this value would get interpreted as (uid_t)-1 by chown
570 * and its siblings. That's not what the user meant! So we
571 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100572 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700573 */
574 if (uid == (uid_t)-1)
575 goto overflow;
576
577 /* Ensure the value wasn't truncated. */
578 if (sizeof(uid_t) < sizeof(long) &&
579 (unsigned long)uid != uresult)
580 goto overflow;
581 /* fallthrough */
582
583success:
584 Py_DECREF(index);
585 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200586 return 1;
587
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700588underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200589 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700590 "uid is less than minimum");
591 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200592
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700593overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200594 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700595 "uid is greater than maximum");
596 /* fallthrough */
597
598fail:
599 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200600 return 0;
601}
602
603int
604_Py_Gid_Converter(PyObject *obj, void *p)
605{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700606 gid_t gid;
607 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200608 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200609 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700610 unsigned long uresult;
611
612 index = PyNumber_Index(obj);
613 if (index == NULL) {
614 PyErr_Format(PyExc_TypeError,
615 "gid should be integer, not %.200s",
616 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200617 return 0;
618 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700619
620 /*
621 * Handling gid_t is complicated for two reasons:
622 * * Although gid_t is (always?) unsigned, it still
623 * accepts -1.
624 * * We don't know its size in advance--it may be
625 * bigger than an int, or it may be smaller than
626 * a long.
627 *
628 * So a bit of defensive programming is in order.
629 * Start with interpreting the value passed
630 * in as a signed long and see if it works.
631 */
632
633 result = PyLong_AsLongAndOverflow(index, &overflow);
634
635 if (!overflow) {
636 gid = (gid_t)result;
637
638 if (result == -1) {
639 if (PyErr_Occurred())
640 goto fail;
641 /* It's a legitimate -1, we're done. */
642 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200643 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700644
645 /* Any other negative number is disallowed. */
646 if (result < 0) {
647 goto underflow;
648 }
649
650 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200651 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700652 (long)gid != result)
653 goto underflow;
654 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200655 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700656
657 if (overflow < 0)
658 goto underflow;
659
660 /*
661 * Okay, the value overflowed a signed long. If it
662 * fits in an *unsigned* long, it may still be okay,
663 * as gid_t may be unsigned long on this platform.
664 */
665 uresult = PyLong_AsUnsignedLong(index);
666 if (PyErr_Occurred()) {
667 if (PyErr_ExceptionMatches(PyExc_OverflowError))
668 goto overflow;
669 goto fail;
670 }
671
672 gid = (gid_t)uresult;
673
674 /*
675 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
676 * but this value would get interpreted as (gid_t)-1 by chown
677 * and its siblings. That's not what the user meant! So we
678 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100679 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700680 */
681 if (gid == (gid_t)-1)
682 goto overflow;
683
684 /* Ensure the value wasn't truncated. */
685 if (sizeof(gid_t) < sizeof(long) &&
686 (unsigned long)gid != uresult)
687 goto overflow;
688 /* fallthrough */
689
690success:
691 Py_DECREF(index);
692 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200693 return 1;
694
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700695underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200696 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700697 "gid is less than minimum");
698 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200699
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700700overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200701 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700702 "gid is greater than maximum");
703 /* fallthrough */
704
705fail:
706 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200707 return 0;
708}
709#endif /* MS_WINDOWS */
710
711
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700712#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800713
714
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200715#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
716static int
717_Py_Dev_Converter(PyObject *obj, void *p)
718{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200719 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200720 if (PyErr_Occurred())
721 return 0;
722 return 1;
723}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800724#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200725
726
Larry Hastings9cf065c2012-06-22 16:30:09 -0700727#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400728/*
729 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
730 * without the int cast, the value gets interpreted as uint (4291925331),
731 * which doesn't play nicely with all the initializer lines in this file that
732 * look like this:
733 * int dir_fd = DEFAULT_DIR_FD;
734 */
735#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700736#else
737#define DEFAULT_DIR_FD (-100)
738#endif
739
740static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300741_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200742{
743 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700744 long long_value;
745
746 PyObject *index = PyNumber_Index(o);
747 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700748 return 0;
749 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700750
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300751 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700752 long_value = PyLong_AsLongAndOverflow(index, &overflow);
753 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300754 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200755 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700756 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700757 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700758 return 0;
759 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200760 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700761 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700762 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700763 return 0;
764 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700765
Larry Hastings9cf065c2012-06-22 16:30:09 -0700766 *p = (int)long_value;
767 return 1;
768}
769
770static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200771dir_fd_converter(PyObject *o, void *p)
772{
773 if (o == Py_None) {
774 *(int *)p = DEFAULT_DIR_FD;
775 return 1;
776 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300777 else if (PyIndex_Check(o)) {
778 return _fd_converter(o, (int *)p);
779 }
780 else {
781 PyErr_Format(PyExc_TypeError,
782 "argument should be integer or None, not %.200s",
783 Py_TYPE(o)->tp_name);
784 return 0;
785 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700786}
787
788
Larry Hastings9cf065c2012-06-22 16:30:09 -0700789/*
790 * A PyArg_ParseTuple "converter" function
791 * that handles filesystem paths in the manner
792 * preferred by the os module.
793 *
794 * path_converter accepts (Unicode) strings and their
795 * subclasses, and bytes and their subclasses. What
796 * it does with the argument depends on the platform:
797 *
798 * * On Windows, if we get a (Unicode) string we
799 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700800 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700801 *
802 * * On all other platforms, strings are encoded
803 * to bytes using PyUnicode_FSConverter, then we
804 * extract the char * from the bytes object and
805 * return that.
806 *
807 * path_converter also optionally accepts signed
808 * integers (representing open file descriptors) instead
809 * of path strings.
810 *
811 * Input fields:
812 * path.nullable
813 * If nonzero, the path is permitted to be None.
814 * path.allow_fd
815 * If nonzero, the path is permitted to be a file handle
816 * (a signed int) instead of a string.
817 * path.function_name
818 * If non-NULL, path_converter will use that as the name
819 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700820 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700821 * path.argument_name
822 * If non-NULL, path_converter will use that as the name
823 * of the parameter in error messages.
824 * (If path.argument_name is NULL it uses "path".)
825 *
826 * Output fields:
827 * path.wide
828 * Points to the path if it was expressed as Unicode
829 * and was not encoded. (Only used on Windows.)
830 * path.narrow
831 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700832 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000833 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700834 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700835 * path.fd
836 * Contains a file descriptor if path.accept_fd was true
837 * and the caller provided a signed integer instead of any
838 * sort of string.
839 *
840 * WARNING: if your "path" parameter is optional, and is
841 * unspecified, path_converter will never get called.
842 * So if you set allow_fd, you *MUST* initialize path.fd = -1
843 * yourself!
844 * path.length
845 * The length of the path in characters, if specified as
846 * a string.
847 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800848 * The original object passed in (if get a PathLike object,
849 * the result of PyOS_FSPath() is treated as the original object).
850 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700851 * path.cleanup
852 * For internal use only. May point to a temporary object.
853 * (Pay no attention to the man behind the curtain.)
854 *
855 * At most one of path.wide or path.narrow will be non-NULL.
856 * If path was None and path.nullable was set,
857 * or if path was an integer and path.allow_fd was set,
858 * both path.wide and path.narrow will be NULL
859 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200860 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700861 * path_converter takes care to not write to the path_t
862 * unless it's successful. However it must reset the
863 * "cleanup" field each time it's called.
864 *
865 * Use as follows:
866 * path_t path;
867 * memset(&path, 0, sizeof(path));
868 * PyArg_ParseTuple(args, "O&", path_converter, &path);
869 * // ... use values from path ...
870 * path_cleanup(&path);
871 *
872 * (Note that if PyArg_Parse fails you don't need to call
873 * path_cleanup(). However it is safe to do so.)
874 */
875typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100876 const char *function_name;
877 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700878 int nullable;
879 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300880 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700881#ifdef MS_WINDOWS
882 BOOL narrow;
883#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300884 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700885#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700886 int fd;
887 Py_ssize_t length;
888 PyObject *object;
889 PyObject *cleanup;
890} path_t;
891
Steve Dowercc16be82016-09-08 10:35:16 -0700892#ifdef MS_WINDOWS
893#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
894 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
895#else
Larry Hastings2f936352014-08-05 14:04:04 +1000896#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
897 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700898#endif
Larry Hastings31826802013-10-19 00:09:25 -0700899
Larry Hastings9cf065c2012-06-22 16:30:09 -0700900static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800901path_cleanup(path_t *path)
902{
903 Py_CLEAR(path->object);
904 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700905}
906
907static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300908path_converter(PyObject *o, void *p)
909{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700910 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800911 PyObject *bytes = NULL;
912 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700913 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300914 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700915#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800916 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700917 const wchar_t *wide;
918#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700919
920#define FORMAT_EXCEPTION(exc, fmt) \
921 PyErr_Format(exc, "%s%s" fmt, \
922 path->function_name ? path->function_name : "", \
923 path->function_name ? ": " : "", \
924 path->argument_name ? path->argument_name : "path")
925
926 /* Py_CLEANUP_SUPPORTED support */
927 if (o == NULL) {
928 path_cleanup(path);
929 return 1;
930 }
931
Brett Cannon3f9183b2016-08-26 14:44:48 -0700932 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800933 path->object = path->cleanup = NULL;
934 /* path->object owns a reference to the original object */
935 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700936
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300937 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700938 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700939#ifdef MS_WINDOWS
940 path->narrow = FALSE;
941#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700942 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700943#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700944 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800945 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700946 }
947
Brett Cannon3f9183b2016-08-26 14:44:48 -0700948 /* Only call this here so that we don't treat the return value of
949 os.fspath() as an fd or buffer. */
950 is_index = path->allow_fd && PyIndex_Check(o);
951 is_buffer = PyObject_CheckBuffer(o);
952 is_bytes = PyBytes_Check(o);
953 is_unicode = PyUnicode_Check(o);
954
955 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
956 /* Inline PyOS_FSPath() for better error messages. */
957 _Py_IDENTIFIER(__fspath__);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000958 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700959
960 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
961 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800962 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700963 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000964 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700965 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000966 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700967 goto error_exit;
968 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000969 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700970 is_unicode = 1;
971 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000972 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700973 is_bytes = 1;
974 }
975 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000976 PyErr_Format(PyExc_TypeError,
977 "expected %.200s.__fspath__() to return str or bytes, "
978 "not %.200s", Py_TYPE(o)->tp_name,
979 Py_TYPE(res)->tp_name);
980 Py_DECREF(res);
981 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700982 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +0000983
984 /* still owns a reference to the original object */
985 Py_DECREF(o);
986 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700987 }
988
989 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700990#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +0200991 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +0100992 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800993 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700994 }
Victor Stinner59799a82013-11-13 14:17:30 +0100995 if (length > 32767) {
996 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +0800997 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700998 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300999 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001000 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001001 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001002 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001003
1004 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001005 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001006 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001007 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001008#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001009 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001010 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001011 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001012#endif
1013 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001014 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001015 bytes = o;
1016 Py_INCREF(bytes);
1017 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001018 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001019 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001020 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001021 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1022 "%s%s%s should be %s, not %.200s",
1023 path->function_name ? path->function_name : "",
1024 path->function_name ? ": " : "",
1025 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001026 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1027 "integer or None" :
1028 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1029 path->nullable ? "string, bytes, os.PathLike or None" :
1030 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001031 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001032 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001033 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001034 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001035 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001036 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001037 }
1038 }
Steve Dowercc16be82016-09-08 10:35:16 -07001039 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001040 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001041 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001042 }
1043 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001044#ifdef MS_WINDOWS
1045 path->narrow = FALSE;
1046#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001047 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001048#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001049 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001050 }
1051 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001052 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001053 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1054 path->function_name ? path->function_name : "",
1055 path->function_name ? ": " : "",
1056 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001057 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1058 "integer or None" :
1059 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1060 path->nullable ? "string, bytes, os.PathLike or None" :
1061 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001062 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001063 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001064 }
1065
Larry Hastings9cf065c2012-06-22 16:30:09 -07001066 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001067 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001068 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001069 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001070 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001071 }
1072
Steve Dowercc16be82016-09-08 10:35:16 -07001073#ifdef MS_WINDOWS
1074 wo = PyUnicode_DecodeFSDefaultAndSize(
1075 narrow,
1076 length
1077 );
1078 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001079 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001080 }
1081
Xiang Zhang04316c42017-01-08 23:26:57 +08001082 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001083 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001084 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001085 }
1086 if (length > 32767) {
1087 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001088 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001089 }
1090 if (wcslen(wide) != length) {
1091 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001092 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001093 }
1094 path->wide = wide;
1095 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001096 path->cleanup = wo;
1097 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001098#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001099 path->wide = NULL;
1100 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001101 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001102 /* Still a reference owned by path->object, don't have to
1103 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001104 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001105 }
1106 else {
1107 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001108 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001109#endif
1110 path->fd = -1;
1111
1112 success_exit:
1113 path->length = length;
1114 path->object = o;
1115 return Py_CLEANUP_SUPPORTED;
1116
1117 error_exit:
1118 Py_XDECREF(o);
1119 Py_XDECREF(bytes);
1120#ifdef MS_WINDOWS
1121 Py_XDECREF(wo);
1122#endif
1123 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001124}
1125
1126static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001127argument_unavailable_error(const char *function_name, const char *argument_name)
1128{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001129 PyErr_Format(PyExc_NotImplementedError,
1130 "%s%s%s unavailable on this platform",
1131 (function_name != NULL) ? function_name : "",
1132 (function_name != NULL) ? ": ": "",
1133 argument_name);
1134}
1135
1136static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001137dir_fd_unavailable(PyObject *o, void *p)
1138{
1139 int dir_fd;
1140 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001141 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001142 if (dir_fd != DEFAULT_DIR_FD) {
1143 argument_unavailable_error(NULL, "dir_fd");
1144 return 0;
1145 }
1146 *(int *)p = dir_fd;
1147 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001148}
1149
1150static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001151fd_specified(const char *function_name, int fd)
1152{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001153 if (fd == -1)
1154 return 0;
1155
1156 argument_unavailable_error(function_name, "fd");
1157 return 1;
1158}
1159
1160static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001161follow_symlinks_specified(const char *function_name, int follow_symlinks)
1162{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001163 if (follow_symlinks)
1164 return 0;
1165
1166 argument_unavailable_error(function_name, "follow_symlinks");
1167 return 1;
1168}
1169
1170static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001171path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1172{
Steve Dowercc16be82016-09-08 10:35:16 -07001173 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1174#ifndef MS_WINDOWS
1175 && !path->narrow
1176#endif
1177 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001178 PyErr_Format(PyExc_ValueError,
1179 "%s: can't specify dir_fd without matching path",
1180 function_name);
1181 return 1;
1182 }
1183 return 0;
1184}
1185
1186static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001187dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1188{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001189 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1190 PyErr_Format(PyExc_ValueError,
1191 "%s: can't specify both dir_fd and fd",
1192 function_name);
1193 return 1;
1194 }
1195 return 0;
1196}
1197
1198static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001199fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1200 int follow_symlinks)
1201{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001202 if ((fd > 0) && (!follow_symlinks)) {
1203 PyErr_Format(PyExc_ValueError,
1204 "%s: cannot use fd and follow_symlinks together",
1205 function_name);
1206 return 1;
1207 }
1208 return 0;
1209}
1210
1211static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001212dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1213 int follow_symlinks)
1214{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001215 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1216 PyErr_Format(PyExc_ValueError,
1217 "%s: cannot use dir_fd and follow_symlinks together",
1218 function_name);
1219 return 1;
1220 }
1221 return 0;
1222}
1223
Larry Hastings2f936352014-08-05 14:04:04 +10001224#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001225 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001226#else
Larry Hastings2f936352014-08-05 14:04:04 +10001227 typedef off_t Py_off_t;
1228#endif
1229
1230static int
1231Py_off_t_converter(PyObject *arg, void *addr)
1232{
1233#ifdef HAVE_LARGEFILE_SUPPORT
1234 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1235#else
1236 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001237#endif
1238 if (PyErr_Occurred())
1239 return 0;
1240 return 1;
1241}
Larry Hastings2f936352014-08-05 14:04:04 +10001242
1243static PyObject *
1244PyLong_FromPy_off_t(Py_off_t offset)
1245{
1246#ifdef HAVE_LARGEFILE_SUPPORT
1247 return PyLong_FromLongLong(offset);
1248#else
1249 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001250#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001251}
1252
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001253#ifdef HAVE_SIGSET_T
1254/* Convert an iterable of integers to a sigset.
1255 Return 1 on success, return 0 and raise an exception on error. */
1256int
1257_Py_Sigset_Converter(PyObject *obj, void *addr)
1258{
1259 sigset_t *mask = (sigset_t *)addr;
1260 PyObject *iterator, *item;
1261 long signum;
1262 int overflow;
1263
1264 if (sigemptyset(mask)) {
1265 /* Probably only if mask == NULL. */
1266 PyErr_SetFromErrno(PyExc_OSError);
1267 return 0;
1268 }
1269
1270 iterator = PyObject_GetIter(obj);
1271 if (iterator == NULL) {
1272 return 0;
1273 }
1274
1275 while ((item = PyIter_Next(iterator)) != NULL) {
1276 signum = PyLong_AsLongAndOverflow(item, &overflow);
1277 Py_DECREF(item);
1278 if (signum <= 0 || signum >= NSIG) {
1279 if (overflow || signum != -1 || !PyErr_Occurred()) {
1280 PyErr_Format(PyExc_ValueError,
1281 "signal number %ld out of range", signum);
1282 }
1283 goto error;
1284 }
1285 if (sigaddset(mask, (int)signum)) {
1286 if (errno != EINVAL) {
1287 /* Probably impossible */
1288 PyErr_SetFromErrno(PyExc_OSError);
1289 goto error;
1290 }
1291 /* For backwards compatibility, allow idioms such as
1292 * `range(1, NSIG)` but warn about invalid signal numbers
1293 */
1294 const char msg[] =
1295 "invalid signal number %ld, please use valid_signals()";
1296 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1297 goto error;
1298 }
1299 }
1300 }
1301 if (!PyErr_Occurred()) {
1302 Py_DECREF(iterator);
1303 return 1;
1304 }
1305
1306error:
1307 Py_DECREF(iterator);
1308 return 0;
1309}
1310#endif /* HAVE_SIGSET_T */
1311
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001312#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001313
1314static int
Brian Curtind25aef52011-06-13 15:16:04 -05001315win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001316{
Martin Panter70214ad2016-08-04 02:38:59 +00001317 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1318 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001319 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001320
1321 if (0 == DeviceIoControl(
1322 reparse_point_handle,
1323 FSCTL_GET_REPARSE_POINT,
1324 NULL, 0, /* in buffer */
1325 target_buffer, sizeof(target_buffer),
1326 &n_bytes_returned,
1327 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001328 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001329
1330 if (reparse_tag)
1331 *reparse_tag = rdb->ReparseTag;
1332
Brian Curtind25aef52011-06-13 15:16:04 -05001333 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001334}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001335
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001336#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001337
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001338/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001339#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001340/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001341** environ directly, we must obtain it with _NSGetEnviron(). See also
1342** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001343*/
1344#include <crt_externs.h>
1345static char **environ;
1346#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001347extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001348#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001349
Barry Warsaw53699e91996-12-10 23:23:01 +00001350static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001351convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001352{
Victor Stinner8c62be82010-05-06 00:08:46 +00001353 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001354#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001356#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001357 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001358#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001359
Victor Stinner8c62be82010-05-06 00:08:46 +00001360 d = PyDict_New();
1361 if (d == NULL)
1362 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001363#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001364 if (environ == NULL)
1365 environ = *_NSGetEnviron();
1366#endif
1367#ifdef MS_WINDOWS
1368 /* _wenviron must be initialized in this way if the program is started
1369 through main() instead of wmain(). */
1370 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001371 e = _wenviron;
Victor Stinner8c62be82010-05-06 00:08:46 +00001372#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001373 e = environ;
1374#endif
1375 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001376 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001377 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001378 PyObject *k;
1379 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001380#ifdef MS_WINDOWS
1381 const wchar_t *p = wcschr(*e, L'=');
1382#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001383 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001384#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001385 if (p == NULL)
1386 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001387#ifdef MS_WINDOWS
1388 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1389#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001390 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001391#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001392 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001393 Py_DECREF(d);
1394 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001395 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001396#ifdef MS_WINDOWS
1397 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1398#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001399 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001400#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001403 Py_DECREF(d);
1404 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001405 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001406 if (PyDict_GetItemWithError(d, k) == NULL) {
1407 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1408 Py_DECREF(v);
1409 Py_DECREF(k);
1410 Py_DECREF(d);
1411 return NULL;
1412 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001413 }
1414 Py_DECREF(k);
1415 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001416 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001417 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001418}
1419
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001420/* Set a POSIX-specific error from errno, and return NULL */
1421
Barry Warsawd58d7641998-07-23 16:14:40 +00001422static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001423posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001424{
Victor Stinner8c62be82010-05-06 00:08:46 +00001425 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001426}
Mark Hammondef8b6542001-05-13 08:04:26 +00001427
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001428#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001429static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001430win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001431{
Victor Stinner8c62be82010-05-06 00:08:46 +00001432 /* XXX We should pass the function name along in the future.
1433 (winreg.c also wants to pass the function name.)
1434 This would however require an additional param to the
1435 Windows error object, which is non-trivial.
1436 */
1437 errno = GetLastError();
1438 if (filename)
1439 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1440 else
1441 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001442}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001443
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001444static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001445win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001446{
1447 /* XXX - see win32_error for comments on 'function' */
1448 errno = GetLastError();
1449 if (filename)
1450 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001451 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001452 errno,
1453 filename);
1454 else
1455 return PyErr_SetFromWindowsErr(errno);
1456}
1457
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001458#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001459
Larry Hastings9cf065c2012-06-22 16:30:09 -07001460static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001461posix_path_object_error(PyObject *path)
1462{
1463 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1464}
1465
1466static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001467path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001468{
1469#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001470 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1471 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001472#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001473 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001474#endif
1475}
1476
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001477static PyObject *
1478path_object_error2(PyObject *path, PyObject *path2)
1479{
1480#ifdef MS_WINDOWS
1481 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1482 PyExc_OSError, 0, path, path2);
1483#else
1484 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1485#endif
1486}
1487
1488static PyObject *
1489path_error(path_t *path)
1490{
1491 return path_object_error(path->object);
1492}
Larry Hastings31826802013-10-19 00:09:25 -07001493
Larry Hastingsb0827312014-02-09 22:05:19 -08001494static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001495posix_path_error(path_t *path)
1496{
1497 return posix_path_object_error(path->object);
1498}
1499
1500static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001501path_error2(path_t *path, path_t *path2)
1502{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001503 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001504}
1505
1506
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001507/* POSIX generic methods */
1508
Larry Hastings2f936352014-08-05 14:04:04 +10001509static int
1510fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001511{
Victor Stinner8c62be82010-05-06 00:08:46 +00001512 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001513 int *pointer = (int *)p;
1514 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001516 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001517 *pointer = fd;
1518 return 1;
1519}
1520
1521static PyObject *
1522posix_fildes_fd(int fd, int (*func)(int))
1523{
1524 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001525 int async_err = 0;
1526
1527 do {
1528 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001529 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001530 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001531 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001532 Py_END_ALLOW_THREADS
1533 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1534 if (res != 0)
1535 return (!async_err) ? posix_error() : NULL;
1536 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001537}
Guido van Rossum21142a01999-01-08 21:05:37 +00001538
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001539
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001540#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001541/* This is a reimplementation of the C library's chdir function,
1542 but one that produces Win32 errors instead of DOS error codes.
1543 chdir is essentially a wrapper around SetCurrentDirectory; however,
1544 it also needs to set "magic" environment variables indicating
1545 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001546static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001547win32_wchdir(LPCWSTR path)
1548{
Victor Stinnered537822015-12-13 21:40:26 +01001549 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001550 int result;
1551 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001552
Victor Stinner8c62be82010-05-06 00:08:46 +00001553 if(!SetCurrentDirectoryW(path))
1554 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001555 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001556 if (!result)
1557 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001558 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001559 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001560 if (!new_path) {
1561 SetLastError(ERROR_OUTOFMEMORY);
1562 return FALSE;
1563 }
1564 result = GetCurrentDirectoryW(result, new_path);
1565 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001566 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001567 return FALSE;
1568 }
1569 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001570 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1571 wcsncmp(new_path, L"//", 2) == 0);
1572 if (!is_unc_like_path) {
1573 env[1] = new_path[0];
1574 result = SetEnvironmentVariableW(env, new_path);
1575 }
Victor Stinnered537822015-12-13 21:40:26 +01001576 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001577 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001578 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001579}
1580#endif
1581
Martin v. Löwis14694662006-02-03 12:54:16 +00001582#ifdef MS_WINDOWS
1583/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1584 - time stamps are restricted to second resolution
1585 - file modification times suffer from forth-and-back conversions between
1586 UTC and local time
1587 Therefore, we implement our own stat, based on the Win32 API directly.
1588*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001589#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001590#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001591
Victor Stinner6036e442015-03-08 01:58:04 +01001592static void
Steve Dowercc16be82016-09-08 10:35:16 -07001593find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1594 BY_HANDLE_FILE_INFORMATION *info,
1595 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001596{
1597 memset(info, 0, sizeof(*info));
1598 info->dwFileAttributes = pFileData->dwFileAttributes;
1599 info->ftCreationTime = pFileData->ftCreationTime;
1600 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1601 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1602 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1603 info->nFileSizeLow = pFileData->nFileSizeLow;
1604/* info->nNumberOfLinks = 1; */
1605 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1606 *reparse_tag = pFileData->dwReserved0;
1607 else
1608 *reparse_tag = 0;
1609}
1610
Guido van Rossumd8faa362007-04-27 19:54:29 +00001611static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001612attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001613{
Victor Stinner8c62be82010-05-06 00:08:46 +00001614 HANDLE hFindFile;
1615 WIN32_FIND_DATAW FileData;
1616 hFindFile = FindFirstFileW(pszFile, &FileData);
1617 if (hFindFile == INVALID_HANDLE_VALUE)
1618 return FALSE;
1619 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001620 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001621 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001622}
1623
Brian Curtind25aef52011-06-13 15:16:04 -05001624static BOOL
1625get_target_path(HANDLE hdl, wchar_t **target_path)
1626{
1627 int buf_size, result_length;
1628 wchar_t *buf;
1629
1630 /* We have a good handle to the target, use it to determine
1631 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001632 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1633 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001634 if(!buf_size)
1635 return FALSE;
1636
Victor Stinnerc36674a2016-03-16 14:30:16 +01001637 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001638 if (!buf) {
1639 SetLastError(ERROR_OUTOFMEMORY);
1640 return FALSE;
1641 }
1642
Steve Dower2ea51c92015-03-20 21:49:12 -07001643 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001644 buf, buf_size, VOLUME_NAME_DOS);
1645
1646 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001647 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001648 return FALSE;
1649 }
1650
Brian Curtind25aef52011-06-13 15:16:04 -05001651 buf[result_length] = 0;
1652
1653 *target_path = buf;
1654 return TRUE;
1655}
1656
1657static int
Steve Dowercc16be82016-09-08 10:35:16 -07001658win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001659 BOOL traverse)
1660{
Victor Stinner26de69d2011-06-17 15:15:38 +02001661 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001662 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001663 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001664 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001665 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001666 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001667
Steve Dowercc16be82016-09-08 10:35:16 -07001668 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001669 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001670 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001671 0, /* share mode */
1672 NULL, /* security attributes */
1673 OPEN_EXISTING,
1674 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001675 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1676 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001677 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001678 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1679 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001680 NULL);
1681
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001682 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001683 /* Either the target doesn't exist, or we don't have access to
1684 get a handle to it. If the former, we need to return an error.
1685 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001686 DWORD lastError = GetLastError();
1687 if (lastError != ERROR_ACCESS_DENIED &&
1688 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001689 return -1;
1690 /* Could not get attributes on open file. Fall back to
1691 reading the directory. */
1692 if (!attributes_from_dir(path, &info, &reparse_tag))
1693 /* Very strange. This should not fail now */
1694 return -1;
1695 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1696 if (traverse) {
1697 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001698 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001699 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001700 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001701 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001702 } else {
1703 if (!GetFileInformationByHandle(hFile, &info)) {
1704 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001705 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001706 }
1707 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Mark Becwarb82bfac2019-02-02 16:08:23 -05001708 if (!win32_get_reparse_tag(hFile, &reparse_tag)) {
1709 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001710 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001711 }
Brian Curtind25aef52011-06-13 15:16:04 -05001712 /* Close the outer open file handle now that we're about to
1713 reopen it with different flags. */
1714 if (!CloseHandle(hFile))
1715 return -1;
1716
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001717 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001718 /* In order to call GetFinalPathNameByHandle we need to open
1719 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001720 hFile2 = CreateFileW(
1721 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1722 NULL, OPEN_EXISTING,
1723 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1724 NULL);
1725 if (hFile2 == INVALID_HANDLE_VALUE)
1726 return -1;
1727
Mark Becwarb82bfac2019-02-02 16:08:23 -05001728 if (!get_target_path(hFile2, &target_path)) {
1729 CloseHandle(hFile2);
Brian Curtind25aef52011-06-13 15:16:04 -05001730 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001731 }
1732
1733 if (!CloseHandle(hFile2)) {
1734 return -1;
1735 }
Brian Curtind25aef52011-06-13 15:16:04 -05001736
Steve Dowercc16be82016-09-08 10:35:16 -07001737 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001738 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001739 return code;
1740 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001741 } else
1742 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001743 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001744 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001745
1746 /* Set S_IEXEC if it is an .exe, .bat, ... */
1747 dot = wcsrchr(path, '.');
1748 if (dot) {
1749 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1750 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1751 result->st_mode |= 0111;
1752 }
1753 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001754}
1755
1756static int
Steve Dowercc16be82016-09-08 10:35:16 -07001757win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001758{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001759 /* Protocol violation: we explicitly clear errno, instead of
1760 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001761 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001762 errno = 0;
1763 return code;
1764}
Brian Curtind25aef52011-06-13 15:16:04 -05001765/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001766
1767 In Posix, stat automatically traverses symlinks and returns the stat
1768 structure for the target. In Windows, the equivalent GetFileAttributes by
1769 default does not traverse symlinks and instead returns attributes for
1770 the symlink.
1771
1772 Therefore, win32_lstat will get the attributes traditionally, and
1773 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001774 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001775
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001776static int
Steve Dowercc16be82016-09-08 10:35:16 -07001777win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001778{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001779 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001780}
1781
Victor Stinner8c62be82010-05-06 00:08:46 +00001782static int
Steve Dowercc16be82016-09-08 10:35:16 -07001783win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001784{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001785 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001786}
1787
Martin v. Löwis14694662006-02-03 12:54:16 +00001788#endif /* MS_WINDOWS */
1789
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001790PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001791"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001792This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001793 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001794or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1795\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001796Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1797or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001798\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001799See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001800
1801static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001802 {"st_mode", "protection bits"},
1803 {"st_ino", "inode"},
1804 {"st_dev", "device"},
1805 {"st_nlink", "number of hard links"},
1806 {"st_uid", "user ID of owner"},
1807 {"st_gid", "group ID of owner"},
1808 {"st_size", "total size, in bytes"},
1809 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1810 {NULL, "integer time of last access"},
1811 {NULL, "integer time of last modification"},
1812 {NULL, "integer time of last change"},
1813 {"st_atime", "time of last access"},
1814 {"st_mtime", "time of last modification"},
1815 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001816 {"st_atime_ns", "time of last access in nanoseconds"},
1817 {"st_mtime_ns", "time of last modification in nanoseconds"},
1818 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001819#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001820 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001821#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001822#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001824#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001825#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001826 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001827#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001828#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001829 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001830#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001831#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001832 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001833#endif
1834#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001835 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001836#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001837#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1838 {"st_file_attributes", "Windows file attribute bits"},
1839#endif
jcea6c51d512018-01-28 14:00:08 +01001840#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1841 {"st_fstype", "Type of filesystem"},
1842#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001843 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001844};
1845
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001846#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001847#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001848#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001849#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001850#endif
1851
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001852#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001853#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1854#else
1855#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1856#endif
1857
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001858#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001859#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1860#else
1861#define ST_RDEV_IDX ST_BLOCKS_IDX
1862#endif
1863
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001864#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1865#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1866#else
1867#define ST_FLAGS_IDX ST_RDEV_IDX
1868#endif
1869
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001870#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001871#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001872#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001873#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001874#endif
1875
1876#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1877#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1878#else
1879#define ST_BIRTHTIME_IDX ST_GEN_IDX
1880#endif
1881
Zachary Ware63f277b2014-06-19 09:46:37 -05001882#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1883#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1884#else
1885#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1886#endif
1887
jcea6c51d512018-01-28 14:00:08 +01001888#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1889#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1890#else
1891#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1892#endif
1893
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001894static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001895 "stat_result", /* name */
1896 stat_result__doc__, /* doc */
1897 stat_result_fields,
1898 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001899};
1900
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001901PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001902"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1903This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001904 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001905or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001906\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001907See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001908
1909static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001910 {"f_bsize", },
1911 {"f_frsize", },
1912 {"f_blocks", },
1913 {"f_bfree", },
1914 {"f_bavail", },
1915 {"f_files", },
1916 {"f_ffree", },
1917 {"f_favail", },
1918 {"f_flag", },
1919 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01001920 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00001921 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001922};
1923
1924static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001925 "statvfs_result", /* name */
1926 statvfs_result__doc__, /* doc */
1927 statvfs_result_fields,
1928 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001929};
1930
Ross Lagerwall7807c352011-03-17 20:20:30 +02001931#if defined(HAVE_WAITID) && !defined(__APPLE__)
1932PyDoc_STRVAR(waitid_result__doc__,
1933"waitid_result: Result from waitid.\n\n\
1934This object may be accessed either as a tuple of\n\
1935 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1936or via the attributes si_pid, si_uid, and so on.\n\
1937\n\
1938See os.waitid for more information.");
1939
1940static PyStructSequence_Field waitid_result_fields[] = {
1941 {"si_pid", },
1942 {"si_uid", },
1943 {"si_signo", },
1944 {"si_status", },
1945 {"si_code", },
1946 {0}
1947};
1948
1949static PyStructSequence_Desc waitid_result_desc = {
1950 "waitid_result", /* name */
1951 waitid_result__doc__, /* doc */
1952 waitid_result_fields,
1953 5
1954};
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001955static PyTypeObject* WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +02001956#endif
1957
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001958static int initialized;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001959static PyTypeObject* StatResultType;
1960static PyTypeObject* StatVFSResultType;
William Orr81574b82018-10-01 22:19:56 -07001961#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001962static PyTypeObject* SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001963#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001964static newfunc structseq_new;
1965
1966static PyObject *
1967statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1968{
Victor Stinner8c62be82010-05-06 00:08:46 +00001969 PyStructSequence *result;
1970 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001971
Victor Stinner8c62be82010-05-06 00:08:46 +00001972 result = (PyStructSequence*)structseq_new(type, args, kwds);
1973 if (!result)
1974 return NULL;
1975 /* If we have been initialized from a tuple,
1976 st_?time might be set to None. Initialize it
1977 from the int slots. */
1978 for (i = 7; i <= 9; i++) {
1979 if (result->ob_item[i+3] == Py_None) {
1980 Py_DECREF(Py_None);
1981 Py_INCREF(result->ob_item[i]);
1982 result->ob_item[i+3] = result->ob_item[i];
1983 }
1984 }
1985 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001986}
1987
1988
Larry Hastings6fe20b32012-04-19 15:07:49 -07001989static PyObject *billion = NULL;
1990
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001991static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001992fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001993{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001994 PyObject *s = _PyLong_FromTime_t(sec);
1995 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1996 PyObject *s_in_ns = NULL;
1997 PyObject *ns_total = NULL;
1998 PyObject *float_s = NULL;
1999
2000 if (!(s && ns_fractional))
2001 goto exit;
2002
2003 s_in_ns = PyNumber_Multiply(s, billion);
2004 if (!s_in_ns)
2005 goto exit;
2006
2007 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2008 if (!ns_total)
2009 goto exit;
2010
Victor Stinner01b5aab2017-10-24 02:02:00 -07002011 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2012 if (!float_s) {
2013 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002014 }
2015
2016 PyStructSequence_SET_ITEM(v, index, s);
2017 PyStructSequence_SET_ITEM(v, index+3, float_s);
2018 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2019 s = NULL;
2020 float_s = NULL;
2021 ns_total = NULL;
2022exit:
2023 Py_XDECREF(s);
2024 Py_XDECREF(ns_fractional);
2025 Py_XDECREF(s_in_ns);
2026 Py_XDECREF(ns_total);
2027 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002028}
2029
Tim Peters5aa91602002-01-30 05:46:57 +00002030/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002031 (used by posix_stat() and posix_fstat()) */
2032static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002033_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002034{
Victor Stinner8c62be82010-05-06 00:08:46 +00002035 unsigned long ansec, mnsec, cnsec;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002036 PyObject *v = PyStructSequence_New(StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002037 if (v == NULL)
2038 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002039
Victor Stinner8c62be82010-05-06 00:08:46 +00002040 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002041 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002042 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002043#ifdef MS_WINDOWS
2044 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002045#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002046 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002047#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002048 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002049#if defined(MS_WINDOWS)
2050 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2051 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2052#else
2053 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2054 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2055#endif
xdegaye50e86032017-05-22 11:15:08 +02002056 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2057 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002058
Martin v. Löwis14694662006-02-03 12:54:16 +00002059#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002060 ansec = st->st_atim.tv_nsec;
2061 mnsec = st->st_mtim.tv_nsec;
2062 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002063#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002064 ansec = st->st_atimespec.tv_nsec;
2065 mnsec = st->st_mtimespec.tv_nsec;
2066 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002067#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002068 ansec = st->st_atime_nsec;
2069 mnsec = st->st_mtime_nsec;
2070 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002071#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002072 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002073#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002074 fill_time(v, 7, st->st_atime, ansec);
2075 fill_time(v, 8, st->st_mtime, mnsec);
2076 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002077
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002078#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002079 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2080 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002081#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002082#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002083 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2084 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002085#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002086#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002087 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2088 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002089#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002090#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002091 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2092 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002093#endif
2094#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002095 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002096 PyObject *val;
2097 unsigned long bsec,bnsec;
2098 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002099#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002100 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002101#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002102 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002103#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002104 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002105 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2106 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002107 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002108#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002109#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002110 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2111 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002112#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002113#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2114 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2115 PyLong_FromUnsignedLong(st->st_file_attributes));
2116#endif
jcea6c51d512018-01-28 14:00:08 +01002117#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2118 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2119 PyUnicode_FromString(st->st_fstype));
2120#endif
Fred Drake699f3522000-06-29 21:12:41 +00002121
Victor Stinner8c62be82010-05-06 00:08:46 +00002122 if (PyErr_Occurred()) {
2123 Py_DECREF(v);
2124 return NULL;
2125 }
Fred Drake699f3522000-06-29 21:12:41 +00002126
Victor Stinner8c62be82010-05-06 00:08:46 +00002127 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002128}
2129
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002130/* POSIX methods */
2131
Guido van Rossum94f6f721999-01-06 18:42:14 +00002132
2133static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002134posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002135 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002136{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002137 STRUCT_STAT st;
2138 int result;
2139
2140#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2141 if (follow_symlinks_specified(function_name, follow_symlinks))
2142 return NULL;
2143#endif
2144
2145 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2146 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2147 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2148 return NULL;
2149
2150 Py_BEGIN_ALLOW_THREADS
2151 if (path->fd != -1)
2152 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002153#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002154 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002155 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002156 else
Steve Dowercc16be82016-09-08 10:35:16 -07002157 result = win32_lstat(path->wide, &st);
2158#else
2159 else
2160#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002161 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2162 result = LSTAT(path->narrow, &st);
2163 else
Steve Dowercc16be82016-09-08 10:35:16 -07002164#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002165#ifdef HAVE_FSTATAT
2166 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2167 result = fstatat(dir_fd, path->narrow, &st,
2168 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2169 else
Steve Dowercc16be82016-09-08 10:35:16 -07002170#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002171 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002172#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002173 Py_END_ALLOW_THREADS
2174
Victor Stinner292c8352012-10-30 02:17:38 +01002175 if (result != 0) {
2176 return path_error(path);
2177 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002178
2179 return _pystat_fromstructstat(&st);
2180}
2181
Larry Hastings2f936352014-08-05 14:04:04 +10002182/*[python input]
2183
2184for s in """
2185
2186FACCESSAT
2187FCHMODAT
2188FCHOWNAT
2189FSTATAT
2190LINKAT
2191MKDIRAT
2192MKFIFOAT
2193MKNODAT
2194OPENAT
2195READLINKAT
2196SYMLINKAT
2197UNLINKAT
2198
2199""".strip().split():
2200 s = s.strip()
2201 print("""
2202#ifdef HAVE_{s}
2203 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002204#else
Larry Hastings2f936352014-08-05 14:04:04 +10002205 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002206#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002207""".rstrip().format(s=s))
2208
2209for s in """
2210
2211FCHDIR
2212FCHMOD
2213FCHOWN
2214FDOPENDIR
2215FEXECVE
2216FPATHCONF
2217FSTATVFS
2218FTRUNCATE
2219
2220""".strip().split():
2221 s = s.strip()
2222 print("""
2223#ifdef HAVE_{s}
2224 #define PATH_HAVE_{s} 1
2225#else
2226 #define PATH_HAVE_{s} 0
2227#endif
2228
2229""".rstrip().format(s=s))
2230[python start generated code]*/
2231
2232#ifdef HAVE_FACCESSAT
2233 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2234#else
2235 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2236#endif
2237
2238#ifdef HAVE_FCHMODAT
2239 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2240#else
2241 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2242#endif
2243
2244#ifdef HAVE_FCHOWNAT
2245 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2246#else
2247 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2248#endif
2249
2250#ifdef HAVE_FSTATAT
2251 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2252#else
2253 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2254#endif
2255
2256#ifdef HAVE_LINKAT
2257 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2258#else
2259 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2260#endif
2261
2262#ifdef HAVE_MKDIRAT
2263 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2264#else
2265 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2266#endif
2267
2268#ifdef HAVE_MKFIFOAT
2269 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2270#else
2271 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2272#endif
2273
2274#ifdef HAVE_MKNODAT
2275 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2276#else
2277 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2278#endif
2279
2280#ifdef HAVE_OPENAT
2281 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2282#else
2283 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2284#endif
2285
2286#ifdef HAVE_READLINKAT
2287 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2288#else
2289 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2290#endif
2291
2292#ifdef HAVE_SYMLINKAT
2293 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2294#else
2295 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2296#endif
2297
2298#ifdef HAVE_UNLINKAT
2299 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2300#else
2301 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2302#endif
2303
2304#ifdef HAVE_FCHDIR
2305 #define PATH_HAVE_FCHDIR 1
2306#else
2307 #define PATH_HAVE_FCHDIR 0
2308#endif
2309
2310#ifdef HAVE_FCHMOD
2311 #define PATH_HAVE_FCHMOD 1
2312#else
2313 #define PATH_HAVE_FCHMOD 0
2314#endif
2315
2316#ifdef HAVE_FCHOWN
2317 #define PATH_HAVE_FCHOWN 1
2318#else
2319 #define PATH_HAVE_FCHOWN 0
2320#endif
2321
2322#ifdef HAVE_FDOPENDIR
2323 #define PATH_HAVE_FDOPENDIR 1
2324#else
2325 #define PATH_HAVE_FDOPENDIR 0
2326#endif
2327
2328#ifdef HAVE_FEXECVE
2329 #define PATH_HAVE_FEXECVE 1
2330#else
2331 #define PATH_HAVE_FEXECVE 0
2332#endif
2333
2334#ifdef HAVE_FPATHCONF
2335 #define PATH_HAVE_FPATHCONF 1
2336#else
2337 #define PATH_HAVE_FPATHCONF 0
2338#endif
2339
2340#ifdef HAVE_FSTATVFS
2341 #define PATH_HAVE_FSTATVFS 1
2342#else
2343 #define PATH_HAVE_FSTATVFS 0
2344#endif
2345
2346#ifdef HAVE_FTRUNCATE
2347 #define PATH_HAVE_FTRUNCATE 1
2348#else
2349 #define PATH_HAVE_FTRUNCATE 0
2350#endif
2351/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002352
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002353#ifdef MS_WINDOWS
2354 #undef PATH_HAVE_FTRUNCATE
2355 #define PATH_HAVE_FTRUNCATE 1
2356#endif
Larry Hastings31826802013-10-19 00:09:25 -07002357
Larry Hastings61272b72014-01-07 12:41:53 -08002358/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002359
2360class path_t_converter(CConverter):
2361
2362 type = "path_t"
2363 impl_by_reference = True
2364 parse_by_reference = True
2365
2366 converter = 'path_converter'
2367
2368 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002369 # right now path_t doesn't support default values.
2370 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002371 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002372 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002373
Larry Hastings2f936352014-08-05 14:04:04 +10002374 if self.c_default not in (None, 'Py_None'):
2375 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002376
2377 self.nullable = nullable
2378 self.allow_fd = allow_fd
2379
Larry Hastings7726ac92014-01-31 22:03:12 -08002380 def pre_render(self):
2381 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002382 if isinstance(value, str):
2383 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002384 return str(int(bool(value)))
2385
2386 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002387 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002388 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002389 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002390 strify(self.nullable),
2391 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002392 )
2393
2394 def cleanup(self):
2395 return "path_cleanup(&" + self.name + ");\n"
2396
2397
2398class dir_fd_converter(CConverter):
2399 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002400
Larry Hastings2f936352014-08-05 14:04:04 +10002401 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002402 if self.default in (unspecified, None):
2403 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002404 if isinstance(requires, str):
2405 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2406 else:
2407 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002408
Larry Hastings2f936352014-08-05 14:04:04 +10002409class fildes_converter(CConverter):
2410 type = 'int'
2411 converter = 'fildes_converter'
2412
2413class uid_t_converter(CConverter):
2414 type = "uid_t"
2415 converter = '_Py_Uid_Converter'
2416
2417class gid_t_converter(CConverter):
2418 type = "gid_t"
2419 converter = '_Py_Gid_Converter'
2420
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002421class dev_t_converter(CConverter):
2422 type = 'dev_t'
2423 converter = '_Py_Dev_Converter'
2424
2425class dev_t_return_converter(unsigned_long_return_converter):
2426 type = 'dev_t'
2427 conversion_fn = '_PyLong_FromDev'
2428 unsigned_cast = '(dev_t)'
2429
Larry Hastings2f936352014-08-05 14:04:04 +10002430class FSConverter_converter(CConverter):
2431 type = 'PyObject *'
2432 converter = 'PyUnicode_FSConverter'
2433 def converter_init(self):
2434 if self.default is not unspecified:
2435 fail("FSConverter_converter does not support default values")
2436 self.c_default = 'NULL'
2437
2438 def cleanup(self):
2439 return "Py_XDECREF(" + self.name + ");\n"
2440
2441class pid_t_converter(CConverter):
2442 type = 'pid_t'
2443 format_unit = '" _Py_PARSE_PID "'
2444
2445class idtype_t_converter(int_converter):
2446 type = 'idtype_t'
2447
2448class id_t_converter(CConverter):
2449 type = 'id_t'
2450 format_unit = '" _Py_PARSE_PID "'
2451
Benjamin Petersonca470632016-09-06 13:47:26 -07002452class intptr_t_converter(CConverter):
2453 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002454 format_unit = '" _Py_PARSE_INTPTR "'
2455
2456class Py_off_t_converter(CConverter):
2457 type = 'Py_off_t'
2458 converter = 'Py_off_t_converter'
2459
2460class Py_off_t_return_converter(long_return_converter):
2461 type = 'Py_off_t'
2462 conversion_fn = 'PyLong_FromPy_off_t'
2463
2464class path_confname_converter(CConverter):
2465 type="int"
2466 converter="conv_path_confname"
2467
2468class confstr_confname_converter(path_confname_converter):
2469 converter='conv_confstr_confname'
2470
2471class sysconf_confname_converter(path_confname_converter):
2472 converter="conv_sysconf_confname"
2473
2474class sched_param_converter(CConverter):
2475 type = 'struct sched_param'
2476 converter = 'convert_sched_param'
2477 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002478
Larry Hastings61272b72014-01-07 12:41:53 -08002479[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002480/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002481
Larry Hastings61272b72014-01-07 12:41:53 -08002482/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002483
Larry Hastings2a727912014-01-16 11:32:01 -08002484os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002485
2486 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002487 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002488 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002489
2490 *
2491
Larry Hastings2f936352014-08-05 14:04:04 +10002492 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002493 If not None, it should be a file descriptor open to a directory,
2494 and path should be a relative string; path will then be relative to
2495 that directory.
2496
2497 follow_symlinks: bool = True
2498 If False, and the last element of the path is a symbolic link,
2499 stat will examine the symbolic link itself instead of the file
2500 the link points to.
2501
2502Perform a stat system call on the given path.
2503
2504dir_fd and follow_symlinks may not be implemented
2505 on your platform. If they are unavailable, using them will raise a
2506 NotImplementedError.
2507
2508It's an error to use dir_fd or follow_symlinks when specifying path as
2509 an open file descriptor.
2510
Larry Hastings61272b72014-01-07 12:41:53 -08002511[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002512
Larry Hastings31826802013-10-19 00:09:25 -07002513static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002514os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002515/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002516{
2517 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2518}
2519
Larry Hastings2f936352014-08-05 14:04:04 +10002520
2521/*[clinic input]
2522os.lstat
2523
2524 path : path_t
2525
2526 *
2527
2528 dir_fd : dir_fd(requires='fstatat') = None
2529
2530Perform a stat system call on the given path, without following symbolic links.
2531
2532Like stat(), but do not follow symbolic links.
2533Equivalent to stat(path, follow_symlinks=False).
2534[clinic start generated code]*/
2535
Larry Hastings2f936352014-08-05 14:04:04 +10002536static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002537os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2538/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002539{
2540 int follow_symlinks = 0;
2541 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2542}
Larry Hastings31826802013-10-19 00:09:25 -07002543
Larry Hastings2f936352014-08-05 14:04:04 +10002544
Larry Hastings61272b72014-01-07 12:41:53 -08002545/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002546os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002547
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002548 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002549 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002550
2551 mode: int
2552 Operating-system mode bitfield. Can be F_OK to test existence,
2553 or the inclusive-OR of R_OK, W_OK, and X_OK.
2554
2555 *
2556
Larry Hastings2f936352014-08-05 14:04:04 +10002557 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002558 If not None, it should be a file descriptor open to a directory,
2559 and path should be relative; path will then be relative to that
2560 directory.
2561
2562 effective_ids: bool = False
2563 If True, access will use the effective uid/gid instead of
2564 the real uid/gid.
2565
2566 follow_symlinks: bool = True
2567 If False, and the last element of the path is a symbolic link,
2568 access will examine the symbolic link itself instead of the file
2569 the link points to.
2570
2571Use the real uid/gid to test for access to a path.
2572
2573{parameters}
2574dir_fd, effective_ids, and follow_symlinks may not be implemented
2575 on your platform. If they are unavailable, using them will raise a
2576 NotImplementedError.
2577
2578Note that most operations will use the effective uid/gid, therefore this
2579 routine can be used in a suid/sgid environment to test if the invoking user
2580 has the specified access to the path.
2581
Larry Hastings61272b72014-01-07 12:41:53 -08002582[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002583
Larry Hastings2f936352014-08-05 14:04:04 +10002584static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002585os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002586 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002587/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002588{
Larry Hastings2f936352014-08-05 14:04:04 +10002589 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002590
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002591#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002592 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002593#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002594 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002595#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002596
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597#ifndef HAVE_FACCESSAT
2598 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002599 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002600
2601 if (effective_ids) {
2602 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002603 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002604 }
2605#endif
2606
2607#ifdef MS_WINDOWS
2608 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002609 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002610 Py_END_ALLOW_THREADS
2611
2612 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002613 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002614 * * we didn't get a -1, and
2615 * * write access wasn't requested,
2616 * * or the file isn't read-only,
2617 * * or it's a directory.
2618 * (Directories cannot be read-only on Windows.)
2619 */
Larry Hastings2f936352014-08-05 14:04:04 +10002620 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002621 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002622 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002623 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002624#else
2625
2626 Py_BEGIN_ALLOW_THREADS
2627#ifdef HAVE_FACCESSAT
2628 if ((dir_fd != DEFAULT_DIR_FD) ||
2629 effective_ids ||
2630 !follow_symlinks) {
2631 int flags = 0;
2632 if (!follow_symlinks)
2633 flags |= AT_SYMLINK_NOFOLLOW;
2634 if (effective_ids)
2635 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002636 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002637 }
2638 else
2639#endif
Larry Hastings31826802013-10-19 00:09:25 -07002640 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002641 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002642 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002643#endif
2644
Larry Hastings9cf065c2012-06-22 16:30:09 -07002645 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002646}
2647
Guido van Rossumd371ff11999-01-25 16:12:23 +00002648#ifndef F_OK
2649#define F_OK 0
2650#endif
2651#ifndef R_OK
2652#define R_OK 4
2653#endif
2654#ifndef W_OK
2655#define W_OK 2
2656#endif
2657#ifndef X_OK
2658#define X_OK 1
2659#endif
2660
Larry Hastings31826802013-10-19 00:09:25 -07002661
Guido van Rossumd371ff11999-01-25 16:12:23 +00002662#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002663/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002664os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002665
2666 fd: int
2667 Integer file descriptor handle.
2668
2669 /
2670
2671Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002672[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002673
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002674static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002675os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002676/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002677{
2678 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002679
Larry Hastings31826802013-10-19 00:09:25 -07002680 ret = ttyname(fd);
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002681 if (ret == NULL) {
2682 return posix_error();
2683 }
2684 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002685}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002686#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002687
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002688#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002689/*[clinic input]
2690os.ctermid
2691
2692Return the name of the controlling terminal for this process.
2693[clinic start generated code]*/
2694
Larry Hastings2f936352014-08-05 14:04:04 +10002695static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002696os_ctermid_impl(PyObject *module)
2697/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002698{
Victor Stinner8c62be82010-05-06 00:08:46 +00002699 char *ret;
2700 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002701
Greg Wardb48bc172000-03-01 21:51:56 +00002702#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002703 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002704#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002705 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002706#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002707 if (ret == NULL)
2708 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002709 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002710}
Larry Hastings2f936352014-08-05 14:04:04 +10002711#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002712
Larry Hastings2f936352014-08-05 14:04:04 +10002713
2714/*[clinic input]
2715os.chdir
2716
2717 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2718
2719Change the current working directory to the specified path.
2720
2721path may always be specified as a string.
2722On some platforms, path may also be specified as an open file descriptor.
2723 If this functionality is unavailable, using it raises an exception.
2724[clinic start generated code]*/
2725
Larry Hastings2f936352014-08-05 14:04:04 +10002726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002727os_chdir_impl(PyObject *module, path_t *path)
2728/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002729{
2730 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002731
2732 Py_BEGIN_ALLOW_THREADS
2733#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002734 /* on unix, success = 0, on windows, success = !0 */
2735 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002736#else
2737#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002738 if (path->fd != -1)
2739 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002740 else
2741#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002742 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002743#endif
2744 Py_END_ALLOW_THREADS
2745
2746 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002747 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002748 }
2749
Larry Hastings2f936352014-08-05 14:04:04 +10002750 Py_RETURN_NONE;
2751}
2752
2753
2754#ifdef HAVE_FCHDIR
2755/*[clinic input]
2756os.fchdir
2757
2758 fd: fildes
2759
2760Change to the directory of the given file descriptor.
2761
2762fd must be opened on a directory, not a file.
2763Equivalent to os.chdir(fd).
2764
2765[clinic start generated code]*/
2766
Fred Drake4d1e64b2002-04-15 19:40:07 +00002767static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002768os_fchdir_impl(PyObject *module, int fd)
2769/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002770{
Larry Hastings2f936352014-08-05 14:04:04 +10002771 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002772}
2773#endif /* HAVE_FCHDIR */
2774
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002775
Larry Hastings2f936352014-08-05 14:04:04 +10002776/*[clinic input]
2777os.chmod
2778
2779 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002780 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002781 On some platforms, path may also be specified as an open file descriptor.
2782 If this functionality is unavailable, using it raises an exception.
2783
2784 mode: int
2785 Operating-system mode bitfield.
2786
2787 *
2788
2789 dir_fd : dir_fd(requires='fchmodat') = None
2790 If not None, it should be a file descriptor open to a directory,
2791 and path should be relative; path will then be relative to that
2792 directory.
2793
2794 follow_symlinks: bool = True
2795 If False, and the last element of the path is a symbolic link,
2796 chmod will modify the symbolic link itself instead of the file
2797 the link points to.
2798
2799Change the access permissions of a file.
2800
2801It is an error to use dir_fd or follow_symlinks when specifying path as
2802 an open file descriptor.
2803dir_fd and follow_symlinks may not be implemented on your platform.
2804 If they are unavailable, using them will raise a NotImplementedError.
2805
2806[clinic start generated code]*/
2807
Larry Hastings2f936352014-08-05 14:04:04 +10002808static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002809os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002810 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002811/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002812{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002813 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002814
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002815#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002816 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002817#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002818
Larry Hastings9cf065c2012-06-22 16:30:09 -07002819#ifdef HAVE_FCHMODAT
2820 int fchmodat_nofollow_unsupported = 0;
2821#endif
2822
Larry Hastings9cf065c2012-06-22 16:30:09 -07002823#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2824 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002825 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002826#endif
2827
2828#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002829 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002830 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002831 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002832 result = 0;
2833 else {
2834 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002835 attr &= ~FILE_ATTRIBUTE_READONLY;
2836 else
2837 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002838 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002839 }
2840 Py_END_ALLOW_THREADS
2841
2842 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002843 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002844 }
2845#else /* MS_WINDOWS */
2846 Py_BEGIN_ALLOW_THREADS
2847#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002848 if (path->fd != -1)
2849 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002850 else
2851#endif
2852#ifdef HAVE_LCHMOD
2853 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002854 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002855 else
2856#endif
2857#ifdef HAVE_FCHMODAT
2858 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2859 /*
2860 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2861 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002862 * and then says it isn't implemented yet.
2863 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002864 *
2865 * Once it is supported, os.chmod will automatically
2866 * support dir_fd and follow_symlinks=False. (Hopefully.)
2867 * Until then, we need to be careful what exception we raise.
2868 */
Larry Hastings2f936352014-08-05 14:04:04 +10002869 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002870 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2871 /*
2872 * But wait! We can't throw the exception without allowing threads,
2873 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2874 */
2875 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002876 result &&
2877 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2878 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002879 }
2880 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002881#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002882 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002883 Py_END_ALLOW_THREADS
2884
2885 if (result) {
2886#ifdef HAVE_FCHMODAT
2887 if (fchmodat_nofollow_unsupported) {
2888 if (dir_fd != DEFAULT_DIR_FD)
2889 dir_fd_and_follow_symlinks_invalid("chmod",
2890 dir_fd, follow_symlinks);
2891 else
2892 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002893 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002894 }
2895 else
2896#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002897 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002898 }
2899#endif
2900
Larry Hastings2f936352014-08-05 14:04:04 +10002901 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002902}
2903
Larry Hastings9cf065c2012-06-22 16:30:09 -07002904
Christian Heimes4e30a842007-11-30 22:12:06 +00002905#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002906/*[clinic input]
2907os.fchmod
2908
2909 fd: int
2910 mode: int
2911
2912Change the access permissions of the file given by file descriptor fd.
2913
2914Equivalent to os.chmod(fd, mode).
2915[clinic start generated code]*/
2916
Larry Hastings2f936352014-08-05 14:04:04 +10002917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002918os_fchmod_impl(PyObject *module, int fd, int mode)
2919/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002920{
2921 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002922 int async_err = 0;
2923
2924 do {
2925 Py_BEGIN_ALLOW_THREADS
2926 res = fchmod(fd, mode);
2927 Py_END_ALLOW_THREADS
2928 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2929 if (res != 0)
2930 return (!async_err) ? posix_error() : NULL;
2931
Victor Stinner8c62be82010-05-06 00:08:46 +00002932 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002933}
2934#endif /* HAVE_FCHMOD */
2935
Larry Hastings2f936352014-08-05 14:04:04 +10002936
Christian Heimes4e30a842007-11-30 22:12:06 +00002937#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002938/*[clinic input]
2939os.lchmod
2940
2941 path: path_t
2942 mode: int
2943
2944Change the access permissions of a file, without following symbolic links.
2945
2946If path is a symlink, this affects the link itself rather than the target.
2947Equivalent to chmod(path, mode, follow_symlinks=False)."
2948[clinic start generated code]*/
2949
Larry Hastings2f936352014-08-05 14:04:04 +10002950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002951os_lchmod_impl(PyObject *module, path_t *path, int mode)
2952/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002953{
Victor Stinner8c62be82010-05-06 00:08:46 +00002954 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002955 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002956 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002957 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002958 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002959 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002960 return NULL;
2961 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002962 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002963}
2964#endif /* HAVE_LCHMOD */
2965
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002966
Thomas Wouterscf297e42007-02-23 15:07:44 +00002967#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002968/*[clinic input]
2969os.chflags
2970
2971 path: path_t
2972 flags: unsigned_long(bitwise=True)
2973 follow_symlinks: bool=True
2974
2975Set file flags.
2976
2977If follow_symlinks is False, and the last element of the path is a symbolic
2978 link, chflags will change flags on the symbolic link itself instead of the
2979 file the link points to.
2980follow_symlinks may not be implemented on your platform. If it is
2981unavailable, using it will raise a NotImplementedError.
2982
2983[clinic start generated code]*/
2984
Larry Hastings2f936352014-08-05 14:04:04 +10002985static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002986os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002987 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002988/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002989{
2990 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002991
2992#ifndef HAVE_LCHFLAGS
2993 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002994 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002995#endif
2996
Victor Stinner8c62be82010-05-06 00:08:46 +00002997 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002998#ifdef HAVE_LCHFLAGS
2999 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003000 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003001 else
3002#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003003 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003004 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003005
Larry Hastings2f936352014-08-05 14:04:04 +10003006 if (result)
3007 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003008
Larry Hastings2f936352014-08-05 14:04:04 +10003009 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003010}
3011#endif /* HAVE_CHFLAGS */
3012
Larry Hastings2f936352014-08-05 14:04:04 +10003013
Thomas Wouterscf297e42007-02-23 15:07:44 +00003014#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003015/*[clinic input]
3016os.lchflags
3017
3018 path: path_t
3019 flags: unsigned_long(bitwise=True)
3020
3021Set file flags.
3022
3023This function will not follow symbolic links.
3024Equivalent to chflags(path, flags, follow_symlinks=False).
3025[clinic start generated code]*/
3026
Larry Hastings2f936352014-08-05 14:04:04 +10003027static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003028os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3029/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003030{
Victor Stinner8c62be82010-05-06 00:08:46 +00003031 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003032 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003033 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003034 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003035 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003036 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003037 }
Victor Stinner292c8352012-10-30 02:17:38 +01003038 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003039}
3040#endif /* HAVE_LCHFLAGS */
3041
Larry Hastings2f936352014-08-05 14:04:04 +10003042
Martin v. Löwis244edc82001-10-04 22:44:26 +00003043#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003044/*[clinic input]
3045os.chroot
3046 path: path_t
3047
3048Change root directory to path.
3049
3050[clinic start generated code]*/
3051
Larry Hastings2f936352014-08-05 14:04:04 +10003052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003053os_chroot_impl(PyObject *module, path_t *path)
3054/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003055{
3056 int res;
3057 Py_BEGIN_ALLOW_THREADS
3058 res = chroot(path->narrow);
3059 Py_END_ALLOW_THREADS
3060 if (res < 0)
3061 return path_error(path);
3062 Py_RETURN_NONE;
3063}
3064#endif /* HAVE_CHROOT */
3065
Martin v. Löwis244edc82001-10-04 22:44:26 +00003066
Guido van Rossum21142a01999-01-08 21:05:37 +00003067#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003068/*[clinic input]
3069os.fsync
3070
3071 fd: fildes
3072
3073Force write of fd to disk.
3074[clinic start generated code]*/
3075
Larry Hastings2f936352014-08-05 14:04:04 +10003076static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003077os_fsync_impl(PyObject *module, int fd)
3078/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003079{
3080 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003081}
3082#endif /* HAVE_FSYNC */
3083
Larry Hastings2f936352014-08-05 14:04:04 +10003084
Ross Lagerwall7807c352011-03-17 20:20:30 +02003085#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003086/*[clinic input]
3087os.sync
3088
3089Force write of everything to disk.
3090[clinic start generated code]*/
3091
Larry Hastings2f936352014-08-05 14:04:04 +10003092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003093os_sync_impl(PyObject *module)
3094/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003095{
3096 Py_BEGIN_ALLOW_THREADS
3097 sync();
3098 Py_END_ALLOW_THREADS
3099 Py_RETURN_NONE;
3100}
Larry Hastings2f936352014-08-05 14:04:04 +10003101#endif /* HAVE_SYNC */
3102
Ross Lagerwall7807c352011-03-17 20:20:30 +02003103
Guido van Rossum21142a01999-01-08 21:05:37 +00003104#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003105#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003106extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3107#endif
3108
Larry Hastings2f936352014-08-05 14:04:04 +10003109/*[clinic input]
3110os.fdatasync
3111
3112 fd: fildes
3113
3114Force write of fd to disk without forcing update of metadata.
3115[clinic start generated code]*/
3116
Larry Hastings2f936352014-08-05 14:04:04 +10003117static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003118os_fdatasync_impl(PyObject *module, int fd)
3119/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003120{
3121 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003122}
3123#endif /* HAVE_FDATASYNC */
3124
3125
Fredrik Lundh10723342000-07-10 16:38:09 +00003126#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003127/*[clinic input]
3128os.chown
3129
3130 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003131 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003132
3133 uid: uid_t
3134
3135 gid: gid_t
3136
3137 *
3138
3139 dir_fd : dir_fd(requires='fchownat') = None
3140 If not None, it should be a file descriptor open to a directory,
3141 and path should be relative; path will then be relative to that
3142 directory.
3143
3144 follow_symlinks: bool = True
3145 If False, and the last element of the path is a symbolic link,
3146 stat will examine the symbolic link itself instead of the file
3147 the link points to.
3148
3149Change the owner and group id of path to the numeric uid and gid.\
3150
3151path may always be specified as a string.
3152On some platforms, path may also be specified as an open file descriptor.
3153 If this functionality is unavailable, using it raises an exception.
3154If dir_fd is not None, it should be a file descriptor open to a directory,
3155 and path should be relative; path will then be relative to that directory.
3156If follow_symlinks is False, and the last element of the path is a symbolic
3157 link, chown will modify the symbolic link itself instead of the file the
3158 link points to.
3159It is an error to use dir_fd or follow_symlinks when specifying path as
3160 an open file descriptor.
3161dir_fd and follow_symlinks may not be implemented on your platform.
3162 If they are unavailable, using them will raise a NotImplementedError.
3163
3164[clinic start generated code]*/
3165
Larry Hastings2f936352014-08-05 14:04:04 +10003166static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003167os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003168 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003169/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003170{
3171 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003172
3173#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3174 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003175 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003176#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003177 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3178 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3179 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003180
3181#ifdef __APPLE__
3182 /*
3183 * This is for Mac OS X 10.3, which doesn't have lchown.
3184 * (But we still have an lchown symbol because of weak-linking.)
3185 * It doesn't have fchownat either. So there's no possibility
3186 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003187 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003188 if ((!follow_symlinks) && (lchown == NULL)) {
3189 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003190 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003191 }
3192#endif
3193
Victor Stinner8c62be82010-05-06 00:08:46 +00003194 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003195#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003196 if (path->fd != -1)
3197 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003198 else
3199#endif
3200#ifdef HAVE_LCHOWN
3201 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003202 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003203 else
3204#endif
3205#ifdef HAVE_FCHOWNAT
3206 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003207 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003208 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3209 else
3210#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003211 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003212 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003213
Larry Hastings2f936352014-08-05 14:04:04 +10003214 if (result)
3215 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003216
Larry Hastings2f936352014-08-05 14:04:04 +10003217 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003218}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003219#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003220
Larry Hastings2f936352014-08-05 14:04:04 +10003221
Christian Heimes4e30a842007-11-30 22:12:06 +00003222#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003223/*[clinic input]
3224os.fchown
3225
3226 fd: int
3227 uid: uid_t
3228 gid: gid_t
3229
3230Change the owner and group id of the file specified by file descriptor.
3231
3232Equivalent to os.chown(fd, uid, gid).
3233
3234[clinic start generated code]*/
3235
Larry Hastings2f936352014-08-05 14:04:04 +10003236static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003237os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3238/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003239{
Victor Stinner8c62be82010-05-06 00:08:46 +00003240 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003241 int async_err = 0;
3242
3243 do {
3244 Py_BEGIN_ALLOW_THREADS
3245 res = fchown(fd, uid, gid);
3246 Py_END_ALLOW_THREADS
3247 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3248 if (res != 0)
3249 return (!async_err) ? posix_error() : NULL;
3250
Victor Stinner8c62be82010-05-06 00:08:46 +00003251 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003252}
3253#endif /* HAVE_FCHOWN */
3254
Larry Hastings2f936352014-08-05 14:04:04 +10003255
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003256#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003257/*[clinic input]
3258os.lchown
3259
3260 path : path_t
3261 uid: uid_t
3262 gid: gid_t
3263
3264Change the owner and group id of path to the numeric uid and gid.
3265
3266This function will not follow symbolic links.
3267Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3268[clinic start generated code]*/
3269
Larry Hastings2f936352014-08-05 14:04:04 +10003270static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003271os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3272/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003273{
Victor Stinner8c62be82010-05-06 00:08:46 +00003274 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003275 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003276 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003277 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003278 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003279 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003280 }
Larry Hastings2f936352014-08-05 14:04:04 +10003281 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003282}
3283#endif /* HAVE_LCHOWN */
3284
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003285
Barry Warsaw53699e91996-12-10 23:23:01 +00003286static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003287posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003288{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003289 char *buf, *tmpbuf;
3290 char *cwd;
3291 const size_t chunk = 1024;
3292 size_t buflen = 0;
3293 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003294
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003295#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003297 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 wchar_t *wbuf2 = wbuf;
3299 PyObject *resobj;
3300 DWORD len;
3301 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003302 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 /* If the buffer is large enough, len does not include the
3304 terminating \0. If the buffer is too small, len includes
3305 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003306 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003307 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 if (wbuf2)
3309 len = GetCurrentDirectoryW(len, wbuf2);
3310 }
3311 Py_END_ALLOW_THREADS
3312 if (!wbuf2) {
3313 PyErr_NoMemory();
3314 return NULL;
3315 }
3316 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003317 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003318 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003319 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003320 }
3321 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003322 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003323 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 return resobj;
3325 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003326
3327 if (win32_warn_bytes_api())
3328 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003329#endif
3330
Victor Stinner4403d7d2015-04-25 00:16:10 +02003331 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003332 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003333 do {
3334 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003335#ifdef MS_WINDOWS
3336 if (buflen > INT_MAX) {
3337 PyErr_NoMemory();
3338 break;
3339 }
3340#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003341 tmpbuf = PyMem_RawRealloc(buf, buflen);
3342 if (tmpbuf == NULL)
3343 break;
3344
3345 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003346#ifdef MS_WINDOWS
3347 cwd = getcwd(buf, (int)buflen);
3348#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003349 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003350#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003351 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003352 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003353
3354 if (cwd == NULL) {
3355 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003356 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003357 }
3358
Victor Stinner8c62be82010-05-06 00:08:46 +00003359 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003360 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3361 else
3362 obj = PyUnicode_DecodeFSDefault(buf);
3363 PyMem_RawFree(buf);
3364
3365 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003366}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003367
Larry Hastings2f936352014-08-05 14:04:04 +10003368
3369/*[clinic input]
3370os.getcwd
3371
3372Return a unicode string representing the current working directory.
3373[clinic start generated code]*/
3374
Larry Hastings2f936352014-08-05 14:04:04 +10003375static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003376os_getcwd_impl(PyObject *module)
3377/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003378{
3379 return posix_getcwd(0);
3380}
3381
Larry Hastings2f936352014-08-05 14:04:04 +10003382
3383/*[clinic input]
3384os.getcwdb
3385
3386Return a bytes string representing the current working directory.
3387[clinic start generated code]*/
3388
Larry Hastings2f936352014-08-05 14:04:04 +10003389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003390os_getcwdb_impl(PyObject *module)
3391/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003392{
3393 return posix_getcwd(1);
3394}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003395
Larry Hastings2f936352014-08-05 14:04:04 +10003396
Larry Hastings9cf065c2012-06-22 16:30:09 -07003397#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3398#define HAVE_LINK 1
3399#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003400
Guido van Rossumb6775db1994-08-01 11:34:53 +00003401#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003402/*[clinic input]
3403
3404os.link
3405
3406 src : path_t
3407 dst : path_t
3408 *
3409 src_dir_fd : dir_fd = None
3410 dst_dir_fd : dir_fd = None
3411 follow_symlinks: bool = True
3412
3413Create a hard link to a file.
3414
3415If either src_dir_fd or dst_dir_fd is not None, it should be a file
3416 descriptor open to a directory, and the respective path string (src or dst)
3417 should be relative; the path will then be relative to that directory.
3418If follow_symlinks is False, and the last element of src is a symbolic
3419 link, link will create a link to the symbolic link itself instead of the
3420 file the link points to.
3421src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3422 platform. If they are unavailable, using them will raise a
3423 NotImplementedError.
3424[clinic start generated code]*/
3425
Larry Hastings2f936352014-08-05 14:04:04 +10003426static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003427os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003428 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003429/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003430{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003431#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003432 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003433#else
3434 int result;
3435#endif
3436
Larry Hastings9cf065c2012-06-22 16:30:09 -07003437#ifndef HAVE_LINKAT
3438 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3439 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003440 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003441 }
3442#endif
3443
Steve Dowercc16be82016-09-08 10:35:16 -07003444#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003445 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003446 PyErr_SetString(PyExc_NotImplementedError,
3447 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003448 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003449 }
Steve Dowercc16be82016-09-08 10:35:16 -07003450#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003451
Brian Curtin1b9df392010-11-24 20:24:31 +00003452#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003453 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003454 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003455 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003456
Larry Hastings2f936352014-08-05 14:04:04 +10003457 if (!result)
3458 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003459#else
3460 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003461#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003462 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3463 (dst_dir_fd != DEFAULT_DIR_FD) ||
3464 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003465 result = linkat(src_dir_fd, src->narrow,
3466 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003467 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3468 else
Steve Dowercc16be82016-09-08 10:35:16 -07003469#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003470 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003471 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003472
Larry Hastings2f936352014-08-05 14:04:04 +10003473 if (result)
3474 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003475#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003476
Larry Hastings2f936352014-08-05 14:04:04 +10003477 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003478}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003479#endif
3480
Brian Curtin1b9df392010-11-24 20:24:31 +00003481
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003482#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003483static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003484_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003485{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003486 PyObject *v;
3487 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3488 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003489 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003490 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003491 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003492 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003493
Steve Dowercc16be82016-09-08 10:35:16 -07003494 WIN32_FIND_DATAW wFileData;
3495 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003496
Steve Dowercc16be82016-09-08 10:35:16 -07003497 if (!path->wide) { /* Default arg: "." */
3498 po_wchars = L".";
3499 len = 1;
3500 } else {
3501 po_wchars = path->wide;
3502 len = wcslen(path->wide);
3503 }
3504 /* The +5 is so we can append "\\*.*\0" */
3505 wnamebuf = PyMem_New(wchar_t, len + 5);
3506 if (!wnamebuf) {
3507 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003508 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003509 }
Steve Dowercc16be82016-09-08 10:35:16 -07003510 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003511 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003512 wchar_t wch = wnamebuf[len-1];
3513 if (wch != SEP && wch != ALTSEP && wch != L':')
3514 wnamebuf[len++] = SEP;
3515 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003516 }
Steve Dowercc16be82016-09-08 10:35:16 -07003517 if ((list = PyList_New(0)) == NULL) {
3518 goto exit;
3519 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003520 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003521 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003522 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003523 if (hFindFile == INVALID_HANDLE_VALUE) {
3524 int error = GetLastError();
3525 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003526 goto exit;
3527 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003528 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003529 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 }
3531 do {
3532 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003533 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3534 wcscmp(wFileData.cFileName, L"..") != 0) {
3535 v = PyUnicode_FromWideChar(wFileData.cFileName,
3536 wcslen(wFileData.cFileName));
3537 if (path->narrow && v) {
3538 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3539 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003540 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003541 Py_DECREF(list);
3542 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003543 break;
3544 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003545 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003547 Py_DECREF(list);
3548 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 break;
3550 }
3551 Py_DECREF(v);
3552 }
3553 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003554 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003555 Py_END_ALLOW_THREADS
3556 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3557 it got to the end of the directory. */
3558 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003559 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003560 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003561 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003562 }
3563 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003564
Larry Hastings9cf065c2012-06-22 16:30:09 -07003565exit:
3566 if (hFindFile != INVALID_HANDLE_VALUE) {
3567 if (FindClose(hFindFile) == FALSE) {
3568 if (list != NULL) {
3569 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003570 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003571 }
3572 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003573 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003574 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003575
Larry Hastings9cf065c2012-06-22 16:30:09 -07003576 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003577} /* end of _listdir_windows_no_opendir */
3578
3579#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3580
3581static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003582_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003583{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003584 PyObject *v;
3585 DIR *dirp = NULL;
3586 struct dirent *ep;
3587 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003588#ifdef HAVE_FDOPENDIR
3589 int fd = -1;
3590#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003591
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003594 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003595 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003596 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003597 if (fd == -1)
3598 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003599
Larry Hastingsfdaea062012-06-25 04:42:23 -07003600 return_str = 1;
3601
Larry Hastings9cf065c2012-06-22 16:30:09 -07003602 Py_BEGIN_ALLOW_THREADS
3603 dirp = fdopendir(fd);
3604 Py_END_ALLOW_THREADS
3605 }
3606 else
3607#endif
3608 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003609 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003610 if (path->narrow) {
3611 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003612 /* only return bytes if they specified a bytes-like object */
3613 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003614 }
3615 else {
3616 name = ".";
3617 return_str = 1;
3618 }
3619
Larry Hastings9cf065c2012-06-22 16:30:09 -07003620 Py_BEGIN_ALLOW_THREADS
3621 dirp = opendir(name);
3622 Py_END_ALLOW_THREADS
3623 }
3624
3625 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003626 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003627#ifdef HAVE_FDOPENDIR
3628 if (fd != -1) {
3629 Py_BEGIN_ALLOW_THREADS
3630 close(fd);
3631 Py_END_ALLOW_THREADS
3632 }
3633#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003634 goto exit;
3635 }
3636 if ((list = PyList_New(0)) == NULL) {
3637 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003638 }
3639 for (;;) {
3640 errno = 0;
3641 Py_BEGIN_ALLOW_THREADS
3642 ep = readdir(dirp);
3643 Py_END_ALLOW_THREADS
3644 if (ep == NULL) {
3645 if (errno == 0) {
3646 break;
3647 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003648 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003649 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003650 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003651 }
3652 }
3653 if (ep->d_name[0] == '.' &&
3654 (NAMLEN(ep) == 1 ||
3655 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3656 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003657 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003658 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3659 else
3660 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003661 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003662 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003663 break;
3664 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003665 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003666 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003667 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003668 break;
3669 }
3670 Py_DECREF(v);
3671 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003672
Larry Hastings9cf065c2012-06-22 16:30:09 -07003673exit:
3674 if (dirp != NULL) {
3675 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003676#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003677 if (fd > -1)
3678 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003679#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003680 closedir(dirp);
3681 Py_END_ALLOW_THREADS
3682 }
3683
Larry Hastings9cf065c2012-06-22 16:30:09 -07003684 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003685} /* end of _posix_listdir */
3686#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003687
Larry Hastings2f936352014-08-05 14:04:04 +10003688
3689/*[clinic input]
3690os.listdir
3691
3692 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3693
3694Return a list containing the names of the files in the directory.
3695
BNMetricsb9427072018-11-02 15:20:19 +00003696path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003697 the filenames returned will also be bytes; in all other circumstances
3698 the filenames returned will be str.
3699If path is None, uses the path='.'.
3700On some platforms, path may also be specified as an open file descriptor;\
3701 the file descriptor must refer to a directory.
3702 If this functionality is unavailable, using it raises NotImplementedError.
3703
3704The list is in arbitrary order. It does not include the special
3705entries '.' and '..' even if they are present in the directory.
3706
3707
3708[clinic start generated code]*/
3709
Larry Hastings2f936352014-08-05 14:04:04 +10003710static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003711os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003712/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003713{
3714#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3715 return _listdir_windows_no_opendir(path, NULL);
3716#else
3717 return _posix_listdir(path, NULL);
3718#endif
3719}
3720
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003721#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003722/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003723/*[clinic input]
3724os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003725
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003726 path: path_t
3727 /
3728
3729[clinic start generated code]*/
3730
3731static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003732os__getfullpathname_impl(PyObject *module, path_t *path)
3733/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003734{
Steve Dowercc16be82016-09-08 10:35:16 -07003735 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3736 wchar_t *wtemp;
3737 DWORD result;
3738 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003739
Steve Dowercc16be82016-09-08 10:35:16 -07003740 result = GetFullPathNameW(path->wide,
3741 Py_ARRAY_LENGTH(woutbuf),
3742 woutbuf, &wtemp);
3743 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3744 woutbufp = PyMem_New(wchar_t, result);
3745 if (!woutbufp)
3746 return PyErr_NoMemory();
3747 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003748 }
Steve Dowercc16be82016-09-08 10:35:16 -07003749 if (result) {
3750 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3751 if (path->narrow)
3752 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3753 } else
3754 v = win32_error_object("GetFullPathNameW", path->object);
3755 if (woutbufp != woutbuf)
3756 PyMem_Free(woutbufp);
3757 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003758}
Brian Curtind40e6f72010-07-08 21:39:08 +00003759
Brian Curtind25aef52011-06-13 15:16:04 -05003760
Larry Hastings2f936352014-08-05 14:04:04 +10003761/*[clinic input]
3762os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003763
Steve Dower23ad6d02018-02-22 10:39:10 -08003764 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003765 /
3766
3767A helper function for samepath on windows.
3768[clinic start generated code]*/
3769
Larry Hastings2f936352014-08-05 14:04:04 +10003770static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003771os__getfinalpathname_impl(PyObject *module, path_t *path)
3772/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003773{
3774 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003775 wchar_t buf[MAXPATHLEN], *target_path = buf;
3776 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003777 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003778 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003779
Steve Dower23ad6d02018-02-22 10:39:10 -08003780 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003781 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003782 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003783 0, /* desired access */
3784 0, /* share mode */
3785 NULL, /* security attributes */
3786 OPEN_EXISTING,
3787 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3788 FILE_FLAG_BACKUP_SEMANTICS,
3789 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003790 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003791
Steve Dower23ad6d02018-02-22 10:39:10 -08003792 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003793 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003794 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003795
3796 /* We have a good handle to the target, use it to determine the
3797 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003798 while (1) {
3799 Py_BEGIN_ALLOW_THREADS
3800 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3801 buf_size, VOLUME_NAME_DOS);
3802 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003803
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003804 if (!result_length) {
3805 result = win32_error_object("GetFinalPathNameByHandleW",
3806 path->object);
3807 goto cleanup;
3808 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003809
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003810 if (result_length < buf_size) {
3811 break;
3812 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003813
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003814 wchar_t *tmp;
3815 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3816 result_length * sizeof(*tmp));
3817 if (!tmp) {
3818 result = PyErr_NoMemory();
3819 goto cleanup;
3820 }
3821
3822 buf_size = result_length;
3823 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003824 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003825
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003826 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dower23ad6d02018-02-22 10:39:10 -08003827 if (path->narrow)
3828 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dower23ad6d02018-02-22 10:39:10 -08003829
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003830cleanup:
3831 if (target_path != buf) {
3832 PyMem_Free(target_path);
3833 }
3834 CloseHandle(hFile);
3835 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003836}
Brian Curtin62857742010-09-06 17:07:27 +00003837
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003838/*[clinic input]
3839os._isdir
3840
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003841 path as arg: object
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003842 /
3843
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003844Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003845[clinic start generated code]*/
3846
Brian Curtin9c669cc2011-06-08 18:17:18 -05003847static PyObject *
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003848os__isdir(PyObject *module, PyObject *arg)
3849/*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003850{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003851 DWORD attributes;
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003852 path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
3853
3854 if (!path_converter(arg, &path)) {
3855 if (PyErr_ExceptionMatches(PyExc_ValueError)) {
3856 PyErr_Clear();
3857 Py_RETURN_FALSE;
3858 }
3859 return NULL;
3860 }
Brian Curtin9c669cc2011-06-08 18:17:18 -05003861
Steve Dowerb22a6772016-07-17 20:49:38 -07003862 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003863 attributes = GetFileAttributesW(path.wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003864 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003865
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003866 path_cleanup(&path);
Brian Curtin9c669cc2011-06-08 18:17:18 -05003867 if (attributes == INVALID_FILE_ATTRIBUTES)
3868 Py_RETURN_FALSE;
3869
Brian Curtin9c669cc2011-06-08 18:17:18 -05003870 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3871 Py_RETURN_TRUE;
3872 else
3873 Py_RETURN_FALSE;
3874}
Tim Golden6b528062013-08-01 12:44:00 +01003875
Tim Golden6b528062013-08-01 12:44:00 +01003876
Larry Hastings2f936352014-08-05 14:04:04 +10003877/*[clinic input]
3878os._getvolumepathname
3879
Steve Dower23ad6d02018-02-22 10:39:10 -08003880 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003881
3882A helper function for ismount on Win32.
3883[clinic start generated code]*/
3884
Larry Hastings2f936352014-08-05 14:04:04 +10003885static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003886os__getvolumepathname_impl(PyObject *module, path_t *path)
3887/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003888{
3889 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003890 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003891 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003892 BOOL ret;
3893
Tim Golden6b528062013-08-01 12:44:00 +01003894 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003895 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003896
Victor Stinner850a18e2017-10-24 16:53:32 -07003897 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003898 PyErr_SetString(PyExc_OverflowError, "path too long");
3899 return NULL;
3900 }
3901
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003902 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003903 if (mountpath == NULL)
3904 return PyErr_NoMemory();
3905
3906 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003907 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003908 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003909 Py_END_ALLOW_THREADS
3910
3911 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003912 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003913 goto exit;
3914 }
3915 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08003916 if (path->narrow)
3917 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003918
3919exit:
3920 PyMem_Free(mountpath);
3921 return result;
3922}
Tim Golden6b528062013-08-01 12:44:00 +01003923
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003924#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003925
Larry Hastings2f936352014-08-05 14:04:04 +10003926
3927/*[clinic input]
3928os.mkdir
3929
3930 path : path_t
3931
3932 mode: int = 0o777
3933
3934 *
3935
3936 dir_fd : dir_fd(requires='mkdirat') = None
3937
3938# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3939
3940Create a directory.
3941
3942If dir_fd is not None, it should be a file descriptor open to a directory,
3943 and path should be relative; path will then be relative to that directory.
3944dir_fd may not be implemented on your platform.
3945 If it is unavailable, using it will raise a NotImplementedError.
3946
3947The mode argument is ignored on Windows.
3948[clinic start generated code]*/
3949
Larry Hastings2f936352014-08-05 14:04:04 +10003950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003951os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3952/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003953{
3954 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003955
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003956#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003957 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003958 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003959 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003960
Larry Hastings2f936352014-08-05 14:04:04 +10003961 if (!result)
3962 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003963#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003964 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003965#if HAVE_MKDIRAT
3966 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003967 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003968 else
3969#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02003970#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003971 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003972#else
Larry Hastings2f936352014-08-05 14:04:04 +10003973 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003974#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003975 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003976 if (result < 0)
3977 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07003978#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10003979 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003980}
3981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003982
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003983/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3984#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003985#include <sys/resource.h>
3986#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003987
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003988
3989#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10003990/*[clinic input]
3991os.nice
3992
3993 increment: int
3994 /
3995
3996Add increment to the priority of process and return the new priority.
3997[clinic start generated code]*/
3998
Larry Hastings2f936352014-08-05 14:04:04 +10003999static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004000os_nice_impl(PyObject *module, int increment)
4001/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004002{
4003 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004004
Victor Stinner8c62be82010-05-06 00:08:46 +00004005 /* There are two flavours of 'nice': one that returns the new
4006 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004007 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004008 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004009
Victor Stinner8c62be82010-05-06 00:08:46 +00004010 If we are of the nice family that returns the new priority, we
4011 need to clear errno before the call, and check if errno is filled
4012 before calling posix_error() on a returnvalue of -1, because the
4013 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004014
Victor Stinner8c62be82010-05-06 00:08:46 +00004015 errno = 0;
4016 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004017#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004018 if (value == 0)
4019 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004020#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004021 if (value == -1 && errno != 0)
4022 /* either nice() or getpriority() returned an error */
4023 return posix_error();
4024 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004025}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004026#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004027
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004028
4029#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004030/*[clinic input]
4031os.getpriority
4032
4033 which: int
4034 who: int
4035
4036Return program scheduling priority.
4037[clinic start generated code]*/
4038
Larry Hastings2f936352014-08-05 14:04:04 +10004039static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004040os_getpriority_impl(PyObject *module, int which, int who)
4041/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004042{
4043 int retval;
4044
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004045 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004046 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004047 if (errno != 0)
4048 return posix_error();
4049 return PyLong_FromLong((long)retval);
4050}
4051#endif /* HAVE_GETPRIORITY */
4052
4053
4054#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004055/*[clinic input]
4056os.setpriority
4057
4058 which: int
4059 who: int
4060 priority: int
4061
4062Set program scheduling priority.
4063[clinic start generated code]*/
4064
Larry Hastings2f936352014-08-05 14:04:04 +10004065static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004066os_setpriority_impl(PyObject *module, int which, int who, int priority)
4067/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004068{
4069 int retval;
4070
4071 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004072 if (retval == -1)
4073 return posix_error();
4074 Py_RETURN_NONE;
4075}
4076#endif /* HAVE_SETPRIORITY */
4077
4078
Barry Warsaw53699e91996-12-10 23:23:01 +00004079static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004080internal_rename(path_t *src, path_t *dst, int src_dir_fd, int dst_dir_fd, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004081{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004082 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004083 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004084
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004085#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004086 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004087 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004088#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004089 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004090#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004091
Larry Hastings9cf065c2012-06-22 16:30:09 -07004092 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4093 (dst_dir_fd != DEFAULT_DIR_FD);
4094#ifndef HAVE_RENAMEAT
4095 if (dir_fd_specified) {
4096 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004097 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004098 }
4099#endif
4100
Larry Hastings9cf065c2012-06-22 16:30:09 -07004101#ifdef MS_WINDOWS
4102 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004103 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004104 Py_END_ALLOW_THREADS
4105
Larry Hastings2f936352014-08-05 14:04:04 +10004106 if (!result)
4107 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004108
4109#else
Steve Dowercc16be82016-09-08 10:35:16 -07004110 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4111 PyErr_Format(PyExc_ValueError,
4112 "%s: src and dst must be the same type", function_name);
4113 return NULL;
4114 }
4115
Larry Hastings9cf065c2012-06-22 16:30:09 -07004116 Py_BEGIN_ALLOW_THREADS
4117#ifdef HAVE_RENAMEAT
4118 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004119 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004120 else
4121#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004122 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004123 Py_END_ALLOW_THREADS
4124
Larry Hastings2f936352014-08-05 14:04:04 +10004125 if (result)
4126 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004127#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004128 Py_RETURN_NONE;
4129}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004130
Larry Hastings2f936352014-08-05 14:04:04 +10004131
4132/*[clinic input]
4133os.rename
4134
4135 src : path_t
4136 dst : path_t
4137 *
4138 src_dir_fd : dir_fd = None
4139 dst_dir_fd : dir_fd = None
4140
4141Rename a file or directory.
4142
4143If either src_dir_fd or dst_dir_fd is not None, it should be a file
4144 descriptor open to a directory, and the respective path string (src or dst)
4145 should be relative; the path will then be relative to that directory.
4146src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4147 If they are unavailable, using them will raise a NotImplementedError.
4148[clinic start generated code]*/
4149
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004150static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004151os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004152 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004153/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004154{
Larry Hastings2f936352014-08-05 14:04:04 +10004155 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004156}
4157
Larry Hastings2f936352014-08-05 14:04:04 +10004158
4159/*[clinic input]
4160os.replace = os.rename
4161
4162Rename a file or directory, overwriting the destination.
4163
4164If either src_dir_fd or dst_dir_fd is not None, it should be a file
4165 descriptor open to a directory, and the respective path string (src or dst)
4166 should be relative; the path will then be relative to that directory.
4167src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004168 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004169[clinic start generated code]*/
4170
Larry Hastings2f936352014-08-05 14:04:04 +10004171static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004172os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4173 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004174/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004175{
4176 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4177}
4178
4179
4180/*[clinic input]
4181os.rmdir
4182
4183 path: path_t
4184 *
4185 dir_fd: dir_fd(requires='unlinkat') = None
4186
4187Remove a directory.
4188
4189If dir_fd is not None, it should be a file descriptor open to a directory,
4190 and path should be relative; path will then be relative to that directory.
4191dir_fd may not be implemented on your platform.
4192 If it is unavailable, using it will raise a NotImplementedError.
4193[clinic start generated code]*/
4194
Larry Hastings2f936352014-08-05 14:04:04 +10004195static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004196os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4197/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004198{
4199 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004200
4201 Py_BEGIN_ALLOW_THREADS
4202#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004203 /* Windows, success=1, UNIX, success=0 */
4204 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004205#else
4206#ifdef HAVE_UNLINKAT
4207 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004208 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004209 else
4210#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004211 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004212#endif
4213 Py_END_ALLOW_THREADS
4214
Larry Hastings2f936352014-08-05 14:04:04 +10004215 if (result)
4216 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004217
Larry Hastings2f936352014-08-05 14:04:04 +10004218 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004219}
4220
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004221
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004222#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004223#ifdef MS_WINDOWS
4224/*[clinic input]
4225os.system -> long
4226
4227 command: Py_UNICODE
4228
4229Execute the command in a subshell.
4230[clinic start generated code]*/
4231
Larry Hastings2f936352014-08-05 14:04:04 +10004232static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004233os_system_impl(PyObject *module, const Py_UNICODE *command)
4234/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004235{
4236 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004237 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004238 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004239 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004240 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004241 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004242 return result;
4243}
4244#else /* MS_WINDOWS */
4245/*[clinic input]
4246os.system -> long
4247
4248 command: FSConverter
4249
4250Execute the command in a subshell.
4251[clinic start generated code]*/
4252
Larry Hastings2f936352014-08-05 14:04:04 +10004253static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004254os_system_impl(PyObject *module, PyObject *command)
4255/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004256{
4257 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004258 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004259 Py_BEGIN_ALLOW_THREADS
4260 result = system(bytes);
4261 Py_END_ALLOW_THREADS
4262 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004263}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004264#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004265#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004267
Larry Hastings2f936352014-08-05 14:04:04 +10004268/*[clinic input]
4269os.umask
4270
4271 mask: int
4272 /
4273
4274Set the current numeric umask and return the previous umask.
4275[clinic start generated code]*/
4276
Larry Hastings2f936352014-08-05 14:04:04 +10004277static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004278os_umask_impl(PyObject *module, int mask)
4279/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004280{
4281 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004282 if (i < 0)
4283 return posix_error();
4284 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004285}
4286
Brian Curtind40e6f72010-07-08 21:39:08 +00004287#ifdef MS_WINDOWS
4288
4289/* override the default DeleteFileW behavior so that directory
4290symlinks can be removed with this function, the same as with
4291Unix symlinks */
4292BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4293{
4294 WIN32_FILE_ATTRIBUTE_DATA info;
4295 WIN32_FIND_DATAW find_data;
4296 HANDLE find_data_handle;
4297 int is_directory = 0;
4298 int is_link = 0;
4299
4300 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4301 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004302
Brian Curtind40e6f72010-07-08 21:39:08 +00004303 /* Get WIN32_FIND_DATA structure for the path to determine if
4304 it is a symlink */
4305 if(is_directory &&
4306 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4307 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4308
4309 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004310 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4311 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4312 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4313 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004314 FindClose(find_data_handle);
4315 }
4316 }
4317 }
4318
4319 if (is_directory && is_link)
4320 return RemoveDirectoryW(lpFileName);
4321
4322 return DeleteFileW(lpFileName);
4323}
4324#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004325
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004326
Larry Hastings2f936352014-08-05 14:04:04 +10004327/*[clinic input]
4328os.unlink
4329
4330 path: path_t
4331 *
4332 dir_fd: dir_fd(requires='unlinkat')=None
4333
4334Remove a file (same as remove()).
4335
4336If dir_fd is not None, it should be a file descriptor open to a directory,
4337 and path should be relative; path will then be relative to that directory.
4338dir_fd may not be implemented on your platform.
4339 If it is unavailable, using it will raise a NotImplementedError.
4340
4341[clinic start generated code]*/
4342
Larry Hastings2f936352014-08-05 14:04:04 +10004343static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004344os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4345/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004346{
4347 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004348
4349 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004350 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004351#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004352 /* Windows, success=1, UNIX, success=0 */
4353 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004354#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004355#ifdef HAVE_UNLINKAT
4356 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004357 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004358 else
4359#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004360 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004361#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004362 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004363 Py_END_ALLOW_THREADS
4364
Larry Hastings2f936352014-08-05 14:04:04 +10004365 if (result)
4366 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004367
Larry Hastings2f936352014-08-05 14:04:04 +10004368 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004369}
4370
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004371
Larry Hastings2f936352014-08-05 14:04:04 +10004372/*[clinic input]
4373os.remove = os.unlink
4374
4375Remove a file (same as unlink()).
4376
4377If dir_fd is not None, it should be a file descriptor open to a directory,
4378 and path should be relative; path will then be relative to that directory.
4379dir_fd may not be implemented on your platform.
4380 If it is unavailable, using it will raise a NotImplementedError.
4381[clinic start generated code]*/
4382
Larry Hastings2f936352014-08-05 14:04:04 +10004383static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004384os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4385/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004386{
4387 return os_unlink_impl(module, path, dir_fd);
4388}
4389
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004390
Larry Hastings605a62d2012-06-24 04:33:36 -07004391static PyStructSequence_Field uname_result_fields[] = {
4392 {"sysname", "operating system name"},
4393 {"nodename", "name of machine on network (implementation-defined)"},
4394 {"release", "operating system release"},
4395 {"version", "operating system version"},
4396 {"machine", "hardware identifier"},
4397 {NULL}
4398};
4399
4400PyDoc_STRVAR(uname_result__doc__,
4401"uname_result: Result from os.uname().\n\n\
4402This object may be accessed either as a tuple of\n\
4403 (sysname, nodename, release, version, machine),\n\
4404or via the attributes sysname, nodename, release, version, and machine.\n\
4405\n\
4406See os.uname for more information.");
4407
4408static PyStructSequence_Desc uname_result_desc = {
4409 "uname_result", /* name */
4410 uname_result__doc__, /* doc */
4411 uname_result_fields,
4412 5
4413};
4414
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004415static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004416
4417
4418#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004419/*[clinic input]
4420os.uname
4421
4422Return an object identifying the current operating system.
4423
4424The object behaves like a named tuple with the following fields:
4425 (sysname, nodename, release, version, machine)
4426
4427[clinic start generated code]*/
4428
Larry Hastings2f936352014-08-05 14:04:04 +10004429static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004430os_uname_impl(PyObject *module)
4431/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004432{
Victor Stinner8c62be82010-05-06 00:08:46 +00004433 struct utsname u;
4434 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004435 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004436
Victor Stinner8c62be82010-05-06 00:08:46 +00004437 Py_BEGIN_ALLOW_THREADS
4438 res = uname(&u);
4439 Py_END_ALLOW_THREADS
4440 if (res < 0)
4441 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004442
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004443 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004444 if (value == NULL)
4445 return NULL;
4446
4447#define SET(i, field) \
4448 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004449 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004450 if (!o) { \
4451 Py_DECREF(value); \
4452 return NULL; \
4453 } \
4454 PyStructSequence_SET_ITEM(value, i, o); \
4455 } \
4456
4457 SET(0, u.sysname);
4458 SET(1, u.nodename);
4459 SET(2, u.release);
4460 SET(3, u.version);
4461 SET(4, u.machine);
4462
4463#undef SET
4464
4465 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004466}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004467#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004468
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004469
Larry Hastings9cf065c2012-06-22 16:30:09 -07004470
4471typedef struct {
4472 int now;
4473 time_t atime_s;
4474 long atime_ns;
4475 time_t mtime_s;
4476 long mtime_ns;
4477} utime_t;
4478
4479/*
Victor Stinner484df002014-10-09 13:52:31 +02004480 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004481 * they also intentionally leak the declaration of a pointer named "time"
4482 */
4483#define UTIME_TO_TIMESPEC \
4484 struct timespec ts[2]; \
4485 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004486 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004487 time = NULL; \
4488 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004489 ts[0].tv_sec = ut->atime_s; \
4490 ts[0].tv_nsec = ut->atime_ns; \
4491 ts[1].tv_sec = ut->mtime_s; \
4492 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004493 time = ts; \
4494 } \
4495
4496#define UTIME_TO_TIMEVAL \
4497 struct timeval tv[2]; \
4498 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004499 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004500 time = NULL; \
4501 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004502 tv[0].tv_sec = ut->atime_s; \
4503 tv[0].tv_usec = ut->atime_ns / 1000; \
4504 tv[1].tv_sec = ut->mtime_s; \
4505 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004506 time = tv; \
4507 } \
4508
4509#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004510 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004511 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004512 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004513 time = NULL; \
4514 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004515 u.actime = ut->atime_s; \
4516 u.modtime = ut->mtime_s; \
4517 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004518 }
4519
4520#define UTIME_TO_TIME_T \
4521 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004522 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004523 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004524 time = NULL; \
4525 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004526 timet[0] = ut->atime_s; \
4527 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004528 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004529 } \
4530
4531
Victor Stinner528a9ab2015-09-03 21:30:26 +02004532#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004533
4534static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004535utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004536{
4537#ifdef HAVE_UTIMENSAT
4538 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4539 UTIME_TO_TIMESPEC;
4540 return utimensat(dir_fd, path, time, flags);
4541#elif defined(HAVE_FUTIMESAT)
4542 UTIME_TO_TIMEVAL;
4543 /*
4544 * follow_symlinks will never be false here;
4545 * we only allow !follow_symlinks and dir_fd together
4546 * if we have utimensat()
4547 */
4548 assert(follow_symlinks);
4549 return futimesat(dir_fd, path, time);
4550#endif
4551}
4552
Larry Hastings2f936352014-08-05 14:04:04 +10004553 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4554#else
4555 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004556#endif
4557
Victor Stinner528a9ab2015-09-03 21:30:26 +02004558#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004559
4560static int
Victor Stinner484df002014-10-09 13:52:31 +02004561utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004562{
4563#ifdef HAVE_FUTIMENS
4564 UTIME_TO_TIMESPEC;
4565 return futimens(fd, time);
4566#else
4567 UTIME_TO_TIMEVAL;
4568 return futimes(fd, time);
4569#endif
4570}
4571
Larry Hastings2f936352014-08-05 14:04:04 +10004572 #define PATH_UTIME_HAVE_FD 1
4573#else
4574 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004575#endif
4576
Victor Stinner5ebae872015-09-22 01:29:33 +02004577#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4578# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4579#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004580
Victor Stinner4552ced2015-09-21 22:37:15 +02004581#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004582
4583static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004584utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004585{
4586#ifdef HAVE_UTIMENSAT
4587 UTIME_TO_TIMESPEC;
4588 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4589#else
4590 UTIME_TO_TIMEVAL;
4591 return lutimes(path, time);
4592#endif
4593}
4594
4595#endif
4596
4597#ifndef MS_WINDOWS
4598
4599static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004600utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004601{
4602#ifdef HAVE_UTIMENSAT
4603 UTIME_TO_TIMESPEC;
4604 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4605#elif defined(HAVE_UTIMES)
4606 UTIME_TO_TIMEVAL;
4607 return utimes(path, time);
4608#elif defined(HAVE_UTIME_H)
4609 UTIME_TO_UTIMBUF;
4610 return utime(path, time);
4611#else
4612 UTIME_TO_TIME_T;
4613 return utime(path, time);
4614#endif
4615}
4616
4617#endif
4618
Larry Hastings76ad59b2012-05-03 00:30:07 -07004619static int
4620split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4621{
4622 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004623 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004624 divmod = PyNumber_Divmod(py_long, billion);
4625 if (!divmod)
4626 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004627 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4628 PyErr_Format(PyExc_TypeError,
4629 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4630 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4631 goto exit;
4632 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004633 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4634 if ((*s == -1) && PyErr_Occurred())
4635 goto exit;
4636 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004637 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004638 goto exit;
4639
4640 result = 1;
4641exit:
4642 Py_XDECREF(divmod);
4643 return result;
4644}
4645
Larry Hastings2f936352014-08-05 14:04:04 +10004646
4647/*[clinic input]
4648os.utime
4649
4650 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4651 times: object = NULL
4652 *
4653 ns: object = NULL
4654 dir_fd: dir_fd(requires='futimensat') = None
4655 follow_symlinks: bool=True
4656
Martin Panter0ff89092015-09-09 01:56:53 +00004657# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004658
4659Set the access and modified time of path.
4660
4661path may always be specified as a string.
4662On some platforms, path may also be specified as an open file descriptor.
4663 If this functionality is unavailable, using it raises an exception.
4664
4665If times is not None, it must be a tuple (atime, mtime);
4666 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004667If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004668 atime_ns and mtime_ns should be expressed as integer nanoseconds
4669 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004670If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004671Specifying tuples for both times and ns is an error.
4672
4673If dir_fd is not None, it should be a file descriptor open to a directory,
4674 and path should be relative; path will then be relative to that directory.
4675If follow_symlinks is False, and the last element of the path is a symbolic
4676 link, utime will modify the symbolic link itself instead of the file the
4677 link points to.
4678It is an error to use dir_fd or follow_symlinks when specifying path
4679 as an open file descriptor.
4680dir_fd and follow_symlinks may not be available on your platform.
4681 If they are unavailable, using them will raise a NotImplementedError.
4682
4683[clinic start generated code]*/
4684
Larry Hastings2f936352014-08-05 14:04:04 +10004685static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004686os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4687 int dir_fd, int follow_symlinks)
4688/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004689{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004690#ifdef MS_WINDOWS
4691 HANDLE hFile;
4692 FILETIME atime, mtime;
4693#else
4694 int result;
4695#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004696
Larry Hastings2f936352014-08-05 14:04:04 +10004697 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004698
Christian Heimesb3c87242013-08-01 00:08:16 +02004699 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004700
Larry Hastings9cf065c2012-06-22 16:30:09 -07004701 if (times && (times != Py_None) && ns) {
4702 PyErr_SetString(PyExc_ValueError,
4703 "utime: you may specify either 'times'"
4704 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004705 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004706 }
4707
4708 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004709 time_t a_sec, m_sec;
4710 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004711 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004712 PyErr_SetString(PyExc_TypeError,
4713 "utime: 'times' must be either"
4714 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004715 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004716 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004717 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004718 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004719 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004720 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004721 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004722 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004723 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004724 utime.atime_s = a_sec;
4725 utime.atime_ns = a_nsec;
4726 utime.mtime_s = m_sec;
4727 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004728 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004729 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004730 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004731 PyErr_SetString(PyExc_TypeError,
4732 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004733 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004734 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004735 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004736 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004737 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004738 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004739 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004740 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004741 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004742 }
4743 else {
4744 /* times and ns are both None/unspecified. use "now". */
4745 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004746 }
4747
Victor Stinner4552ced2015-09-21 22:37:15 +02004748#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004749 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004750 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004751#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004752
Larry Hastings2f936352014-08-05 14:04:04 +10004753 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4754 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4755 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004756 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004757
Larry Hastings9cf065c2012-06-22 16:30:09 -07004758#if !defined(HAVE_UTIMENSAT)
4759 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004760 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004761 "utime: cannot use dir_fd and follow_symlinks "
4762 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004763 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004764 }
4765#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004766
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004767#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004768 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004769 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4770 NULL, OPEN_EXISTING,
4771 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004772 Py_END_ALLOW_THREADS
4773 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004774 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004775 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004776 }
4777
Larry Hastings9cf065c2012-06-22 16:30:09 -07004778 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004779 GetSystemTimeAsFileTime(&mtime);
4780 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004781 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004782 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004783 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4784 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004785 }
4786 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4787 /* Avoid putting the file name into the error here,
4788 as that may confuse the user into believing that
4789 something is wrong with the file, when it also
4790 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004791 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004792 CloseHandle(hFile);
4793 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004794 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004795 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004796#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004798
Victor Stinner4552ced2015-09-21 22:37:15 +02004799#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004800 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004801 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004802 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004803#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004804
Victor Stinner528a9ab2015-09-03 21:30:26 +02004805#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004806 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004807 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004808 else
4809#endif
4810
Victor Stinner528a9ab2015-09-03 21:30:26 +02004811#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004812 if (path->fd != -1)
4813 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004814 else
4815#endif
4816
Larry Hastings2f936352014-08-05 14:04:04 +10004817 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004818
4819 Py_END_ALLOW_THREADS
4820
4821 if (result < 0) {
4822 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004823 posix_error();
4824 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004825 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004826
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004827#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004828
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004829 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004830}
4831
Guido van Rossum3b066191991-06-04 19:40:25 +00004832/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004833
Larry Hastings2f936352014-08-05 14:04:04 +10004834
4835/*[clinic input]
4836os._exit
4837
4838 status: int
4839
4840Exit to the system with specified status, without normal exit processing.
4841[clinic start generated code]*/
4842
Larry Hastings2f936352014-08-05 14:04:04 +10004843static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004844os__exit_impl(PyObject *module, int status)
4845/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004846{
4847 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004848 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004849}
4850
Steve Dowercc16be82016-09-08 10:35:16 -07004851#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4852#define EXECV_CHAR wchar_t
4853#else
4854#define EXECV_CHAR char
4855#endif
4856
Martin v. Löwis114619e2002-10-07 06:44:21 +00004857#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4858static void
Steve Dowercc16be82016-09-08 10:35:16 -07004859free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004860{
Victor Stinner8c62be82010-05-06 00:08:46 +00004861 Py_ssize_t i;
4862 for (i = 0; i < count; i++)
4863 PyMem_Free(array[i]);
4864 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004865}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004866
Berker Peksag81816462016-09-15 20:19:47 +03004867static int
4868fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004869{
Victor Stinner8c62be82010-05-06 00:08:46 +00004870 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004871 PyObject *ub;
4872 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004873#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004874 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004875 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004876 *out = PyUnicode_AsWideCharString(ub, &size);
4877 if (*out)
4878 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004879#else
Berker Peksag81816462016-09-15 20:19:47 +03004880 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004881 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004882 size = PyBytes_GET_SIZE(ub);
4883 *out = PyMem_Malloc(size + 1);
4884 if (*out) {
4885 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4886 result = 1;
4887 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004888 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004889#endif
Berker Peksag81816462016-09-15 20:19:47 +03004890 Py_DECREF(ub);
4891 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004892}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004893#endif
4894
Ross Lagerwall7807c352011-03-17 20:20:30 +02004895#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Steve Dowercc16be82016-09-08 10:35:16 -07004896static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004897parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4898{
Victor Stinner8c62be82010-05-06 00:08:46 +00004899 Py_ssize_t i, pos, envc;
4900 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004901 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004902 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004903
Victor Stinner8c62be82010-05-06 00:08:46 +00004904 i = PyMapping_Size(env);
4905 if (i < 0)
4906 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004907 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 if (envlist == NULL) {
4909 PyErr_NoMemory();
4910 return NULL;
4911 }
4912 envc = 0;
4913 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004914 if (!keys)
4915 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004916 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004917 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004918 goto error;
4919 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4920 PyErr_Format(PyExc_TypeError,
4921 "env.keys() or env.values() is not a list");
4922 goto error;
4923 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004924
Victor Stinner8c62be82010-05-06 00:08:46 +00004925 for (pos = 0; pos < i; pos++) {
4926 key = PyList_GetItem(keys, pos);
4927 val = PyList_GetItem(vals, pos);
4928 if (!key || !val)
4929 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004930
Berker Peksag81816462016-09-15 20:19:47 +03004931#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4932 if (!PyUnicode_FSDecoder(key, &key2))
4933 goto error;
4934 if (!PyUnicode_FSDecoder(val, &val2)) {
4935 Py_DECREF(key2);
4936 goto error;
4937 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004938 /* Search from index 1 because on Windows starting '=' is allowed for
4939 defining hidden environment variables. */
4940 if (PyUnicode_GET_LENGTH(key2) == 0 ||
4941 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
4942 {
4943 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004944 Py_DECREF(key2);
4945 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004946 goto error;
4947 }
Berker Peksag81816462016-09-15 20:19:47 +03004948 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
4949#else
4950 if (!PyUnicode_FSConverter(key, &key2))
4951 goto error;
4952 if (!PyUnicode_FSConverter(val, &val2)) {
4953 Py_DECREF(key2);
4954 goto error;
4955 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004956 if (PyBytes_GET_SIZE(key2) == 0 ||
4957 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
4958 {
4959 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004960 Py_DECREF(key2);
4961 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004962 goto error;
4963 }
Berker Peksag81816462016-09-15 20:19:47 +03004964 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
4965 PyBytes_AS_STRING(val2));
4966#endif
4967 Py_DECREF(key2);
4968 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07004969 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00004970 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07004971
4972 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
4973 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 goto error;
4975 }
Berker Peksag81816462016-09-15 20:19:47 +03004976
Steve Dowercc16be82016-09-08 10:35:16 -07004977 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004978 }
4979 Py_DECREF(vals);
4980 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004981
Victor Stinner8c62be82010-05-06 00:08:46 +00004982 envlist[envc] = 0;
4983 *envc_ptr = envc;
4984 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004985
4986error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004987 Py_XDECREF(keys);
4988 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07004989 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004990 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004991}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004992
Steve Dowercc16be82016-09-08 10:35:16 -07004993static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02004994parse_arglist(PyObject* argv, Py_ssize_t *argc)
4995{
4996 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07004997 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004998 if (argvlist == NULL) {
4999 PyErr_NoMemory();
5000 return NULL;
5001 }
5002 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005003 PyObject* item = PySequence_ITEM(argv, i);
5004 if (item == NULL)
5005 goto fail;
5006 if (!fsconvert_strdup(item, &argvlist[i])) {
5007 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005008 goto fail;
5009 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005010 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005011 }
5012 argvlist[*argc] = NULL;
5013 return argvlist;
5014fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005015 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005016 free_string_array(argvlist, *argc);
5017 return NULL;
5018}
Steve Dowercc16be82016-09-08 10:35:16 -07005019
Ross Lagerwall7807c352011-03-17 20:20:30 +02005020#endif
5021
Larry Hastings2f936352014-08-05 14:04:04 +10005022
Ross Lagerwall7807c352011-03-17 20:20:30 +02005023#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005024/*[clinic input]
5025os.execv
5026
Steve Dowercc16be82016-09-08 10:35:16 -07005027 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005028 Path of executable file.
5029 argv: object
5030 Tuple or list of strings.
5031 /
5032
5033Execute an executable path with arguments, replacing current process.
5034[clinic start generated code]*/
5035
Larry Hastings2f936352014-08-05 14:04:04 +10005036static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005037os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5038/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005039{
Steve Dowercc16be82016-09-08 10:35:16 -07005040 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005041 Py_ssize_t argc;
5042
5043 /* execv has two arguments: (path, argv), where
5044 argv is a list or tuple of strings. */
5045
Ross Lagerwall7807c352011-03-17 20:20:30 +02005046 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5047 PyErr_SetString(PyExc_TypeError,
5048 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005049 return NULL;
5050 }
5051 argc = PySequence_Size(argv);
5052 if (argc < 1) {
5053 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005054 return NULL;
5055 }
5056
5057 argvlist = parse_arglist(argv, &argc);
5058 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005059 return NULL;
5060 }
Steve Dowerbce26262016-11-19 19:17:26 -08005061 if (!argvlist[0][0]) {
5062 PyErr_SetString(PyExc_ValueError,
5063 "execv() arg 2 first element cannot be empty");
5064 free_string_array(argvlist, argc);
5065 return NULL;
5066 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005067
Steve Dowerbce26262016-11-19 19:17:26 -08005068 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005069#ifdef HAVE_WEXECV
5070 _wexecv(path->wide, argvlist);
5071#else
5072 execv(path->narrow, argvlist);
5073#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005074 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005075
5076 /* If we get here it's definitely an error */
5077
5078 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005079 return posix_error();
5080}
5081
Larry Hastings2f936352014-08-05 14:04:04 +10005082
5083/*[clinic input]
5084os.execve
5085
5086 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5087 Path of executable file.
5088 argv: object
5089 Tuple or list of strings.
5090 env: object
5091 Dictionary of strings mapping to strings.
5092
5093Execute an executable path with arguments, replacing current process.
5094[clinic start generated code]*/
5095
Larry Hastings2f936352014-08-05 14:04:04 +10005096static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005097os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5098/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005099{
Steve Dowercc16be82016-09-08 10:35:16 -07005100 EXECV_CHAR **argvlist = NULL;
5101 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005102 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005103
Victor Stinner8c62be82010-05-06 00:08:46 +00005104 /* execve has three arguments: (path, argv, env), where
5105 argv is a list or tuple of strings and env is a dictionary
5106 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005107
Ross Lagerwall7807c352011-03-17 20:20:30 +02005108 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005109 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005110 "execve: argv must be a tuple or list");
5111 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005112 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005113 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005114 if (argc < 1) {
5115 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5116 return NULL;
5117 }
5118
Victor Stinner8c62be82010-05-06 00:08:46 +00005119 if (!PyMapping_Check(env)) {
5120 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005121 "execve: environment must be a mapping object");
5122 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005123 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005124
Ross Lagerwall7807c352011-03-17 20:20:30 +02005125 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005126 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005127 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005128 }
Steve Dowerbce26262016-11-19 19:17:26 -08005129 if (!argvlist[0][0]) {
5130 PyErr_SetString(PyExc_ValueError,
5131 "execve: argv first element cannot be empty");
5132 goto fail;
5133 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005134
Victor Stinner8c62be82010-05-06 00:08:46 +00005135 envlist = parse_envlist(env, &envc);
5136 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005137 goto fail;
5138
Steve Dowerbce26262016-11-19 19:17:26 -08005139 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005140#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005141 if (path->fd > -1)
5142 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005143 else
5144#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005145#ifdef HAVE_WEXECV
5146 _wexecve(path->wide, argvlist, envlist);
5147#else
Larry Hastings2f936352014-08-05 14:04:04 +10005148 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005149#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005150 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005151
5152 /* If we get here it's definitely an error */
5153
Alexey Izbyshev83460312018-10-20 03:28:22 +03005154 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005155
Steve Dowercc16be82016-09-08 10:35:16 -07005156 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005157 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005158 if (argvlist)
5159 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005160 return NULL;
5161}
Steve Dowercc16be82016-09-08 10:35:16 -07005162
Larry Hastings9cf065c2012-06-22 16:30:09 -07005163#endif /* HAVE_EXECV */
5164
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005165#ifdef HAVE_POSIX_SPAWN
5166
5167enum posix_spawn_file_actions_identifier {
5168 POSIX_SPAWN_OPEN,
5169 POSIX_SPAWN_CLOSE,
5170 POSIX_SPAWN_DUP2
5171};
5172
William Orr81574b82018-10-01 22:19:56 -07005173#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005174static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005175convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005176#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005177
5178static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005179parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5180 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005181 PyObject *setsigdef, PyObject *scheduler,
5182 posix_spawnattr_t *attrp)
5183{
5184 long all_flags = 0;
5185
5186 errno = posix_spawnattr_init(attrp);
5187 if (errno) {
5188 posix_error();
5189 return -1;
5190 }
5191
5192 if (setpgroup) {
5193 pid_t pgid = PyLong_AsPid(setpgroup);
5194 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5195 goto fail;
5196 }
5197 errno = posix_spawnattr_setpgroup(attrp, pgid);
5198 if (errno) {
5199 posix_error();
5200 goto fail;
5201 }
5202 all_flags |= POSIX_SPAWN_SETPGROUP;
5203 }
5204
5205 if (resetids) {
5206 all_flags |= POSIX_SPAWN_RESETIDS;
5207 }
5208
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005209 if (setsid) {
5210#ifdef POSIX_SPAWN_SETSID
5211 all_flags |= POSIX_SPAWN_SETSID;
5212#elif defined(POSIX_SPAWN_SETSID_NP)
5213 all_flags |= POSIX_SPAWN_SETSID_NP;
5214#else
5215 argument_unavailable_error(func_name, "setsid");
5216 return -1;
5217#endif
5218 }
5219
Pablo Galindo254a4662018-09-07 16:44:24 +01005220 if (setsigmask) {
5221 sigset_t set;
5222 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5223 goto fail;
5224 }
5225 errno = posix_spawnattr_setsigmask(attrp, &set);
5226 if (errno) {
5227 posix_error();
5228 goto fail;
5229 }
5230 all_flags |= POSIX_SPAWN_SETSIGMASK;
5231 }
5232
5233 if (setsigdef) {
5234 sigset_t set;
5235 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5236 goto fail;
5237 }
5238 errno = posix_spawnattr_setsigdefault(attrp, &set);
5239 if (errno) {
5240 posix_error();
5241 goto fail;
5242 }
5243 all_flags |= POSIX_SPAWN_SETSIGDEF;
5244 }
5245
5246 if (scheduler) {
5247#ifdef POSIX_SPAWN_SETSCHEDULER
5248 PyObject *py_schedpolicy;
5249 struct sched_param schedparam;
5250
5251 if (!PyArg_ParseTuple(scheduler, "OO&"
5252 ";A scheduler tuple must have two elements",
5253 &py_schedpolicy, convert_sched_param, &schedparam)) {
5254 goto fail;
5255 }
5256 if (py_schedpolicy != Py_None) {
5257 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5258
5259 if (schedpolicy == -1 && PyErr_Occurred()) {
5260 goto fail;
5261 }
5262 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5263 if (errno) {
5264 posix_error();
5265 goto fail;
5266 }
5267 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5268 }
5269 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5270 if (errno) {
5271 posix_error();
5272 goto fail;
5273 }
5274 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5275#else
5276 PyErr_SetString(PyExc_NotImplementedError,
5277 "The scheduler option is not supported in this system.");
5278 goto fail;
5279#endif
5280 }
5281
5282 errno = posix_spawnattr_setflags(attrp, all_flags);
5283 if (errno) {
5284 posix_error();
5285 goto fail;
5286 }
5287
5288 return 0;
5289
5290fail:
5291 (void)posix_spawnattr_destroy(attrp);
5292 return -1;
5293}
5294
5295static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005296parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005297 posix_spawn_file_actions_t *file_actionsp,
5298 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005299{
5300 PyObject *seq;
5301 PyObject *file_action = NULL;
5302 PyObject *tag_obj;
5303
5304 seq = PySequence_Fast(file_actions,
5305 "file_actions must be a sequence or None");
5306 if (seq == NULL) {
5307 return -1;
5308 }
5309
5310 errno = posix_spawn_file_actions_init(file_actionsp);
5311 if (errno) {
5312 posix_error();
5313 Py_DECREF(seq);
5314 return -1;
5315 }
5316
5317 for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
5318 file_action = PySequence_Fast_GET_ITEM(seq, i);
5319 Py_INCREF(file_action);
5320 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5321 PyErr_SetString(PyExc_TypeError,
5322 "Each file_actions element must be a non-empty tuple");
5323 goto fail;
5324 }
5325 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5326 if (tag == -1 && PyErr_Occurred()) {
5327 goto fail;
5328 }
5329
5330 /* Populate the file_actions object */
5331 switch (tag) {
5332 case POSIX_SPAWN_OPEN: {
5333 int fd, oflag;
5334 PyObject *path;
5335 unsigned long mode;
5336 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5337 ";A open file_action tuple must have 5 elements",
5338 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5339 &oflag, &mode))
5340 {
5341 goto fail;
5342 }
Pablo Galindocb970732018-06-19 09:19:50 +01005343 if (PyList_Append(temp_buffer, path)) {
5344 Py_DECREF(path);
5345 goto fail;
5346 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005347 errno = posix_spawn_file_actions_addopen(file_actionsp,
5348 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005349 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005350 if (errno) {
5351 posix_error();
5352 goto fail;
5353 }
5354 break;
5355 }
5356 case POSIX_SPAWN_CLOSE: {
5357 int fd;
5358 if (!PyArg_ParseTuple(file_action, "Oi"
5359 ";A close file_action tuple must have 2 elements",
5360 &tag_obj, &fd))
5361 {
5362 goto fail;
5363 }
5364 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5365 if (errno) {
5366 posix_error();
5367 goto fail;
5368 }
5369 break;
5370 }
5371 case POSIX_SPAWN_DUP2: {
5372 int fd1, fd2;
5373 if (!PyArg_ParseTuple(file_action, "Oii"
5374 ";A dup2 file_action tuple must have 3 elements",
5375 &tag_obj, &fd1, &fd2))
5376 {
5377 goto fail;
5378 }
5379 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5380 fd1, fd2);
5381 if (errno) {
5382 posix_error();
5383 goto fail;
5384 }
5385 break;
5386 }
5387 default: {
5388 PyErr_SetString(PyExc_TypeError,
5389 "Unknown file_actions identifier");
5390 goto fail;
5391 }
5392 }
5393 Py_DECREF(file_action);
5394 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005395
Serhiy Storchakaef347532018-05-01 16:45:04 +03005396 Py_DECREF(seq);
5397 return 0;
5398
5399fail:
5400 Py_DECREF(seq);
5401 Py_DECREF(file_action);
5402 (void)posix_spawn_file_actions_destroy(file_actionsp);
5403 return -1;
5404}
5405
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005406
5407static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005408py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5409 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005410 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005411 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005412{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005413 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005414 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005415 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005416 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005417 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005418 posix_spawnattr_t attr;
5419 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005420 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005421 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005422 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005423 pid_t pid;
5424 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005425
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005426 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005427 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005428 like posix.environ. */
5429
Serhiy Storchakaef347532018-05-01 16:45:04 +03005430 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005431 PyErr_Format(PyExc_TypeError,
5432 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005433 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005434 }
5435 argc = PySequence_Size(argv);
5436 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005437 PyErr_Format(PyExc_ValueError,
5438 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005439 return NULL;
5440 }
5441
5442 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005443 PyErr_Format(PyExc_TypeError,
5444 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005445 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005446 }
5447
5448 argvlist = parse_arglist(argv, &argc);
5449 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005450 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005451 }
5452 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005453 PyErr_Format(PyExc_ValueError,
5454 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005455 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005456 }
5457
5458 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005459 if (envlist == NULL) {
5460 goto exit;
5461 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005462
Serhiy Storchakad700f972018-09-08 14:48:18 +03005463 if (file_actions != NULL) {
Pablo Galindocb970732018-06-19 09:19:50 +01005464 /* There is a bug in old versions of glibc that makes some of the
5465 * helper functions for manipulating file actions not copy the provided
5466 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5467 * copy the value of path for some old versions of glibc (<2.20).
5468 * The use of temp_buffer here is a workaround that keeps the
5469 * python objects that own the buffers alive until posix_spawn gets called.
5470 * Check https://bugs.python.org/issue33630 and
5471 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5472 temp_buffer = PyList_New(0);
5473 if (!temp_buffer) {
5474 goto exit;
5475 }
5476 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005477 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005478 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005479 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005480 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005481
Victor Stinner325e4ba2019-02-01 15:47:24 +01005482 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5483 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005484 goto exit;
5485 }
5486 attrp = &attr;
5487
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005488 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005489#ifdef HAVE_POSIX_SPAWNP
5490 if (use_posix_spawnp) {
5491 err_code = posix_spawnp(&pid, path->narrow,
5492 file_actionsp, attrp, argvlist, envlist);
5493 }
5494 else
5495#endif /* HAVE_POSIX_SPAWNP */
5496 {
5497 err_code = posix_spawn(&pid, path->narrow,
5498 file_actionsp, attrp, argvlist, envlist);
5499 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005500 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005501
Serhiy Storchakaef347532018-05-01 16:45:04 +03005502 if (err_code) {
5503 errno = err_code;
5504 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005505 goto exit;
5506 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005507#ifdef _Py_MEMORY_SANITIZER
5508 __msan_unpoison(&pid, sizeof(pid));
5509#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005510 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005511
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005512exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005513 if (file_actionsp) {
5514 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005515 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005516 if (attrp) {
5517 (void)posix_spawnattr_destroy(attrp);
5518 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005519 if (envlist) {
5520 free_string_array(envlist, envc);
5521 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005522 if (argvlist) {
5523 free_string_array(argvlist, argc);
5524 }
Pablo Galindocb970732018-06-19 09:19:50 +01005525 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005526 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005527}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005528
5529
5530/*[clinic input]
5531
5532os.posix_spawn
5533 path: path_t
5534 Path of executable file.
5535 argv: object
5536 Tuple or list of strings.
5537 env: object
5538 Dictionary of strings mapping to strings.
5539 /
5540 *
5541 file_actions: object(c_default='NULL') = ()
5542 A sequence of file action tuples.
5543 setpgroup: object = NULL
5544 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5545 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005546 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5547 setsid: bool(accept={int}) = False
5548 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005549 setsigmask: object(c_default='NULL') = ()
5550 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5551 setsigdef: object(c_default='NULL') = ()
5552 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5553 scheduler: object = NULL
5554 A tuple with the scheduler policy (optional) and parameters.
5555
5556Execute the program specified by path in a new process.
5557[clinic start generated code]*/
5558
5559static PyObject *
5560os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5561 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005562 PyObject *setpgroup, int resetids, int setsid,
5563 PyObject *setsigmask, PyObject *setsigdef,
5564 PyObject *scheduler)
5565/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005566{
5567 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005568 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005569 scheduler);
5570}
5571 #endif /* HAVE_POSIX_SPAWN */
5572
5573
5574
5575#ifdef HAVE_POSIX_SPAWNP
5576/*[clinic input]
5577
5578os.posix_spawnp
5579 path: path_t
5580 Path of executable file.
5581 argv: object
5582 Tuple or list of strings.
5583 env: object
5584 Dictionary of strings mapping to strings.
5585 /
5586 *
5587 file_actions: object(c_default='NULL') = ()
5588 A sequence of file action tuples.
5589 setpgroup: object = NULL
5590 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5591 resetids: bool(accept={int}) = False
5592 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005593 setsid: bool(accept={int}) = False
5594 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005595 setsigmask: object(c_default='NULL') = ()
5596 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5597 setsigdef: object(c_default='NULL') = ()
5598 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5599 scheduler: object = NULL
5600 A tuple with the scheduler policy (optional) and parameters.
5601
5602Execute the program specified by path in a new process.
5603[clinic start generated code]*/
5604
5605static PyObject *
5606os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5607 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005608 PyObject *setpgroup, int resetids, int setsid,
5609 PyObject *setsigmask, PyObject *setsigdef,
5610 PyObject *scheduler)
5611/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005612{
5613 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005614 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005615 scheduler);
5616}
5617#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005618
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005619
Steve Dowercc16be82016-09-08 10:35:16 -07005620#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV)
Larry Hastings2f936352014-08-05 14:04:04 +10005621/*[clinic input]
5622os.spawnv
5623
5624 mode: int
5625 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005626 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005627 Path of executable file.
5628 argv: object
5629 Tuple or list of strings.
5630 /
5631
5632Execute the program specified by path in a new process.
5633[clinic start generated code]*/
5634
Larry Hastings2f936352014-08-05 14:04:04 +10005635static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005636os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5637/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005638{
Steve Dowercc16be82016-09-08 10:35:16 -07005639 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005640 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005641 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005642 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005643 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005644
Victor Stinner8c62be82010-05-06 00:08:46 +00005645 /* spawnv has three arguments: (mode, path, argv), where
5646 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005647
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 if (PyList_Check(argv)) {
5649 argc = PyList_Size(argv);
5650 getitem = PyList_GetItem;
5651 }
5652 else if (PyTuple_Check(argv)) {
5653 argc = PyTuple_Size(argv);
5654 getitem = PyTuple_GetItem;
5655 }
5656 else {
5657 PyErr_SetString(PyExc_TypeError,
5658 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005659 return NULL;
5660 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005661 if (argc == 0) {
5662 PyErr_SetString(PyExc_ValueError,
5663 "spawnv() arg 2 cannot be empty");
5664 return NULL;
5665 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005666
Steve Dowercc16be82016-09-08 10:35:16 -07005667 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005668 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005669 return PyErr_NoMemory();
5670 }
5671 for (i = 0; i < argc; i++) {
5672 if (!fsconvert_strdup((*getitem)(argv, i),
5673 &argvlist[i])) {
5674 free_string_array(argvlist, i);
5675 PyErr_SetString(
5676 PyExc_TypeError,
5677 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 return NULL;
5679 }
Steve Dower93ff8722016-11-19 19:03:54 -08005680 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005681 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005682 PyErr_SetString(
5683 PyExc_ValueError,
5684 "spawnv() arg 2 first element cannot be empty");
5685 return NULL;
5686 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005687 }
5688 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005689
Victor Stinner8c62be82010-05-06 00:08:46 +00005690 if (mode == _OLD_P_OVERLAY)
5691 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005692
Victor Stinner8c62be82010-05-06 00:08:46 +00005693 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005694 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005695#ifdef HAVE_WSPAWNV
5696 spawnval = _wspawnv(mode, path->wide, argvlist);
5697#else
5698 spawnval = _spawnv(mode, path->narrow, argvlist);
5699#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005700 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005701 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005702
Victor Stinner8c62be82010-05-06 00:08:46 +00005703 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005704
Victor Stinner8c62be82010-05-06 00:08:46 +00005705 if (spawnval == -1)
5706 return posix_error();
5707 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005708 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005709}
5710
Larry Hastings2f936352014-08-05 14:04:04 +10005711/*[clinic input]
5712os.spawnve
5713
5714 mode: int
5715 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005716 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005717 Path of executable file.
5718 argv: object
5719 Tuple or list of strings.
5720 env: object
5721 Dictionary of strings mapping to strings.
5722 /
5723
5724Execute the program specified by path in a new process.
5725[clinic start generated code]*/
5726
Larry Hastings2f936352014-08-05 14:04:04 +10005727static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005728os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005729 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005730/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005731{
Steve Dowercc16be82016-09-08 10:35:16 -07005732 EXECV_CHAR **argvlist;
5733 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005735 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005736 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005737 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005738 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005739
Victor Stinner8c62be82010-05-06 00:08:46 +00005740 /* spawnve has four arguments: (mode, path, argv, env), where
5741 argv is a list or tuple of strings and env is a dictionary
5742 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005743
Victor Stinner8c62be82010-05-06 00:08:46 +00005744 if (PyList_Check(argv)) {
5745 argc = PyList_Size(argv);
5746 getitem = PyList_GetItem;
5747 }
5748 else if (PyTuple_Check(argv)) {
5749 argc = PyTuple_Size(argv);
5750 getitem = PyTuple_GetItem;
5751 }
5752 else {
5753 PyErr_SetString(PyExc_TypeError,
5754 "spawnve() arg 2 must be a tuple or list");
5755 goto fail_0;
5756 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005757 if (argc == 0) {
5758 PyErr_SetString(PyExc_ValueError,
5759 "spawnve() arg 2 cannot be empty");
5760 goto fail_0;
5761 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005762 if (!PyMapping_Check(env)) {
5763 PyErr_SetString(PyExc_TypeError,
5764 "spawnve() arg 3 must be a mapping object");
5765 goto fail_0;
5766 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005767
Steve Dowercc16be82016-09-08 10:35:16 -07005768 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005769 if (argvlist == NULL) {
5770 PyErr_NoMemory();
5771 goto fail_0;
5772 }
5773 for (i = 0; i < argc; i++) {
5774 if (!fsconvert_strdup((*getitem)(argv, i),
5775 &argvlist[i]))
5776 {
5777 lastarg = i;
5778 goto fail_1;
5779 }
Steve Dowerbce26262016-11-19 19:17:26 -08005780 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005781 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005782 PyErr_SetString(
5783 PyExc_ValueError,
5784 "spawnv() arg 2 first element cannot be empty");
5785 goto fail_1;
5786 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005787 }
5788 lastarg = argc;
5789 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005790
Victor Stinner8c62be82010-05-06 00:08:46 +00005791 envlist = parse_envlist(env, &envc);
5792 if (envlist == NULL)
5793 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005794
Victor Stinner8c62be82010-05-06 00:08:46 +00005795 if (mode == _OLD_P_OVERLAY)
5796 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005797
Victor Stinner8c62be82010-05-06 00:08:46 +00005798 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005799 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005800#ifdef HAVE_WSPAWNV
5801 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
5802#else
5803 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5804#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005805 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005806 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005807
Victor Stinner8c62be82010-05-06 00:08:46 +00005808 if (spawnval == -1)
5809 (void) posix_error();
5810 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005811 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005812
Victor Stinner8c62be82010-05-06 00:08:46 +00005813 while (--envc >= 0)
5814 PyMem_DEL(envlist[envc]);
5815 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005816 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005817 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005818 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005819 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005820}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005821
Guido van Rossuma1065681999-01-25 23:20:23 +00005822#endif /* HAVE_SPAWNV */
5823
5824
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005825#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005826
5827/* Helper function to validate arguments.
5828 Returns 0 on success. non-zero on failure with a TypeError raised.
5829 If obj is non-NULL it must be callable. */
5830static int
5831check_null_or_callable(PyObject *obj, const char* obj_name)
5832{
5833 if (obj && !PyCallable_Check(obj)) {
5834 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5835 obj_name, Py_TYPE(obj)->tp_name);
5836 return -1;
5837 }
5838 return 0;
5839}
5840
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005841/*[clinic input]
5842os.register_at_fork
5843
Gregory P. Smith163468a2017-05-29 10:03:41 -07005844 *
5845 before: object=NULL
5846 A callable to be called in the parent before the fork() syscall.
5847 after_in_child: object=NULL
5848 A callable to be called in the child after fork().
5849 after_in_parent: object=NULL
5850 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005851
Gregory P. Smith163468a2017-05-29 10:03:41 -07005852Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005853
Gregory P. Smith163468a2017-05-29 10:03:41 -07005854'before' callbacks are called in reverse order.
5855'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005856
5857[clinic start generated code]*/
5858
5859static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005860os_register_at_fork_impl(PyObject *module, PyObject *before,
5861 PyObject *after_in_child, PyObject *after_in_parent)
5862/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005863{
5864 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005865
Gregory P. Smith163468a2017-05-29 10:03:41 -07005866 if (!before && !after_in_child && !after_in_parent) {
5867 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5868 return NULL;
5869 }
5870 if (check_null_or_callable(before, "before") ||
5871 check_null_or_callable(after_in_child, "after_in_child") ||
5872 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005873 return NULL;
5874 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02005875 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005876
Gregory P. Smith163468a2017-05-29 10:03:41 -07005877 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005878 return NULL;
5879 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005880 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005881 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005882 }
5883 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5884 return NULL;
5885 }
5886 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005887}
5888#endif /* HAVE_FORK */
5889
5890
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005891#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005892/*[clinic input]
5893os.fork1
5894
5895Fork a child process with a single multiplexed (i.e., not bound) thread.
5896
5897Return 0 to child process and PID of child to parent process.
5898[clinic start generated code]*/
5899
Larry Hastings2f936352014-08-05 14:04:04 +10005900static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005901os_fork1_impl(PyObject *module)
5902/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005903{
Victor Stinner8c62be82010-05-06 00:08:46 +00005904 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005905
Eric Snow59032962018-09-14 14:17:20 -07005906 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
5907 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
5908 return NULL;
5909 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005910 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005911 pid = fork1();
5912 if (pid == 0) {
5913 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005914 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005915 } else {
5916 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005917 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005918 }
5919 if (pid == -1)
5920 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005921 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005922}
Larry Hastings2f936352014-08-05 14:04:04 +10005923#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005924
5925
Guido van Rossumad0ee831995-03-01 10:34:45 +00005926#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005927/*[clinic input]
5928os.fork
5929
5930Fork a child process.
5931
5932Return 0 to child process and PID of child to parent process.
5933[clinic start generated code]*/
5934
Larry Hastings2f936352014-08-05 14:04:04 +10005935static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005936os_fork_impl(PyObject *module)
5937/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005938{
Victor Stinner8c62be82010-05-06 00:08:46 +00005939 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005940
Eric Snow59032962018-09-14 14:17:20 -07005941 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
5942 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
5943 return NULL;
5944 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005945 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 pid = fork();
5947 if (pid == 0) {
5948 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005949 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005950 } else {
5951 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005952 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 }
5954 if (pid == -1)
5955 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005956 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005957}
Larry Hastings2f936352014-08-05 14:04:04 +10005958#endif /* HAVE_FORK */
5959
Guido van Rossum85e3b011991-06-03 12:42:10 +00005960
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005961#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005962#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005963/*[clinic input]
5964os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005965
Larry Hastings2f936352014-08-05 14:04:04 +10005966 policy: int
5967
5968Get the maximum scheduling priority for policy.
5969[clinic start generated code]*/
5970
Larry Hastings2f936352014-08-05 14:04:04 +10005971static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005972os_sched_get_priority_max_impl(PyObject *module, int policy)
5973/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005974{
5975 int max;
5976
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005977 max = sched_get_priority_max(policy);
5978 if (max < 0)
5979 return posix_error();
5980 return PyLong_FromLong(max);
5981}
5982
Larry Hastings2f936352014-08-05 14:04:04 +10005983
5984/*[clinic input]
5985os.sched_get_priority_min
5986
5987 policy: int
5988
5989Get the minimum scheduling priority for policy.
5990[clinic start generated code]*/
5991
Larry Hastings2f936352014-08-05 14:04:04 +10005992static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005993os_sched_get_priority_min_impl(PyObject *module, int policy)
5994/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005995{
5996 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005997 if (min < 0)
5998 return posix_error();
5999 return PyLong_FromLong(min);
6000}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006001#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6002
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006003
Larry Hastings2f936352014-08-05 14:04:04 +10006004#ifdef HAVE_SCHED_SETSCHEDULER
6005/*[clinic input]
6006os.sched_getscheduler
6007 pid: pid_t
6008 /
6009
6010Get the scheduling policy for the process identifiedy by pid.
6011
6012Passing 0 for pid returns the scheduling policy for the calling process.
6013[clinic start generated code]*/
6014
Larry Hastings2f936352014-08-05 14:04:04 +10006015static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006016os_sched_getscheduler_impl(PyObject *module, pid_t pid)
6017/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006018{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006019 int policy;
6020
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006021 policy = sched_getscheduler(pid);
6022 if (policy < 0)
6023 return posix_error();
6024 return PyLong_FromLong(policy);
6025}
Larry Hastings2f936352014-08-05 14:04:04 +10006026#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006027
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006028
William Orr81574b82018-10-01 22:19:56 -07006029#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006030/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006031class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006032
6033@classmethod
6034os.sched_param.__new__
6035
6036 sched_priority: object
6037 A scheduling parameter.
6038
6039Current has only one field: sched_priority");
6040[clinic start generated code]*/
6041
Larry Hastings2f936352014-08-05 14:04:04 +10006042static PyObject *
6043os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006044/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006045{
6046 PyObject *res;
6047
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006048 res = PyStructSequence_New(type);
6049 if (!res)
6050 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006051 Py_INCREF(sched_priority);
6052 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006053 return res;
6054}
6055
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006056
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006057PyDoc_VAR(os_sched_param__doc__);
6058
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006059static PyStructSequence_Field sched_param_fields[] = {
6060 {"sched_priority", "the scheduling priority"},
6061 {0}
6062};
6063
6064static PyStructSequence_Desc sched_param_desc = {
6065 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006066 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006067 sched_param_fields,
6068 1
6069};
6070
6071static int
6072convert_sched_param(PyObject *param, struct sched_param *res)
6073{
6074 long priority;
6075
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006076 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006077 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6078 return 0;
6079 }
6080 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6081 if (priority == -1 && PyErr_Occurred())
6082 return 0;
6083 if (priority > INT_MAX || priority < INT_MIN) {
6084 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6085 return 0;
6086 }
6087 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6088 return 1;
6089}
William Orr81574b82018-10-01 22:19:56 -07006090#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006091
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006092
6093#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006094/*[clinic input]
6095os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006096
Larry Hastings2f936352014-08-05 14:04:04 +10006097 pid: pid_t
6098 policy: int
6099 param: sched_param
6100 /
6101
6102Set the scheduling policy for the process identified by pid.
6103
6104If pid is 0, the calling process is changed.
6105param is an instance of sched_param.
6106[clinic start generated code]*/
6107
Larry Hastings2f936352014-08-05 14:04:04 +10006108static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006109os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006110 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006111/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006112{
Jesus Cea9c822272011-09-10 01:40:52 +02006113 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006114 ** sched_setscheduler() returns 0 in Linux, but the previous
6115 ** scheduling policy under Solaris/Illumos, and others.
6116 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006117 */
Larry Hastings2f936352014-08-05 14:04:04 +10006118 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006119 return posix_error();
6120 Py_RETURN_NONE;
6121}
Larry Hastings2f936352014-08-05 14:04:04 +10006122#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006123
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006124
6125#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006126/*[clinic input]
6127os.sched_getparam
6128 pid: pid_t
6129 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006130
Larry Hastings2f936352014-08-05 14:04:04 +10006131Returns scheduling parameters for the process identified by pid.
6132
6133If pid is 0, returns parameters for the calling process.
6134Return value is an instance of sched_param.
6135[clinic start generated code]*/
6136
Larry Hastings2f936352014-08-05 14:04:04 +10006137static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006138os_sched_getparam_impl(PyObject *module, pid_t pid)
6139/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006140{
6141 struct sched_param param;
6142 PyObject *result;
6143 PyObject *priority;
6144
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006145 if (sched_getparam(pid, &param))
6146 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006147 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006148 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006149 return NULL;
6150 priority = PyLong_FromLong(param.sched_priority);
6151 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006152 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006153 return NULL;
6154 }
Larry Hastings2f936352014-08-05 14:04:04 +10006155 PyStructSequence_SET_ITEM(result, 0, priority);
6156 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006157}
6158
Larry Hastings2f936352014-08-05 14:04:04 +10006159
6160/*[clinic input]
6161os.sched_setparam
6162 pid: pid_t
6163 param: sched_param
6164 /
6165
6166Set scheduling parameters for the process identified by pid.
6167
6168If pid is 0, sets parameters for the calling process.
6169param should be an instance of sched_param.
6170[clinic start generated code]*/
6171
Larry Hastings2f936352014-08-05 14:04:04 +10006172static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006173os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006174 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006175/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006176{
6177 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006178 return posix_error();
6179 Py_RETURN_NONE;
6180}
Larry Hastings2f936352014-08-05 14:04:04 +10006181#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006182
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006183
6184#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006185/*[clinic input]
6186os.sched_rr_get_interval -> double
6187 pid: pid_t
6188 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006189
Larry Hastings2f936352014-08-05 14:04:04 +10006190Return the round-robin quantum for the process identified by pid, in seconds.
6191
6192Value returned is a float.
6193[clinic start generated code]*/
6194
Larry Hastings2f936352014-08-05 14:04:04 +10006195static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006196os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6197/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006198{
6199 struct timespec interval;
6200 if (sched_rr_get_interval(pid, &interval)) {
6201 posix_error();
6202 return -1.0;
6203 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006204#ifdef _Py_MEMORY_SANITIZER
6205 __msan_unpoison(&interval, sizeof(interval));
6206#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006207 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6208}
6209#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006210
Larry Hastings2f936352014-08-05 14:04:04 +10006211
6212/*[clinic input]
6213os.sched_yield
6214
6215Voluntarily relinquish the CPU.
6216[clinic start generated code]*/
6217
Larry Hastings2f936352014-08-05 14:04:04 +10006218static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006219os_sched_yield_impl(PyObject *module)
6220/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006221{
6222 if (sched_yield())
6223 return posix_error();
6224 Py_RETURN_NONE;
6225}
6226
Benjamin Peterson2740af82011-08-02 17:41:34 -05006227#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006228/* The minimum number of CPUs allocated in a cpu_set_t */
6229static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006230
Larry Hastings2f936352014-08-05 14:04:04 +10006231/*[clinic input]
6232os.sched_setaffinity
6233 pid: pid_t
6234 mask : object
6235 /
6236
6237Set the CPU affinity of the process identified by pid to mask.
6238
6239mask should be an iterable of integers identifying CPUs.
6240[clinic start generated code]*/
6241
Larry Hastings2f936352014-08-05 14:04:04 +10006242static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006243os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6244/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006245{
Antoine Pitrou84869872012-08-04 16:16:35 +02006246 int ncpus;
6247 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006248 cpu_set_t *cpu_set = NULL;
6249 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006250
Larry Hastings2f936352014-08-05 14:04:04 +10006251 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006252 if (iterator == NULL)
6253 return NULL;
6254
6255 ncpus = NCPUS_START;
6256 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006257 cpu_set = CPU_ALLOC(ncpus);
6258 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006259 PyErr_NoMemory();
6260 goto error;
6261 }
Larry Hastings2f936352014-08-05 14:04:04 +10006262 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006263
6264 while ((item = PyIter_Next(iterator))) {
6265 long cpu;
6266 if (!PyLong_Check(item)) {
6267 PyErr_Format(PyExc_TypeError,
6268 "expected an iterator of ints, "
6269 "but iterator yielded %R",
6270 Py_TYPE(item));
6271 Py_DECREF(item);
6272 goto error;
6273 }
6274 cpu = PyLong_AsLong(item);
6275 Py_DECREF(item);
6276 if (cpu < 0) {
6277 if (!PyErr_Occurred())
6278 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6279 goto error;
6280 }
6281 if (cpu > INT_MAX - 1) {
6282 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6283 goto error;
6284 }
6285 if (cpu >= ncpus) {
6286 /* Grow CPU mask to fit the CPU number */
6287 int newncpus = ncpus;
6288 cpu_set_t *newmask;
6289 size_t newsetsize;
6290 while (newncpus <= cpu) {
6291 if (newncpus > INT_MAX / 2)
6292 newncpus = cpu + 1;
6293 else
6294 newncpus = newncpus * 2;
6295 }
6296 newmask = CPU_ALLOC(newncpus);
6297 if (newmask == NULL) {
6298 PyErr_NoMemory();
6299 goto error;
6300 }
6301 newsetsize = CPU_ALLOC_SIZE(newncpus);
6302 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006303 memcpy(newmask, cpu_set, setsize);
6304 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006305 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006306 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006307 ncpus = newncpus;
6308 }
Larry Hastings2f936352014-08-05 14:04:04 +10006309 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006310 }
6311 Py_CLEAR(iterator);
6312
Larry Hastings2f936352014-08-05 14:04:04 +10006313 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006314 posix_error();
6315 goto error;
6316 }
Larry Hastings2f936352014-08-05 14:04:04 +10006317 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006318 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006319
6320error:
Larry Hastings2f936352014-08-05 14:04:04 +10006321 if (cpu_set)
6322 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006323 Py_XDECREF(iterator);
6324 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006325}
6326
Larry Hastings2f936352014-08-05 14:04:04 +10006327
6328/*[clinic input]
6329os.sched_getaffinity
6330 pid: pid_t
6331 /
6332
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006333Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006334
6335The affinity is returned as a set of CPU identifiers.
6336[clinic start generated code]*/
6337
Larry Hastings2f936352014-08-05 14:04:04 +10006338static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006339os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006340/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006341{
Antoine Pitrou84869872012-08-04 16:16:35 +02006342 int cpu, ncpus, count;
6343 size_t setsize;
6344 cpu_set_t *mask = NULL;
6345 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006346
Antoine Pitrou84869872012-08-04 16:16:35 +02006347 ncpus = NCPUS_START;
6348 while (1) {
6349 setsize = CPU_ALLOC_SIZE(ncpus);
6350 mask = CPU_ALLOC(ncpus);
6351 if (mask == NULL)
6352 return PyErr_NoMemory();
6353 if (sched_getaffinity(pid, setsize, mask) == 0)
6354 break;
6355 CPU_FREE(mask);
6356 if (errno != EINVAL)
6357 return posix_error();
6358 if (ncpus > INT_MAX / 2) {
6359 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6360 "a large enough CPU set");
6361 return NULL;
6362 }
6363 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006364 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006365
6366 res = PySet_New(NULL);
6367 if (res == NULL)
6368 goto error;
6369 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6370 if (CPU_ISSET_S(cpu, setsize, mask)) {
6371 PyObject *cpu_num = PyLong_FromLong(cpu);
6372 --count;
6373 if (cpu_num == NULL)
6374 goto error;
6375 if (PySet_Add(res, cpu_num)) {
6376 Py_DECREF(cpu_num);
6377 goto error;
6378 }
6379 Py_DECREF(cpu_num);
6380 }
6381 }
6382 CPU_FREE(mask);
6383 return res;
6384
6385error:
6386 if (mask)
6387 CPU_FREE(mask);
6388 Py_XDECREF(res);
6389 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006390}
6391
Benjamin Peterson2740af82011-08-02 17:41:34 -05006392#endif /* HAVE_SCHED_SETAFFINITY */
6393
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006394#endif /* HAVE_SCHED_H */
6395
Larry Hastings2f936352014-08-05 14:04:04 +10006396
Neal Norwitzb59798b2003-03-21 01:43:31 +00006397/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006398/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6399#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006400#define DEV_PTY_FILE "/dev/ptc"
6401#define HAVE_DEV_PTMX
6402#else
6403#define DEV_PTY_FILE "/dev/ptmx"
6404#endif
6405
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006406#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006407#ifdef HAVE_PTY_H
6408#include <pty.h>
6409#else
6410#ifdef HAVE_LIBUTIL_H
6411#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006412#else
6413#ifdef HAVE_UTIL_H
6414#include <util.h>
6415#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006416#endif /* HAVE_LIBUTIL_H */
6417#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006418#ifdef HAVE_STROPTS_H
6419#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006420#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006421#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006422
Larry Hastings2f936352014-08-05 14:04:04 +10006423
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006424#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006425/*[clinic input]
6426os.openpty
6427
6428Open a pseudo-terminal.
6429
6430Return a tuple of (master_fd, slave_fd) containing open file descriptors
6431for both the master and slave ends.
6432[clinic start generated code]*/
6433
Larry Hastings2f936352014-08-05 14:04:04 +10006434static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006435os_openpty_impl(PyObject *module)
6436/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006437{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006438 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006439#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006440 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006441#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006442#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006443 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006444#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006445 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006446#endif
6447#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006448
Thomas Wouters70c21a12000-07-14 14:28:33 +00006449#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006450 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006451 goto posix_error;
6452
6453 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6454 goto error;
6455 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6456 goto error;
6457
Neal Norwitzb59798b2003-03-21 01:43:31 +00006458#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006459 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6460 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006461 goto posix_error;
6462 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6463 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006464
Victor Stinnerdaf45552013-08-28 00:53:59 +02006465 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006467 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006468
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006469#else
Victor Stinner000de532013-11-25 23:19:58 +01006470 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006471 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006472 goto posix_error;
6473
Victor Stinner8c62be82010-05-06 00:08:46 +00006474 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006475
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 /* change permission of slave */
6477 if (grantpt(master_fd) < 0) {
6478 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006479 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006480 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006481
Victor Stinner8c62be82010-05-06 00:08:46 +00006482 /* unlock slave */
6483 if (unlockpt(master_fd) < 0) {
6484 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006485 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006486 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006487
Victor Stinner8c62be82010-05-06 00:08:46 +00006488 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006489
Victor Stinner8c62be82010-05-06 00:08:46 +00006490 slave_name = ptsname(master_fd); /* get name of slave */
6491 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006492 goto posix_error;
6493
6494 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006495 if (slave_fd == -1)
6496 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006497
6498 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6499 goto posix_error;
6500
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006501#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6503 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006504#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006505 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006506#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006507#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006508#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006509
Victor Stinner8c62be82010-05-06 00:08:46 +00006510 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006511
Victor Stinnerdaf45552013-08-28 00:53:59 +02006512posix_error:
6513 posix_error();
6514error:
6515 if (master_fd != -1)
6516 close(master_fd);
6517 if (slave_fd != -1)
6518 close(slave_fd);
6519 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006520}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006521#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006522
Larry Hastings2f936352014-08-05 14:04:04 +10006523
Fred Drake8cef4cf2000-06-28 16:40:38 +00006524#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006525/*[clinic input]
6526os.forkpty
6527
6528Fork a new process with a new pseudo-terminal as controlling tty.
6529
6530Returns a tuple of (pid, master_fd).
6531Like fork(), return pid of 0 to the child process,
6532and pid of child to the parent process.
6533To both, return fd of newly opened pseudo-terminal.
6534[clinic start generated code]*/
6535
Larry Hastings2f936352014-08-05 14:04:04 +10006536static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006537os_forkpty_impl(PyObject *module)
6538/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006539{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006540 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006541 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006542
Eric Snow59032962018-09-14 14:17:20 -07006543 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6544 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6545 return NULL;
6546 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006547 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 pid = forkpty(&master_fd, NULL, NULL, NULL);
6549 if (pid == 0) {
6550 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006551 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006552 } else {
6553 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006554 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006555 }
6556 if (pid == -1)
6557 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006558 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006559}
Larry Hastings2f936352014-08-05 14:04:04 +10006560#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006561
Ross Lagerwall7807c352011-03-17 20:20:30 +02006562
Guido van Rossumad0ee831995-03-01 10:34:45 +00006563#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006564/*[clinic input]
6565os.getegid
6566
6567Return the current process's effective group id.
6568[clinic start generated code]*/
6569
Larry Hastings2f936352014-08-05 14:04:04 +10006570static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006571os_getegid_impl(PyObject *module)
6572/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006573{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006574 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006575}
Larry Hastings2f936352014-08-05 14:04:04 +10006576#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006577
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006578
Guido van Rossumad0ee831995-03-01 10:34:45 +00006579#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006580/*[clinic input]
6581os.geteuid
6582
6583Return the current process's effective user id.
6584[clinic start generated code]*/
6585
Larry Hastings2f936352014-08-05 14:04:04 +10006586static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006587os_geteuid_impl(PyObject *module)
6588/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006589{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006590 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006591}
Larry Hastings2f936352014-08-05 14:04:04 +10006592#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006593
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006594
Guido van Rossumad0ee831995-03-01 10:34:45 +00006595#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006596/*[clinic input]
6597os.getgid
6598
6599Return the current process's group id.
6600[clinic start generated code]*/
6601
Larry Hastings2f936352014-08-05 14:04:04 +10006602static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006603os_getgid_impl(PyObject *module)
6604/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006605{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006606 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006607}
Larry Hastings2f936352014-08-05 14:04:04 +10006608#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006609
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006610
Berker Peksag39404992016-09-15 20:45:16 +03006611#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006612/*[clinic input]
6613os.getpid
6614
6615Return the current process id.
6616[clinic start generated code]*/
6617
Larry Hastings2f936352014-08-05 14:04:04 +10006618static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006619os_getpid_impl(PyObject *module)
6620/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006621{
Victor Stinner8c62be82010-05-06 00:08:46 +00006622 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006623}
Berker Peksag39404992016-09-15 20:45:16 +03006624#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006625
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006626#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006627
6628/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006629PyDoc_STRVAR(posix_getgrouplist__doc__,
6630"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6631Returns a list of groups to which a user belongs.\n\n\
6632 user: username to lookup\n\
6633 group: base group id of the user");
6634
6635static PyObject *
6636posix_getgrouplist(PyObject *self, PyObject *args)
6637{
6638#ifdef NGROUPS_MAX
6639#define MAX_GROUPS NGROUPS_MAX
6640#else
6641 /* defined to be 16 on Solaris7, so this should be a small number */
6642#define MAX_GROUPS 64
6643#endif
6644
6645 const char *user;
6646 int i, ngroups;
6647 PyObject *list;
6648#ifdef __APPLE__
6649 int *groups, basegid;
6650#else
6651 gid_t *groups, basegid;
6652#endif
6653 ngroups = MAX_GROUPS;
6654
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006655#ifdef __APPLE__
6656 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006657 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006658#else
6659 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6660 _Py_Gid_Converter, &basegid))
6661 return NULL;
6662#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006663
6664#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006665 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006666#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006667 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006668#endif
6669 if (groups == NULL)
6670 return PyErr_NoMemory();
6671
6672 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6673 PyMem_Del(groups);
6674 return posix_error();
6675 }
6676
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006677#ifdef _Py_MEMORY_SANITIZER
6678 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6679 __msan_unpoison(&ngroups, sizeof(ngroups));
6680 __msan_unpoison(groups, ngroups*sizeof(*groups));
6681#endif
6682
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006683 list = PyList_New(ngroups);
6684 if (list == NULL) {
6685 PyMem_Del(groups);
6686 return NULL;
6687 }
6688
6689 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006690#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006691 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006692#else
6693 PyObject *o = _PyLong_FromGid(groups[i]);
6694#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006695 if (o == NULL) {
6696 Py_DECREF(list);
6697 PyMem_Del(groups);
6698 return NULL;
6699 }
6700 PyList_SET_ITEM(list, i, o);
6701 }
6702
6703 PyMem_Del(groups);
6704
6705 return list;
6706}
Larry Hastings2f936352014-08-05 14:04:04 +10006707#endif /* HAVE_GETGROUPLIST */
6708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006709
Fred Drakec9680921999-12-13 16:37:25 +00006710#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006711/*[clinic input]
6712os.getgroups
6713
6714Return list of supplemental group IDs for the process.
6715[clinic start generated code]*/
6716
Larry Hastings2f936352014-08-05 14:04:04 +10006717static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006718os_getgroups_impl(PyObject *module)
6719/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006720{
6721 PyObject *result = NULL;
6722
Fred Drakec9680921999-12-13 16:37:25 +00006723#ifdef NGROUPS_MAX
6724#define MAX_GROUPS NGROUPS_MAX
6725#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006727#define MAX_GROUPS 64
6728#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006729 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006730
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006731 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006732 * This is a helper variable to store the intermediate result when
6733 * that happens.
6734 *
6735 * To keep the code readable the OSX behaviour is unconditional,
6736 * according to the POSIX spec this should be safe on all unix-y
6737 * systems.
6738 */
6739 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006740 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006741
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006742#ifdef __APPLE__
6743 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6744 * there are more groups than can fit in grouplist. Therefore, on OS X
6745 * always first call getgroups with length 0 to get the actual number
6746 * of groups.
6747 */
6748 n = getgroups(0, NULL);
6749 if (n < 0) {
6750 return posix_error();
6751 } else if (n <= MAX_GROUPS) {
6752 /* groups will fit in existing array */
6753 alt_grouplist = grouplist;
6754 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006755 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006756 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006757 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006758 }
6759 }
6760
6761 n = getgroups(n, alt_grouplist);
6762 if (n == -1) {
6763 if (alt_grouplist != grouplist) {
6764 PyMem_Free(alt_grouplist);
6765 }
6766 return posix_error();
6767 }
6768#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006769 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006770 if (n < 0) {
6771 if (errno == EINVAL) {
6772 n = getgroups(0, NULL);
6773 if (n == -1) {
6774 return posix_error();
6775 }
6776 if (n == 0) {
6777 /* Avoid malloc(0) */
6778 alt_grouplist = grouplist;
6779 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006780 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006781 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006782 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006783 }
6784 n = getgroups(n, alt_grouplist);
6785 if (n == -1) {
6786 PyMem_Free(alt_grouplist);
6787 return posix_error();
6788 }
6789 }
6790 } else {
6791 return posix_error();
6792 }
6793 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006794#endif
6795
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006796 result = PyList_New(n);
6797 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 int i;
6799 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006800 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006801 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006802 Py_DECREF(result);
6803 result = NULL;
6804 break;
Fred Drakec9680921999-12-13 16:37:25 +00006805 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006806 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006807 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006808 }
6809
6810 if (alt_grouplist != grouplist) {
6811 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006812 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006813
Fred Drakec9680921999-12-13 16:37:25 +00006814 return result;
6815}
Larry Hastings2f936352014-08-05 14:04:04 +10006816#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006817
Antoine Pitroub7572f02009-12-02 20:46:48 +00006818#ifdef HAVE_INITGROUPS
6819PyDoc_STRVAR(posix_initgroups__doc__,
6820"initgroups(username, gid) -> None\n\n\
6821Call the system initgroups() to initialize the group access list with all of\n\
6822the groups of which the specified username is a member, plus the specified\n\
6823group id.");
6824
Larry Hastings2f936352014-08-05 14:04:04 +10006825/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006826static PyObject *
6827posix_initgroups(PyObject *self, PyObject *args)
6828{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006829 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006830 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006831 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006832#ifdef __APPLE__
6833 int gid;
6834#else
6835 gid_t gid;
6836#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006837
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006838#ifdef __APPLE__
6839 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6840 PyUnicode_FSConverter, &oname,
6841 &gid))
6842#else
6843 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6844 PyUnicode_FSConverter, &oname,
6845 _Py_Gid_Converter, &gid))
6846#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006847 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006848 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006849
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006850 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006851 Py_DECREF(oname);
6852 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006854
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006855 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006856}
Larry Hastings2f936352014-08-05 14:04:04 +10006857#endif /* HAVE_INITGROUPS */
6858
Antoine Pitroub7572f02009-12-02 20:46:48 +00006859
Martin v. Löwis606edc12002-06-13 21:09:11 +00006860#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006861/*[clinic input]
6862os.getpgid
6863
6864 pid: pid_t
6865
6866Call the system call getpgid(), and return the result.
6867[clinic start generated code]*/
6868
Larry Hastings2f936352014-08-05 14:04:04 +10006869static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006870os_getpgid_impl(PyObject *module, pid_t pid)
6871/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006872{
6873 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006874 if (pgid < 0)
6875 return posix_error();
6876 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006877}
6878#endif /* HAVE_GETPGID */
6879
6880
Guido van Rossumb6775db1994-08-01 11:34:53 +00006881#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006882/*[clinic input]
6883os.getpgrp
6884
6885Return the current process group id.
6886[clinic start generated code]*/
6887
Larry Hastings2f936352014-08-05 14:04:04 +10006888static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006889os_getpgrp_impl(PyObject *module)
6890/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006891{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006892#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006893 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006894#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006895 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006896#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006897}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006898#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006899
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006900
Guido van Rossumb6775db1994-08-01 11:34:53 +00006901#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006902/*[clinic input]
6903os.setpgrp
6904
6905Make the current process the leader of its process group.
6906[clinic start generated code]*/
6907
Larry Hastings2f936352014-08-05 14:04:04 +10006908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006909os_setpgrp_impl(PyObject *module)
6910/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006911{
Guido van Rossum64933891994-10-20 21:56:42 +00006912#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006913 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006914#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006915 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006916#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006917 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006918 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006919}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006920#endif /* HAVE_SETPGRP */
6921
Guido van Rossumad0ee831995-03-01 10:34:45 +00006922#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006923
6924#ifdef MS_WINDOWS
6925#include <tlhelp32.h>
6926
6927static PyObject*
6928win32_getppid()
6929{
6930 HANDLE snapshot;
6931 pid_t mypid;
6932 PyObject* result = NULL;
6933 BOOL have_record;
6934 PROCESSENTRY32 pe;
6935
6936 mypid = getpid(); /* This function never fails */
6937
6938 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6939 if (snapshot == INVALID_HANDLE_VALUE)
6940 return PyErr_SetFromWindowsErr(GetLastError());
6941
6942 pe.dwSize = sizeof(pe);
6943 have_record = Process32First(snapshot, &pe);
6944 while (have_record) {
6945 if (mypid == (pid_t)pe.th32ProcessID) {
6946 /* We could cache the ulong value in a static variable. */
6947 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6948 break;
6949 }
6950
6951 have_record = Process32Next(snapshot, &pe);
6952 }
6953
6954 /* If our loop exits and our pid was not found (result will be NULL)
6955 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6956 * error anyway, so let's raise it. */
6957 if (!result)
6958 result = PyErr_SetFromWindowsErr(GetLastError());
6959
6960 CloseHandle(snapshot);
6961
6962 return result;
6963}
6964#endif /*MS_WINDOWS*/
6965
Larry Hastings2f936352014-08-05 14:04:04 +10006966
6967/*[clinic input]
6968os.getppid
6969
6970Return the parent's process id.
6971
6972If the parent process has already exited, Windows machines will still
6973return its id; others systems will return the id of the 'init' process (1).
6974[clinic start generated code]*/
6975
Larry Hastings2f936352014-08-05 14:04:04 +10006976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006977os_getppid_impl(PyObject *module)
6978/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006979{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006980#ifdef MS_WINDOWS
6981 return win32_getppid();
6982#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006983 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006984#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006985}
6986#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006987
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006988
Fred Drake12c6e2d1999-12-14 21:25:03 +00006989#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006990/*[clinic input]
6991os.getlogin
6992
6993Return the actual login name.
6994[clinic start generated code]*/
6995
Larry Hastings2f936352014-08-05 14:04:04 +10006996static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006997os_getlogin_impl(PyObject *module)
6998/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006999{
Victor Stinner8c62be82010-05-06 00:08:46 +00007000 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007001#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007002 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007003 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007004
7005 if (GetUserNameW(user_name, &num_chars)) {
7006 /* num_chars is the number of unicode chars plus null terminator */
7007 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007008 }
7009 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007010 result = PyErr_SetFromWindowsErr(GetLastError());
7011#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 char *name;
7013 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007014
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 errno = 0;
7016 name = getlogin();
7017 if (name == NULL) {
7018 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007019 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007020 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007021 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007022 }
7023 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007024 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007025 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007026#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007027 return result;
7028}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007029#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007030
Larry Hastings2f936352014-08-05 14:04:04 +10007031
Guido van Rossumad0ee831995-03-01 10:34:45 +00007032#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007033/*[clinic input]
7034os.getuid
7035
7036Return the current process's user id.
7037[clinic start generated code]*/
7038
Larry Hastings2f936352014-08-05 14:04:04 +10007039static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007040os_getuid_impl(PyObject *module)
7041/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007042{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007043 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007044}
Larry Hastings2f936352014-08-05 14:04:04 +10007045#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007046
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007047
Brian Curtineb24d742010-04-12 17:16:38 +00007048#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007049#define HAVE_KILL
7050#endif /* MS_WINDOWS */
7051
7052#ifdef HAVE_KILL
7053/*[clinic input]
7054os.kill
7055
7056 pid: pid_t
7057 signal: Py_ssize_t
7058 /
7059
7060Kill a process with a signal.
7061[clinic start generated code]*/
7062
Larry Hastings2f936352014-08-05 14:04:04 +10007063static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007064os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7065/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007066#ifndef MS_WINDOWS
7067{
7068 if (kill(pid, (int)signal) == -1)
7069 return posix_error();
7070 Py_RETURN_NONE;
7071}
7072#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007073{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007074 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007075 DWORD sig = (DWORD)signal;
7076 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007077 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007078
Victor Stinner8c62be82010-05-06 00:08:46 +00007079 /* Console processes which share a common console can be sent CTRL+C or
7080 CTRL+BREAK events, provided they handle said events. */
7081 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007082 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 err = GetLastError();
7084 PyErr_SetFromWindowsErr(err);
7085 }
7086 else
7087 Py_RETURN_NONE;
7088 }
Brian Curtineb24d742010-04-12 17:16:38 +00007089
Victor Stinner8c62be82010-05-06 00:08:46 +00007090 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7091 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007092 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 if (handle == NULL) {
7094 err = GetLastError();
7095 return PyErr_SetFromWindowsErr(err);
7096 }
Brian Curtineb24d742010-04-12 17:16:38 +00007097
Victor Stinner8c62be82010-05-06 00:08:46 +00007098 if (TerminateProcess(handle, sig) == 0) {
7099 err = GetLastError();
7100 result = PyErr_SetFromWindowsErr(err);
7101 } else {
7102 Py_INCREF(Py_None);
7103 result = Py_None;
7104 }
Brian Curtineb24d742010-04-12 17:16:38 +00007105
Victor Stinner8c62be82010-05-06 00:08:46 +00007106 CloseHandle(handle);
7107 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007108}
Larry Hastings2f936352014-08-05 14:04:04 +10007109#endif /* !MS_WINDOWS */
7110#endif /* HAVE_KILL */
7111
7112
7113#ifdef HAVE_KILLPG
7114/*[clinic input]
7115os.killpg
7116
7117 pgid: pid_t
7118 signal: int
7119 /
7120
7121Kill a process group with a signal.
7122[clinic start generated code]*/
7123
Larry Hastings2f936352014-08-05 14:04:04 +10007124static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007125os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7126/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007127{
7128 /* XXX some man pages make the `pgid` parameter an int, others
7129 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7130 take the same type. Moreover, pid_t is always at least as wide as
7131 int (else compilation of this module fails), which is safe. */
7132 if (killpg(pgid, signal) == -1)
7133 return posix_error();
7134 Py_RETURN_NONE;
7135}
7136#endif /* HAVE_KILLPG */
7137
Brian Curtineb24d742010-04-12 17:16:38 +00007138
Guido van Rossumc0125471996-06-28 18:55:32 +00007139#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007140#ifdef HAVE_SYS_LOCK_H
7141#include <sys/lock.h>
7142#endif
7143
Larry Hastings2f936352014-08-05 14:04:04 +10007144/*[clinic input]
7145os.plock
7146 op: int
7147 /
7148
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007149Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007150[clinic start generated code]*/
7151
Larry Hastings2f936352014-08-05 14:04:04 +10007152static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007153os_plock_impl(PyObject *module, int op)
7154/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007155{
Victor Stinner8c62be82010-05-06 00:08:46 +00007156 if (plock(op) == -1)
7157 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007158 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007159}
Larry Hastings2f936352014-08-05 14:04:04 +10007160#endif /* HAVE_PLOCK */
7161
Guido van Rossumc0125471996-06-28 18:55:32 +00007162
Guido van Rossumb6775db1994-08-01 11:34:53 +00007163#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007164/*[clinic input]
7165os.setuid
7166
7167 uid: uid_t
7168 /
7169
7170Set the current process's user id.
7171[clinic start generated code]*/
7172
Larry Hastings2f936352014-08-05 14:04:04 +10007173static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007174os_setuid_impl(PyObject *module, uid_t uid)
7175/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007176{
Victor Stinner8c62be82010-05-06 00:08:46 +00007177 if (setuid(uid) < 0)
7178 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007179 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007180}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007181#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007182
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007183
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007184#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007185/*[clinic input]
7186os.seteuid
7187
7188 euid: uid_t
7189 /
7190
7191Set the current process's effective user id.
7192[clinic start generated code]*/
7193
Larry Hastings2f936352014-08-05 14:04:04 +10007194static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007195os_seteuid_impl(PyObject *module, uid_t euid)
7196/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007197{
7198 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007199 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007200 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007201}
7202#endif /* HAVE_SETEUID */
7203
Larry Hastings2f936352014-08-05 14:04:04 +10007204
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007205#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007206/*[clinic input]
7207os.setegid
7208
7209 egid: gid_t
7210 /
7211
7212Set the current process's effective group id.
7213[clinic start generated code]*/
7214
Larry Hastings2f936352014-08-05 14:04:04 +10007215static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007216os_setegid_impl(PyObject *module, gid_t egid)
7217/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007218{
7219 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007221 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007222}
7223#endif /* HAVE_SETEGID */
7224
Larry Hastings2f936352014-08-05 14:04:04 +10007225
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007226#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007227/*[clinic input]
7228os.setreuid
7229
7230 ruid: uid_t
7231 euid: uid_t
7232 /
7233
7234Set the current process's real and effective user ids.
7235[clinic start generated code]*/
7236
Larry Hastings2f936352014-08-05 14:04:04 +10007237static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007238os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7239/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007240{
Victor Stinner8c62be82010-05-06 00:08:46 +00007241 if (setreuid(ruid, euid) < 0) {
7242 return posix_error();
7243 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007244 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007246}
7247#endif /* HAVE_SETREUID */
7248
Larry Hastings2f936352014-08-05 14:04:04 +10007249
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007250#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007251/*[clinic input]
7252os.setregid
7253
7254 rgid: gid_t
7255 egid: gid_t
7256 /
7257
7258Set the current process's real and effective group ids.
7259[clinic start generated code]*/
7260
Larry Hastings2f936352014-08-05 14:04:04 +10007261static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007262os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7263/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007264{
7265 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007266 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007267 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007268}
7269#endif /* HAVE_SETREGID */
7270
Larry Hastings2f936352014-08-05 14:04:04 +10007271
Guido van Rossumb6775db1994-08-01 11:34:53 +00007272#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007273/*[clinic input]
7274os.setgid
7275 gid: gid_t
7276 /
7277
7278Set the current process's group id.
7279[clinic start generated code]*/
7280
Larry Hastings2f936352014-08-05 14:04:04 +10007281static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007282os_setgid_impl(PyObject *module, gid_t gid)
7283/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007284{
Victor Stinner8c62be82010-05-06 00:08:46 +00007285 if (setgid(gid) < 0)
7286 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007287 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007288}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007289#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007290
Larry Hastings2f936352014-08-05 14:04:04 +10007291
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007292#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007293/*[clinic input]
7294os.setgroups
7295
7296 groups: object
7297 /
7298
7299Set the groups of the current process to list.
7300[clinic start generated code]*/
7301
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007302static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007303os_setgroups(PyObject *module, PyObject *groups)
7304/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007305{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007306 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007308
Victor Stinner8c62be82010-05-06 00:08:46 +00007309 if (!PySequence_Check(groups)) {
7310 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7311 return NULL;
7312 }
7313 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007314 if (len < 0) {
7315 return NULL;
7316 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 if (len > MAX_GROUPS) {
7318 PyErr_SetString(PyExc_ValueError, "too many groups");
7319 return NULL;
7320 }
7321 for(i = 0; i < len; i++) {
7322 PyObject *elem;
7323 elem = PySequence_GetItem(groups, i);
7324 if (!elem)
7325 return NULL;
7326 if (!PyLong_Check(elem)) {
7327 PyErr_SetString(PyExc_TypeError,
7328 "groups must be integers");
7329 Py_DECREF(elem);
7330 return NULL;
7331 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007332 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007333 Py_DECREF(elem);
7334 return NULL;
7335 }
7336 }
7337 Py_DECREF(elem);
7338 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007339
Victor Stinner8c62be82010-05-06 00:08:46 +00007340 if (setgroups(len, grouplist) < 0)
7341 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007342 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007343}
7344#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007346#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7347static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007348wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007349{
Victor Stinner8c62be82010-05-06 00:08:46 +00007350 PyObject *result;
7351 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007352 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007353
Victor Stinner8c62be82010-05-06 00:08:46 +00007354 if (pid == -1)
7355 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007356
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 if (struct_rusage == NULL) {
7358 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7359 if (m == NULL)
7360 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007361 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007362 Py_DECREF(m);
7363 if (struct_rusage == NULL)
7364 return NULL;
7365 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007366
Victor Stinner8c62be82010-05-06 00:08:46 +00007367 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7368 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7369 if (!result)
7370 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007371
7372#ifndef doubletime
7373#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7374#endif
7375
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007377 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007378 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007379 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007380#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007381 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7382 SET_INT(result, 2, ru->ru_maxrss);
7383 SET_INT(result, 3, ru->ru_ixrss);
7384 SET_INT(result, 4, ru->ru_idrss);
7385 SET_INT(result, 5, ru->ru_isrss);
7386 SET_INT(result, 6, ru->ru_minflt);
7387 SET_INT(result, 7, ru->ru_majflt);
7388 SET_INT(result, 8, ru->ru_nswap);
7389 SET_INT(result, 9, ru->ru_inblock);
7390 SET_INT(result, 10, ru->ru_oublock);
7391 SET_INT(result, 11, ru->ru_msgsnd);
7392 SET_INT(result, 12, ru->ru_msgrcv);
7393 SET_INT(result, 13, ru->ru_nsignals);
7394 SET_INT(result, 14, ru->ru_nvcsw);
7395 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007396#undef SET_INT
7397
Victor Stinner8c62be82010-05-06 00:08:46 +00007398 if (PyErr_Occurred()) {
7399 Py_DECREF(result);
7400 return NULL;
7401 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007402
Victor Stinner8c62be82010-05-06 00:08:46 +00007403 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007404}
7405#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7406
Larry Hastings2f936352014-08-05 14:04:04 +10007407
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007408#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007409/*[clinic input]
7410os.wait3
7411
7412 options: int
7413Wait for completion of a child process.
7414
7415Returns a tuple of information about the child process:
7416 (pid, status, rusage)
7417[clinic start generated code]*/
7418
Larry Hastings2f936352014-08-05 14:04:04 +10007419static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007420os_wait3_impl(PyObject *module, int options)
7421/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007422{
Victor Stinner8c62be82010-05-06 00:08:46 +00007423 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007424 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007425 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007426 WAIT_TYPE status;
7427 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007428
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007429 do {
7430 Py_BEGIN_ALLOW_THREADS
7431 pid = wait3(&status, options, &ru);
7432 Py_END_ALLOW_THREADS
7433 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7434 if (pid < 0)
7435 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007436
Victor Stinner4195b5c2012-02-08 23:03:19 +01007437 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007438}
7439#endif /* HAVE_WAIT3 */
7440
Larry Hastings2f936352014-08-05 14:04:04 +10007441
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007442#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007443/*[clinic input]
7444
7445os.wait4
7446
7447 pid: pid_t
7448 options: int
7449
7450Wait for completion of a specific child process.
7451
7452Returns a tuple of information about the child process:
7453 (pid, status, rusage)
7454[clinic start generated code]*/
7455
Larry Hastings2f936352014-08-05 14:04:04 +10007456static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007457os_wait4_impl(PyObject *module, pid_t pid, int options)
7458/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007459{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007460 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007462 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007463 WAIT_TYPE status;
7464 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007465
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007466 do {
7467 Py_BEGIN_ALLOW_THREADS
7468 res = wait4(pid, &status, options, &ru);
7469 Py_END_ALLOW_THREADS
7470 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7471 if (res < 0)
7472 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007473
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007474 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007475}
7476#endif /* HAVE_WAIT4 */
7477
Larry Hastings2f936352014-08-05 14:04:04 +10007478
Ross Lagerwall7807c352011-03-17 20:20:30 +02007479#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007480/*[clinic input]
7481os.waitid
7482
7483 idtype: idtype_t
7484 Must be one of be P_PID, P_PGID or P_ALL.
7485 id: id_t
7486 The id to wait on.
7487 options: int
7488 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7489 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7490 /
7491
7492Returns the result of waiting for a process or processes.
7493
7494Returns either waitid_result or None if WNOHANG is specified and there are
7495no children in a waitable state.
7496[clinic start generated code]*/
7497
Larry Hastings2f936352014-08-05 14:04:04 +10007498static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007499os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7500/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007501{
7502 PyObject *result;
7503 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007504 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007505 siginfo_t si;
7506 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007507
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007508 do {
7509 Py_BEGIN_ALLOW_THREADS
7510 res = waitid(idtype, id, &si, options);
7511 Py_END_ALLOW_THREADS
7512 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7513 if (res < 0)
7514 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007515
7516 if (si.si_pid == 0)
7517 Py_RETURN_NONE;
7518
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007519 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007520 if (!result)
7521 return NULL;
7522
7523 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007524 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007525 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7526 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7527 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7528 if (PyErr_Occurred()) {
7529 Py_DECREF(result);
7530 return NULL;
7531 }
7532
7533 return result;
7534}
Larry Hastings2f936352014-08-05 14:04:04 +10007535#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007536
Larry Hastings2f936352014-08-05 14:04:04 +10007537
7538#if defined(HAVE_WAITPID)
7539/*[clinic input]
7540os.waitpid
7541 pid: pid_t
7542 options: int
7543 /
7544
7545Wait for completion of a given child process.
7546
7547Returns a tuple of information regarding the child process:
7548 (pid, status)
7549
7550The options argument is ignored on Windows.
7551[clinic start generated code]*/
7552
Larry Hastings2f936352014-08-05 14:04:04 +10007553static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007554os_waitpid_impl(PyObject *module, pid_t pid, int options)
7555/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007556{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007557 pid_t res;
7558 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007559 WAIT_TYPE status;
7560 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007561
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007562 do {
7563 Py_BEGIN_ALLOW_THREADS
7564 res = waitpid(pid, &status, options);
7565 Py_END_ALLOW_THREADS
7566 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7567 if (res < 0)
7568 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007569
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007570 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007571}
Tim Petersab034fa2002-02-01 11:27:43 +00007572#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007573/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007574/*[clinic input]
7575os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007576 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007577 options: int
7578 /
7579
7580Wait for completion of a given process.
7581
7582Returns a tuple of information regarding the process:
7583 (pid, status << 8)
7584
7585The options argument is ignored on Windows.
7586[clinic start generated code]*/
7587
Larry Hastings2f936352014-08-05 14:04:04 +10007588static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007589os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007590/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007591{
7592 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007593 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007594 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007595
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007596 do {
7597 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007598 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007599 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007600 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007601 Py_END_ALLOW_THREADS
7602 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007603 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007604 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007605
Victor Stinner8c62be82010-05-06 00:08:46 +00007606 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007607 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007608}
Larry Hastings2f936352014-08-05 14:04:04 +10007609#endif
7610
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007611
Guido van Rossumad0ee831995-03-01 10:34:45 +00007612#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007613/*[clinic input]
7614os.wait
7615
7616Wait for completion of a child process.
7617
7618Returns a tuple of information about the child process:
7619 (pid, status)
7620[clinic start generated code]*/
7621
Larry Hastings2f936352014-08-05 14:04:04 +10007622static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007623os_wait_impl(PyObject *module)
7624/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007625{
Victor Stinner8c62be82010-05-06 00:08:46 +00007626 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007627 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007628 WAIT_TYPE status;
7629 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007630
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007631 do {
7632 Py_BEGIN_ALLOW_THREADS
7633 pid = wait(&status);
7634 Py_END_ALLOW_THREADS
7635 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7636 if (pid < 0)
7637 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007638
Victor Stinner8c62be82010-05-06 00:08:46 +00007639 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007640}
Larry Hastings2f936352014-08-05 14:04:04 +10007641#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007642
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007643
Larry Hastings9cf065c2012-06-22 16:30:09 -07007644#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007645/*[clinic input]
7646os.readlink
7647
7648 path: path_t
7649 *
7650 dir_fd: dir_fd(requires='readlinkat') = None
7651
7652Return a string representing the path to which the symbolic link points.
7653
7654If dir_fd is not None, it should be a file descriptor open to a directory,
7655and path should be relative; path will then be relative to that directory.
7656
7657dir_fd may not be implemented on your platform. If it is unavailable,
7658using it will raise a NotImplementedError.
7659[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007660
Barry Warsaw53699e91996-12-10 23:23:01 +00007661static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007662os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7663/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007664{
Berker Peksage0b5b202018-08-15 13:03:41 +03007665#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007666 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007667 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007668
7669 Py_BEGIN_ALLOW_THREADS
7670#ifdef HAVE_READLINKAT
7671 if (dir_fd != DEFAULT_DIR_FD)
7672 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7673 else
7674#endif
7675 length = readlink(path->narrow, buffer, MAXPATHLEN);
7676 Py_END_ALLOW_THREADS
7677
7678 if (length < 0) {
7679 return path_error(path);
7680 }
7681 buffer[length] = '\0';
7682
7683 if (PyUnicode_Check(path->object))
7684 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7685 else
7686 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007687#elif defined(MS_WINDOWS)
7688 DWORD n_bytes_returned;
7689 DWORD io_result;
7690 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007691 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7692 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
7693 const wchar_t *print_name;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007694 PyObject *result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007695
Larry Hastings2f936352014-08-05 14:04:04 +10007696 /* First get a handle to the reparse point */
7697 Py_BEGIN_ALLOW_THREADS
7698 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007699 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007700 0,
7701 0,
7702 0,
7703 OPEN_EXISTING,
7704 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7705 0);
7706 Py_END_ALLOW_THREADS
7707
Berker Peksage0b5b202018-08-15 13:03:41 +03007708 if (reparse_point_handle == INVALID_HANDLE_VALUE) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007709 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007710 }
Larry Hastings2f936352014-08-05 14:04:04 +10007711
7712 Py_BEGIN_ALLOW_THREADS
7713 /* New call DeviceIoControl to read the reparse point */
7714 io_result = DeviceIoControl(
7715 reparse_point_handle,
7716 FSCTL_GET_REPARSE_POINT,
7717 0, 0, /* in buffer */
7718 target_buffer, sizeof(target_buffer),
7719 &n_bytes_returned,
7720 0 /* we're not using OVERLAPPED_IO */
7721 );
7722 CloseHandle(reparse_point_handle);
7723 Py_END_ALLOW_THREADS
7724
Berker Peksage0b5b202018-08-15 13:03:41 +03007725 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007726 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007727 }
Larry Hastings2f936352014-08-05 14:04:04 +10007728
7729 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7730 {
7731 PyErr_SetString(PyExc_ValueError,
7732 "not a symbolic link");
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007733 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10007734 }
SSE43c34aad2018-02-13 00:10:35 +07007735 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7736 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007737
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007738 result = PyUnicode_FromWideChar(print_name,
7739 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
7740 if (path->narrow) {
7741 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Berker Peksage0b5b202018-08-15 13:03:41 +03007742 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007743 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007744#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007745}
Berker Peksage0b5b202018-08-15 13:03:41 +03007746#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007747
Larry Hastings9cf065c2012-06-22 16:30:09 -07007748#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007749
7750#if defined(MS_WINDOWS)
7751
7752/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Steve Dower6921e732018-03-05 14:26:08 -08007753static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007754
Larry Hastings9cf065c2012-06-22 16:30:09 -07007755static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007756check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007757{
7758 HINSTANCE hKernel32;
7759 /* only recheck */
Steve Dowercc16be82016-09-08 10:35:16 -07007760 if (Py_CreateSymbolicLinkW)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007761 return 1;
Tony Roberts4860f012019-02-02 18:16:42 +01007762
7763 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007764 hKernel32 = GetModuleHandleW(L"KERNEL32");
7765 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7766 "CreateSymbolicLinkW");
Tony Roberts4860f012019-02-02 18:16:42 +01007767 Py_END_ALLOW_THREADS
7768
Steve Dowercc16be82016-09-08 10:35:16 -07007769 return Py_CreateSymbolicLinkW != NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007770}
7771
Steve Dower6921e732018-03-05 14:26:08 -08007772/* Remove the last portion of the path - return 0 on success */
7773static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007774_dirnameW(WCHAR *path)
7775{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007776 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007777 size_t length = wcsnlen_s(path, MAX_PATH);
7778 if (length == MAX_PATH) {
7779 return -1;
7780 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007781
7782 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007783 for(ptr = path + length; ptr != path; ptr--) {
7784 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007785 break;
Steve Dower6921e732018-03-05 14:26:08 -08007786 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007787 }
7788 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007789 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007790}
7791
Victor Stinner31b3b922013-06-05 01:49:17 +02007792/* Is this path absolute? */
7793static int
7794_is_absW(const WCHAR *path)
7795{
Steve Dower6921e732018-03-05 14:26:08 -08007796 return path[0] == L'\\' || path[0] == L'/' ||
7797 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007798}
7799
Steve Dower6921e732018-03-05 14:26:08 -08007800/* join root and rest with a backslash - return 0 on success */
7801static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007802_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7803{
Victor Stinner31b3b922013-06-05 01:49:17 +02007804 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007805 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007806 }
7807
Steve Dower6921e732018-03-05 14:26:08 -08007808 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7809 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007810 }
Steve Dower6921e732018-03-05 14:26:08 -08007811
7812 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7813 return -1;
7814 }
7815
7816 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007817}
7818
Victor Stinner31b3b922013-06-05 01:49:17 +02007819/* Return True if the path at src relative to dest is a directory */
7820static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007821_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007822{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007823 WIN32_FILE_ATTRIBUTE_DATA src_info;
7824 WCHAR dest_parent[MAX_PATH];
7825 WCHAR src_resolved[MAX_PATH] = L"";
7826
7827 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007828 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7829 _dirnameW(dest_parent)) {
7830 return 0;
7831 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007832 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007833 if (_joinW(src_resolved, dest_parent, src)) {
7834 return 0;
7835 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007836 return (
7837 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7838 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7839 );
7840}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007841#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007842
Larry Hastings2f936352014-08-05 14:04:04 +10007843
7844/*[clinic input]
7845os.symlink
7846 src: path_t
7847 dst: path_t
7848 target_is_directory: bool = False
7849 *
7850 dir_fd: dir_fd(requires='symlinkat')=None
7851
7852# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7853
7854Create a symbolic link pointing to src named dst.
7855
7856target_is_directory is required on Windows if the target is to be
7857 interpreted as a directory. (On Windows, symlink requires
7858 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7859 target_is_directory is ignored on non-Windows platforms.
7860
7861If dir_fd is not None, it should be a file descriptor open to a directory,
7862 and path should be relative; path will then be relative to that directory.
7863dir_fd may not be implemented on your platform.
7864 If it is unavailable, using it will raise a NotImplementedError.
7865
7866[clinic start generated code]*/
7867
Larry Hastings2f936352014-08-05 14:04:04 +10007868static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007869os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007870 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007871/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007872{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007873#ifdef MS_WINDOWS
7874 DWORD result;
7875#else
7876 int result;
7877#endif
7878
Larry Hastings9cf065c2012-06-22 16:30:09 -07007879#ifdef MS_WINDOWS
7880 if (!check_CreateSymbolicLink()) {
7881 PyErr_SetString(PyExc_NotImplementedError,
7882 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007883 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007884 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007885 if (!win32_can_symlink) {
7886 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007887 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007888 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007889#endif
7890
Larry Hastings9cf065c2012-06-22 16:30:09 -07007891#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007892
Larry Hastings9cf065c2012-06-22 16:30:09 -07007893 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08007894 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07007895 /* if src is a directory, ensure target_is_directory==1 */
7896 target_is_directory |= _check_dirW(src->wide, dst->wide);
7897 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
7898 target_is_directory);
Steve Dower6921e732018-03-05 14:26:08 -08007899 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007900 Py_END_ALLOW_THREADS
7901
Larry Hastings2f936352014-08-05 14:04:04 +10007902 if (!result)
7903 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007904
7905#else
7906
Steve Dower6921e732018-03-05 14:26:08 -08007907 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
7908 PyErr_SetString(PyExc_ValueError,
7909 "symlink: src and dst must be the same type");
7910 return NULL;
7911 }
7912
Larry Hastings9cf065c2012-06-22 16:30:09 -07007913 Py_BEGIN_ALLOW_THREADS
7914#if HAVE_SYMLINKAT
7915 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007916 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007917 else
7918#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007919 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007920 Py_END_ALLOW_THREADS
7921
Larry Hastings2f936352014-08-05 14:04:04 +10007922 if (result)
7923 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007924#endif
7925
Larry Hastings2f936352014-08-05 14:04:04 +10007926 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007927}
7928#endif /* HAVE_SYMLINK */
7929
Larry Hastings9cf065c2012-06-22 16:30:09 -07007930
Brian Curtind40e6f72010-07-08 21:39:08 +00007931
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007932
Larry Hastings605a62d2012-06-24 04:33:36 -07007933static PyStructSequence_Field times_result_fields[] = {
7934 {"user", "user time"},
7935 {"system", "system time"},
7936 {"children_user", "user time of children"},
7937 {"children_system", "system time of children"},
7938 {"elapsed", "elapsed time since an arbitrary point in the past"},
7939 {NULL}
7940};
7941
7942PyDoc_STRVAR(times_result__doc__,
7943"times_result: Result from os.times().\n\n\
7944This object may be accessed either as a tuple of\n\
7945 (user, system, children_user, children_system, elapsed),\n\
7946or via the attributes user, system, children_user, children_system,\n\
7947and elapsed.\n\
7948\n\
7949See os.times for more information.");
7950
7951static PyStructSequence_Desc times_result_desc = {
7952 "times_result", /* name */
7953 times_result__doc__, /* doc */
7954 times_result_fields,
7955 5
7956};
7957
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007958static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07007959
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007960#ifdef MS_WINDOWS
7961#define HAVE_TIMES /* mandatory, for the method table */
7962#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007963
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007964#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007965
7966static PyObject *
7967build_times_result(double user, double system,
7968 double children_user, double children_system,
7969 double elapsed)
7970{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007971 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07007972 if (value == NULL)
7973 return NULL;
7974
7975#define SET(i, field) \
7976 { \
7977 PyObject *o = PyFloat_FromDouble(field); \
7978 if (!o) { \
7979 Py_DECREF(value); \
7980 return NULL; \
7981 } \
7982 PyStructSequence_SET_ITEM(value, i, o); \
7983 } \
7984
7985 SET(0, user);
7986 SET(1, system);
7987 SET(2, children_user);
7988 SET(3, children_system);
7989 SET(4, elapsed);
7990
7991#undef SET
7992
7993 return value;
7994}
7995
Larry Hastings605a62d2012-06-24 04:33:36 -07007996
Larry Hastings2f936352014-08-05 14:04:04 +10007997#ifndef MS_WINDOWS
7998#define NEED_TICKS_PER_SECOND
7999static long ticks_per_second = -1;
8000#endif /* MS_WINDOWS */
8001
8002/*[clinic input]
8003os.times
8004
8005Return a collection containing process timing information.
8006
8007The object returned behaves like a named tuple with these fields:
8008 (utime, stime, cutime, cstime, elapsed_time)
8009All fields are floating point numbers.
8010[clinic start generated code]*/
8011
Larry Hastings2f936352014-08-05 14:04:04 +10008012static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008013os_times_impl(PyObject *module)
8014/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008015#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008016{
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 FILETIME create, exit, kernel, user;
8018 HANDLE hProc;
8019 hProc = GetCurrentProcess();
8020 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8021 /* The fields of a FILETIME structure are the hi and lo part
8022 of a 64-bit value expressed in 100 nanosecond units.
8023 1e7 is one second in such units; 1e-7 the inverse.
8024 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8025 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008026 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 (double)(user.dwHighDateTime*429.4967296 +
8028 user.dwLowDateTime*1e-7),
8029 (double)(kernel.dwHighDateTime*429.4967296 +
8030 kernel.dwLowDateTime*1e-7),
8031 (double)0,
8032 (double)0,
8033 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008034}
Larry Hastings2f936352014-08-05 14:04:04 +10008035#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008036{
Larry Hastings2f936352014-08-05 14:04:04 +10008037
8038
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008039 struct tms t;
8040 clock_t c;
8041 errno = 0;
8042 c = times(&t);
8043 if (c == (clock_t) -1)
8044 return posix_error();
8045 return build_times_result(
8046 (double)t.tms_utime / ticks_per_second,
8047 (double)t.tms_stime / ticks_per_second,
8048 (double)t.tms_cutime / ticks_per_second,
8049 (double)t.tms_cstime / ticks_per_second,
8050 (double)c / ticks_per_second);
8051}
Larry Hastings2f936352014-08-05 14:04:04 +10008052#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008053#endif /* HAVE_TIMES */
8054
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008055
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008056#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008057/*[clinic input]
8058os.getsid
8059
8060 pid: pid_t
8061 /
8062
8063Call the system call getsid(pid) and return the result.
8064[clinic start generated code]*/
8065
Larry Hastings2f936352014-08-05 14:04:04 +10008066static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008067os_getsid_impl(PyObject *module, pid_t pid)
8068/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008069{
Victor Stinner8c62be82010-05-06 00:08:46 +00008070 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 sid = getsid(pid);
8072 if (sid < 0)
8073 return posix_error();
8074 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008075}
8076#endif /* HAVE_GETSID */
8077
8078
Guido van Rossumb6775db1994-08-01 11:34:53 +00008079#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008080/*[clinic input]
8081os.setsid
8082
8083Call the system call setsid().
8084[clinic start generated code]*/
8085
Larry Hastings2f936352014-08-05 14:04:04 +10008086static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008087os_setsid_impl(PyObject *module)
8088/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008089{
Victor Stinner8c62be82010-05-06 00:08:46 +00008090 if (setsid() < 0)
8091 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008092 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008093}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008094#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008095
Larry Hastings2f936352014-08-05 14:04:04 +10008096
Guido van Rossumb6775db1994-08-01 11:34:53 +00008097#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008098/*[clinic input]
8099os.setpgid
8100
8101 pid: pid_t
8102 pgrp: pid_t
8103 /
8104
8105Call the system call setpgid(pid, pgrp).
8106[clinic start generated code]*/
8107
Larry Hastings2f936352014-08-05 14:04:04 +10008108static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008109os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8110/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008111{
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 if (setpgid(pid, pgrp) < 0)
8113 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008114 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008115}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008116#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008117
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008118
Guido van Rossumb6775db1994-08-01 11:34:53 +00008119#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008120/*[clinic input]
8121os.tcgetpgrp
8122
8123 fd: int
8124 /
8125
8126Return the process group associated with the terminal specified by fd.
8127[clinic start generated code]*/
8128
Larry Hastings2f936352014-08-05 14:04:04 +10008129static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008130os_tcgetpgrp_impl(PyObject *module, int fd)
8131/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008132{
8133 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008134 if (pgid < 0)
8135 return posix_error();
8136 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008137}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008138#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008139
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008140
Guido van Rossumb6775db1994-08-01 11:34:53 +00008141#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008142/*[clinic input]
8143os.tcsetpgrp
8144
8145 fd: int
8146 pgid: pid_t
8147 /
8148
8149Set the process group associated with the terminal specified by fd.
8150[clinic start generated code]*/
8151
Larry Hastings2f936352014-08-05 14:04:04 +10008152static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008153os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8154/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008155{
Victor Stinner8c62be82010-05-06 00:08:46 +00008156 if (tcsetpgrp(fd, pgid) < 0)
8157 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008158 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008159}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008160#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008161
Guido van Rossum687dd131993-05-17 08:34:16 +00008162/* Functions acting on file descriptors */
8163
Victor Stinnerdaf45552013-08-28 00:53:59 +02008164#ifdef O_CLOEXEC
8165extern int _Py_open_cloexec_works;
8166#endif
8167
Larry Hastings2f936352014-08-05 14:04:04 +10008168
8169/*[clinic input]
8170os.open -> int
8171 path: path_t
8172 flags: int
8173 mode: int = 0o777
8174 *
8175 dir_fd: dir_fd(requires='openat') = None
8176
8177# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8178
8179Open a file for low level IO. Returns a file descriptor (integer).
8180
8181If dir_fd is not None, it should be a file descriptor open to a directory,
8182 and path should be relative; path will then be relative to that directory.
8183dir_fd may not be implemented on your platform.
8184 If it is unavailable, using it will raise a NotImplementedError.
8185[clinic start generated code]*/
8186
Larry Hastings2f936352014-08-05 14:04:04 +10008187static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008188os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8189/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008190{
8191 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008192 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008193
Victor Stinnerdaf45552013-08-28 00:53:59 +02008194#ifdef O_CLOEXEC
8195 int *atomic_flag_works = &_Py_open_cloexec_works;
8196#elif !defined(MS_WINDOWS)
8197 int *atomic_flag_works = NULL;
8198#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008199
Victor Stinnerdaf45552013-08-28 00:53:59 +02008200#ifdef MS_WINDOWS
8201 flags |= O_NOINHERIT;
8202#elif defined(O_CLOEXEC)
8203 flags |= O_CLOEXEC;
8204#endif
8205
Steve Dower8fc89802015-04-12 00:26:27 -04008206 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008207 do {
8208 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008209#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008210 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008211#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008212#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008213 if (dir_fd != DEFAULT_DIR_FD)
8214 fd = openat(dir_fd, path->narrow, flags, mode);
8215 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008216#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008217 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008218#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008219 Py_END_ALLOW_THREADS
8220 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008221 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008222
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008223 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008224 if (!async_err)
8225 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008226 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008227 }
8228
Victor Stinnerdaf45552013-08-28 00:53:59 +02008229#ifndef MS_WINDOWS
8230 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8231 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008232 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008233 }
8234#endif
8235
Larry Hastings2f936352014-08-05 14:04:04 +10008236 return fd;
8237}
8238
8239
8240/*[clinic input]
8241os.close
8242
8243 fd: int
8244
8245Close a file descriptor.
8246[clinic start generated code]*/
8247
Barry Warsaw53699e91996-12-10 23:23:01 +00008248static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008249os_close_impl(PyObject *module, int fd)
8250/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008251{
Larry Hastings2f936352014-08-05 14:04:04 +10008252 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008253 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8254 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8255 * for more details.
8256 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008257 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008258 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008259 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008260 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008261 Py_END_ALLOW_THREADS
8262 if (res < 0)
8263 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008264 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008265}
8266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008267
Larry Hastings2f936352014-08-05 14:04:04 +10008268/*[clinic input]
8269os.closerange
8270
8271 fd_low: int
8272 fd_high: int
8273 /
8274
8275Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8276[clinic start generated code]*/
8277
Larry Hastings2f936352014-08-05 14:04:04 +10008278static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008279os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8280/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008281{
8282 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00008283 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008284 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07008285 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008286 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04008287 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008288 Py_END_ALLOW_THREADS
8289 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008290}
8291
8292
Larry Hastings2f936352014-08-05 14:04:04 +10008293/*[clinic input]
8294os.dup -> int
8295
8296 fd: int
8297 /
8298
8299Return a duplicate of a file descriptor.
8300[clinic start generated code]*/
8301
Larry Hastings2f936352014-08-05 14:04:04 +10008302static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008303os_dup_impl(PyObject *module, int fd)
8304/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008305{
8306 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008307}
8308
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008309
Larry Hastings2f936352014-08-05 14:04:04 +10008310/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008311os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008312 fd: int
8313 fd2: int
8314 inheritable: bool=True
8315
8316Duplicate file descriptor.
8317[clinic start generated code]*/
8318
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008319static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008320os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008321/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008322{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008323 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008324#if defined(HAVE_DUP3) && \
8325 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8326 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008327 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008328#endif
8329
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008330 if (fd < 0 || fd2 < 0) {
8331 posix_error();
8332 return -1;
8333 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008334
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008335 /* dup2() can fail with EINTR if the target FD is already open, because it
8336 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8337 * upon close(), and therefore below.
8338 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008339#ifdef MS_WINDOWS
8340 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008341 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008342 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008343 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008344 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008345 if (res < 0) {
8346 posix_error();
8347 return -1;
8348 }
8349 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008350
8351 /* Character files like console cannot be make non-inheritable */
8352 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8353 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008354 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008355 }
8356
8357#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8358 Py_BEGIN_ALLOW_THREADS
8359 if (!inheritable)
8360 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8361 else
8362 res = dup2(fd, fd2);
8363 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008364 if (res < 0) {
8365 posix_error();
8366 return -1;
8367 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008368
8369#else
8370
8371#ifdef HAVE_DUP3
8372 if (!inheritable && dup3_works != 0) {
8373 Py_BEGIN_ALLOW_THREADS
8374 res = dup3(fd, fd2, O_CLOEXEC);
8375 Py_END_ALLOW_THREADS
8376 if (res < 0) {
8377 if (dup3_works == -1)
8378 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008379 if (dup3_works) {
8380 posix_error();
8381 return -1;
8382 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008383 }
8384 }
8385
8386 if (inheritable || dup3_works == 0)
8387 {
8388#endif
8389 Py_BEGIN_ALLOW_THREADS
8390 res = dup2(fd, fd2);
8391 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008392 if (res < 0) {
8393 posix_error();
8394 return -1;
8395 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008396
8397 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8398 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008399 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008400 }
8401#ifdef HAVE_DUP3
8402 }
8403#endif
8404
8405#endif
8406
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008407 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008408}
8409
Larry Hastings2f936352014-08-05 14:04:04 +10008410
Ross Lagerwall7807c352011-03-17 20:20:30 +02008411#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008412/*[clinic input]
8413os.lockf
8414
8415 fd: int
8416 An open file descriptor.
8417 command: int
8418 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8419 length: Py_off_t
8420 The number of bytes to lock, starting at the current position.
8421 /
8422
8423Apply, test or remove a POSIX lock on an open file descriptor.
8424
8425[clinic start generated code]*/
8426
Larry Hastings2f936352014-08-05 14:04:04 +10008427static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008428os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8429/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008430{
8431 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008432
8433 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008434 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008435 Py_END_ALLOW_THREADS
8436
8437 if (res < 0)
8438 return posix_error();
8439
8440 Py_RETURN_NONE;
8441}
Larry Hastings2f936352014-08-05 14:04:04 +10008442#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008443
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008444
Larry Hastings2f936352014-08-05 14:04:04 +10008445/*[clinic input]
8446os.lseek -> Py_off_t
8447
8448 fd: int
8449 position: Py_off_t
8450 how: int
8451 /
8452
8453Set the position of a file descriptor. Return the new position.
8454
8455Return the new cursor position in number of bytes
8456relative to the beginning of the file.
8457[clinic start generated code]*/
8458
Larry Hastings2f936352014-08-05 14:04:04 +10008459static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008460os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8461/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008462{
8463 Py_off_t result;
8464
Guido van Rossum687dd131993-05-17 08:34:16 +00008465#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8467 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008468 case 0: how = SEEK_SET; break;
8469 case 1: how = SEEK_CUR; break;
8470 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008471 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008472#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008473
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008475 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008476#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008477 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008478#else
Larry Hastings2f936352014-08-05 14:04:04 +10008479 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008480#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008481 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008483 if (result < 0)
8484 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008485
Larry Hastings2f936352014-08-05 14:04:04 +10008486 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008487}
8488
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008489
Larry Hastings2f936352014-08-05 14:04:04 +10008490/*[clinic input]
8491os.read
8492 fd: int
8493 length: Py_ssize_t
8494 /
8495
8496Read from a file descriptor. Returns a bytes object.
8497[clinic start generated code]*/
8498
Larry Hastings2f936352014-08-05 14:04:04 +10008499static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008500os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8501/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008502{
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 Py_ssize_t n;
8504 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008505
8506 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 errno = EINVAL;
8508 return posix_error();
8509 }
Larry Hastings2f936352014-08-05 14:04:04 +10008510
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008511 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008512
8513 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008514 if (buffer == NULL)
8515 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008516
Victor Stinner66aab0c2015-03-19 22:53:20 +01008517 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8518 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008519 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008520 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 }
Larry Hastings2f936352014-08-05 14:04:04 +10008522
8523 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008524 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008525
Victor Stinner8c62be82010-05-06 00:08:46 +00008526 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008527}
8528
Ross Lagerwall7807c352011-03-17 20:20:30 +02008529#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008530 || defined(__APPLE__))) \
8531 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8532 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8533static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008534iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008535{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008536 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008537
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008538 *iov = PyMem_New(struct iovec, cnt);
8539 if (*iov == NULL) {
8540 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008541 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008542 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008543
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008544 *buf = PyMem_New(Py_buffer, cnt);
8545 if (*buf == NULL) {
8546 PyMem_Del(*iov);
8547 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008548 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008549 }
8550
8551 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008552 PyObject *item = PySequence_GetItem(seq, i);
8553 if (item == NULL)
8554 goto fail;
8555 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8556 Py_DECREF(item);
8557 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008558 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008559 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008560 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008561 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008562 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008563 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008564
8565fail:
8566 PyMem_Del(*iov);
8567 for (j = 0; j < i; j++) {
8568 PyBuffer_Release(&(*buf)[j]);
8569 }
8570 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008571 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008572}
8573
8574static void
8575iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8576{
8577 int i;
8578 PyMem_Del(iov);
8579 for (i = 0; i < cnt; i++) {
8580 PyBuffer_Release(&buf[i]);
8581 }
8582 PyMem_Del(buf);
8583}
8584#endif
8585
Larry Hastings2f936352014-08-05 14:04:04 +10008586
Ross Lagerwall7807c352011-03-17 20:20:30 +02008587#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008588/*[clinic input]
8589os.readv -> Py_ssize_t
8590
8591 fd: int
8592 buffers: object
8593 /
8594
8595Read from a file descriptor fd into an iterable of buffers.
8596
8597The buffers should be mutable buffers accepting bytes.
8598readv will transfer data into each buffer until it is full
8599and then move on to the next buffer in the sequence to hold
8600the rest of the data.
8601
8602readv returns the total number of bytes read,
8603which may be less than the total capacity of all the buffers.
8604[clinic start generated code]*/
8605
Larry Hastings2f936352014-08-05 14:04:04 +10008606static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008607os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8608/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008609{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008610 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008611 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008612 struct iovec *iov;
8613 Py_buffer *buf;
8614
Larry Hastings2f936352014-08-05 14:04:04 +10008615 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008616 PyErr_SetString(PyExc_TypeError,
8617 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008618 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008619 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008620
Larry Hastings2f936352014-08-05 14:04:04 +10008621 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008622 if (cnt < 0)
8623 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008624
8625 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8626 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008627
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008628 do {
8629 Py_BEGIN_ALLOW_THREADS
8630 n = readv(fd, iov, cnt);
8631 Py_END_ALLOW_THREADS
8632 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008633
8634 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008635 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008636 if (!async_err)
8637 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008638 return -1;
8639 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008640
Larry Hastings2f936352014-08-05 14:04:04 +10008641 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008642}
Larry Hastings2f936352014-08-05 14:04:04 +10008643#endif /* HAVE_READV */
8644
Ross Lagerwall7807c352011-03-17 20:20:30 +02008645
8646#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008647/*[clinic input]
8648# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8649os.pread
8650
8651 fd: int
8652 length: int
8653 offset: Py_off_t
8654 /
8655
8656Read a number of bytes from a file descriptor starting at a particular offset.
8657
8658Read length bytes from file descriptor fd, starting at offset bytes from
8659the beginning of the file. The file offset remains unchanged.
8660[clinic start generated code]*/
8661
Larry Hastings2f936352014-08-05 14:04:04 +10008662static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008663os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8664/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008665{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008666 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008667 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008668 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008669
Larry Hastings2f936352014-08-05 14:04:04 +10008670 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008671 errno = EINVAL;
8672 return posix_error();
8673 }
Larry Hastings2f936352014-08-05 14:04:04 +10008674 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008675 if (buffer == NULL)
8676 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008677
8678 do {
8679 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008680 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008681 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008682 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008683 Py_END_ALLOW_THREADS
8684 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8685
Ross Lagerwall7807c352011-03-17 20:20:30 +02008686 if (n < 0) {
8687 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008688 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008689 }
Larry Hastings2f936352014-08-05 14:04:04 +10008690 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008691 _PyBytes_Resize(&buffer, n);
8692 return buffer;
8693}
Larry Hastings2f936352014-08-05 14:04:04 +10008694#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008695
Pablo Galindo4defba32018-01-27 16:16:37 +00008696#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8697/*[clinic input]
8698os.preadv -> Py_ssize_t
8699
8700 fd: int
8701 buffers: object
8702 offset: Py_off_t
8703 flags: int = 0
8704 /
8705
8706Reads from a file descriptor into a number of mutable bytes-like objects.
8707
8708Combines the functionality of readv() and pread(). As readv(), it will
8709transfer data into each buffer until it is full and then move on to the next
8710buffer in the sequence to hold the rest of the data. Its fourth argument,
8711specifies the file offset at which the input operation is to be performed. It
8712will return the total number of bytes read (which can be less than the total
8713capacity of all the objects).
8714
8715The flags argument contains a bitwise OR of zero or more of the following flags:
8716
8717- RWF_HIPRI
8718- RWF_NOWAIT
8719
8720Using non-zero flags requires Linux 4.6 or newer.
8721[clinic start generated code]*/
8722
8723static Py_ssize_t
8724os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8725 int flags)
8726/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8727{
8728 Py_ssize_t cnt, n;
8729 int async_err = 0;
8730 struct iovec *iov;
8731 Py_buffer *buf;
8732
8733 if (!PySequence_Check(buffers)) {
8734 PyErr_SetString(PyExc_TypeError,
8735 "preadv2() arg 2 must be a sequence");
8736 return -1;
8737 }
8738
8739 cnt = PySequence_Size(buffers);
8740 if (cnt < 0) {
8741 return -1;
8742 }
8743
8744#ifndef HAVE_PREADV2
8745 if(flags != 0) {
8746 argument_unavailable_error("preadv2", "flags");
8747 return -1;
8748 }
8749#endif
8750
8751 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8752 return -1;
8753 }
8754#ifdef HAVE_PREADV2
8755 do {
8756 Py_BEGIN_ALLOW_THREADS
8757 _Py_BEGIN_SUPPRESS_IPH
8758 n = preadv2(fd, iov, cnt, offset, flags);
8759 _Py_END_SUPPRESS_IPH
8760 Py_END_ALLOW_THREADS
8761 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8762#else
8763 do {
8764 Py_BEGIN_ALLOW_THREADS
8765 _Py_BEGIN_SUPPRESS_IPH
8766 n = preadv(fd, iov, cnt, offset);
8767 _Py_END_SUPPRESS_IPH
8768 Py_END_ALLOW_THREADS
8769 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8770#endif
8771
8772 iov_cleanup(iov, buf, cnt);
8773 if (n < 0) {
8774 if (!async_err) {
8775 posix_error();
8776 }
8777 return -1;
8778 }
8779
8780 return n;
8781}
8782#endif /* HAVE_PREADV */
8783
Larry Hastings2f936352014-08-05 14:04:04 +10008784
8785/*[clinic input]
8786os.write -> Py_ssize_t
8787
8788 fd: int
8789 data: Py_buffer
8790 /
8791
8792Write a bytes object to a file descriptor.
8793[clinic start generated code]*/
8794
Larry Hastings2f936352014-08-05 14:04:04 +10008795static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008796os_write_impl(PyObject *module, int fd, Py_buffer *data)
8797/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008798{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008799 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008800}
8801
8802#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008803PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008804"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008805sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008806 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008807Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008808
Larry Hastings2f936352014-08-05 14:04:04 +10008809/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008810static PyObject *
8811posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8812{
8813 int in, out;
8814 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008815 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008816 off_t offset;
8817
8818#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8819#ifndef __APPLE__
8820 Py_ssize_t len;
8821#endif
8822 PyObject *headers = NULL, *trailers = NULL;
8823 Py_buffer *hbuf, *tbuf;
8824 off_t sbytes;
8825 struct sf_hdtr sf;
8826 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008827 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008828 static char *keywords[] = {"out", "in",
8829 "offset", "count",
8830 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008831
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008832 sf.headers = NULL;
8833 sf.trailers = NULL;
8834
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008835#ifdef __APPLE__
8836 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008837 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008838#else
8839 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008840 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008841#endif
8842 &headers, &trailers, &flags))
8843 return NULL;
8844 if (headers != NULL) {
8845 if (!PySequence_Check(headers)) {
8846 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008847 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008848 return NULL;
8849 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008850 Py_ssize_t i = PySequence_Size(headers);
8851 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008852 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008853 if (i > INT_MAX) {
8854 PyErr_SetString(PyExc_OverflowError,
8855 "sendfile() header is too large");
8856 return NULL;
8857 }
8858 if (i > 0) {
8859 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008860 if (iov_setup(&(sf.headers), &hbuf,
8861 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008862 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008863#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008864 for (i = 0; i < sf.hdr_cnt; i++) {
8865 Py_ssize_t blen = sf.headers[i].iov_len;
8866# define OFF_T_MAX 0x7fffffffffffffff
8867 if (sbytes >= OFF_T_MAX - blen) {
8868 PyErr_SetString(PyExc_OverflowError,
8869 "sendfile() header is too large");
8870 return NULL;
8871 }
8872 sbytes += blen;
8873 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008874#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008875 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008876 }
8877 }
8878 if (trailers != NULL) {
8879 if (!PySequence_Check(trailers)) {
8880 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008881 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008882 return NULL;
8883 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008884 Py_ssize_t i = PySequence_Size(trailers);
8885 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008886 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008887 if (i > INT_MAX) {
8888 PyErr_SetString(PyExc_OverflowError,
8889 "sendfile() trailer is too large");
8890 return NULL;
8891 }
8892 if (i > 0) {
8893 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008894 if (iov_setup(&(sf.trailers), &tbuf,
8895 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008896 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008897 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008898 }
8899 }
8900
Steve Dower8fc89802015-04-12 00:26:27 -04008901 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008902 do {
8903 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008904#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008905 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008906#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008907 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008908#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008909 Py_END_ALLOW_THREADS
8910 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008911 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008912
8913 if (sf.headers != NULL)
8914 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8915 if (sf.trailers != NULL)
8916 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8917
8918 if (ret < 0) {
8919 if ((errno == EAGAIN) || (errno == EBUSY)) {
8920 if (sbytes != 0) {
8921 // some data has been sent
8922 goto done;
8923 }
8924 else {
8925 // no data has been sent; upper application is supposed
8926 // to retry on EAGAIN or EBUSY
8927 return posix_error();
8928 }
8929 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008930 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008931 }
8932 goto done;
8933
8934done:
8935 #if !defined(HAVE_LARGEFILE_SUPPORT)
8936 return Py_BuildValue("l", sbytes);
8937 #else
8938 return Py_BuildValue("L", sbytes);
8939 #endif
8940
8941#else
8942 Py_ssize_t count;
8943 PyObject *offobj;
8944 static char *keywords[] = {"out", "in",
8945 "offset", "count", NULL};
8946 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8947 keywords, &out, &in, &offobj, &count))
8948 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07008949#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008950 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008951 do {
8952 Py_BEGIN_ALLOW_THREADS
8953 ret = sendfile(out, in, NULL, count);
8954 Py_END_ALLOW_THREADS
8955 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008956 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008957 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008958 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008959 }
8960#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008961 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008962 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008963
8964 do {
8965 Py_BEGIN_ALLOW_THREADS
8966 ret = sendfile(out, in, &offset, count);
8967 Py_END_ALLOW_THREADS
8968 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008969 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008970 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008971 return Py_BuildValue("n", ret);
8972#endif
8973}
Larry Hastings2f936352014-08-05 14:04:04 +10008974#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008975
Larry Hastings2f936352014-08-05 14:04:04 +10008976
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008977#if defined(__APPLE__)
8978/*[clinic input]
8979os._fcopyfile
8980
8981 infd: int
8982 outfd: int
8983 flags: int
8984 /
8985
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07008986Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008987[clinic start generated code]*/
8988
8989static PyObject *
8990os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07008991/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008992{
8993 int ret;
8994
8995 Py_BEGIN_ALLOW_THREADS
8996 ret = fcopyfile(infd, outfd, NULL, flags);
8997 Py_END_ALLOW_THREADS
8998 if (ret < 0)
8999 return posix_error();
9000 Py_RETURN_NONE;
9001}
9002#endif
9003
9004
Larry Hastings2f936352014-08-05 14:04:04 +10009005/*[clinic input]
9006os.fstat
9007
9008 fd : int
9009
9010Perform a stat system call on the given file descriptor.
9011
9012Like stat(), but for an open file descriptor.
9013Equivalent to os.stat(fd).
9014[clinic start generated code]*/
9015
Larry Hastings2f936352014-08-05 14:04:04 +10009016static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009017os_fstat_impl(PyObject *module, int fd)
9018/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009019{
Victor Stinner8c62be82010-05-06 00:08:46 +00009020 STRUCT_STAT st;
9021 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009022 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009023
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009024 do {
9025 Py_BEGIN_ALLOW_THREADS
9026 res = FSTAT(fd, &st);
9027 Py_END_ALLOW_THREADS
9028 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009029 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009030#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009031 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009032#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009033 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009034#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009035 }
Tim Peters5aa91602002-01-30 05:46:57 +00009036
Victor Stinner4195b5c2012-02-08 23:03:19 +01009037 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009038}
9039
Larry Hastings2f936352014-08-05 14:04:04 +10009040
9041/*[clinic input]
9042os.isatty -> bool
9043 fd: int
9044 /
9045
9046Return True if the fd is connected to a terminal.
9047
9048Return True if the file descriptor is an open file descriptor
9049connected to the slave end of a terminal.
9050[clinic start generated code]*/
9051
Larry Hastings2f936352014-08-05 14:04:04 +10009052static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009053os_isatty_impl(PyObject *module, int fd)
9054/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009055{
Steve Dower8fc89802015-04-12 00:26:27 -04009056 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009057 _Py_BEGIN_SUPPRESS_IPH
9058 return_value = isatty(fd);
9059 _Py_END_SUPPRESS_IPH
9060 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009061}
9062
9063
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009064#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009065/*[clinic input]
9066os.pipe
9067
9068Create a pipe.
9069
9070Returns a tuple of two file descriptors:
9071 (read_fd, write_fd)
9072[clinic start generated code]*/
9073
Larry Hastings2f936352014-08-05 14:04:04 +10009074static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009075os_pipe_impl(PyObject *module)
9076/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009077{
Victor Stinner8c62be82010-05-06 00:08:46 +00009078 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009079#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009080 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009081 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009082 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009083#else
9084 int res;
9085#endif
9086
9087#ifdef MS_WINDOWS
9088 attr.nLength = sizeof(attr);
9089 attr.lpSecurityDescriptor = NULL;
9090 attr.bInheritHandle = FALSE;
9091
9092 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009093 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009094 ok = CreatePipe(&read, &write, &attr, 0);
9095 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009096 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9097 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009098 if (fds[0] == -1 || fds[1] == -1) {
9099 CloseHandle(read);
9100 CloseHandle(write);
9101 ok = 0;
9102 }
9103 }
Steve Dowerc3630612016-11-19 18:41:16 -08009104 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009105 Py_END_ALLOW_THREADS
9106
Victor Stinner8c62be82010-05-06 00:08:46 +00009107 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009108 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009109#else
9110
9111#ifdef HAVE_PIPE2
9112 Py_BEGIN_ALLOW_THREADS
9113 res = pipe2(fds, O_CLOEXEC);
9114 Py_END_ALLOW_THREADS
9115
9116 if (res != 0 && errno == ENOSYS)
9117 {
9118#endif
9119 Py_BEGIN_ALLOW_THREADS
9120 res = pipe(fds);
9121 Py_END_ALLOW_THREADS
9122
9123 if (res == 0) {
9124 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9125 close(fds[0]);
9126 close(fds[1]);
9127 return NULL;
9128 }
9129 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9130 close(fds[0]);
9131 close(fds[1]);
9132 return NULL;
9133 }
9134 }
9135#ifdef HAVE_PIPE2
9136 }
9137#endif
9138
9139 if (res != 0)
9140 return PyErr_SetFromErrno(PyExc_OSError);
9141#endif /* !MS_WINDOWS */
9142 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009143}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009144#endif /* HAVE_PIPE */
9145
Larry Hastings2f936352014-08-05 14:04:04 +10009146
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009147#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009148/*[clinic input]
9149os.pipe2
9150
9151 flags: int
9152 /
9153
9154Create a pipe with flags set atomically.
9155
9156Returns a tuple of two file descriptors:
9157 (read_fd, write_fd)
9158
9159flags can be constructed by ORing together one or more of these values:
9160O_NONBLOCK, O_CLOEXEC.
9161[clinic start generated code]*/
9162
Larry Hastings2f936352014-08-05 14:04:04 +10009163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009164os_pipe2_impl(PyObject *module, int flags)
9165/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009166{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009167 int fds[2];
9168 int res;
9169
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009170 res = pipe2(fds, flags);
9171 if (res != 0)
9172 return posix_error();
9173 return Py_BuildValue("(ii)", fds[0], fds[1]);
9174}
9175#endif /* HAVE_PIPE2 */
9176
Larry Hastings2f936352014-08-05 14:04:04 +10009177
Ross Lagerwall7807c352011-03-17 20:20:30 +02009178#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009179/*[clinic input]
9180os.writev -> Py_ssize_t
9181 fd: int
9182 buffers: object
9183 /
9184
9185Iterate over buffers, and write the contents of each to a file descriptor.
9186
9187Returns the total number of bytes written.
9188buffers must be a sequence of bytes-like objects.
9189[clinic start generated code]*/
9190
Larry Hastings2f936352014-08-05 14:04:04 +10009191static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009192os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9193/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009194{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009195 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009196 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009197 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009198 struct iovec *iov;
9199 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009200
9201 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009202 PyErr_SetString(PyExc_TypeError,
9203 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009204 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009205 }
Larry Hastings2f936352014-08-05 14:04:04 +10009206 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009207 if (cnt < 0)
9208 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009209
Larry Hastings2f936352014-08-05 14:04:04 +10009210 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9211 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009212 }
9213
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009214 do {
9215 Py_BEGIN_ALLOW_THREADS
9216 result = writev(fd, iov, cnt);
9217 Py_END_ALLOW_THREADS
9218 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009219
9220 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009221 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009222 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009223
Georg Brandl306336b2012-06-24 12:55:33 +02009224 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009225}
Larry Hastings2f936352014-08-05 14:04:04 +10009226#endif /* HAVE_WRITEV */
9227
9228
9229#ifdef HAVE_PWRITE
9230/*[clinic input]
9231os.pwrite -> Py_ssize_t
9232
9233 fd: int
9234 buffer: Py_buffer
9235 offset: Py_off_t
9236 /
9237
9238Write bytes to a file descriptor starting at a particular offset.
9239
9240Write buffer to fd, starting at offset bytes from the beginning of
9241the file. Returns the number of bytes writte. Does not change the
9242current file offset.
9243[clinic start generated code]*/
9244
Larry Hastings2f936352014-08-05 14:04:04 +10009245static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009246os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9247/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009248{
9249 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009250 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009251
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009252 do {
9253 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009254 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009255 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009256 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009257 Py_END_ALLOW_THREADS
9258 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009259
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009260 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009261 posix_error();
9262 return size;
9263}
9264#endif /* HAVE_PWRITE */
9265
Pablo Galindo4defba32018-01-27 16:16:37 +00009266#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9267/*[clinic input]
9268os.pwritev -> Py_ssize_t
9269
9270 fd: int
9271 buffers: object
9272 offset: Py_off_t
9273 flags: int = 0
9274 /
9275
9276Writes the contents of bytes-like objects to a file descriptor at a given offset.
9277
9278Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9279of bytes-like objects. Buffers are processed in array order. Entire contents of first
9280buffer is written before proceeding to second, and so on. The operating system may
9281set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9282This function writes the contents of each object to the file descriptor and returns
9283the total number of bytes written.
9284
9285The flags argument contains a bitwise OR of zero or more of the following flags:
9286
9287- RWF_DSYNC
9288- RWF_SYNC
9289
9290Using non-zero flags requires Linux 4.7 or newer.
9291[clinic start generated code]*/
9292
9293static Py_ssize_t
9294os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9295 int flags)
9296/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9297{
9298 Py_ssize_t cnt;
9299 Py_ssize_t result;
9300 int async_err = 0;
9301 struct iovec *iov;
9302 Py_buffer *buf;
9303
9304 if (!PySequence_Check(buffers)) {
9305 PyErr_SetString(PyExc_TypeError,
9306 "pwritev() arg 2 must be a sequence");
9307 return -1;
9308 }
9309
9310 cnt = PySequence_Size(buffers);
9311 if (cnt < 0) {
9312 return -1;
9313 }
9314
9315#ifndef HAVE_PWRITEV2
9316 if(flags != 0) {
9317 argument_unavailable_error("pwritev2", "flags");
9318 return -1;
9319 }
9320#endif
9321
9322 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9323 return -1;
9324 }
9325#ifdef HAVE_PWRITEV2
9326 do {
9327 Py_BEGIN_ALLOW_THREADS
9328 _Py_BEGIN_SUPPRESS_IPH
9329 result = pwritev2(fd, iov, cnt, offset, flags);
9330 _Py_END_SUPPRESS_IPH
9331 Py_END_ALLOW_THREADS
9332 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9333#else
9334 do {
9335 Py_BEGIN_ALLOW_THREADS
9336 _Py_BEGIN_SUPPRESS_IPH
9337 result = pwritev(fd, iov, cnt, offset);
9338 _Py_END_SUPPRESS_IPH
9339 Py_END_ALLOW_THREADS
9340 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9341#endif
9342
9343 iov_cleanup(iov, buf, cnt);
9344 if (result < 0) {
9345 if (!async_err) {
9346 posix_error();
9347 }
9348 return -1;
9349 }
9350
9351 return result;
9352}
9353#endif /* HAVE_PWRITEV */
9354
9355
9356
Larry Hastings2f936352014-08-05 14:04:04 +10009357
9358#ifdef HAVE_MKFIFO
9359/*[clinic input]
9360os.mkfifo
9361
9362 path: path_t
9363 mode: int=0o666
9364 *
9365 dir_fd: dir_fd(requires='mkfifoat')=None
9366
9367Create a "fifo" (a POSIX named pipe).
9368
9369If dir_fd is not None, it should be a file descriptor open to a directory,
9370 and path should be relative; path will then be relative to that directory.
9371dir_fd may not be implemented on your platform.
9372 If it is unavailable, using it will raise a NotImplementedError.
9373[clinic start generated code]*/
9374
Larry Hastings2f936352014-08-05 14:04:04 +10009375static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009376os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9377/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009378{
9379 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009380 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009381
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009382 do {
9383 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009384#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009385 if (dir_fd != DEFAULT_DIR_FD)
9386 result = mkfifoat(dir_fd, path->narrow, mode);
9387 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009388#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009389 result = mkfifo(path->narrow, mode);
9390 Py_END_ALLOW_THREADS
9391 } while (result != 0 && errno == EINTR &&
9392 !(async_err = PyErr_CheckSignals()));
9393 if (result != 0)
9394 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009395
9396 Py_RETURN_NONE;
9397}
9398#endif /* HAVE_MKFIFO */
9399
9400
9401#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9402/*[clinic input]
9403os.mknod
9404
9405 path: path_t
9406 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009407 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009408 *
9409 dir_fd: dir_fd(requires='mknodat')=None
9410
9411Create a node in the file system.
9412
9413Create a node in the file system (file, device special file or named pipe)
9414at path. mode specifies both the permissions to use and the
9415type of node to be created, being combined (bitwise OR) with one of
9416S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9417device defines the newly created device special file (probably using
9418os.makedev()). Otherwise device is ignored.
9419
9420If dir_fd is not None, it should be a file descriptor open to a directory,
9421 and path should be relative; path will then be relative to that directory.
9422dir_fd may not be implemented on your platform.
9423 If it is unavailable, using it will raise a NotImplementedError.
9424[clinic start generated code]*/
9425
Larry Hastings2f936352014-08-05 14:04:04 +10009426static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009427os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009428 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009429/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009430{
9431 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009432 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009433
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009434 do {
9435 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009436#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009437 if (dir_fd != DEFAULT_DIR_FD)
9438 result = mknodat(dir_fd, path->narrow, mode, device);
9439 else
Larry Hastings2f936352014-08-05 14:04:04 +10009440#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009441 result = mknod(path->narrow, mode, device);
9442 Py_END_ALLOW_THREADS
9443 } while (result != 0 && errno == EINTR &&
9444 !(async_err = PyErr_CheckSignals()));
9445 if (result != 0)
9446 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009447
9448 Py_RETURN_NONE;
9449}
9450#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9451
9452
9453#ifdef HAVE_DEVICE_MACROS
9454/*[clinic input]
9455os.major -> unsigned_int
9456
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009457 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009458 /
9459
9460Extracts a device major number from a raw device number.
9461[clinic start generated code]*/
9462
Larry Hastings2f936352014-08-05 14:04:04 +10009463static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009464os_major_impl(PyObject *module, dev_t device)
9465/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009466{
9467 return major(device);
9468}
9469
9470
9471/*[clinic input]
9472os.minor -> unsigned_int
9473
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009474 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009475 /
9476
9477Extracts a device minor number from a raw device number.
9478[clinic start generated code]*/
9479
Larry Hastings2f936352014-08-05 14:04:04 +10009480static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009481os_minor_impl(PyObject *module, dev_t device)
9482/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009483{
9484 return minor(device);
9485}
9486
9487
9488/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009489os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009490
9491 major: int
9492 minor: int
9493 /
9494
9495Composes a raw device number from the major and minor device numbers.
9496[clinic start generated code]*/
9497
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009498static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009499os_makedev_impl(PyObject *module, int major, int minor)
9500/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009501{
9502 return makedev(major, minor);
9503}
9504#endif /* HAVE_DEVICE_MACROS */
9505
9506
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009507#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009508/*[clinic input]
9509os.ftruncate
9510
9511 fd: int
9512 length: Py_off_t
9513 /
9514
9515Truncate a file, specified by file descriptor, to a specific length.
9516[clinic start generated code]*/
9517
Larry Hastings2f936352014-08-05 14:04:04 +10009518static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009519os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9520/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009521{
9522 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009523 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009524
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009525 do {
9526 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009527 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009528#ifdef MS_WINDOWS
9529 result = _chsize_s(fd, length);
9530#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009531 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009532#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009533 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009534 Py_END_ALLOW_THREADS
9535 } while (result != 0 && errno == EINTR &&
9536 !(async_err = PyErr_CheckSignals()));
9537 if (result != 0)
9538 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009539 Py_RETURN_NONE;
9540}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009541#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009542
9543
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009544#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009545/*[clinic input]
9546os.truncate
9547 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9548 length: Py_off_t
9549
9550Truncate a file, specified by path, to a specific length.
9551
9552On some platforms, path may also be specified as an open file descriptor.
9553 If this functionality is unavailable, using it raises an exception.
9554[clinic start generated code]*/
9555
Larry Hastings2f936352014-08-05 14:04:04 +10009556static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009557os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9558/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009559{
9560 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009561#ifdef MS_WINDOWS
9562 int fd;
9563#endif
9564
9565 if (path->fd != -1)
9566 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009567
9568 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009569 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009570#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009571 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009572 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009573 result = -1;
9574 else {
9575 result = _chsize_s(fd, length);
9576 close(fd);
9577 if (result < 0)
9578 errno = result;
9579 }
9580#else
9581 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009582#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009583 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009584 Py_END_ALLOW_THREADS
9585 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009586 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009587
9588 Py_RETURN_NONE;
9589}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009590#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009591
Ross Lagerwall7807c352011-03-17 20:20:30 +02009592
Victor Stinnerd6b17692014-09-30 12:20:05 +02009593/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9594 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9595 defined, which is the case in Python on AIX. AIX bug report:
9596 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9597#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9598# define POSIX_FADVISE_AIX_BUG
9599#endif
9600
Victor Stinnerec39e262014-09-30 12:35:58 +02009601
Victor Stinnerd6b17692014-09-30 12:20:05 +02009602#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009603/*[clinic input]
9604os.posix_fallocate
9605
9606 fd: int
9607 offset: Py_off_t
9608 length: Py_off_t
9609 /
9610
9611Ensure a file has allocated at least a particular number of bytes on disk.
9612
9613Ensure that the file specified by fd encompasses a range of bytes
9614starting at offset bytes from the beginning and continuing for length bytes.
9615[clinic start generated code]*/
9616
Larry Hastings2f936352014-08-05 14:04:04 +10009617static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009618os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009619 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009620/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009621{
9622 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009623 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009624
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009625 do {
9626 Py_BEGIN_ALLOW_THREADS
9627 result = posix_fallocate(fd, offset, length);
9628 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009629 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9630
9631 if (result == 0)
9632 Py_RETURN_NONE;
9633
9634 if (async_err)
9635 return NULL;
9636
9637 errno = result;
9638 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009639}
Victor Stinnerec39e262014-09-30 12:35:58 +02009640#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009641
Ross Lagerwall7807c352011-03-17 20:20:30 +02009642
Victor Stinnerd6b17692014-09-30 12:20:05 +02009643#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009644/*[clinic input]
9645os.posix_fadvise
9646
9647 fd: int
9648 offset: Py_off_t
9649 length: Py_off_t
9650 advice: int
9651 /
9652
9653Announce an intention to access data in a specific pattern.
9654
9655Announce an intention to access data in a specific pattern, thus allowing
9656the kernel to make optimizations.
9657The advice applies to the region of the file specified by fd starting at
9658offset and continuing for length bytes.
9659advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9660POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9661POSIX_FADV_DONTNEED.
9662[clinic start generated code]*/
9663
Larry Hastings2f936352014-08-05 14:04:04 +10009664static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009665os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009666 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009667/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009668{
9669 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009670 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009671
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009672 do {
9673 Py_BEGIN_ALLOW_THREADS
9674 result = posix_fadvise(fd, offset, length, advice);
9675 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009676 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9677
9678 if (result == 0)
9679 Py_RETURN_NONE;
9680
9681 if (async_err)
9682 return NULL;
9683
9684 errno = result;
9685 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009686}
Victor Stinnerec39e262014-09-30 12:35:58 +02009687#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009688
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009689#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009690
Fred Drake762e2061999-08-26 17:23:54 +00009691/* Save putenv() parameters as values here, so we can collect them when they
9692 * get re-set with another call for the same key. */
9693static PyObject *posix_putenv_garbage;
9694
Larry Hastings2f936352014-08-05 14:04:04 +10009695static void
9696posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009697{
Larry Hastings2f936352014-08-05 14:04:04 +10009698 /* Install the first arg and newstr in posix_putenv_garbage;
9699 * this will cause previous value to be collected. This has to
9700 * happen after the real putenv() call because the old value
9701 * was still accessible until then. */
9702 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9703 /* really not much we can do; just leak */
9704 PyErr_Clear();
9705 else
9706 Py_DECREF(value);
9707}
9708
9709
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009710#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009711/*[clinic input]
9712os.putenv
9713
9714 name: unicode
9715 value: unicode
9716 /
9717
9718Change or add an environment variable.
9719[clinic start generated code]*/
9720
Larry Hastings2f936352014-08-05 14:04:04 +10009721static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009722os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9723/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009724{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009725 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009726 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009727
Serhiy Storchaka77703942017-06-25 07:33:01 +03009728 /* Search from index 1 because on Windows starting '=' is allowed for
9729 defining hidden environment variables. */
9730 if (PyUnicode_GET_LENGTH(name) == 0 ||
9731 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9732 {
9733 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9734 return NULL;
9735 }
Larry Hastings2f936352014-08-05 14:04:04 +10009736 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9737 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009738 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009739 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009740
9741 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9742 if (env == NULL)
9743 goto error;
9744 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009745 PyErr_Format(PyExc_ValueError,
9746 "the environment variable is longer than %u characters",
9747 _MAX_ENV);
9748 goto error;
9749 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009750 if (wcslen(env) != (size_t)size) {
9751 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009752 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009753 }
9754
Larry Hastings2f936352014-08-05 14:04:04 +10009755 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009756 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009757 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009758 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009759
Larry Hastings2f936352014-08-05 14:04:04 +10009760 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009761 Py_RETURN_NONE;
9762
9763error:
Larry Hastings2f936352014-08-05 14:04:04 +10009764 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009765 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009766}
Larry Hastings2f936352014-08-05 14:04:04 +10009767#else /* MS_WINDOWS */
9768/*[clinic input]
9769os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009770
Larry Hastings2f936352014-08-05 14:04:04 +10009771 name: FSConverter
9772 value: FSConverter
9773 /
9774
9775Change or add an environment variable.
9776[clinic start generated code]*/
9777
Larry Hastings2f936352014-08-05 14:04:04 +10009778static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009779os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9780/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009781{
9782 PyObject *bytes = NULL;
9783 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009784 const char *name_string = PyBytes_AS_STRING(name);
9785 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009786
Serhiy Storchaka77703942017-06-25 07:33:01 +03009787 if (strchr(name_string, '=') != NULL) {
9788 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9789 return NULL;
9790 }
Larry Hastings2f936352014-08-05 14:04:04 +10009791 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9792 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009793 return NULL;
9794 }
9795
9796 env = PyBytes_AS_STRING(bytes);
9797 if (putenv(env)) {
9798 Py_DECREF(bytes);
9799 return posix_error();
9800 }
9801
9802 posix_putenv_garbage_setitem(name, bytes);
9803 Py_RETURN_NONE;
9804}
9805#endif /* MS_WINDOWS */
9806#endif /* HAVE_PUTENV */
9807
9808
9809#ifdef HAVE_UNSETENV
9810/*[clinic input]
9811os.unsetenv
9812 name: FSConverter
9813 /
9814
9815Delete an environment variable.
9816[clinic start generated code]*/
9817
Larry Hastings2f936352014-08-05 14:04:04 +10009818static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009819os_unsetenv_impl(PyObject *module, PyObject *name)
9820/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009821{
Victor Stinner984890f2011-11-24 13:53:38 +01009822#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009823 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009824#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009825
Victor Stinner984890f2011-11-24 13:53:38 +01009826#ifdef HAVE_BROKEN_UNSETENV
9827 unsetenv(PyBytes_AS_STRING(name));
9828#else
Victor Stinner65170952011-11-22 22:16:17 +01009829 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009830 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009831 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009832#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009833
Victor Stinner8c62be82010-05-06 00:08:46 +00009834 /* Remove the key from posix_putenv_garbage;
9835 * this will cause it to be collected. This has to
9836 * happen after the real unsetenv() call because the
9837 * old value was still accessible until then.
9838 */
Victor Stinner65170952011-11-22 22:16:17 +01009839 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009840 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02009841 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
9842 return NULL;
9843 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009844 PyErr_Clear();
9845 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009846 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009847}
Larry Hastings2f936352014-08-05 14:04:04 +10009848#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009849
Larry Hastings2f936352014-08-05 14:04:04 +10009850
9851/*[clinic input]
9852os.strerror
9853
9854 code: int
9855 /
9856
9857Translate an error code to a message string.
9858[clinic start generated code]*/
9859
Larry Hastings2f936352014-08-05 14:04:04 +10009860static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009861os_strerror_impl(PyObject *module, int code)
9862/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009863{
9864 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009865 if (message == NULL) {
9866 PyErr_SetString(PyExc_ValueError,
9867 "strerror() argument out of range");
9868 return NULL;
9869 }
Victor Stinner1b579672011-12-17 05:47:23 +01009870 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009871}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009872
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009873
Guido van Rossumc9641791998-08-04 15:26:23 +00009874#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009875#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009876/*[clinic input]
9877os.WCOREDUMP -> bool
9878
9879 status: int
9880 /
9881
9882Return True if the process returning status was dumped to a core file.
9883[clinic start generated code]*/
9884
Larry Hastings2f936352014-08-05 14:04:04 +10009885static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009886os_WCOREDUMP_impl(PyObject *module, int status)
9887/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009888{
9889 WAIT_TYPE wait_status;
9890 WAIT_STATUS_INT(wait_status) = status;
9891 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009892}
9893#endif /* WCOREDUMP */
9894
Larry Hastings2f936352014-08-05 14:04:04 +10009895
Fred Drake106c1a02002-04-23 15:58:02 +00009896#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009897/*[clinic input]
9898os.WIFCONTINUED -> bool
9899
9900 status: int
9901
9902Return True if a particular process was continued from a job control stop.
9903
9904Return True if the process returning status was continued from a
9905job control stop.
9906[clinic start generated code]*/
9907
Larry Hastings2f936352014-08-05 14:04:04 +10009908static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009909os_WIFCONTINUED_impl(PyObject *module, int status)
9910/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009911{
9912 WAIT_TYPE wait_status;
9913 WAIT_STATUS_INT(wait_status) = status;
9914 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009915}
9916#endif /* WIFCONTINUED */
9917
Larry Hastings2f936352014-08-05 14:04:04 +10009918
Guido van Rossumc9641791998-08-04 15:26:23 +00009919#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009920/*[clinic input]
9921os.WIFSTOPPED -> bool
9922
9923 status: int
9924
9925Return True if the process returning status was stopped.
9926[clinic start generated code]*/
9927
Larry Hastings2f936352014-08-05 14:04:04 +10009928static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009929os_WIFSTOPPED_impl(PyObject *module, int status)
9930/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009931{
9932 WAIT_TYPE wait_status;
9933 WAIT_STATUS_INT(wait_status) = status;
9934 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009935}
9936#endif /* WIFSTOPPED */
9937
Larry Hastings2f936352014-08-05 14:04:04 +10009938
Guido van Rossumc9641791998-08-04 15:26:23 +00009939#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009940/*[clinic input]
9941os.WIFSIGNALED -> bool
9942
9943 status: int
9944
9945Return True if the process returning status was terminated by a signal.
9946[clinic start generated code]*/
9947
Larry Hastings2f936352014-08-05 14:04:04 +10009948static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009949os_WIFSIGNALED_impl(PyObject *module, int status)
9950/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009951{
9952 WAIT_TYPE wait_status;
9953 WAIT_STATUS_INT(wait_status) = status;
9954 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009955}
9956#endif /* WIFSIGNALED */
9957
Larry Hastings2f936352014-08-05 14:04:04 +10009958
Guido van Rossumc9641791998-08-04 15:26:23 +00009959#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009960/*[clinic input]
9961os.WIFEXITED -> bool
9962
9963 status: int
9964
9965Return True if the process returning status exited via the exit() system call.
9966[clinic start generated code]*/
9967
Larry Hastings2f936352014-08-05 14:04:04 +10009968static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009969os_WIFEXITED_impl(PyObject *module, int status)
9970/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009971{
9972 WAIT_TYPE wait_status;
9973 WAIT_STATUS_INT(wait_status) = status;
9974 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009975}
9976#endif /* WIFEXITED */
9977
Larry Hastings2f936352014-08-05 14:04:04 +10009978
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009979#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009980/*[clinic input]
9981os.WEXITSTATUS -> int
9982
9983 status: int
9984
9985Return the process return code from status.
9986[clinic start generated code]*/
9987
Larry Hastings2f936352014-08-05 14:04:04 +10009988static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009989os_WEXITSTATUS_impl(PyObject *module, int status)
9990/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009991{
9992 WAIT_TYPE wait_status;
9993 WAIT_STATUS_INT(wait_status) = status;
9994 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009995}
9996#endif /* WEXITSTATUS */
9997
Larry Hastings2f936352014-08-05 14:04:04 +10009998
Guido van Rossumc9641791998-08-04 15:26:23 +00009999#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010000/*[clinic input]
10001os.WTERMSIG -> int
10002
10003 status: int
10004
10005Return the signal that terminated the process that provided the status value.
10006[clinic start generated code]*/
10007
Larry Hastings2f936352014-08-05 14:04:04 +100010008static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010009os_WTERMSIG_impl(PyObject *module, int status)
10010/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010011{
10012 WAIT_TYPE wait_status;
10013 WAIT_STATUS_INT(wait_status) = status;
10014 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010015}
10016#endif /* WTERMSIG */
10017
Larry Hastings2f936352014-08-05 14:04:04 +100010018
Guido van Rossumc9641791998-08-04 15:26:23 +000010019#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010020/*[clinic input]
10021os.WSTOPSIG -> int
10022
10023 status: int
10024
10025Return the signal that stopped the process that provided the status value.
10026[clinic start generated code]*/
10027
Larry Hastings2f936352014-08-05 14:04:04 +100010028static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010029os_WSTOPSIG_impl(PyObject *module, int status)
10030/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010031{
10032 WAIT_TYPE wait_status;
10033 WAIT_STATUS_INT(wait_status) = status;
10034 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010035}
10036#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010037#endif /* HAVE_SYS_WAIT_H */
10038
10039
Thomas Wouters477c8d52006-05-27 19:21:47 +000010040#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010041#ifdef _SCO_DS
10042/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10043 needed definitions in sys/statvfs.h */
10044#define _SVID3
10045#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010046#include <sys/statvfs.h>
10047
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010048static PyObject*
10049_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010050 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010051 if (v == NULL)
10052 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010053
10054#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010055 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10056 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10057 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10058 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10059 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10060 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10061 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10062 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10063 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10064 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010065#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010066 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10067 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10068 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010069 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010070 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010071 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010072 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010073 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010074 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010075 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010076 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010077 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010079 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010080 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10081 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010082#endif
Michael Felt502d5512018-01-05 13:01:58 +010010083/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10084 * (issue #32390). */
10085#if defined(_AIX) && defined(_ALL_SOURCE)
10086 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10087#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010088 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010089#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010090 if (PyErr_Occurred()) {
10091 Py_DECREF(v);
10092 return NULL;
10093 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010094
Victor Stinner8c62be82010-05-06 00:08:46 +000010095 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010096}
10097
Larry Hastings2f936352014-08-05 14:04:04 +100010098
10099/*[clinic input]
10100os.fstatvfs
10101 fd: int
10102 /
10103
10104Perform an fstatvfs system call on the given fd.
10105
10106Equivalent to statvfs(fd).
10107[clinic start generated code]*/
10108
Larry Hastings2f936352014-08-05 14:04:04 +100010109static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010110os_fstatvfs_impl(PyObject *module, int fd)
10111/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010112{
10113 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010114 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010115 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010116
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010117 do {
10118 Py_BEGIN_ALLOW_THREADS
10119 result = fstatvfs(fd, &st);
10120 Py_END_ALLOW_THREADS
10121 } while (result != 0 && errno == EINTR &&
10122 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010123 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010124 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010125
Victor Stinner8c62be82010-05-06 00:08:46 +000010126 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010127}
Larry Hastings2f936352014-08-05 14:04:04 +100010128#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010129
10130
Thomas Wouters477c8d52006-05-27 19:21:47 +000010131#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010132#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010133/*[clinic input]
10134os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010135
Larry Hastings2f936352014-08-05 14:04:04 +100010136 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10137
10138Perform a statvfs system call on the given path.
10139
10140path may always be specified as a string.
10141On some platforms, path may also be specified as an open file descriptor.
10142 If this functionality is unavailable, using it raises an exception.
10143[clinic start generated code]*/
10144
Larry Hastings2f936352014-08-05 14:04:04 +100010145static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010146os_statvfs_impl(PyObject *module, path_t *path)
10147/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010148{
10149 int result;
10150 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010151
10152 Py_BEGIN_ALLOW_THREADS
10153#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010154 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010155#ifdef __APPLE__
10156 /* handle weak-linking on Mac OS X 10.3 */
10157 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010158 fd_specified("statvfs", path->fd);
10159 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010160 }
10161#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010162 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010163 }
10164 else
10165#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010166 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010167 Py_END_ALLOW_THREADS
10168
10169 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010170 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010171 }
10172
Larry Hastings2f936352014-08-05 14:04:04 +100010173 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010174}
Larry Hastings2f936352014-08-05 14:04:04 +100010175#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10176
Guido van Rossum94f6f721999-01-06 18:42:14 +000010177
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010178#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010179/*[clinic input]
10180os._getdiskusage
10181
Steve Dower23ad6d02018-02-22 10:39:10 -080010182 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010183
10184Return disk usage statistics about the given path as a (total, free) tuple.
10185[clinic start generated code]*/
10186
Larry Hastings2f936352014-08-05 14:04:04 +100010187static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010188os__getdiskusage_impl(PyObject *module, path_t *path)
10189/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010190{
10191 BOOL retval;
10192 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010193 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010194
10195 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010196 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010197 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010198 if (retval == 0) {
10199 if (GetLastError() == ERROR_DIRECTORY) {
10200 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010201
Joe Pamerc8c02492018-09-25 10:57:36 -040010202 dir_path = PyMem_New(wchar_t, path->length + 1);
10203 if (dir_path == NULL) {
10204 return PyErr_NoMemory();
10205 }
10206
10207 wcscpy_s(dir_path, path->length + 1, path->wide);
10208
10209 if (_dirnameW(dir_path) != -1) {
10210 Py_BEGIN_ALLOW_THREADS
10211 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10212 Py_END_ALLOW_THREADS
10213 }
10214 /* Record the last error in case it's modified by PyMem_Free. */
10215 err = GetLastError();
10216 PyMem_Free(dir_path);
10217 if (retval) {
10218 goto success;
10219 }
10220 }
10221 return PyErr_SetFromWindowsErr(err);
10222 }
10223
10224success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010225 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10226}
Larry Hastings2f936352014-08-05 14:04:04 +100010227#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010228
10229
Fred Drakec9680921999-12-13 16:37:25 +000010230/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10231 * It maps strings representing configuration variable names to
10232 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010233 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010234 * rarely-used constants. There are three separate tables that use
10235 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010236 *
10237 * This code is always included, even if none of the interfaces that
10238 * need it are included. The #if hackery needed to avoid it would be
10239 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010240 */
10241struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010242 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010243 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010244};
10245
Fred Drake12c6e2d1999-12-14 21:25:03 +000010246static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010247conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010248 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010249{
Christian Heimes217cfd12007-12-02 14:31:20 +000010250 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010251 int value = _PyLong_AsInt(arg);
10252 if (value == -1 && PyErr_Occurred())
10253 return 0;
10254 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010255 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010256 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010257 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010258 /* look up the value in the table using a binary search */
10259 size_t lo = 0;
10260 size_t mid;
10261 size_t hi = tablesize;
10262 int cmp;
10263 const char *confname;
10264 if (!PyUnicode_Check(arg)) {
10265 PyErr_SetString(PyExc_TypeError,
10266 "configuration names must be strings or integers");
10267 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010269 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010270 if (confname == NULL)
10271 return 0;
10272 while (lo < hi) {
10273 mid = (lo + hi) / 2;
10274 cmp = strcmp(confname, table[mid].name);
10275 if (cmp < 0)
10276 hi = mid;
10277 else if (cmp > 0)
10278 lo = mid + 1;
10279 else {
10280 *valuep = table[mid].value;
10281 return 1;
10282 }
10283 }
10284 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10285 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010286 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010287}
10288
10289
10290#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10291static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010292#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010293 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010294#endif
10295#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010296 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010297#endif
Fred Drakec9680921999-12-13 16:37:25 +000010298#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010299 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010300#endif
10301#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010302 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010303#endif
10304#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010305 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010306#endif
10307#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010308 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010309#endif
10310#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010311 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010312#endif
10313#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010314 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010315#endif
10316#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010317 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010318#endif
10319#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010320 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010321#endif
10322#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010323 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010324#endif
10325#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010326 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010327#endif
10328#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010329 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010330#endif
10331#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010332 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010333#endif
10334#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010335 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010336#endif
10337#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010338 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010339#endif
10340#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010341 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010342#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010343#ifdef _PC_ACL_ENABLED
10344 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10345#endif
10346#ifdef _PC_MIN_HOLE_SIZE
10347 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10348#endif
10349#ifdef _PC_ALLOC_SIZE_MIN
10350 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10351#endif
10352#ifdef _PC_REC_INCR_XFER_SIZE
10353 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10354#endif
10355#ifdef _PC_REC_MAX_XFER_SIZE
10356 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10357#endif
10358#ifdef _PC_REC_MIN_XFER_SIZE
10359 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10360#endif
10361#ifdef _PC_REC_XFER_ALIGN
10362 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10363#endif
10364#ifdef _PC_SYMLINK_MAX
10365 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10366#endif
10367#ifdef _PC_XATTR_ENABLED
10368 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10369#endif
10370#ifdef _PC_XATTR_EXISTS
10371 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10372#endif
10373#ifdef _PC_TIMESTAMP_RESOLUTION
10374 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10375#endif
Fred Drakec9680921999-12-13 16:37:25 +000010376};
10377
Fred Drakec9680921999-12-13 16:37:25 +000010378static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010379conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010380{
10381 return conv_confname(arg, valuep, posix_constants_pathconf,
10382 sizeof(posix_constants_pathconf)
10383 / sizeof(struct constdef));
10384}
10385#endif
10386
Larry Hastings2f936352014-08-05 14:04:04 +100010387
Fred Drakec9680921999-12-13 16:37:25 +000010388#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010389/*[clinic input]
10390os.fpathconf -> long
10391
10392 fd: int
10393 name: path_confname
10394 /
10395
10396Return the configuration limit name for the file descriptor fd.
10397
10398If there is no limit, return -1.
10399[clinic start generated code]*/
10400
Larry Hastings2f936352014-08-05 14:04:04 +100010401static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010402os_fpathconf_impl(PyObject *module, int fd, int name)
10403/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010404{
10405 long limit;
10406
10407 errno = 0;
10408 limit = fpathconf(fd, name);
10409 if (limit == -1 && errno != 0)
10410 posix_error();
10411
10412 return limit;
10413}
10414#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010415
10416
10417#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010418/*[clinic input]
10419os.pathconf -> long
10420 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10421 name: path_confname
10422
10423Return the configuration limit name for the file or directory path.
10424
10425If there is no limit, return -1.
10426On some platforms, path may also be specified as an open file descriptor.
10427 If this functionality is unavailable, using it raises an exception.
10428[clinic start generated code]*/
10429
Larry Hastings2f936352014-08-05 14:04:04 +100010430static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010431os_pathconf_impl(PyObject *module, path_t *path, int name)
10432/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010433{
Victor Stinner8c62be82010-05-06 00:08:46 +000010434 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010435
Victor Stinner8c62be82010-05-06 00:08:46 +000010436 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010437#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010438 if (path->fd != -1)
10439 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010440 else
10441#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010442 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010443 if (limit == -1 && errno != 0) {
10444 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010445 /* could be a path or name problem */
10446 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010447 else
Larry Hastings2f936352014-08-05 14:04:04 +100010448 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010449 }
Larry Hastings2f936352014-08-05 14:04:04 +100010450
10451 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010452}
Larry Hastings2f936352014-08-05 14:04:04 +100010453#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010454
10455#ifdef HAVE_CONFSTR
10456static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010457#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010458 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010459#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010460#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010461 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010462#endif
10463#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010464 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010465#endif
Fred Draked86ed291999-12-15 15:34:33 +000010466#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010467 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010468#endif
10469#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010471#endif
10472#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010473 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010474#endif
10475#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010477#endif
Fred Drakec9680921999-12-13 16:37:25 +000010478#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010479 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010480#endif
10481#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010482 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010483#endif
10484#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010485 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010486#endif
10487#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010488 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010489#endif
10490#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010491 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010492#endif
10493#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010494 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010495#endif
10496#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010498#endif
10499#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010500 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010501#endif
Fred Draked86ed291999-12-15 15:34:33 +000010502#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010503 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010504#endif
Fred Drakec9680921999-12-13 16:37:25 +000010505#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010506 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010507#endif
Fred Draked86ed291999-12-15 15:34:33 +000010508#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010509 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010510#endif
10511#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010512 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010513#endif
10514#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010516#endif
10517#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010518 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010519#endif
Fred Drakec9680921999-12-13 16:37:25 +000010520#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010521 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010522#endif
10523#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010524 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010525#endif
10526#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010527 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010528#endif
10529#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010531#endif
10532#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010533 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010534#endif
10535#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010537#endif
10538#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010539 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010540#endif
10541#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010542 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010543#endif
10544#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010546#endif
10547#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010549#endif
10550#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010551 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010552#endif
10553#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010554 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010555#endif
10556#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010557 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010558#endif
10559#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010560 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010561#endif
10562#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010564#endif
10565#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010567#endif
Fred Draked86ed291999-12-15 15:34:33 +000010568#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010570#endif
10571#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010573#endif
10574#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010576#endif
10577#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010579#endif
10580#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010582#endif
10583#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010585#endif
10586#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010588#endif
10589#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010591#endif
10592#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010594#endif
10595#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010596 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010597#endif
10598#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010600#endif
10601#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010602 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010603#endif
10604#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010606#endif
Fred Drakec9680921999-12-13 16:37:25 +000010607};
10608
10609static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010610conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010611{
10612 return conv_confname(arg, valuep, posix_constants_confstr,
10613 sizeof(posix_constants_confstr)
10614 / sizeof(struct constdef));
10615}
10616
Larry Hastings2f936352014-08-05 14:04:04 +100010617
10618/*[clinic input]
10619os.confstr
10620
10621 name: confstr_confname
10622 /
10623
10624Return a string-valued system configuration variable.
10625[clinic start generated code]*/
10626
Larry Hastings2f936352014-08-05 14:04:04 +100010627static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010628os_confstr_impl(PyObject *module, int name)
10629/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010630{
10631 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010632 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010633 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010634
Victor Stinnercb043522010-09-10 23:49:04 +000010635 errno = 0;
10636 len = confstr(name, buffer, sizeof(buffer));
10637 if (len == 0) {
10638 if (errno) {
10639 posix_error();
10640 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010641 }
10642 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010643 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010644 }
10645 }
Victor Stinnercb043522010-09-10 23:49:04 +000010646
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010647 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010648 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010649 char *buf = PyMem_Malloc(len);
10650 if (buf == NULL)
10651 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010652 len2 = confstr(name, buf, len);
10653 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010654 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010655 PyMem_Free(buf);
10656 }
10657 else
10658 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010659 return result;
10660}
Larry Hastings2f936352014-08-05 14:04:04 +100010661#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010662
10663
10664#ifdef HAVE_SYSCONF
10665static struct constdef posix_constants_sysconf[] = {
10666#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010667 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010668#endif
10669#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010671#endif
10672#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010673 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010674#endif
10675#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010677#endif
10678#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010679 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010680#endif
10681#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010682 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010683#endif
10684#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010685 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010686#endif
10687#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010688 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010689#endif
10690#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010691 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010692#endif
10693#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010694 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010695#endif
Fred Draked86ed291999-12-15 15:34:33 +000010696#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010698#endif
10699#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010701#endif
Fred Drakec9680921999-12-13 16:37:25 +000010702#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010704#endif
Fred Drakec9680921999-12-13 16:37:25 +000010705#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010707#endif
10708#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010710#endif
10711#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010713#endif
10714#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010716#endif
10717#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010719#endif
Fred Draked86ed291999-12-15 15:34:33 +000010720#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010721 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010722#endif
Fred Drakec9680921999-12-13 16:37:25 +000010723#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010724 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010725#endif
10726#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010727 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010728#endif
10729#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010730 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010731#endif
10732#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010733 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010734#endif
10735#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010736 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010737#endif
Fred Draked86ed291999-12-15 15:34:33 +000010738#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010739 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010740#endif
Fred Drakec9680921999-12-13 16:37:25 +000010741#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010742 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010743#endif
10744#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010745 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010746#endif
10747#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010748 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010749#endif
10750#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010751 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010752#endif
10753#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010754 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010755#endif
10756#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010757 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010758#endif
10759#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010760 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010761#endif
10762#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010763 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010764#endif
10765#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010766 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010767#endif
10768#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010769 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010770#endif
10771#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010772 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010773#endif
10774#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010775 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010776#endif
10777#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010778 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010779#endif
10780#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010781 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010782#endif
10783#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010784 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010785#endif
10786#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010787 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010788#endif
10789#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010790 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010791#endif
10792#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010793 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010794#endif
10795#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010797#endif
10798#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010799 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010800#endif
10801#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010802 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010803#endif
10804#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010805 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010806#endif
10807#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010808 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010809#endif
Fred Draked86ed291999-12-15 15:34:33 +000010810#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010811 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010812#endif
Fred Drakec9680921999-12-13 16:37:25 +000010813#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010814 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010815#endif
10816#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010817 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010818#endif
10819#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010820 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010821#endif
Fred Draked86ed291999-12-15 15:34:33 +000010822#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010823 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010824#endif
Fred Drakec9680921999-12-13 16:37:25 +000010825#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010826 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010827#endif
Fred Draked86ed291999-12-15 15:34:33 +000010828#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010829 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010830#endif
10831#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010832 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010833#endif
Fred Drakec9680921999-12-13 16:37:25 +000010834#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010836#endif
10837#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010839#endif
10840#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010841 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010842#endif
10843#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010844 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010845#endif
Fred Draked86ed291999-12-15 15:34:33 +000010846#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010847 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010848#endif
Fred Drakec9680921999-12-13 16:37:25 +000010849#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010850 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010851#endif
10852#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010853 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010854#endif
10855#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010856 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010857#endif
10858#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010859 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010860#endif
10861#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010862 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010863#endif
10864#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010865 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010866#endif
10867#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010869#endif
Fred Draked86ed291999-12-15 15:34:33 +000010870#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010872#endif
Fred Drakec9680921999-12-13 16:37:25 +000010873#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010874 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010875#endif
10876#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010877 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010878#endif
Fred Draked86ed291999-12-15 15:34:33 +000010879#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010880 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010881#endif
Fred Drakec9680921999-12-13 16:37:25 +000010882#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010884#endif
10885#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010886 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010887#endif
10888#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010890#endif
10891#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010892 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010893#endif
10894#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010895 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010896#endif
10897#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010898 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010899#endif
10900#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010901 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010902#endif
10903#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010904 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010905#endif
10906#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010907 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010908#endif
Fred Draked86ed291999-12-15 15:34:33 +000010909#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010910 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010911#endif
10912#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010913 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010914#endif
Fred Drakec9680921999-12-13 16:37:25 +000010915#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010916 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010917#endif
10918#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010919 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010920#endif
10921#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010922 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010923#endif
10924#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010925 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010926#endif
10927#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010928 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010929#endif
10930#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010931 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010932#endif
10933#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010934 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010935#endif
10936#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010937 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010938#endif
10939#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010940 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010941#endif
10942#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010943 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010944#endif
10945#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010946 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010947#endif
10948#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010949 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010950#endif
10951#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010952 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010953#endif
10954#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010955 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010956#endif
10957#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010958 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010959#endif
10960#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010962#endif
10963#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010964 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010965#endif
10966#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010967 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010968#endif
10969#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010971#endif
10972#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010973 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
10975#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010976 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010977#endif
10978#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010979 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010980#endif
10981#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010982 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010983#endif
10984#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010986#endif
10987#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010988 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010989#endif
10990#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010992#endif
10993#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010994 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010995#endif
10996#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010997 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010998#endif
10999#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011000 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011001#endif
11002#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011003 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011004#endif
11005#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011006 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011007#endif
11008#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011009 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011010#endif
11011#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011012 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011013#endif
11014#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011015 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011016#endif
11017#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011018 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011019#endif
Fred Draked86ed291999-12-15 15:34:33 +000011020#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011021 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011022#endif
Fred Drakec9680921999-12-13 16:37:25 +000011023#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011024 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011025#endif
11026#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011027 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011028#endif
11029#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011030 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011031#endif
11032#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011033 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011034#endif
11035#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011036 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011037#endif
11038#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011039 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011040#endif
11041#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011042 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011043#endif
11044#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011045 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011046#endif
11047#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011048 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011049#endif
11050#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011051 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011052#endif
11053#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011054 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011055#endif
11056#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011057 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011058#endif
11059#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011060 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011061#endif
11062#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011063 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011064#endif
11065#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011066 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011067#endif
11068#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011069 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011070#endif
11071#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011072 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011073#endif
11074#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011075 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011076#endif
11077#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011079#endif
11080#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011081 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011082#endif
11083#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011085#endif
11086#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011087 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011088#endif
11089#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011090 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011091#endif
11092#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011094#endif
11095#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011096 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011097#endif
11098#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011100#endif
11101#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011102 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011103#endif
11104#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011106#endif
11107#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011108 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011109#endif
11110#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011111 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011112#endif
11113#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011115#endif
11116#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011117 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011118#endif
11119#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011120 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011121#endif
11122#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011123 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011124#endif
11125#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011127#endif
11128#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011129 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011130#endif
11131#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011132 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011133#endif
11134#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011135 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011136#endif
11137#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011138 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011139#endif
11140#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011141 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011142#endif
11143#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011144 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011145#endif
11146#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011147 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011148#endif
11149#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011150 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011151#endif
11152#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011153 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011154#endif
11155#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011156 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011157#endif
11158};
11159
11160static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011161conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011162{
11163 return conv_confname(arg, valuep, posix_constants_sysconf,
11164 sizeof(posix_constants_sysconf)
11165 / sizeof(struct constdef));
11166}
11167
Larry Hastings2f936352014-08-05 14:04:04 +100011168
11169/*[clinic input]
11170os.sysconf -> long
11171 name: sysconf_confname
11172 /
11173
11174Return an integer-valued system configuration variable.
11175[clinic start generated code]*/
11176
Larry Hastings2f936352014-08-05 14:04:04 +100011177static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011178os_sysconf_impl(PyObject *module, int name)
11179/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011180{
11181 long value;
11182
11183 errno = 0;
11184 value = sysconf(name);
11185 if (value == -1 && errno != 0)
11186 posix_error();
11187 return value;
11188}
11189#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011190
11191
Fred Drakebec628d1999-12-15 18:31:10 +000011192/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011193 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011194 * the exported dictionaries that are used to publish information about the
11195 * names available on the host platform.
11196 *
11197 * Sorting the table at runtime ensures that the table is properly ordered
11198 * when used, even for platforms we're not able to test on. It also makes
11199 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011200 */
Fred Drakebec628d1999-12-15 18:31:10 +000011201
11202static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011203cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011204{
11205 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011207 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011208 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011209
11210 return strcmp(c1->name, c2->name);
11211}
11212
11213static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011214setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011215 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011216{
Fred Drakebec628d1999-12-15 18:31:10 +000011217 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011218 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011219
11220 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11221 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011222 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011224
Barry Warsaw3155db32000-04-13 15:20:40 +000011225 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011226 PyObject *o = PyLong_FromLong(table[i].value);
11227 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11228 Py_XDECREF(o);
11229 Py_DECREF(d);
11230 return -1;
11231 }
11232 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011233 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011234 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011235}
11236
Fred Drakebec628d1999-12-15 18:31:10 +000011237/* Return -1 on failure, 0 on success. */
11238static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011239setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011240{
11241#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011242 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011243 sizeof(posix_constants_pathconf)
11244 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011245 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011246 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011247#endif
11248#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011249 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011250 sizeof(posix_constants_confstr)
11251 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011252 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011253 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011254#endif
11255#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011256 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011257 sizeof(posix_constants_sysconf)
11258 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011259 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011260 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011261#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011262 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011263}
Fred Draked86ed291999-12-15 15:34:33 +000011264
11265
Larry Hastings2f936352014-08-05 14:04:04 +100011266/*[clinic input]
11267os.abort
11268
11269Abort the interpreter immediately.
11270
11271This function 'dumps core' or otherwise fails in the hardest way possible
11272on the hosting operating system. This function never returns.
11273[clinic start generated code]*/
11274
Larry Hastings2f936352014-08-05 14:04:04 +100011275static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011276os_abort_impl(PyObject *module)
11277/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011278{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011279 abort();
11280 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011281#ifndef __clang__
11282 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11283 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11284 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011285 Py_FatalError("abort() called from Python code didn't abort!");
11286 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011287#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011288}
Fred Drakebec628d1999-12-15 18:31:10 +000011289
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011290#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011291/* Grab ShellExecute dynamically from shell32 */
11292static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011293static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11294 LPCWSTR, INT);
11295static int
11296check_ShellExecute()
11297{
11298 HINSTANCE hShell32;
11299
11300 /* only recheck */
11301 if (-1 == has_ShellExecute) {
11302 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011303 /* Security note: this call is not vulnerable to "DLL hijacking".
11304 SHELL32 is part of "KnownDLLs" and so Windows always load
11305 the system SHELL32.DLL, even if there is another SHELL32.DLL
11306 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011307 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011308 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011309 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11310 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011311 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011312 } else {
11313 has_ShellExecute = 0;
11314 }
Tony Roberts4860f012019-02-02 18:16:42 +010011315 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011316 }
11317 return has_ShellExecute;
11318}
11319
11320
Steve Dowercc16be82016-09-08 10:35:16 -070011321/*[clinic input]
11322os.startfile
11323 filepath: path_t
11324 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011325
Steve Dowercc16be82016-09-08 10:35:16 -070011326startfile(filepath [, operation])
11327
11328Start a file with its associated application.
11329
11330When "operation" is not specified or "open", this acts like
11331double-clicking the file in Explorer, or giving the file name as an
11332argument to the DOS "start" command: the file is opened with whatever
11333application (if any) its extension is associated.
11334When another "operation" is given, it specifies what should be done with
11335the file. A typical operation is "print".
11336
11337startfile returns as soon as the associated application is launched.
11338There is no option to wait for the application to close, and no way
11339to retrieve the application's exit status.
11340
11341The filepath is relative to the current directory. If you want to use
11342an absolute path, make sure the first character is not a slash ("/");
11343the underlying Win32 ShellExecute function doesn't work if it is.
11344[clinic start generated code]*/
11345
11346static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011347os_startfile_impl(PyObject *module, path_t *filepath,
11348 const Py_UNICODE *operation)
11349/*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011350{
11351 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011352
11353 if(!check_ShellExecute()) {
11354 /* If the OS doesn't have ShellExecute, return a
11355 NotImplementedError. */
11356 return PyErr_Format(PyExc_NotImplementedError,
11357 "startfile not available on this platform");
11358 }
11359
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011361 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011362 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011363 Py_END_ALLOW_THREADS
11364
Victor Stinner8c62be82010-05-06 00:08:46 +000011365 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011366 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011367 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011368 }
Steve Dowercc16be82016-09-08 10:35:16 -070011369 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011370}
Larry Hastings2f936352014-08-05 14:04:04 +100011371#endif /* MS_WINDOWS */
11372
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011373
Martin v. Löwis438b5342002-12-27 10:16:42 +000011374#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011375/*[clinic input]
11376os.getloadavg
11377
11378Return average recent system load information.
11379
11380Return the number of processes in the system run queue averaged over
11381the last 1, 5, and 15 minutes as a tuple of three floats.
11382Raises OSError if the load average was unobtainable.
11383[clinic start generated code]*/
11384
Larry Hastings2f936352014-08-05 14:04:04 +100011385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011386os_getloadavg_impl(PyObject *module)
11387/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011388{
11389 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011390 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011391 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11392 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011393 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011394 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011395}
Larry Hastings2f936352014-08-05 14:04:04 +100011396#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011397
Larry Hastings2f936352014-08-05 14:04:04 +100011398
11399/*[clinic input]
11400os.device_encoding
11401 fd: int
11402
11403Return a string describing the encoding of a terminal's file descriptor.
11404
11405The file descriptor must be attached to a terminal.
11406If the device is not a terminal, return None.
11407[clinic start generated code]*/
11408
Larry Hastings2f936352014-08-05 14:04:04 +100011409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011410os_device_encoding_impl(PyObject *module, int fd)
11411/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011412{
Brett Cannonefb00c02012-02-29 18:31:31 -050011413 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011414}
11415
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011416
Larry Hastings2f936352014-08-05 14:04:04 +100011417#ifdef HAVE_SETRESUID
11418/*[clinic input]
11419os.setresuid
11420
11421 ruid: uid_t
11422 euid: uid_t
11423 suid: uid_t
11424 /
11425
11426Set the current process's real, effective, and saved user ids.
11427[clinic start generated code]*/
11428
Larry Hastings2f936352014-08-05 14:04:04 +100011429static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011430os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11431/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011432{
Victor Stinner8c62be82010-05-06 00:08:46 +000011433 if (setresuid(ruid, euid, suid) < 0)
11434 return posix_error();
11435 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011436}
Larry Hastings2f936352014-08-05 14:04:04 +100011437#endif /* HAVE_SETRESUID */
11438
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011439
11440#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011441/*[clinic input]
11442os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011443
Larry Hastings2f936352014-08-05 14:04:04 +100011444 rgid: gid_t
11445 egid: gid_t
11446 sgid: gid_t
11447 /
11448
11449Set the current process's real, effective, and saved group ids.
11450[clinic start generated code]*/
11451
Larry Hastings2f936352014-08-05 14:04:04 +100011452static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011453os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11454/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011455{
Victor Stinner8c62be82010-05-06 00:08:46 +000011456 if (setresgid(rgid, egid, sgid) < 0)
11457 return posix_error();
11458 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011459}
Larry Hastings2f936352014-08-05 14:04:04 +100011460#endif /* HAVE_SETRESGID */
11461
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011462
11463#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011464/*[clinic input]
11465os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011466
Larry Hastings2f936352014-08-05 14:04:04 +100011467Return a tuple of the current process's real, effective, and saved user ids.
11468[clinic start generated code]*/
11469
Larry Hastings2f936352014-08-05 14:04:04 +100011470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011471os_getresuid_impl(PyObject *module)
11472/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011473{
Victor Stinner8c62be82010-05-06 00:08:46 +000011474 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011475 if (getresuid(&ruid, &euid, &suid) < 0)
11476 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011477 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11478 _PyLong_FromUid(euid),
11479 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011480}
Larry Hastings2f936352014-08-05 14:04:04 +100011481#endif /* HAVE_GETRESUID */
11482
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011483
11484#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011485/*[clinic input]
11486os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011487
Larry Hastings2f936352014-08-05 14:04:04 +100011488Return a tuple of the current process's real, effective, and saved group ids.
11489[clinic start generated code]*/
11490
Larry Hastings2f936352014-08-05 14:04:04 +100011491static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011492os_getresgid_impl(PyObject *module)
11493/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011494{
11495 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011496 if (getresgid(&rgid, &egid, &sgid) < 0)
11497 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011498 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11499 _PyLong_FromGid(egid),
11500 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011501}
Larry Hastings2f936352014-08-05 14:04:04 +100011502#endif /* HAVE_GETRESGID */
11503
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011504
Benjamin Peterson9428d532011-09-14 11:45:52 -040011505#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011506/*[clinic input]
11507os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011508
Larry Hastings2f936352014-08-05 14:04:04 +100011509 path: path_t(allow_fd=True)
11510 attribute: path_t
11511 *
11512 follow_symlinks: bool = True
11513
11514Return the value of extended attribute attribute on path.
11515
BNMetricsb9427072018-11-02 15:20:19 +000011516path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011517If follow_symlinks is False, and the last element of the path is a symbolic
11518 link, getxattr will examine the symbolic link itself instead of the file
11519 the link points to.
11520
11521[clinic start generated code]*/
11522
Larry Hastings2f936352014-08-05 14:04:04 +100011523static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011524os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011525 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011526/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011527{
11528 Py_ssize_t i;
11529 PyObject *buffer = NULL;
11530
11531 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11532 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011533
Larry Hastings9cf065c2012-06-22 16:30:09 -070011534 for (i = 0; ; i++) {
11535 void *ptr;
11536 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011537 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011538 Py_ssize_t buffer_size = buffer_sizes[i];
11539 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011540 path_error(path);
11541 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011542 }
11543 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11544 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011545 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011546 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011547
Larry Hastings9cf065c2012-06-22 16:30:09 -070011548 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011549 if (path->fd >= 0)
11550 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011551 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011552 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011553 else
Larry Hastings2f936352014-08-05 14:04:04 +100011554 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011555 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011556
Larry Hastings9cf065c2012-06-22 16:30:09 -070011557 if (result < 0) {
11558 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011559 if (errno == ERANGE)
11560 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011561 path_error(path);
11562 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011563 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011564
Larry Hastings9cf065c2012-06-22 16:30:09 -070011565 if (result != buffer_size) {
11566 /* Can only shrink. */
11567 _PyBytes_Resize(&buffer, result);
11568 }
11569 break;
11570 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011571
Larry Hastings9cf065c2012-06-22 16:30:09 -070011572 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011573}
11574
Larry Hastings2f936352014-08-05 14:04:04 +100011575
11576/*[clinic input]
11577os.setxattr
11578
11579 path: path_t(allow_fd=True)
11580 attribute: path_t
11581 value: Py_buffer
11582 flags: int = 0
11583 *
11584 follow_symlinks: bool = True
11585
11586Set extended attribute attribute on path to value.
11587
BNMetricsb9427072018-11-02 15:20:19 +000011588path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011589If follow_symlinks is False, and the last element of the path is a symbolic
11590 link, setxattr will modify the symbolic link itself instead of the file
11591 the link points to.
11592
11593[clinic start generated code]*/
11594
Benjamin Peterson799bd802011-08-31 22:15:17 -040011595static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011596os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011597 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011598/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011599{
Larry Hastings2f936352014-08-05 14:04:04 +100011600 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011601
Larry Hastings2f936352014-08-05 14:04:04 +100011602 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011603 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011604
Benjamin Peterson799bd802011-08-31 22:15:17 -040011605 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011606 if (path->fd > -1)
11607 result = fsetxattr(path->fd, attribute->narrow,
11608 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011609 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011610 result = setxattr(path->narrow, attribute->narrow,
11611 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011612 else
Larry Hastings2f936352014-08-05 14:04:04 +100011613 result = lsetxattr(path->narrow, attribute->narrow,
11614 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011615 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011616
Larry Hastings9cf065c2012-06-22 16:30:09 -070011617 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011618 path_error(path);
11619 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011620 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011621
Larry Hastings2f936352014-08-05 14:04:04 +100011622 Py_RETURN_NONE;
11623}
11624
11625
11626/*[clinic input]
11627os.removexattr
11628
11629 path: path_t(allow_fd=True)
11630 attribute: path_t
11631 *
11632 follow_symlinks: bool = True
11633
11634Remove extended attribute attribute on path.
11635
BNMetricsb9427072018-11-02 15:20:19 +000011636path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011637If follow_symlinks is False, and the last element of the path is a symbolic
11638 link, removexattr will modify the symbolic link itself instead of the file
11639 the link points to.
11640
11641[clinic start generated code]*/
11642
Larry Hastings2f936352014-08-05 14:04:04 +100011643static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011644os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011645 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011646/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011647{
11648 ssize_t result;
11649
11650 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11651 return NULL;
11652
11653 Py_BEGIN_ALLOW_THREADS;
11654 if (path->fd > -1)
11655 result = fremovexattr(path->fd, attribute->narrow);
11656 else if (follow_symlinks)
11657 result = removexattr(path->narrow, attribute->narrow);
11658 else
11659 result = lremovexattr(path->narrow, attribute->narrow);
11660 Py_END_ALLOW_THREADS;
11661
11662 if (result) {
11663 return path_error(path);
11664 }
11665
11666 Py_RETURN_NONE;
11667}
11668
11669
11670/*[clinic input]
11671os.listxattr
11672
11673 path: path_t(allow_fd=True, nullable=True) = None
11674 *
11675 follow_symlinks: bool = True
11676
11677Return a list of extended attributes on path.
11678
BNMetricsb9427072018-11-02 15:20:19 +000011679path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011680if path is None, listxattr will examine the current directory.
11681If follow_symlinks is False, and the last element of the path is a symbolic
11682 link, listxattr will examine the symbolic link itself instead of the file
11683 the link points to.
11684[clinic start generated code]*/
11685
Larry Hastings2f936352014-08-05 14:04:04 +100011686static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011687os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011688/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011689{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011690 Py_ssize_t i;
11691 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011692 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011693 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011694
Larry Hastings2f936352014-08-05 14:04:04 +100011695 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011696 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011697
Larry Hastings2f936352014-08-05 14:04:04 +100011698 name = path->narrow ? path->narrow : ".";
11699
Larry Hastings9cf065c2012-06-22 16:30:09 -070011700 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011701 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011702 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011703 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011704 Py_ssize_t buffer_size = buffer_sizes[i];
11705 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011706 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011707 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011708 break;
11709 }
11710 buffer = PyMem_MALLOC(buffer_size);
11711 if (!buffer) {
11712 PyErr_NoMemory();
11713 break;
11714 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011715
Larry Hastings9cf065c2012-06-22 16:30:09 -070011716 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011717 if (path->fd > -1)
11718 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011719 else if (follow_symlinks)
11720 length = listxattr(name, buffer, buffer_size);
11721 else
11722 length = llistxattr(name, buffer, buffer_size);
11723 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011724
Larry Hastings9cf065c2012-06-22 16:30:09 -070011725 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011726 if (errno == ERANGE) {
11727 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011728 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011729 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011730 }
Larry Hastings2f936352014-08-05 14:04:04 +100011731 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011732 break;
11733 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011734
Larry Hastings9cf065c2012-06-22 16:30:09 -070011735 result = PyList_New(0);
11736 if (!result) {
11737 goto exit;
11738 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011739
Larry Hastings9cf065c2012-06-22 16:30:09 -070011740 end = buffer + length;
11741 for (trace = start = buffer; trace != end; trace++) {
11742 if (!*trace) {
11743 int error;
11744 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11745 trace - start);
11746 if (!attribute) {
11747 Py_DECREF(result);
11748 result = NULL;
11749 goto exit;
11750 }
11751 error = PyList_Append(result, attribute);
11752 Py_DECREF(attribute);
11753 if (error) {
11754 Py_DECREF(result);
11755 result = NULL;
11756 goto exit;
11757 }
11758 start = trace + 1;
11759 }
11760 }
11761 break;
11762 }
11763exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011764 if (buffer)
11765 PyMem_FREE(buffer);
11766 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011767}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011768#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011769
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011770
Larry Hastings2f936352014-08-05 14:04:04 +100011771/*[clinic input]
11772os.urandom
11773
11774 size: Py_ssize_t
11775 /
11776
11777Return a bytes object containing random bytes suitable for cryptographic use.
11778[clinic start generated code]*/
11779
Larry Hastings2f936352014-08-05 14:04:04 +100011780static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011781os_urandom_impl(PyObject *module, Py_ssize_t size)
11782/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011783{
11784 PyObject *bytes;
11785 int result;
11786
Georg Brandl2fb477c2012-02-21 00:33:36 +010011787 if (size < 0)
11788 return PyErr_Format(PyExc_ValueError,
11789 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011790 bytes = PyBytes_FromStringAndSize(NULL, size);
11791 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011792 return NULL;
11793
Victor Stinnere66987e2016-09-06 16:33:52 -070011794 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011795 if (result == -1) {
11796 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011797 return NULL;
11798 }
Larry Hastings2f936352014-08-05 14:04:04 +100011799 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011800}
11801
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011802/* Terminal size querying */
11803
Eddie Elizondo474eedf2018-11-13 04:09:31 -080011804static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011805
11806PyDoc_STRVAR(TerminalSize_docstring,
11807 "A tuple of (columns, lines) for holding terminal window size");
11808
11809static PyStructSequence_Field TerminalSize_fields[] = {
11810 {"columns", "width of the terminal window in characters"},
11811 {"lines", "height of the terminal window in characters"},
11812 {NULL, NULL}
11813};
11814
11815static PyStructSequence_Desc TerminalSize_desc = {
11816 "os.terminal_size",
11817 TerminalSize_docstring,
11818 TerminalSize_fields,
11819 2,
11820};
11821
11822#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011823/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011824PyDoc_STRVAR(termsize__doc__,
11825 "Return the size of the terminal window as (columns, lines).\n" \
11826 "\n" \
11827 "The optional argument fd (default standard output) specifies\n" \
11828 "which file descriptor should be queried.\n" \
11829 "\n" \
11830 "If the file descriptor is not connected to a terminal, an OSError\n" \
11831 "is thrown.\n" \
11832 "\n" \
11833 "This function will only be defined if an implementation is\n" \
11834 "available for this system.\n" \
11835 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080011836 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011837 "normally be used, os.get_terminal_size is the low-level implementation.");
11838
11839static PyObject*
11840get_terminal_size(PyObject *self, PyObject *args)
11841{
11842 int columns, lines;
11843 PyObject *termsize;
11844
11845 int fd = fileno(stdout);
11846 /* Under some conditions stdout may not be connected and
11847 * fileno(stdout) may point to an invalid file descriptor. For example
11848 * GUI apps don't have valid standard streams by default.
11849 *
11850 * If this happens, and the optional fd argument is not present,
11851 * the ioctl below will fail returning EBADF. This is what we want.
11852 */
11853
11854 if (!PyArg_ParseTuple(args, "|i", &fd))
11855 return NULL;
11856
11857#ifdef TERMSIZE_USE_IOCTL
11858 {
11859 struct winsize w;
11860 if (ioctl(fd, TIOCGWINSZ, &w))
11861 return PyErr_SetFromErrno(PyExc_OSError);
11862 columns = w.ws_col;
11863 lines = w.ws_row;
11864 }
11865#endif /* TERMSIZE_USE_IOCTL */
11866
11867#ifdef TERMSIZE_USE_CONIO
11868 {
11869 DWORD nhandle;
11870 HANDLE handle;
11871 CONSOLE_SCREEN_BUFFER_INFO csbi;
11872 switch (fd) {
11873 case 0: nhandle = STD_INPUT_HANDLE;
11874 break;
11875 case 1: nhandle = STD_OUTPUT_HANDLE;
11876 break;
11877 case 2: nhandle = STD_ERROR_HANDLE;
11878 break;
11879 default:
11880 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11881 }
11882 handle = GetStdHandle(nhandle);
11883 if (handle == NULL)
11884 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11885 if (handle == INVALID_HANDLE_VALUE)
11886 return PyErr_SetFromWindowsErr(0);
11887
11888 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11889 return PyErr_SetFromWindowsErr(0);
11890
11891 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11892 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11893 }
11894#endif /* TERMSIZE_USE_CONIO */
11895
Eddie Elizondo474eedf2018-11-13 04:09:31 -080011896 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011897 if (termsize == NULL)
11898 return NULL;
11899 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11900 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11901 if (PyErr_Occurred()) {
11902 Py_DECREF(termsize);
11903 return NULL;
11904 }
11905 return termsize;
11906}
11907#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11908
Larry Hastings2f936352014-08-05 14:04:04 +100011909
11910/*[clinic input]
11911os.cpu_count
11912
Charles-François Natali80d62e62015-08-13 20:37:08 +010011913Return the number of CPUs in the system; return None if indeterminable.
11914
11915This number is not equivalent to the number of CPUs the current process can
11916use. The number of usable CPUs can be obtained with
11917``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100011918[clinic start generated code]*/
11919
Larry Hastings2f936352014-08-05 14:04:04 +100011920static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011921os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030011922/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011923{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011924 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011925#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011926 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
11927 Need to fallback to Vista behavior if this call isn't present */
11928 HINSTANCE hKernel32;
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011929 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
Tony Roberts4860f012019-02-02 18:16:42 +010011930 Py_BEGIN_ALLOW_THREADS
11931 hKernel32 = GetModuleHandleW(L"KERNEL32");
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011932 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11933 "GetMaximumProcessorCount");
Tony Roberts4860f012019-02-02 18:16:42 +010011934 Py_END_ALLOW_THREADS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011935 if (_GetMaximumProcessorCount != NULL) {
11936 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11937 }
11938 else {
11939 SYSTEM_INFO sysinfo;
11940 GetSystemInfo(&sysinfo);
11941 ncpu = sysinfo.dwNumberOfProcessors;
11942 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011943#elif defined(__hpux)
11944 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11945#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11946 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011947#elif defined(__DragonFly__) || \
11948 defined(__OpenBSD__) || \
11949 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011950 defined(__NetBSD__) || \
11951 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011952 int mib[2];
11953 size_t len = sizeof(ncpu);
11954 mib[0] = CTL_HW;
11955 mib[1] = HW_NCPU;
11956 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11957 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011958#endif
11959 if (ncpu >= 1)
11960 return PyLong_FromLong(ncpu);
11961 else
11962 Py_RETURN_NONE;
11963}
11964
Victor Stinnerdaf45552013-08-28 00:53:59 +020011965
Larry Hastings2f936352014-08-05 14:04:04 +100011966/*[clinic input]
11967os.get_inheritable -> bool
11968
11969 fd: int
11970 /
11971
11972Get the close-on-exe flag of the specified file descriptor.
11973[clinic start generated code]*/
11974
Larry Hastings2f936352014-08-05 14:04:04 +100011975static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011976os_get_inheritable_impl(PyObject *module, int fd)
11977/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011978{
Steve Dower8fc89802015-04-12 00:26:27 -040011979 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040011980 _Py_BEGIN_SUPPRESS_IPH
11981 return_value = _Py_get_inheritable(fd);
11982 _Py_END_SUPPRESS_IPH
11983 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011984}
11985
11986
11987/*[clinic input]
11988os.set_inheritable
11989 fd: int
11990 inheritable: int
11991 /
11992
11993Set the inheritable flag of the specified file descriptor.
11994[clinic start generated code]*/
11995
Larry Hastings2f936352014-08-05 14:04:04 +100011996static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011997os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11998/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011999{
Steve Dower8fc89802015-04-12 00:26:27 -040012000 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012001
Steve Dower8fc89802015-04-12 00:26:27 -040012002 _Py_BEGIN_SUPPRESS_IPH
12003 result = _Py_set_inheritable(fd, inheritable, NULL);
12004 _Py_END_SUPPRESS_IPH
12005 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012006 return NULL;
12007 Py_RETURN_NONE;
12008}
12009
12010
12011#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012012/*[clinic input]
12013os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012014 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012015 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012016
Larry Hastings2f936352014-08-05 14:04:04 +100012017Get the close-on-exe flag of the specified file descriptor.
12018[clinic start generated code]*/
12019
Larry Hastings2f936352014-08-05 14:04:04 +100012020static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012021os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012022/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012023{
12024 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012025
12026 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12027 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012028 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012029 }
12030
Larry Hastings2f936352014-08-05 14:04:04 +100012031 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012032}
12033
Victor Stinnerdaf45552013-08-28 00:53:59 +020012034
Larry Hastings2f936352014-08-05 14:04:04 +100012035/*[clinic input]
12036os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012037 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012038 inheritable: bool
12039 /
12040
12041Set the inheritable flag of the specified handle.
12042[clinic start generated code]*/
12043
Larry Hastings2f936352014-08-05 14:04:04 +100012044static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012045os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012046 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012047/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012048{
12049 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012050 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12051 PyErr_SetFromWindowsErr(0);
12052 return NULL;
12053 }
12054 Py_RETURN_NONE;
12055}
Larry Hastings2f936352014-08-05 14:04:04 +100012056#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012057
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012058#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012059/*[clinic input]
12060os.get_blocking -> bool
12061 fd: int
12062 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012063
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012064Get the blocking mode of the file descriptor.
12065
12066Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12067[clinic start generated code]*/
12068
12069static int
12070os_get_blocking_impl(PyObject *module, int fd)
12071/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012072{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012073 int blocking;
12074
Steve Dower8fc89802015-04-12 00:26:27 -040012075 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012076 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012077 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012078 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012079}
12080
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012081/*[clinic input]
12082os.set_blocking
12083 fd: int
12084 blocking: bool(accept={int})
12085 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012086
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012087Set the blocking mode of the specified file descriptor.
12088
12089Set the O_NONBLOCK flag if blocking is False,
12090clear the O_NONBLOCK flag otherwise.
12091[clinic start generated code]*/
12092
12093static PyObject *
12094os_set_blocking_impl(PyObject *module, int fd, int blocking)
12095/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012096{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012097 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012098
Steve Dower8fc89802015-04-12 00:26:27 -040012099 _Py_BEGIN_SUPPRESS_IPH
12100 result = _Py_set_blocking(fd, blocking);
12101 _Py_END_SUPPRESS_IPH
12102 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012103 return NULL;
12104 Py_RETURN_NONE;
12105}
12106#endif /* !MS_WINDOWS */
12107
12108
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012109/*[clinic input]
12110class os.DirEntry "DirEntry *" "&DirEntryType"
12111[clinic start generated code]*/
12112/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012113
12114typedef struct {
12115 PyObject_HEAD
12116 PyObject *name;
12117 PyObject *path;
12118 PyObject *stat;
12119 PyObject *lstat;
12120#ifdef MS_WINDOWS
12121 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012122 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012123 int got_file_index;
12124#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012125#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012126 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012127#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012128 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012129 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012130#endif
12131} DirEntry;
12132
12133static void
12134DirEntry_dealloc(DirEntry *entry)
12135{
12136 Py_XDECREF(entry->name);
12137 Py_XDECREF(entry->path);
12138 Py_XDECREF(entry->stat);
12139 Py_XDECREF(entry->lstat);
12140 Py_TYPE(entry)->tp_free((PyObject *)entry);
12141}
12142
12143/* Forward reference */
12144static int
12145DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12146
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012147/*[clinic input]
12148os.DirEntry.is_symlink -> bool
12149
12150Return True if the entry is a symbolic link; cached per entry.
12151[clinic start generated code]*/
12152
Victor Stinner6036e442015-03-08 01:58:04 +010012153static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012154os_DirEntry_is_symlink_impl(DirEntry *self)
12155/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012156{
12157#ifdef MS_WINDOWS
12158 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012159#elif defined(HAVE_DIRENT_D_TYPE)
12160 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012161 if (self->d_type != DT_UNKNOWN)
12162 return self->d_type == DT_LNK;
12163 else
12164 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012165#else
12166 /* POSIX without d_type */
12167 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012168#endif
12169}
12170
12171static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012172DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12173{
12174 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012175 STRUCT_STAT st;
12176 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012177
12178#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012179 if (!PyUnicode_FSDecoder(self->path, &ub))
12180 return NULL;
12181 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012182#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012183 if (!PyUnicode_FSConverter(self->path, &ub))
12184 return NULL;
12185 const char *path = PyBytes_AS_STRING(ub);
12186 if (self->dir_fd != DEFAULT_DIR_FD) {
12187#ifdef HAVE_FSTATAT
12188 result = fstatat(self->dir_fd, path, &st,
12189 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12190#else
12191 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12192 return NULL;
12193#endif /* HAVE_FSTATAT */
12194 }
12195 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012196#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012197 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012198 if (follow_symlinks)
12199 result = STAT(path, &st);
12200 else
12201 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012202 }
12203 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012204
12205 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012206 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012207
12208 return _pystat_fromstructstat(&st);
12209}
12210
12211static PyObject *
12212DirEntry_get_lstat(DirEntry *self)
12213{
12214 if (!self->lstat) {
12215#ifdef MS_WINDOWS
12216 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12217#else /* POSIX */
12218 self->lstat = DirEntry_fetch_stat(self, 0);
12219#endif
12220 }
12221 Py_XINCREF(self->lstat);
12222 return self->lstat;
12223}
12224
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012225/*[clinic input]
12226os.DirEntry.stat
12227 *
12228 follow_symlinks: bool = True
12229
12230Return stat_result object for the entry; cached per entry.
12231[clinic start generated code]*/
12232
Victor Stinner6036e442015-03-08 01:58:04 +010012233static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012234os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12235/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012236{
12237 if (!follow_symlinks)
12238 return DirEntry_get_lstat(self);
12239
12240 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012241 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012242 if (result == -1)
12243 return NULL;
12244 else if (result)
12245 self->stat = DirEntry_fetch_stat(self, 1);
12246 else
12247 self->stat = DirEntry_get_lstat(self);
12248 }
12249
12250 Py_XINCREF(self->stat);
12251 return self->stat;
12252}
12253
Victor Stinner6036e442015-03-08 01:58:04 +010012254/* Set exception and return -1 on error, 0 for False, 1 for True */
12255static int
12256DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12257{
12258 PyObject *stat = NULL;
12259 PyObject *st_mode = NULL;
12260 long mode;
12261 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012262#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012263 int is_symlink;
12264 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012265#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012266#ifdef MS_WINDOWS
12267 unsigned long dir_bits;
12268#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012269 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012270
12271#ifdef MS_WINDOWS
12272 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12273 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012274#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012275 is_symlink = self->d_type == DT_LNK;
12276 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12277#endif
12278
Victor Stinner35a97c02015-03-08 02:59:09 +010012279#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012280 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012281#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012282 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012283 if (!stat) {
12284 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12285 /* If file doesn't exist (anymore), then return False
12286 (i.e., say it's not a file/directory) */
12287 PyErr_Clear();
12288 return 0;
12289 }
12290 goto error;
12291 }
12292 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12293 if (!st_mode)
12294 goto error;
12295
12296 mode = PyLong_AsLong(st_mode);
12297 if (mode == -1 && PyErr_Occurred())
12298 goto error;
12299 Py_CLEAR(st_mode);
12300 Py_CLEAR(stat);
12301 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012302#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012303 }
12304 else if (is_symlink) {
12305 assert(mode_bits != S_IFLNK);
12306 result = 0;
12307 }
12308 else {
12309 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12310#ifdef MS_WINDOWS
12311 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12312 if (mode_bits == S_IFDIR)
12313 result = dir_bits != 0;
12314 else
12315 result = dir_bits == 0;
12316#else /* POSIX */
12317 if (mode_bits == S_IFDIR)
12318 result = self->d_type == DT_DIR;
12319 else
12320 result = self->d_type == DT_REG;
12321#endif
12322 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012323#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012324
12325 return result;
12326
12327error:
12328 Py_XDECREF(st_mode);
12329 Py_XDECREF(stat);
12330 return -1;
12331}
12332
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012333/*[clinic input]
12334os.DirEntry.is_dir -> bool
12335 *
12336 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012337
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012338Return True if the entry is a directory; cached per entry.
12339[clinic start generated code]*/
12340
12341static int
12342os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12343/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12344{
12345 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012346}
12347
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012348/*[clinic input]
12349os.DirEntry.is_file -> bool
12350 *
12351 follow_symlinks: bool = True
12352
12353Return True if the entry is a file; cached per entry.
12354[clinic start generated code]*/
12355
12356static int
12357os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12358/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012359{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012360 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012361}
12362
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012363/*[clinic input]
12364os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012365
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012366Return inode of the entry; cached per entry.
12367[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012368
12369static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012370os_DirEntry_inode_impl(DirEntry *self)
12371/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012372{
12373#ifdef MS_WINDOWS
12374 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012375 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012376 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012377 STRUCT_STAT stat;
12378 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012379
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012380 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012381 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012382 path = PyUnicode_AsUnicode(unicode);
12383 result = LSTAT(path, &stat);
12384 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012385
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012386 if (result != 0)
12387 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012388
12389 self->win32_file_index = stat.st_ino;
12390 self->got_file_index = 1;
12391 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012392 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12393 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012394#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012395 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12396 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012397#endif
12398}
12399
12400static PyObject *
12401DirEntry_repr(DirEntry *self)
12402{
12403 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12404}
12405
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012406/*[clinic input]
12407os.DirEntry.__fspath__
12408
12409Returns the path for the entry.
12410[clinic start generated code]*/
12411
Brett Cannon96881cd2016-06-10 14:37:21 -070012412static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012413os_DirEntry___fspath___impl(DirEntry *self)
12414/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012415{
12416 Py_INCREF(self->path);
12417 return self->path;
12418}
12419
Victor Stinner6036e442015-03-08 01:58:04 +010012420static PyMemberDef DirEntry_members[] = {
12421 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12422 "the entry's base filename, relative to scandir() \"path\" argument"},
12423 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12424 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12425 {NULL}
12426};
12427
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012428#include "clinic/posixmodule.c.h"
12429
Victor Stinner6036e442015-03-08 01:58:04 +010012430static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012431 OS_DIRENTRY_IS_DIR_METHODDEF
12432 OS_DIRENTRY_IS_FILE_METHODDEF
12433 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12434 OS_DIRENTRY_STAT_METHODDEF
12435 OS_DIRENTRY_INODE_METHODDEF
12436 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012437 {NULL}
12438};
12439
Benjamin Peterson5646de42015-04-12 17:56:34 -040012440static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012441 PyVarObject_HEAD_INIT(NULL, 0)
12442 MODNAME ".DirEntry", /* tp_name */
12443 sizeof(DirEntry), /* tp_basicsize */
12444 0, /* tp_itemsize */
12445 /* methods */
12446 (destructor)DirEntry_dealloc, /* tp_dealloc */
12447 0, /* tp_print */
12448 0, /* tp_getattr */
12449 0, /* tp_setattr */
12450 0, /* tp_compare */
12451 (reprfunc)DirEntry_repr, /* tp_repr */
12452 0, /* tp_as_number */
12453 0, /* tp_as_sequence */
12454 0, /* tp_as_mapping */
12455 0, /* tp_hash */
12456 0, /* tp_call */
12457 0, /* tp_str */
12458 0, /* tp_getattro */
12459 0, /* tp_setattro */
12460 0, /* tp_as_buffer */
12461 Py_TPFLAGS_DEFAULT, /* tp_flags */
12462 0, /* tp_doc */
12463 0, /* tp_traverse */
12464 0, /* tp_clear */
12465 0, /* tp_richcompare */
12466 0, /* tp_weaklistoffset */
12467 0, /* tp_iter */
12468 0, /* tp_iternext */
12469 DirEntry_methods, /* tp_methods */
12470 DirEntry_members, /* tp_members */
12471};
12472
12473#ifdef MS_WINDOWS
12474
12475static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012476join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012477{
12478 Py_ssize_t path_len;
12479 Py_ssize_t size;
12480 wchar_t *result;
12481 wchar_t ch;
12482
12483 if (!path_wide) { /* Default arg: "." */
12484 path_wide = L".";
12485 path_len = 1;
12486 }
12487 else {
12488 path_len = wcslen(path_wide);
12489 }
12490
12491 /* The +1's are for the path separator and the NUL */
12492 size = path_len + 1 + wcslen(filename) + 1;
12493 result = PyMem_New(wchar_t, size);
12494 if (!result) {
12495 PyErr_NoMemory();
12496 return NULL;
12497 }
12498 wcscpy(result, path_wide);
12499 if (path_len > 0) {
12500 ch = result[path_len - 1];
12501 if (ch != SEP && ch != ALTSEP && ch != L':')
12502 result[path_len++] = SEP;
12503 wcscpy(result + path_len, filename);
12504 }
12505 return result;
12506}
12507
12508static PyObject *
12509DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12510{
12511 DirEntry *entry;
12512 BY_HANDLE_FILE_INFORMATION file_info;
12513 ULONG reparse_tag;
12514 wchar_t *joined_path;
12515
12516 entry = PyObject_New(DirEntry, &DirEntryType);
12517 if (!entry)
12518 return NULL;
12519 entry->name = NULL;
12520 entry->path = NULL;
12521 entry->stat = NULL;
12522 entry->lstat = NULL;
12523 entry->got_file_index = 0;
12524
12525 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12526 if (!entry->name)
12527 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012528 if (path->narrow) {
12529 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12530 if (!entry->name)
12531 goto error;
12532 }
Victor Stinner6036e442015-03-08 01:58:04 +010012533
12534 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12535 if (!joined_path)
12536 goto error;
12537
12538 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12539 PyMem_Free(joined_path);
12540 if (!entry->path)
12541 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012542 if (path->narrow) {
12543 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12544 if (!entry->path)
12545 goto error;
12546 }
Victor Stinner6036e442015-03-08 01:58:04 +010012547
Steve Dowercc16be82016-09-08 10:35:16 -070012548 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012549 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12550
12551 return (PyObject *)entry;
12552
12553error:
12554 Py_DECREF(entry);
12555 return NULL;
12556}
12557
12558#else /* POSIX */
12559
12560static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012561join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012562{
12563 Py_ssize_t path_len;
12564 Py_ssize_t size;
12565 char *result;
12566
12567 if (!path_narrow) { /* Default arg: "." */
12568 path_narrow = ".";
12569 path_len = 1;
12570 }
12571 else {
12572 path_len = strlen(path_narrow);
12573 }
12574
12575 if (filename_len == -1)
12576 filename_len = strlen(filename);
12577
12578 /* The +1's are for the path separator and the NUL */
12579 size = path_len + 1 + filename_len + 1;
12580 result = PyMem_New(char, size);
12581 if (!result) {
12582 PyErr_NoMemory();
12583 return NULL;
12584 }
12585 strcpy(result, path_narrow);
12586 if (path_len > 0 && result[path_len - 1] != '/')
12587 result[path_len++] = '/';
12588 strcpy(result + path_len, filename);
12589 return result;
12590}
12591
12592static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012593DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012594 ino_t d_ino
12595#ifdef HAVE_DIRENT_D_TYPE
12596 , unsigned char d_type
12597#endif
12598 )
Victor Stinner6036e442015-03-08 01:58:04 +010012599{
12600 DirEntry *entry;
12601 char *joined_path;
12602
12603 entry = PyObject_New(DirEntry, &DirEntryType);
12604 if (!entry)
12605 return NULL;
12606 entry->name = NULL;
12607 entry->path = NULL;
12608 entry->stat = NULL;
12609 entry->lstat = NULL;
12610
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012611 if (path->fd != -1) {
12612 entry->dir_fd = path->fd;
12613 joined_path = NULL;
12614 }
12615 else {
12616 entry->dir_fd = DEFAULT_DIR_FD;
12617 joined_path = join_path_filename(path->narrow, name, name_len);
12618 if (!joined_path)
12619 goto error;
12620 }
Victor Stinner6036e442015-03-08 01:58:04 +010012621
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012622 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012623 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012624 if (joined_path)
12625 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012626 }
12627 else {
12628 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012629 if (joined_path)
12630 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012631 }
12632 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012633 if (!entry->name)
12634 goto error;
12635
12636 if (path->fd != -1) {
12637 entry->path = entry->name;
12638 Py_INCREF(entry->path);
12639 }
12640 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012641 goto error;
12642
Victor Stinner35a97c02015-03-08 02:59:09 +010012643#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012644 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012645#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012646 entry->d_ino = d_ino;
12647
12648 return (PyObject *)entry;
12649
12650error:
12651 Py_XDECREF(entry);
12652 return NULL;
12653}
12654
12655#endif
12656
12657
12658typedef struct {
12659 PyObject_HEAD
12660 path_t path;
12661#ifdef MS_WINDOWS
12662 HANDLE handle;
12663 WIN32_FIND_DATAW file_data;
12664 int first_time;
12665#else /* POSIX */
12666 DIR *dirp;
12667#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012668#ifdef HAVE_FDOPENDIR
12669 int fd;
12670#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012671} ScandirIterator;
12672
12673#ifdef MS_WINDOWS
12674
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012675static int
12676ScandirIterator_is_closed(ScandirIterator *iterator)
12677{
12678 return iterator->handle == INVALID_HANDLE_VALUE;
12679}
12680
Victor Stinner6036e442015-03-08 01:58:04 +010012681static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012682ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012683{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012684 HANDLE handle = iterator->handle;
12685
12686 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012687 return;
12688
Victor Stinner6036e442015-03-08 01:58:04 +010012689 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012690 Py_BEGIN_ALLOW_THREADS
12691 FindClose(handle);
12692 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012693}
12694
12695static PyObject *
12696ScandirIterator_iternext(ScandirIterator *iterator)
12697{
12698 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12699 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012700 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012701
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012702 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012703 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012704 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012705
12706 while (1) {
12707 if (!iterator->first_time) {
12708 Py_BEGIN_ALLOW_THREADS
12709 success = FindNextFileW(iterator->handle, file_data);
12710 Py_END_ALLOW_THREADS
12711 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012712 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012713 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012714 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012715 break;
12716 }
12717 }
12718 iterator->first_time = 0;
12719
12720 /* Skip over . and .. */
12721 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012722 wcscmp(file_data->cFileName, L"..") != 0) {
12723 entry = DirEntry_from_find_data(&iterator->path, file_data);
12724 if (!entry)
12725 break;
12726 return entry;
12727 }
Victor Stinner6036e442015-03-08 01:58:04 +010012728
12729 /* Loop till we get a non-dot directory or finish iterating */
12730 }
12731
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012732 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012733 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012734 return NULL;
12735}
12736
12737#else /* POSIX */
12738
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012739static int
12740ScandirIterator_is_closed(ScandirIterator *iterator)
12741{
12742 return !iterator->dirp;
12743}
12744
Victor Stinner6036e442015-03-08 01:58:04 +010012745static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012746ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012747{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012748 DIR *dirp = iterator->dirp;
12749
12750 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012751 return;
12752
Victor Stinner6036e442015-03-08 01:58:04 +010012753 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012754 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012755#ifdef HAVE_FDOPENDIR
12756 if (iterator->path.fd != -1)
12757 rewinddir(dirp);
12758#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012759 closedir(dirp);
12760 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012761 return;
12762}
12763
12764static PyObject *
12765ScandirIterator_iternext(ScandirIterator *iterator)
12766{
12767 struct dirent *direntp;
12768 Py_ssize_t name_len;
12769 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012770 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012771
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012772 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012773 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012774 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012775
12776 while (1) {
12777 errno = 0;
12778 Py_BEGIN_ALLOW_THREADS
12779 direntp = readdir(iterator->dirp);
12780 Py_END_ALLOW_THREADS
12781
12782 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012783 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012784 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012785 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012786 break;
12787 }
12788
12789 /* Skip over . and .. */
12790 name_len = NAMLEN(direntp);
12791 is_dot = direntp->d_name[0] == '.' &&
12792 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12793 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012794 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012795 name_len, direntp->d_ino
12796#ifdef HAVE_DIRENT_D_TYPE
12797 , direntp->d_type
12798#endif
12799 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012800 if (!entry)
12801 break;
12802 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012803 }
12804
12805 /* Loop till we get a non-dot directory or finish iterating */
12806 }
12807
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012808 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012809 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012810 return NULL;
12811}
12812
12813#endif
12814
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012815static PyObject *
12816ScandirIterator_close(ScandirIterator *self, PyObject *args)
12817{
12818 ScandirIterator_closedir(self);
12819 Py_RETURN_NONE;
12820}
12821
12822static PyObject *
12823ScandirIterator_enter(PyObject *self, PyObject *args)
12824{
12825 Py_INCREF(self);
12826 return self;
12827}
12828
12829static PyObject *
12830ScandirIterator_exit(ScandirIterator *self, PyObject *args)
12831{
12832 ScandirIterator_closedir(self);
12833 Py_RETURN_NONE;
12834}
12835
Victor Stinner6036e442015-03-08 01:58:04 +010012836static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010012837ScandirIterator_finalize(ScandirIterator *iterator)
12838{
12839 PyObject *error_type, *error_value, *error_traceback;
12840
12841 /* Save the current exception, if any. */
12842 PyErr_Fetch(&error_type, &error_value, &error_traceback);
12843
12844 if (!ScandirIterator_is_closed(iterator)) {
12845 ScandirIterator_closedir(iterator);
12846
12847 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
12848 "unclosed scandir iterator %R", iterator)) {
12849 /* Spurious errors can appear at shutdown */
12850 if (PyErr_ExceptionMatches(PyExc_Warning)) {
12851 PyErr_WriteUnraisable((PyObject *) iterator);
12852 }
12853 }
12854 }
12855
Victor Stinner7bfa4092016-03-23 00:43:54 +010012856 path_cleanup(&iterator->path);
12857
12858 /* Restore the saved exception. */
12859 PyErr_Restore(error_type, error_value, error_traceback);
12860}
12861
12862static void
Victor Stinner6036e442015-03-08 01:58:04 +010012863ScandirIterator_dealloc(ScandirIterator *iterator)
12864{
Victor Stinner7bfa4092016-03-23 00:43:54 +010012865 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
12866 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012867
Victor Stinner6036e442015-03-08 01:58:04 +010012868 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12869}
12870
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012871static PyMethodDef ScandirIterator_methods[] = {
12872 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
12873 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
12874 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
12875 {NULL}
12876};
12877
Benjamin Peterson5646de42015-04-12 17:56:34 -040012878static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012879 PyVarObject_HEAD_INIT(NULL, 0)
12880 MODNAME ".ScandirIterator", /* tp_name */
12881 sizeof(ScandirIterator), /* tp_basicsize */
12882 0, /* tp_itemsize */
12883 /* methods */
12884 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12885 0, /* tp_print */
12886 0, /* tp_getattr */
12887 0, /* tp_setattr */
12888 0, /* tp_compare */
12889 0, /* tp_repr */
12890 0, /* tp_as_number */
12891 0, /* tp_as_sequence */
12892 0, /* tp_as_mapping */
12893 0, /* tp_hash */
12894 0, /* tp_call */
12895 0, /* tp_str */
12896 0, /* tp_getattro */
12897 0, /* tp_setattro */
12898 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012899 Py_TPFLAGS_DEFAULT
12900 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010012901 0, /* tp_doc */
12902 0, /* tp_traverse */
12903 0, /* tp_clear */
12904 0, /* tp_richcompare */
12905 0, /* tp_weaklistoffset */
12906 PyObject_SelfIter, /* tp_iter */
12907 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012908 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012909 0, /* tp_members */
12910 0, /* tp_getset */
12911 0, /* tp_base */
12912 0, /* tp_dict */
12913 0, /* tp_descr_get */
12914 0, /* tp_descr_set */
12915 0, /* tp_dictoffset */
12916 0, /* tp_init */
12917 0, /* tp_alloc */
12918 0, /* tp_new */
12919 0, /* tp_free */
12920 0, /* tp_is_gc */
12921 0, /* tp_bases */
12922 0, /* tp_mro */
12923 0, /* tp_cache */
12924 0, /* tp_subclasses */
12925 0, /* tp_weaklist */
12926 0, /* tp_del */
12927 0, /* tp_version_tag */
12928 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010012929};
12930
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012931/*[clinic input]
12932os.scandir
12933
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012934 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012935
12936Return an iterator of DirEntry objects for given path.
12937
BNMetricsb9427072018-11-02 15:20:19 +000012938path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012939is bytes, the names of yielded DirEntry objects will also be bytes; in
12940all other circumstances they will be str.
12941
12942If path is None, uses the path='.'.
12943[clinic start generated code]*/
12944
Victor Stinner6036e442015-03-08 01:58:04 +010012945static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012946os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000012947/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012948{
12949 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010012950#ifdef MS_WINDOWS
12951 wchar_t *path_strW;
12952#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012953 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012954#ifdef HAVE_FDOPENDIR
12955 int fd = -1;
12956#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012957#endif
12958
12959 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12960 if (!iterator)
12961 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012962
12963#ifdef MS_WINDOWS
12964 iterator->handle = INVALID_HANDLE_VALUE;
12965#else
12966 iterator->dirp = NULL;
12967#endif
12968
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012969 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020012970 /* Move the ownership to iterator->path */
12971 path->object = NULL;
12972 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012973
12974#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010012975 iterator->first_time = 1;
12976
12977 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12978 if (!path_strW)
12979 goto error;
12980
12981 Py_BEGIN_ALLOW_THREADS
12982 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12983 Py_END_ALLOW_THREADS
12984
12985 PyMem_Free(path_strW);
12986
12987 if (iterator->handle == INVALID_HANDLE_VALUE) {
12988 path_error(&iterator->path);
12989 goto error;
12990 }
12991#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012992 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012993#ifdef HAVE_FDOPENDIR
12994 if (path->fd != -1) {
12995 /* closedir() closes the FD, so we duplicate it */
12996 fd = _Py_dup(path->fd);
12997 if (fd == -1)
12998 goto error;
12999
13000 Py_BEGIN_ALLOW_THREADS
13001 iterator->dirp = fdopendir(fd);
13002 Py_END_ALLOW_THREADS
13003 }
13004 else
13005#endif
13006 {
13007 if (iterator->path.narrow)
13008 path_str = iterator->path.narrow;
13009 else
13010 path_str = ".";
13011
13012 Py_BEGIN_ALLOW_THREADS
13013 iterator->dirp = opendir(path_str);
13014 Py_END_ALLOW_THREADS
13015 }
Victor Stinner6036e442015-03-08 01:58:04 +010013016
13017 if (!iterator->dirp) {
13018 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013019#ifdef HAVE_FDOPENDIR
13020 if (fd != -1) {
13021 Py_BEGIN_ALLOW_THREADS
13022 close(fd);
13023 Py_END_ALLOW_THREADS
13024 }
13025#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013026 goto error;
13027 }
13028#endif
13029
13030 return (PyObject *)iterator;
13031
13032error:
13033 Py_DECREF(iterator);
13034 return NULL;
13035}
13036
Ethan Furman410ef8e2016-06-04 12:06:26 -070013037/*
13038 Return the file system path representation of the object.
13039
13040 If the object is str or bytes, then allow it to pass through with
13041 an incremented refcount. If the object defines __fspath__(), then
13042 return the result of that method. All other types raise a TypeError.
13043*/
13044PyObject *
13045PyOS_FSPath(PyObject *path)
13046{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013047 /* For error message reasons, this function is manually inlined in
13048 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013049 _Py_IDENTIFIER(__fspath__);
13050 PyObject *func = NULL;
13051 PyObject *path_repr = NULL;
13052
13053 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13054 Py_INCREF(path);
13055 return path;
13056 }
13057
13058 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13059 if (NULL == func) {
13060 return PyErr_Format(PyExc_TypeError,
13061 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013062 "not %.200s",
13063 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013064 }
13065
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013066 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013067 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013068 if (NULL == path_repr) {
13069 return NULL;
13070 }
13071
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013072 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13073 PyErr_Format(PyExc_TypeError,
13074 "expected %.200s.__fspath__() to return str or bytes, "
13075 "not %.200s", Py_TYPE(path)->tp_name,
13076 Py_TYPE(path_repr)->tp_name);
13077 Py_DECREF(path_repr);
13078 return NULL;
13079 }
13080
Ethan Furman410ef8e2016-06-04 12:06:26 -070013081 return path_repr;
13082}
13083
13084/*[clinic input]
13085os.fspath
13086
13087 path: object
13088
13089Return the file system path representation of the object.
13090
Brett Cannonb4f43e92016-06-09 14:32:08 -070013091If the object is str or bytes, then allow it to pass through as-is. If the
13092object defines __fspath__(), then return the result of that method. All other
13093types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013094[clinic start generated code]*/
13095
13096static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013097os_fspath_impl(PyObject *module, PyObject *path)
13098/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013099{
13100 return PyOS_FSPath(path);
13101}
Victor Stinner6036e442015-03-08 01:58:04 +010013102
Victor Stinner9b1f4742016-09-06 16:18:52 -070013103#ifdef HAVE_GETRANDOM_SYSCALL
13104/*[clinic input]
13105os.getrandom
13106
13107 size: Py_ssize_t
13108 flags: int=0
13109
13110Obtain a series of random bytes.
13111[clinic start generated code]*/
13112
13113static PyObject *
13114os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13115/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13116{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013117 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013118 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013119
13120 if (size < 0) {
13121 errno = EINVAL;
13122 return posix_error();
13123 }
13124
Victor Stinnerec2319c2016-09-20 23:00:59 +020013125 bytes = PyBytes_FromStringAndSize(NULL, size);
13126 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013127 PyErr_NoMemory();
13128 return NULL;
13129 }
13130
13131 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013132 n = syscall(SYS_getrandom,
13133 PyBytes_AS_STRING(bytes),
13134 PyBytes_GET_SIZE(bytes),
13135 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013136 if (n < 0 && errno == EINTR) {
13137 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013138 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013139 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013140
13141 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013142 continue;
13143 }
13144 break;
13145 }
13146
13147 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013148 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013149 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013150 }
13151
Victor Stinnerec2319c2016-09-20 23:00:59 +020013152 if (n != size) {
13153 _PyBytes_Resize(&bytes, n);
13154 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013155
13156 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013157
13158error:
13159 Py_DECREF(bytes);
13160 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013161}
13162#endif /* HAVE_GETRANDOM_SYSCALL */
13163
Larry Hastings31826802013-10-19 00:09:25 -070013164
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013165static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013166
13167 OS_STAT_METHODDEF
13168 OS_ACCESS_METHODDEF
13169 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013170 OS_CHDIR_METHODDEF
13171 OS_CHFLAGS_METHODDEF
13172 OS_CHMOD_METHODDEF
13173 OS_FCHMOD_METHODDEF
13174 OS_LCHMOD_METHODDEF
13175 OS_CHOWN_METHODDEF
13176 OS_FCHOWN_METHODDEF
13177 OS_LCHOWN_METHODDEF
13178 OS_LCHFLAGS_METHODDEF
13179 OS_CHROOT_METHODDEF
13180 OS_CTERMID_METHODDEF
13181 OS_GETCWD_METHODDEF
13182 OS_GETCWDB_METHODDEF
13183 OS_LINK_METHODDEF
13184 OS_LISTDIR_METHODDEF
13185 OS_LSTAT_METHODDEF
13186 OS_MKDIR_METHODDEF
13187 OS_NICE_METHODDEF
13188 OS_GETPRIORITY_METHODDEF
13189 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013190 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013191 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013192 OS_READLINK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013193 OS_RENAME_METHODDEF
13194 OS_REPLACE_METHODDEF
13195 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013196 OS_SYMLINK_METHODDEF
13197 OS_SYSTEM_METHODDEF
13198 OS_UMASK_METHODDEF
13199 OS_UNAME_METHODDEF
13200 OS_UNLINK_METHODDEF
13201 OS_REMOVE_METHODDEF
13202 OS_UTIME_METHODDEF
13203 OS_TIMES_METHODDEF
13204 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013205 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013206 OS_EXECV_METHODDEF
13207 OS_EXECVE_METHODDEF
13208 OS_SPAWNV_METHODDEF
13209 OS_SPAWNVE_METHODDEF
13210 OS_FORK1_METHODDEF
13211 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013212 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013213 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13214 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13215 OS_SCHED_GETPARAM_METHODDEF
13216 OS_SCHED_GETSCHEDULER_METHODDEF
13217 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13218 OS_SCHED_SETPARAM_METHODDEF
13219 OS_SCHED_SETSCHEDULER_METHODDEF
13220 OS_SCHED_YIELD_METHODDEF
13221 OS_SCHED_SETAFFINITY_METHODDEF
13222 OS_SCHED_GETAFFINITY_METHODDEF
13223 OS_OPENPTY_METHODDEF
13224 OS_FORKPTY_METHODDEF
13225 OS_GETEGID_METHODDEF
13226 OS_GETEUID_METHODDEF
13227 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013228#ifdef HAVE_GETGROUPLIST
13229 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13230#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013231 OS_GETGROUPS_METHODDEF
13232 OS_GETPID_METHODDEF
13233 OS_GETPGRP_METHODDEF
13234 OS_GETPPID_METHODDEF
13235 OS_GETUID_METHODDEF
13236 OS_GETLOGIN_METHODDEF
13237 OS_KILL_METHODDEF
13238 OS_KILLPG_METHODDEF
13239 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013240#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013241 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013242#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013243 OS_SETUID_METHODDEF
13244 OS_SETEUID_METHODDEF
13245 OS_SETREUID_METHODDEF
13246 OS_SETGID_METHODDEF
13247 OS_SETEGID_METHODDEF
13248 OS_SETREGID_METHODDEF
13249 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013250#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013251 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013252#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013253 OS_GETPGID_METHODDEF
13254 OS_SETPGRP_METHODDEF
13255 OS_WAIT_METHODDEF
13256 OS_WAIT3_METHODDEF
13257 OS_WAIT4_METHODDEF
13258 OS_WAITID_METHODDEF
13259 OS_WAITPID_METHODDEF
13260 OS_GETSID_METHODDEF
13261 OS_SETSID_METHODDEF
13262 OS_SETPGID_METHODDEF
13263 OS_TCGETPGRP_METHODDEF
13264 OS_TCSETPGRP_METHODDEF
13265 OS_OPEN_METHODDEF
13266 OS_CLOSE_METHODDEF
13267 OS_CLOSERANGE_METHODDEF
13268 OS_DEVICE_ENCODING_METHODDEF
13269 OS_DUP_METHODDEF
13270 OS_DUP2_METHODDEF
13271 OS_LOCKF_METHODDEF
13272 OS_LSEEK_METHODDEF
13273 OS_READ_METHODDEF
13274 OS_READV_METHODDEF
13275 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013276 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013277 OS_WRITE_METHODDEF
13278 OS_WRITEV_METHODDEF
13279 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013280 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013281#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013282 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013283 posix_sendfile__doc__},
13284#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013285 OS_FSTAT_METHODDEF
13286 OS_ISATTY_METHODDEF
13287 OS_PIPE_METHODDEF
13288 OS_PIPE2_METHODDEF
13289 OS_MKFIFO_METHODDEF
13290 OS_MKNOD_METHODDEF
13291 OS_MAJOR_METHODDEF
13292 OS_MINOR_METHODDEF
13293 OS_MAKEDEV_METHODDEF
13294 OS_FTRUNCATE_METHODDEF
13295 OS_TRUNCATE_METHODDEF
13296 OS_POSIX_FALLOCATE_METHODDEF
13297 OS_POSIX_FADVISE_METHODDEF
13298 OS_PUTENV_METHODDEF
13299 OS_UNSETENV_METHODDEF
13300 OS_STRERROR_METHODDEF
13301 OS_FCHDIR_METHODDEF
13302 OS_FSYNC_METHODDEF
13303 OS_SYNC_METHODDEF
13304 OS_FDATASYNC_METHODDEF
13305 OS_WCOREDUMP_METHODDEF
13306 OS_WIFCONTINUED_METHODDEF
13307 OS_WIFSTOPPED_METHODDEF
13308 OS_WIFSIGNALED_METHODDEF
13309 OS_WIFEXITED_METHODDEF
13310 OS_WEXITSTATUS_METHODDEF
13311 OS_WTERMSIG_METHODDEF
13312 OS_WSTOPSIG_METHODDEF
13313 OS_FSTATVFS_METHODDEF
13314 OS_STATVFS_METHODDEF
13315 OS_CONFSTR_METHODDEF
13316 OS_SYSCONF_METHODDEF
13317 OS_FPATHCONF_METHODDEF
13318 OS_PATHCONF_METHODDEF
13319 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013320 OS__GETFULLPATHNAME_METHODDEF
13321 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013322 OS__GETDISKUSAGE_METHODDEF
13323 OS__GETFINALPATHNAME_METHODDEF
13324 OS__GETVOLUMEPATHNAME_METHODDEF
13325 OS_GETLOADAVG_METHODDEF
13326 OS_URANDOM_METHODDEF
13327 OS_SETRESUID_METHODDEF
13328 OS_SETRESGID_METHODDEF
13329 OS_GETRESUID_METHODDEF
13330 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013331
Larry Hastings2f936352014-08-05 14:04:04 +100013332 OS_GETXATTR_METHODDEF
13333 OS_SETXATTR_METHODDEF
13334 OS_REMOVEXATTR_METHODDEF
13335 OS_LISTXATTR_METHODDEF
13336
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013337#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13338 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13339#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013340 OS_CPU_COUNT_METHODDEF
13341 OS_GET_INHERITABLE_METHODDEF
13342 OS_SET_INHERITABLE_METHODDEF
13343 OS_GET_HANDLE_INHERITABLE_METHODDEF
13344 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013345#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013346 OS_GET_BLOCKING_METHODDEF
13347 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013348#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013349 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013350 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013351 OS_GETRANDOM_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000013352 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013353};
13354
13355
Brian Curtin52173d42010-12-02 18:29:18 +000013356#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013357static int
Brian Curtin52173d42010-12-02 18:29:18 +000013358enable_symlink()
13359{
13360 HANDLE tok;
13361 TOKEN_PRIVILEGES tok_priv;
13362 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000013363
13364 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013365 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013366
13367 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013368 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013369
13370 tok_priv.PrivilegeCount = 1;
13371 tok_priv.Privileges[0].Luid = luid;
13372 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
13373
13374 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
13375 sizeof(TOKEN_PRIVILEGES),
13376 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013377 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013378
Brian Curtin3b4499c2010-12-28 14:31:47 +000013379 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
13380 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000013381}
13382#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
13383
Barry Warsaw4a342091996-12-19 23:50:02 +000013384static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013385all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013386{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013387#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013388 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013389#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013390#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013391 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013392#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013393#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013394 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013395#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013396#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013397 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013398#endif
Fred Drakec9680921999-12-13 16:37:25 +000013399#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013400 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013401#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013402#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013403 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013404#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013405#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013406 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013407#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013408#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013409 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013410#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013411#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013412 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013413#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013414#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013415 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013416#endif
13417#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013418 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013419#endif
13420#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013421 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013422#endif
13423#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013424 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013425#endif
13426#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013427 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013428#endif
13429#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013430 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013431#endif
13432#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013433 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013434#endif
13435#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013436 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013437#endif
13438#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013439 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013440#endif
13441#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013442 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013443#endif
13444#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013445 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013446#endif
13447#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013448 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013449#endif
13450#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013451 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013452#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013453#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013454 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013455#endif
13456#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013457 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013458#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013459#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013460 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013461#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013462#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013463 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013464#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013465#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013466#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013467 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013468#endif
13469#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013470 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013471#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013472#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013473#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013474 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013475#endif
13476#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013477 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013478#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013479#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013480 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013481#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013482#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013483 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013484#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013485#ifdef O_TMPFILE
13486 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13487#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013488#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013489 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013490#endif
13491#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013492 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013493#endif
13494#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013495 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013496#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013497#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013498 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013499#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013500#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013501 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013502#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013503
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013504
Jesus Cea94363612012-06-22 18:32:07 +020013505#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013506 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013507#endif
13508#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013509 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013510#endif
13511
Tim Peters5aa91602002-01-30 05:46:57 +000013512/* MS Windows */
13513#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013514 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013515 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013516#endif
13517#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013518 /* Optimize for short life (keep in memory). */
13519 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013520 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013521#endif
13522#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013523 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013524 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013525#endif
13526#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013527 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013528 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013529#endif
13530#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013531 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013532 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013533#endif
13534
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013535/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013536#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013537 /* Send a SIGIO signal whenever input or output
13538 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013539 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013540#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013541#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013542 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013543 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013544#endif
13545#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013546 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013547 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013548#endif
13549#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013550 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013551 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013552#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013553#ifdef O_NOLINKS
13554 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013555 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013556#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013557#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013558 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013559 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013560#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013561
Victor Stinner8c62be82010-05-06 00:08:46 +000013562 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013563#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013564 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013565#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013566#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013567 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013568#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013569#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013570 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013571#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013572#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013573 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013574#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013575#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013576 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013577#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013578#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013579 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013580#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013581#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013582 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013583#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013584#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013585 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013586#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013587#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013588 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013589#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013590#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013591 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013592#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013593#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013594 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013595#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013596#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013597 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013598#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013599#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013600 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013601#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013602#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013603 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013604#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013605#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013606 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013607#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013608#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013609 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013610#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013611#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013612 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013613#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013614
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013615 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013616#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013617 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013618#endif /* ST_RDONLY */
13619#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013620 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013621#endif /* ST_NOSUID */
13622
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013623 /* GNU extensions */
13624#ifdef ST_NODEV
13625 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13626#endif /* ST_NODEV */
13627#ifdef ST_NOEXEC
13628 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13629#endif /* ST_NOEXEC */
13630#ifdef ST_SYNCHRONOUS
13631 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13632#endif /* ST_SYNCHRONOUS */
13633#ifdef ST_MANDLOCK
13634 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13635#endif /* ST_MANDLOCK */
13636#ifdef ST_WRITE
13637 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13638#endif /* ST_WRITE */
13639#ifdef ST_APPEND
13640 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13641#endif /* ST_APPEND */
13642#ifdef ST_NOATIME
13643 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13644#endif /* ST_NOATIME */
13645#ifdef ST_NODIRATIME
13646 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13647#endif /* ST_NODIRATIME */
13648#ifdef ST_RELATIME
13649 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13650#endif /* ST_RELATIME */
13651
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013652 /* FreeBSD sendfile() constants */
13653#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013654 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013655#endif
13656#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013657 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013658#endif
13659#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013660 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013661#endif
13662
Ross Lagerwall7807c352011-03-17 20:20:30 +020013663 /* constants for posix_fadvise */
13664#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013665 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013666#endif
13667#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013668 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013669#endif
13670#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013671 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013672#endif
13673#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013674 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013675#endif
13676#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013677 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013678#endif
13679#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013680 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013681#endif
13682
13683 /* constants for waitid */
13684#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013685 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13686 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13687 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013688#endif
13689#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013690 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013691#endif
13692#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013693 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013694#endif
13695#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013696 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013697#endif
13698#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013699 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013700#endif
13701#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013702 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013703#endif
13704#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013705 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013706#endif
13707#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013708 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013709#endif
13710
13711 /* constants for lockf */
13712#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013713 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013714#endif
13715#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013716 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013717#endif
13718#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013719 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013720#endif
13721#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013722 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013723#endif
13724
Pablo Galindo4defba32018-01-27 16:16:37 +000013725#ifdef RWF_DSYNC
13726 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
13727#endif
13728#ifdef RWF_HIPRI
13729 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
13730#endif
13731#ifdef RWF_SYNC
13732 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
13733#endif
13734#ifdef RWF_NOWAIT
13735 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
13736#endif
13737
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013738/* constants for posix_spawn */
13739#ifdef HAVE_POSIX_SPAWN
13740 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
13741 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
13742 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
13743#endif
13744
Guido van Rossum246bc171999-02-01 23:54:31 +000013745#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013746 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
13747 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
13748 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
13749 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
13750 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000013751#endif
13752
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013753#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013754#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013755 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013756#endif
13757#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013758 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013759#endif
13760#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013761 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013762#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013763#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080013764 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013765#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013766#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013767 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013768#endif
13769#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013770 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013771#endif
13772#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013773 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013774#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013775#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013776 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013777#endif
13778#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013779 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013780#endif
13781#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013782 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013783#endif
13784#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013785 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013786#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013787#endif
13788
Benjamin Peterson9428d532011-09-14 11:45:52 -040013789#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013790 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
13791 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
13792 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040013793#endif
13794
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013795#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013796 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013797#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013798#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013799 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013800#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013801#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013802 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013803#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013804#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013805 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013806#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013807#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013808 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013809#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013810#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013811 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013812#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013813#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013814 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013815#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010013816#if HAVE_DECL_RTLD_MEMBER
13817 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
13818#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020013819
Victor Stinner9b1f4742016-09-06 16:18:52 -070013820#ifdef HAVE_GETRANDOM_SYSCALL
13821 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
13822 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
13823#endif
13824
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013825#if defined(__APPLE__)
13826 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
13827#endif
13828
Victor Stinner8c62be82010-05-06 00:08:46 +000013829 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000013830}
13831
13832
Martin v. Löwis1a214512008-06-11 05:26:20 +000013833static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000013834 PyModuleDef_HEAD_INIT,
13835 MODNAME,
13836 posix__doc__,
13837 -1,
13838 posix_methods,
13839 NULL,
13840 NULL,
13841 NULL,
13842 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000013843};
13844
13845
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013846static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070013847
13848#ifdef HAVE_FACCESSAT
13849 "HAVE_FACCESSAT",
13850#endif
13851
13852#ifdef HAVE_FCHDIR
13853 "HAVE_FCHDIR",
13854#endif
13855
13856#ifdef HAVE_FCHMOD
13857 "HAVE_FCHMOD",
13858#endif
13859
13860#ifdef HAVE_FCHMODAT
13861 "HAVE_FCHMODAT",
13862#endif
13863
13864#ifdef HAVE_FCHOWN
13865 "HAVE_FCHOWN",
13866#endif
13867
Larry Hastings00964ed2013-08-12 13:49:30 -040013868#ifdef HAVE_FCHOWNAT
13869 "HAVE_FCHOWNAT",
13870#endif
13871
Larry Hastings9cf065c2012-06-22 16:30:09 -070013872#ifdef HAVE_FEXECVE
13873 "HAVE_FEXECVE",
13874#endif
13875
13876#ifdef HAVE_FDOPENDIR
13877 "HAVE_FDOPENDIR",
13878#endif
13879
Georg Brandl306336b2012-06-24 12:55:33 +020013880#ifdef HAVE_FPATHCONF
13881 "HAVE_FPATHCONF",
13882#endif
13883
Larry Hastings9cf065c2012-06-22 16:30:09 -070013884#ifdef HAVE_FSTATAT
13885 "HAVE_FSTATAT",
13886#endif
13887
13888#ifdef HAVE_FSTATVFS
13889 "HAVE_FSTATVFS",
13890#endif
13891
Steve Dowerfe0a41a2015-03-20 19:50:46 -070013892#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020013893 "HAVE_FTRUNCATE",
13894#endif
13895
Larry Hastings9cf065c2012-06-22 16:30:09 -070013896#ifdef HAVE_FUTIMENS
13897 "HAVE_FUTIMENS",
13898#endif
13899
13900#ifdef HAVE_FUTIMES
13901 "HAVE_FUTIMES",
13902#endif
13903
13904#ifdef HAVE_FUTIMESAT
13905 "HAVE_FUTIMESAT",
13906#endif
13907
13908#ifdef HAVE_LINKAT
13909 "HAVE_LINKAT",
13910#endif
13911
13912#ifdef HAVE_LCHFLAGS
13913 "HAVE_LCHFLAGS",
13914#endif
13915
13916#ifdef HAVE_LCHMOD
13917 "HAVE_LCHMOD",
13918#endif
13919
13920#ifdef HAVE_LCHOWN
13921 "HAVE_LCHOWN",
13922#endif
13923
13924#ifdef HAVE_LSTAT
13925 "HAVE_LSTAT",
13926#endif
13927
13928#ifdef HAVE_LUTIMES
13929 "HAVE_LUTIMES",
13930#endif
13931
13932#ifdef HAVE_MKDIRAT
13933 "HAVE_MKDIRAT",
13934#endif
13935
13936#ifdef HAVE_MKFIFOAT
13937 "HAVE_MKFIFOAT",
13938#endif
13939
13940#ifdef HAVE_MKNODAT
13941 "HAVE_MKNODAT",
13942#endif
13943
13944#ifdef HAVE_OPENAT
13945 "HAVE_OPENAT",
13946#endif
13947
13948#ifdef HAVE_READLINKAT
13949 "HAVE_READLINKAT",
13950#endif
13951
13952#ifdef HAVE_RENAMEAT
13953 "HAVE_RENAMEAT",
13954#endif
13955
13956#ifdef HAVE_SYMLINKAT
13957 "HAVE_SYMLINKAT",
13958#endif
13959
13960#ifdef HAVE_UNLINKAT
13961 "HAVE_UNLINKAT",
13962#endif
13963
13964#ifdef HAVE_UTIMENSAT
13965 "HAVE_UTIMENSAT",
13966#endif
13967
13968#ifdef MS_WINDOWS
13969 "MS_WINDOWS",
13970#endif
13971
13972 NULL
13973};
13974
13975
Mark Hammondfe51c6d2002-08-02 02:27:13 +000013976PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000013977INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000013978{
Victor Stinner8c62be82010-05-06 00:08:46 +000013979 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070013980 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013981 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000013982
Brian Curtin52173d42010-12-02 18:29:18 +000013983#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013984 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000013985#endif
13986
Victor Stinner8c62be82010-05-06 00:08:46 +000013987 m = PyModule_Create(&posixmodule);
13988 if (m == NULL)
13989 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000013990
Victor Stinner8c62be82010-05-06 00:08:46 +000013991 /* Initialize environ dictionary */
13992 v = convertenviron();
13993 Py_XINCREF(v);
13994 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
13995 return NULL;
13996 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000013997
Victor Stinner8c62be82010-05-06 00:08:46 +000013998 if (all_ins(m))
13999 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014000
Victor Stinner8c62be82010-05-06 00:08:46 +000014001 if (setup_confname_tables(m))
14002 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014003
Victor Stinner8c62be82010-05-06 00:08:46 +000014004 Py_INCREF(PyExc_OSError);
14005 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014006
Guido van Rossumb3d39562000-01-31 18:41:26 +000014007#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000014008 if (posix_putenv_garbage == NULL)
14009 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014010#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014011
Victor Stinner8c62be82010-05-06 00:08:46 +000014012 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020014013#if defined(HAVE_WAITID) && !defined(__APPLE__)
14014 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014015 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
14016 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014017 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014018 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014019#endif
14020
Christian Heimes25827622013-10-12 01:27:08 +020014021 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014022 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14023 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14024 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014025 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14026 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014027 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014028 }
14029 structseq_new = StatResultType->tp_new;
14030 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014031
Christian Heimes25827622013-10-12 01:27:08 +020014032 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014033 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14034 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014035 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014036 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014037#ifdef NEED_TICKS_PER_SECOND
14038# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014039 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014040# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014041 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014042# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014043 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014044# endif
14045#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014046
William Orr81574b82018-10-01 22:19:56 -070014047#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014048 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014049 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14050 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014051 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014052 }
14053 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014054#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014055
14056 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014057 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14058 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014059 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014060 }
Victor Stinner6036e442015-03-08 01:58:04 +010014061
14062 /* initialize scandir types */
14063 if (PyType_Ready(&ScandirIteratorType) < 0)
14064 return NULL;
14065 if (PyType_Ready(&DirEntryType) < 0)
14066 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014067 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014068#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014069 Py_INCREF((PyObject*) WaitidResultType);
14070 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014071#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014072 Py_INCREF((PyObject*) StatResultType);
14073 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14074 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014075 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014076 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014077
14078#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014079 Py_INCREF(SchedParamType);
14080 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014081#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014082
Larry Hastings605a62d2012-06-24 04:33:36 -070014083 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014084 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14085 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014086 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014087 }
14088 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014089
14090 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014091 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14092 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014093 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014094 }
14095 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014096
Thomas Wouters477c8d52006-05-27 19:21:47 +000014097#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014098 /*
14099 * Step 2 of weak-linking support on Mac OS X.
14100 *
14101 * The code below removes functions that are not available on the
14102 * currently active platform.
14103 *
14104 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014105 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014106 * OSX 10.4.
14107 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014108#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014109 if (fstatvfs == NULL) {
14110 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14111 return NULL;
14112 }
14113 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014114#endif /* HAVE_FSTATVFS */
14115
14116#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014117 if (statvfs == NULL) {
14118 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14119 return NULL;
14120 }
14121 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014122#endif /* HAVE_STATVFS */
14123
14124# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014125 if (lchown == NULL) {
14126 if (PyObject_DelAttrString(m, "lchown") == -1) {
14127 return NULL;
14128 }
14129 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014130#endif /* HAVE_LCHOWN */
14131
14132
14133#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014134
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014135 Py_INCREF(TerminalSizeType);
14136 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014137
Larry Hastings6fe20b32012-04-19 15:07:49 -070014138 billion = PyLong_FromLong(1000000000);
14139 if (!billion)
14140 return NULL;
14141
Larry Hastings9cf065c2012-06-22 16:30:09 -070014142 /* suppress "function not used" warnings */
14143 {
14144 int ignored;
14145 fd_specified("", -1);
14146 follow_symlinks_specified("", 1);
14147 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14148 dir_fd_converter(Py_None, &ignored);
14149 dir_fd_unavailable(Py_None, &ignored);
14150 }
14151
14152 /*
14153 * provide list of locally available functions
14154 * so os.py can populate support_* lists
14155 */
14156 list = PyList_New(0);
14157 if (!list)
14158 return NULL;
14159 for (trace = have_functions; *trace; trace++) {
14160 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14161 if (!unicode)
14162 return NULL;
14163 if (PyList_Append(list, unicode))
14164 return NULL;
14165 Py_DECREF(unicode);
14166 }
14167 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014168
14169 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014170 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014171
14172 initialized = 1;
14173
Victor Stinner8c62be82010-05-06 00:08:46 +000014174 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014175}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014176
14177#ifdef __cplusplus
14178}
14179#endif