blob: 0f6bd1490434f76dc73506c29dfcfedbea5dfe2c [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();
432
Victor Stinnercaba55b2018-08-03 15:33:52 +0200433 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200434}
435
436static int
437register_at_forker(PyObject **lst, PyObject *func)
438{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700439 if (func == NULL) /* nothing to register? do nothing. */
440 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200441 if (*lst == NULL) {
442 *lst = PyList_New(0);
443 if (*lst == NULL)
444 return -1;
445 }
446 return PyList_Append(*lst, func);
447}
448#endif
449
450/* Legacy wrapper */
451void
452PyOS_AfterFork(void)
453{
454#ifdef HAVE_FORK
455 PyOS_AfterFork_Child();
456#endif
457}
458
459
Victor Stinner6036e442015-03-08 01:58:04 +0100460#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200461/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700462void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
463void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200464 ULONG, struct _Py_stat_struct *);
465#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700466
467#ifdef MS_WINDOWS
468static int
469win32_warn_bytes_api()
470{
471 return PyErr_WarnEx(PyExc_DeprecationWarning,
472 "The Windows bytes API has been deprecated, "
473 "use Unicode filenames instead",
474 1);
475}
476#endif
477
478
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200479#ifndef MS_WINDOWS
480PyObject *
481_PyLong_FromUid(uid_t uid)
482{
483 if (uid == (uid_t)-1)
484 return PyLong_FromLong(-1);
485 return PyLong_FromUnsignedLong(uid);
486}
487
488PyObject *
489_PyLong_FromGid(gid_t gid)
490{
491 if (gid == (gid_t)-1)
492 return PyLong_FromLong(-1);
493 return PyLong_FromUnsignedLong(gid);
494}
495
496int
497_Py_Uid_Converter(PyObject *obj, void *p)
498{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700499 uid_t uid;
500 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200501 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200502 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700503 unsigned long uresult;
504
505 index = PyNumber_Index(obj);
506 if (index == NULL) {
507 PyErr_Format(PyExc_TypeError,
508 "uid should be integer, not %.200s",
509 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200510 return 0;
511 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700512
513 /*
514 * Handling uid_t is complicated for two reasons:
515 * * Although uid_t is (always?) unsigned, it still
516 * accepts -1.
517 * * We don't know its size in advance--it may be
518 * bigger than an int, or it may be smaller than
519 * a long.
520 *
521 * So a bit of defensive programming is in order.
522 * Start with interpreting the value passed
523 * in as a signed long and see if it works.
524 */
525
526 result = PyLong_AsLongAndOverflow(index, &overflow);
527
528 if (!overflow) {
529 uid = (uid_t)result;
530
531 if (result == -1) {
532 if (PyErr_Occurred())
533 goto fail;
534 /* It's a legitimate -1, we're done. */
535 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200536 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700537
538 /* Any other negative number is disallowed. */
539 if (result < 0)
540 goto underflow;
541
542 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200543 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700544 (long)uid != result)
545 goto underflow;
546 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200547 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700548
549 if (overflow < 0)
550 goto underflow;
551
552 /*
553 * Okay, the value overflowed a signed long. If it
554 * fits in an *unsigned* long, it may still be okay,
555 * as uid_t may be unsigned long on this platform.
556 */
557 uresult = PyLong_AsUnsignedLong(index);
558 if (PyErr_Occurred()) {
559 if (PyErr_ExceptionMatches(PyExc_OverflowError))
560 goto overflow;
561 goto fail;
562 }
563
564 uid = (uid_t)uresult;
565
566 /*
567 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
568 * but this value would get interpreted as (uid_t)-1 by chown
569 * and its siblings. That's not what the user meant! So we
570 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100571 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700572 */
573 if (uid == (uid_t)-1)
574 goto overflow;
575
576 /* Ensure the value wasn't truncated. */
577 if (sizeof(uid_t) < sizeof(long) &&
578 (unsigned long)uid != uresult)
579 goto overflow;
580 /* fallthrough */
581
582success:
583 Py_DECREF(index);
584 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200585 return 1;
586
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700587underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200588 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700589 "uid is less than minimum");
590 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200591
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700592overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200593 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700594 "uid is greater than maximum");
595 /* fallthrough */
596
597fail:
598 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200599 return 0;
600}
601
602int
603_Py_Gid_Converter(PyObject *obj, void *p)
604{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700605 gid_t gid;
606 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200607 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200608 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700609 unsigned long uresult;
610
611 index = PyNumber_Index(obj);
612 if (index == NULL) {
613 PyErr_Format(PyExc_TypeError,
614 "gid should be integer, not %.200s",
615 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200616 return 0;
617 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700618
619 /*
620 * Handling gid_t is complicated for two reasons:
621 * * Although gid_t is (always?) unsigned, it still
622 * accepts -1.
623 * * We don't know its size in advance--it may be
624 * bigger than an int, or it may be smaller than
625 * a long.
626 *
627 * So a bit of defensive programming is in order.
628 * Start with interpreting the value passed
629 * in as a signed long and see if it works.
630 */
631
632 result = PyLong_AsLongAndOverflow(index, &overflow);
633
634 if (!overflow) {
635 gid = (gid_t)result;
636
637 if (result == -1) {
638 if (PyErr_Occurred())
639 goto fail;
640 /* It's a legitimate -1, we're done. */
641 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200642 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700643
644 /* Any other negative number is disallowed. */
645 if (result < 0) {
646 goto underflow;
647 }
648
649 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200650 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700651 (long)gid != result)
652 goto underflow;
653 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200654 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700655
656 if (overflow < 0)
657 goto underflow;
658
659 /*
660 * Okay, the value overflowed a signed long. If it
661 * fits in an *unsigned* long, it may still be okay,
662 * as gid_t may be unsigned long on this platform.
663 */
664 uresult = PyLong_AsUnsignedLong(index);
665 if (PyErr_Occurred()) {
666 if (PyErr_ExceptionMatches(PyExc_OverflowError))
667 goto overflow;
668 goto fail;
669 }
670
671 gid = (gid_t)uresult;
672
673 /*
674 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
675 * but this value would get interpreted as (gid_t)-1 by chown
676 * and its siblings. That's not what the user meant! So we
677 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100678 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700679 */
680 if (gid == (gid_t)-1)
681 goto overflow;
682
683 /* Ensure the value wasn't truncated. */
684 if (sizeof(gid_t) < sizeof(long) &&
685 (unsigned long)gid != uresult)
686 goto overflow;
687 /* fallthrough */
688
689success:
690 Py_DECREF(index);
691 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200692 return 1;
693
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700694underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200695 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700696 "gid is less than minimum");
697 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200698
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700699overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200700 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700701 "gid is greater than maximum");
702 /* fallthrough */
703
704fail:
705 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200706 return 0;
707}
708#endif /* MS_WINDOWS */
709
710
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700711#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800712
713
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200714#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
715static int
716_Py_Dev_Converter(PyObject *obj, void *p)
717{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200718 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200719 if (PyErr_Occurred())
720 return 0;
721 return 1;
722}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800723#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200724
725
Larry Hastings9cf065c2012-06-22 16:30:09 -0700726#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400727/*
728 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
729 * without the int cast, the value gets interpreted as uint (4291925331),
730 * which doesn't play nicely with all the initializer lines in this file that
731 * look like this:
732 * int dir_fd = DEFAULT_DIR_FD;
733 */
734#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700735#else
736#define DEFAULT_DIR_FD (-100)
737#endif
738
739static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300740_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200741{
742 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700743 long long_value;
744
745 PyObject *index = PyNumber_Index(o);
746 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700747 return 0;
748 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700749
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300750 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700751 long_value = PyLong_AsLongAndOverflow(index, &overflow);
752 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300753 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200754 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700755 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700756 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700757 return 0;
758 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200759 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700760 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700761 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700762 return 0;
763 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700764
Larry Hastings9cf065c2012-06-22 16:30:09 -0700765 *p = (int)long_value;
766 return 1;
767}
768
769static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200770dir_fd_converter(PyObject *o, void *p)
771{
772 if (o == Py_None) {
773 *(int *)p = DEFAULT_DIR_FD;
774 return 1;
775 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300776 else if (PyIndex_Check(o)) {
777 return _fd_converter(o, (int *)p);
778 }
779 else {
780 PyErr_Format(PyExc_TypeError,
781 "argument should be integer or None, not %.200s",
782 Py_TYPE(o)->tp_name);
783 return 0;
784 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700785}
786
787
Larry Hastings9cf065c2012-06-22 16:30:09 -0700788/*
789 * A PyArg_ParseTuple "converter" function
790 * that handles filesystem paths in the manner
791 * preferred by the os module.
792 *
793 * path_converter accepts (Unicode) strings and their
794 * subclasses, and bytes and their subclasses. What
795 * it does with the argument depends on the platform:
796 *
797 * * On Windows, if we get a (Unicode) string we
798 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700799 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700800 *
801 * * On all other platforms, strings are encoded
802 * to bytes using PyUnicode_FSConverter, then we
803 * extract the char * from the bytes object and
804 * return that.
805 *
806 * path_converter also optionally accepts signed
807 * integers (representing open file descriptors) instead
808 * of path strings.
809 *
810 * Input fields:
811 * path.nullable
812 * If nonzero, the path is permitted to be None.
813 * path.allow_fd
814 * If nonzero, the path is permitted to be a file handle
815 * (a signed int) instead of a string.
816 * path.function_name
817 * If non-NULL, path_converter will use that as the name
818 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700819 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700820 * path.argument_name
821 * If non-NULL, path_converter will use that as the name
822 * of the parameter in error messages.
823 * (If path.argument_name is NULL it uses "path".)
824 *
825 * Output fields:
826 * path.wide
827 * Points to the path if it was expressed as Unicode
828 * and was not encoded. (Only used on Windows.)
829 * path.narrow
830 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700831 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000832 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700833 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700834 * path.fd
835 * Contains a file descriptor if path.accept_fd was true
836 * and the caller provided a signed integer instead of any
837 * sort of string.
838 *
839 * WARNING: if your "path" parameter is optional, and is
840 * unspecified, path_converter will never get called.
841 * So if you set allow_fd, you *MUST* initialize path.fd = -1
842 * yourself!
843 * path.length
844 * The length of the path in characters, if specified as
845 * a string.
846 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800847 * The original object passed in (if get a PathLike object,
848 * the result of PyOS_FSPath() is treated as the original object).
849 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700850 * path.cleanup
851 * For internal use only. May point to a temporary object.
852 * (Pay no attention to the man behind the curtain.)
853 *
854 * At most one of path.wide or path.narrow will be non-NULL.
855 * If path was None and path.nullable was set,
856 * or if path was an integer and path.allow_fd was set,
857 * both path.wide and path.narrow will be NULL
858 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200859 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700860 * path_converter takes care to not write to the path_t
861 * unless it's successful. However it must reset the
862 * "cleanup" field each time it's called.
863 *
864 * Use as follows:
865 * path_t path;
866 * memset(&path, 0, sizeof(path));
867 * PyArg_ParseTuple(args, "O&", path_converter, &path);
868 * // ... use values from path ...
869 * path_cleanup(&path);
870 *
871 * (Note that if PyArg_Parse fails you don't need to call
872 * path_cleanup(). However it is safe to do so.)
873 */
874typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100875 const char *function_name;
876 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700877 int nullable;
878 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300879 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700880#ifdef MS_WINDOWS
881 BOOL narrow;
882#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300883 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700884#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700885 int fd;
886 Py_ssize_t length;
887 PyObject *object;
888 PyObject *cleanup;
889} path_t;
890
Steve Dowercc16be82016-09-08 10:35:16 -0700891#ifdef MS_WINDOWS
892#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
893 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
894#else
Larry Hastings2f936352014-08-05 14:04:04 +1000895#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
896 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700897#endif
Larry Hastings31826802013-10-19 00:09:25 -0700898
Larry Hastings9cf065c2012-06-22 16:30:09 -0700899static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800900path_cleanup(path_t *path)
901{
902 Py_CLEAR(path->object);
903 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700904}
905
906static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300907path_converter(PyObject *o, void *p)
908{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700909 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800910 PyObject *bytes = NULL;
911 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700912 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300913 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700914#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800915 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700916 const wchar_t *wide;
917#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700918
919#define FORMAT_EXCEPTION(exc, fmt) \
920 PyErr_Format(exc, "%s%s" fmt, \
921 path->function_name ? path->function_name : "", \
922 path->function_name ? ": " : "", \
923 path->argument_name ? path->argument_name : "path")
924
925 /* Py_CLEANUP_SUPPORTED support */
926 if (o == NULL) {
927 path_cleanup(path);
928 return 1;
929 }
930
Brett Cannon3f9183b2016-08-26 14:44:48 -0700931 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800932 path->object = path->cleanup = NULL;
933 /* path->object owns a reference to the original object */
934 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700935
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300936 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700937 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700938#ifdef MS_WINDOWS
939 path->narrow = FALSE;
940#else
Larry Hastings9cf065c2012-06-22 16:30:09 -0700941 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700942#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700943 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800944 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700945 }
946
Brett Cannon3f9183b2016-08-26 14:44:48 -0700947 /* Only call this here so that we don't treat the return value of
948 os.fspath() as an fd or buffer. */
949 is_index = path->allow_fd && PyIndex_Check(o);
950 is_buffer = PyObject_CheckBuffer(o);
951 is_bytes = PyBytes_Check(o);
952 is_unicode = PyUnicode_Check(o);
953
954 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
955 /* Inline PyOS_FSPath() for better error messages. */
956 _Py_IDENTIFIER(__fspath__);
957 PyObject *func = NULL;
958
959 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
960 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800961 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700962 }
Xiang Zhang04316c42017-01-08 23:26:57 +0800963 /* still owns a reference to the original object */
964 Py_DECREF(o);
965 o = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700966 Py_DECREF(func);
967 if (NULL == o) {
968 goto error_exit;
969 }
970 else if (PyUnicode_Check(o)) {
971 is_unicode = 1;
972 }
973 else if (PyBytes_Check(o)) {
974 is_bytes = 1;
975 }
976 else {
Xiang Zhang04316c42017-01-08 23:26:57 +0800977 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700978 }
979 }
980
981 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700982#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +0200983 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +0100984 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +0800985 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700986 }
Victor Stinner59799a82013-11-13 14:17:30 +0100987 if (length > 32767) {
988 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +0800989 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700990 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300991 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300992 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +0800993 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300994 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700995
996 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +0800997 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700998 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +0800999 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001000#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001001 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001002 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001003 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001004#endif
1005 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001006 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001007 bytes = o;
1008 Py_INCREF(bytes);
1009 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001010 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001011 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001012 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001013 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1014 "%s%s%s should be %s, not %.200s",
1015 path->function_name ? path->function_name : "",
1016 path->function_name ? ": " : "",
1017 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001018 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1019 "integer or None" :
1020 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1021 path->nullable ? "string, bytes, os.PathLike or None" :
1022 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001023 Py_TYPE(o)->tp_name)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001024 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001025 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001026 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001027 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001028 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001029 }
1030 }
Steve Dowercc16be82016-09-08 10:35:16 -07001031 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001032 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001033 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001034 }
1035 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001036#ifdef MS_WINDOWS
1037 path->narrow = FALSE;
1038#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001039 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001040#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001041 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001042 }
1043 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001044 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001045 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1046 path->function_name ? path->function_name : "",
1047 path->function_name ? ": " : "",
1048 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001049 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1050 "integer or None" :
1051 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1052 path->nullable ? "string, bytes, os.PathLike or None" :
1053 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001054 Py_TYPE(o)->tp_name);
Xiang Zhang04316c42017-01-08 23:26:57 +08001055 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001056 }
1057
Larry Hastings9cf065c2012-06-22 16:30:09 -07001058 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001059 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001060 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001061 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001062 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001063 }
1064
Steve Dowercc16be82016-09-08 10:35:16 -07001065#ifdef MS_WINDOWS
1066 wo = PyUnicode_DecodeFSDefaultAndSize(
1067 narrow,
1068 length
1069 );
1070 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001071 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001072 }
1073
Xiang Zhang04316c42017-01-08 23:26:57 +08001074 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001075 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001076 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001077 }
1078 if (length > 32767) {
1079 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001080 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001081 }
1082 if (wcslen(wide) != length) {
1083 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001084 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001085 }
1086 path->wide = wide;
1087 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001088 path->cleanup = wo;
1089 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001090#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001091 path->wide = NULL;
1092 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001093 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001094 /* Still a reference owned by path->object, don't have to
1095 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001096 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001097 }
1098 else {
1099 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001100 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001101#endif
1102 path->fd = -1;
1103
1104 success_exit:
1105 path->length = length;
1106 path->object = o;
1107 return Py_CLEANUP_SUPPORTED;
1108
1109 error_exit:
1110 Py_XDECREF(o);
1111 Py_XDECREF(bytes);
1112#ifdef MS_WINDOWS
1113 Py_XDECREF(wo);
1114#endif
1115 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001116}
1117
1118static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001119argument_unavailable_error(const char *function_name, const char *argument_name)
1120{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001121 PyErr_Format(PyExc_NotImplementedError,
1122 "%s%s%s unavailable on this platform",
1123 (function_name != NULL) ? function_name : "",
1124 (function_name != NULL) ? ": ": "",
1125 argument_name);
1126}
1127
1128static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001129dir_fd_unavailable(PyObject *o, void *p)
1130{
1131 int dir_fd;
1132 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001133 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001134 if (dir_fd != DEFAULT_DIR_FD) {
1135 argument_unavailable_error(NULL, "dir_fd");
1136 return 0;
1137 }
1138 *(int *)p = dir_fd;
1139 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001140}
1141
1142static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001143fd_specified(const char *function_name, int fd)
1144{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001145 if (fd == -1)
1146 return 0;
1147
1148 argument_unavailable_error(function_name, "fd");
1149 return 1;
1150}
1151
1152static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001153follow_symlinks_specified(const char *function_name, int follow_symlinks)
1154{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001155 if (follow_symlinks)
1156 return 0;
1157
1158 argument_unavailable_error(function_name, "follow_symlinks");
1159 return 1;
1160}
1161
1162static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001163path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1164{
Steve Dowercc16be82016-09-08 10:35:16 -07001165 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1166#ifndef MS_WINDOWS
1167 && !path->narrow
1168#endif
1169 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001170 PyErr_Format(PyExc_ValueError,
1171 "%s: can't specify dir_fd without matching path",
1172 function_name);
1173 return 1;
1174 }
1175 return 0;
1176}
1177
1178static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001179dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1180{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001181 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1182 PyErr_Format(PyExc_ValueError,
1183 "%s: can't specify both dir_fd and fd",
1184 function_name);
1185 return 1;
1186 }
1187 return 0;
1188}
1189
1190static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001191fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1192 int follow_symlinks)
1193{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001194 if ((fd > 0) && (!follow_symlinks)) {
1195 PyErr_Format(PyExc_ValueError,
1196 "%s: cannot use fd and follow_symlinks together",
1197 function_name);
1198 return 1;
1199 }
1200 return 0;
1201}
1202
1203static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001204dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1205 int follow_symlinks)
1206{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1208 PyErr_Format(PyExc_ValueError,
1209 "%s: cannot use dir_fd and follow_symlinks together",
1210 function_name);
1211 return 1;
1212 }
1213 return 0;
1214}
1215
Larry Hastings2f936352014-08-05 14:04:04 +10001216#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001217 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001218#else
Larry Hastings2f936352014-08-05 14:04:04 +10001219 typedef off_t Py_off_t;
1220#endif
1221
1222static int
1223Py_off_t_converter(PyObject *arg, void *addr)
1224{
1225#ifdef HAVE_LARGEFILE_SUPPORT
1226 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1227#else
1228 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001229#endif
1230 if (PyErr_Occurred())
1231 return 0;
1232 return 1;
1233}
Larry Hastings2f936352014-08-05 14:04:04 +10001234
1235static PyObject *
1236PyLong_FromPy_off_t(Py_off_t offset)
1237{
1238#ifdef HAVE_LARGEFILE_SUPPORT
1239 return PyLong_FromLongLong(offset);
1240#else
1241 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001242#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001243}
1244
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001245#ifdef HAVE_SIGSET_T
1246/* Convert an iterable of integers to a sigset.
1247 Return 1 on success, return 0 and raise an exception on error. */
1248int
1249_Py_Sigset_Converter(PyObject *obj, void *addr)
1250{
1251 sigset_t *mask = (sigset_t *)addr;
1252 PyObject *iterator, *item;
1253 long signum;
1254 int overflow;
1255
1256 if (sigemptyset(mask)) {
1257 /* Probably only if mask == NULL. */
1258 PyErr_SetFromErrno(PyExc_OSError);
1259 return 0;
1260 }
1261
1262 iterator = PyObject_GetIter(obj);
1263 if (iterator == NULL) {
1264 return 0;
1265 }
1266
1267 while ((item = PyIter_Next(iterator)) != NULL) {
1268 signum = PyLong_AsLongAndOverflow(item, &overflow);
1269 Py_DECREF(item);
1270 if (signum <= 0 || signum >= NSIG) {
1271 if (overflow || signum != -1 || !PyErr_Occurred()) {
1272 PyErr_Format(PyExc_ValueError,
1273 "signal number %ld out of range", signum);
1274 }
1275 goto error;
1276 }
1277 if (sigaddset(mask, (int)signum)) {
1278 if (errno != EINVAL) {
1279 /* Probably impossible */
1280 PyErr_SetFromErrno(PyExc_OSError);
1281 goto error;
1282 }
1283 /* For backwards compatibility, allow idioms such as
1284 * `range(1, NSIG)` but warn about invalid signal numbers
1285 */
1286 const char msg[] =
1287 "invalid signal number %ld, please use valid_signals()";
1288 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1289 goto error;
1290 }
1291 }
1292 }
1293 if (!PyErr_Occurred()) {
1294 Py_DECREF(iterator);
1295 return 1;
1296 }
1297
1298error:
1299 Py_DECREF(iterator);
1300 return 0;
1301}
1302#endif /* HAVE_SIGSET_T */
1303
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001304#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001305
1306static int
Brian Curtind25aef52011-06-13 15:16:04 -05001307win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001308{
Martin Panter70214ad2016-08-04 02:38:59 +00001309 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1310 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001311 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001312
1313 if (0 == DeviceIoControl(
1314 reparse_point_handle,
1315 FSCTL_GET_REPARSE_POINT,
1316 NULL, 0, /* in buffer */
1317 target_buffer, sizeof(target_buffer),
1318 &n_bytes_returned,
1319 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001320 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001321
1322 if (reparse_tag)
1323 *reparse_tag = rdb->ReparseTag;
1324
Brian Curtind25aef52011-06-13 15:16:04 -05001325 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001326}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001327
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001328#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001329
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001330/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001331#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001332/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001333** environ directly, we must obtain it with _NSGetEnviron(). See also
1334** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001335*/
1336#include <crt_externs.h>
1337static char **environ;
1338#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001339extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001340#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001341
Barry Warsaw53699e91996-12-10 23:23:01 +00001342static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001343convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001344{
Victor Stinner8c62be82010-05-06 00:08:46 +00001345 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001346#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001347 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001348#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001349 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001350#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001351
Victor Stinner8c62be82010-05-06 00:08:46 +00001352 d = PyDict_New();
1353 if (d == NULL)
1354 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001355#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001356 if (environ == NULL)
1357 environ = *_NSGetEnviron();
1358#endif
1359#ifdef MS_WINDOWS
1360 /* _wenviron must be initialized in this way if the program is started
1361 through main() instead of wmain(). */
1362 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001363 e = _wenviron;
Victor Stinner8c62be82010-05-06 00:08:46 +00001364#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001365 e = environ;
1366#endif
1367 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001368 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001369 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001370 PyObject *k;
1371 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001372#ifdef MS_WINDOWS
1373 const wchar_t *p = wcschr(*e, L'=');
1374#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001375 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001376#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001377 if (p == NULL)
1378 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001379#ifdef MS_WINDOWS
1380 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1381#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001382 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001383#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001384 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001385 Py_DECREF(d);
1386 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001387 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001388#ifdef MS_WINDOWS
1389 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1390#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001391 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001392#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001393 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001394 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001395 Py_DECREF(d);
1396 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001397 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001398 if (PyDict_GetItemWithError(d, k) == NULL) {
1399 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1400 Py_DECREF(v);
1401 Py_DECREF(k);
1402 Py_DECREF(d);
1403 return NULL;
1404 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001405 }
1406 Py_DECREF(k);
1407 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001408 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001409 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001410}
1411
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001412/* Set a POSIX-specific error from errno, and return NULL */
1413
Barry Warsawd58d7641998-07-23 16:14:40 +00001414static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001415posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001416{
Victor Stinner8c62be82010-05-06 00:08:46 +00001417 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001418}
Mark Hammondef8b6542001-05-13 08:04:26 +00001419
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001420#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001421static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001422win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001423{
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 /* XXX We should pass the function name along in the future.
1425 (winreg.c also wants to pass the function name.)
1426 This would however require an additional param to the
1427 Windows error object, which is non-trivial.
1428 */
1429 errno = GetLastError();
1430 if (filename)
1431 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1432 else
1433 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001434}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001435
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001436static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001437win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001438{
1439 /* XXX - see win32_error for comments on 'function' */
1440 errno = GetLastError();
1441 if (filename)
1442 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001443 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001444 errno,
1445 filename);
1446 else
1447 return PyErr_SetFromWindowsErr(errno);
1448}
1449
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001450#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001451
Larry Hastings9cf065c2012-06-22 16:30:09 -07001452static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001453posix_path_object_error(PyObject *path)
1454{
1455 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1456}
1457
1458static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001459path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001460{
1461#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001462 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1463 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001464#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001465 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001466#endif
1467}
1468
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001469static PyObject *
1470path_object_error2(PyObject *path, PyObject *path2)
1471{
1472#ifdef MS_WINDOWS
1473 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1474 PyExc_OSError, 0, path, path2);
1475#else
1476 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1477#endif
1478}
1479
1480static PyObject *
1481path_error(path_t *path)
1482{
1483 return path_object_error(path->object);
1484}
Larry Hastings31826802013-10-19 00:09:25 -07001485
Larry Hastingsb0827312014-02-09 22:05:19 -08001486static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001487posix_path_error(path_t *path)
1488{
1489 return posix_path_object_error(path->object);
1490}
1491
1492static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001493path_error2(path_t *path, path_t *path2)
1494{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001495 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001496}
1497
1498
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001499/* POSIX generic methods */
1500
Larry Hastings2f936352014-08-05 14:04:04 +10001501static int
1502fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001503{
Victor Stinner8c62be82010-05-06 00:08:46 +00001504 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001505 int *pointer = (int *)p;
1506 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001507 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001508 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001509 *pointer = fd;
1510 return 1;
1511}
1512
1513static PyObject *
1514posix_fildes_fd(int fd, int (*func)(int))
1515{
1516 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001517 int async_err = 0;
1518
1519 do {
1520 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001521 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001522 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001523 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001524 Py_END_ALLOW_THREADS
1525 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1526 if (res != 0)
1527 return (!async_err) ? posix_error() : NULL;
1528 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001529}
Guido van Rossum21142a01999-01-08 21:05:37 +00001530
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001531
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001532#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001533/* This is a reimplementation of the C library's chdir function,
1534 but one that produces Win32 errors instead of DOS error codes.
1535 chdir is essentially a wrapper around SetCurrentDirectory; however,
1536 it also needs to set "magic" environment variables indicating
1537 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001538static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001539win32_wchdir(LPCWSTR path)
1540{
Victor Stinnered537822015-12-13 21:40:26 +01001541 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001542 int result;
1543 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001544
Victor Stinner8c62be82010-05-06 00:08:46 +00001545 if(!SetCurrentDirectoryW(path))
1546 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001547 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001548 if (!result)
1549 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001550 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001551 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001552 if (!new_path) {
1553 SetLastError(ERROR_OUTOFMEMORY);
1554 return FALSE;
1555 }
1556 result = GetCurrentDirectoryW(result, new_path);
1557 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001558 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001559 return FALSE;
1560 }
1561 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001562 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1563 wcsncmp(new_path, L"//", 2) == 0);
1564 if (!is_unc_like_path) {
1565 env[1] = new_path[0];
1566 result = SetEnvironmentVariableW(env, new_path);
1567 }
Victor Stinnered537822015-12-13 21:40:26 +01001568 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001569 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001570 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001571}
1572#endif
1573
Martin v. Löwis14694662006-02-03 12:54:16 +00001574#ifdef MS_WINDOWS
1575/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1576 - time stamps are restricted to second resolution
1577 - file modification times suffer from forth-and-back conversions between
1578 UTC and local time
1579 Therefore, we implement our own stat, based on the Win32 API directly.
1580*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001581#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001582#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001583
Victor Stinner6036e442015-03-08 01:58:04 +01001584static void
Steve Dowercc16be82016-09-08 10:35:16 -07001585find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1586 BY_HANDLE_FILE_INFORMATION *info,
1587 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001588{
1589 memset(info, 0, sizeof(*info));
1590 info->dwFileAttributes = pFileData->dwFileAttributes;
1591 info->ftCreationTime = pFileData->ftCreationTime;
1592 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1593 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1594 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1595 info->nFileSizeLow = pFileData->nFileSizeLow;
1596/* info->nNumberOfLinks = 1; */
1597 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1598 *reparse_tag = pFileData->dwReserved0;
1599 else
1600 *reparse_tag = 0;
1601}
1602
Guido van Rossumd8faa362007-04-27 19:54:29 +00001603static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001604attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001605{
Victor Stinner8c62be82010-05-06 00:08:46 +00001606 HANDLE hFindFile;
1607 WIN32_FIND_DATAW FileData;
1608 hFindFile = FindFirstFileW(pszFile, &FileData);
1609 if (hFindFile == INVALID_HANDLE_VALUE)
1610 return FALSE;
1611 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001612 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001613 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001614}
1615
Brian Curtind25aef52011-06-13 15:16:04 -05001616static BOOL
1617get_target_path(HANDLE hdl, wchar_t **target_path)
1618{
1619 int buf_size, result_length;
1620 wchar_t *buf;
1621
1622 /* We have a good handle to the target, use it to determine
1623 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001624 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1625 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001626 if(!buf_size)
1627 return FALSE;
1628
Victor Stinnerc36674a2016-03-16 14:30:16 +01001629 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001630 if (!buf) {
1631 SetLastError(ERROR_OUTOFMEMORY);
1632 return FALSE;
1633 }
1634
Steve Dower2ea51c92015-03-20 21:49:12 -07001635 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001636 buf, buf_size, VOLUME_NAME_DOS);
1637
1638 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001639 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001640 return FALSE;
1641 }
1642
1643 if(!CloseHandle(hdl)) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001644 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001645 return FALSE;
1646 }
1647
1648 buf[result_length] = 0;
1649
1650 *target_path = buf;
1651 return TRUE;
1652}
1653
1654static int
Steve Dowercc16be82016-09-08 10:35:16 -07001655win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001656 BOOL traverse)
1657{
Victor Stinner26de69d2011-06-17 15:15:38 +02001658 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001659 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001660 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001661 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001662 wchar_t *target_path;
Steve Dowercc16be82016-09-08 10:35:16 -07001663 const wchar_t *dot;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001664
Steve Dowercc16be82016-09-08 10:35:16 -07001665 hFile = CreateFileW(
Brian Curtinf5e76d02010-11-24 13:14:05 +00001666 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001667 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001668 0, /* share mode */
1669 NULL, /* security attributes */
1670 OPEN_EXISTING,
1671 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001672 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1673 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001674 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001675 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1676 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001677 NULL);
1678
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001679 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001680 /* Either the target doesn't exist, or we don't have access to
1681 get a handle to it. If the former, we need to return an error.
1682 If the latter, we can use attributes_from_dir. */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001683 DWORD lastError = GetLastError();
1684 if (lastError != ERROR_ACCESS_DENIED &&
1685 lastError != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001686 return -1;
1687 /* Could not get attributes on open file. Fall back to
1688 reading the directory. */
1689 if (!attributes_from_dir(path, &info, &reparse_tag))
1690 /* Very strange. This should not fail now */
1691 return -1;
1692 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1693 if (traverse) {
1694 /* Should traverse, but could not open reparse point handle */
Berker Peksag0b4dc482016-09-17 15:49:59 +03001695 SetLastError(lastError);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001696 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001697 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001698 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001699 } else {
1700 if (!GetFileInformationByHandle(hFile, &info)) {
1701 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001702 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001703 }
1704 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001705 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1706 return -1;
1707
1708 /* Close the outer open file handle now that we're about to
1709 reopen it with different flags. */
1710 if (!CloseHandle(hFile))
1711 return -1;
1712
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001713 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001714 /* In order to call GetFinalPathNameByHandle we need to open
1715 the file without the reparse handling flag set. */
Brian Curtind25aef52011-06-13 15:16:04 -05001716 hFile2 = CreateFileW(
1717 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1718 NULL, OPEN_EXISTING,
1719 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1720 NULL);
1721 if (hFile2 == INVALID_HANDLE_VALUE)
1722 return -1;
1723
1724 if (!get_target_path(hFile2, &target_path))
1725 return -1;
1726
Steve Dowercc16be82016-09-08 10:35:16 -07001727 code = win32_xstat_impl(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001728 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001729 return code;
1730 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001731 } else
1732 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001733 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001734 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001735
1736 /* Set S_IEXEC if it is an .exe, .bat, ... */
1737 dot = wcsrchr(path, '.');
1738 if (dot) {
1739 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1740 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1741 result->st_mode |= 0111;
1742 }
1743 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001744}
1745
1746static int
Steve Dowercc16be82016-09-08 10:35:16 -07001747win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001748{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001749 /* Protocol violation: we explicitly clear errno, instead of
1750 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001751 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001752 errno = 0;
1753 return code;
1754}
Brian Curtind25aef52011-06-13 15:16:04 -05001755/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001756
1757 In Posix, stat automatically traverses symlinks and returns the stat
1758 structure for the target. In Windows, the equivalent GetFileAttributes by
1759 default does not traverse symlinks and instead returns attributes for
1760 the symlink.
1761
1762 Therefore, win32_lstat will get the attributes traditionally, and
1763 win32_stat will first explicitly resolve the symlink target and then will
Steve Dowercc16be82016-09-08 10:35:16 -07001764 call win32_lstat on that result. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001765
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001766static int
Steve Dowercc16be82016-09-08 10:35:16 -07001767win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001768{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001769 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001770}
1771
Victor Stinner8c62be82010-05-06 00:08:46 +00001772static int
Steve Dowercc16be82016-09-08 10:35:16 -07001773win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001774{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001775 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001776}
1777
Martin v. Löwis14694662006-02-03 12:54:16 +00001778#endif /* MS_WINDOWS */
1779
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001780PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001781"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001782This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001783 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001784or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1785\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001786Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1787or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001788\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001789See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001790
1791static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 {"st_mode", "protection bits"},
1793 {"st_ino", "inode"},
1794 {"st_dev", "device"},
1795 {"st_nlink", "number of hard links"},
1796 {"st_uid", "user ID of owner"},
1797 {"st_gid", "group ID of owner"},
1798 {"st_size", "total size, in bytes"},
1799 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1800 {NULL, "integer time of last access"},
1801 {NULL, "integer time of last modification"},
1802 {NULL, "integer time of last change"},
1803 {"st_atime", "time of last access"},
1804 {"st_mtime", "time of last modification"},
1805 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001806 {"st_atime_ns", "time of last access in nanoseconds"},
1807 {"st_mtime_ns", "time of last modification in nanoseconds"},
1808 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001809#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001810 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001811#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001812#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001813 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001814#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001815#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001816 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001817#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001818#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001819 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001820#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001821#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001822 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001823#endif
1824#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001825 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001826#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001827#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1828 {"st_file_attributes", "Windows file attribute bits"},
1829#endif
jcea6c51d512018-01-28 14:00:08 +01001830#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1831 {"st_fstype", "Type of filesystem"},
1832#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001834};
1835
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001836#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001837#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001838#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001839#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001840#endif
1841
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001842#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001843#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1844#else
1845#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1846#endif
1847
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001848#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001849#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1850#else
1851#define ST_RDEV_IDX ST_BLOCKS_IDX
1852#endif
1853
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001854#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1855#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1856#else
1857#define ST_FLAGS_IDX ST_RDEV_IDX
1858#endif
1859
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001860#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001861#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001862#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001863#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001864#endif
1865
1866#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1867#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1868#else
1869#define ST_BIRTHTIME_IDX ST_GEN_IDX
1870#endif
1871
Zachary Ware63f277b2014-06-19 09:46:37 -05001872#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1873#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1874#else
1875#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1876#endif
1877
jcea6c51d512018-01-28 14:00:08 +01001878#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1879#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
1880#else
1881#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
1882#endif
1883
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001884static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001885 "stat_result", /* name */
1886 stat_result__doc__, /* doc */
1887 stat_result_fields,
1888 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001889};
1890
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001891PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001892"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1893This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001894 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001895or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001896\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001897See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001898
1899static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001900 {"f_bsize", },
1901 {"f_frsize", },
1902 {"f_blocks", },
1903 {"f_bfree", },
1904 {"f_bavail", },
1905 {"f_files", },
1906 {"f_ffree", },
1907 {"f_favail", },
1908 {"f_flag", },
1909 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01001910 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00001911 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001912};
1913
1914static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001915 "statvfs_result", /* name */
1916 statvfs_result__doc__, /* doc */
1917 statvfs_result_fields,
1918 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001919};
1920
Ross Lagerwall7807c352011-03-17 20:20:30 +02001921#if defined(HAVE_WAITID) && !defined(__APPLE__)
1922PyDoc_STRVAR(waitid_result__doc__,
1923"waitid_result: Result from waitid.\n\n\
1924This object may be accessed either as a tuple of\n\
1925 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1926or via the attributes si_pid, si_uid, and so on.\n\
1927\n\
1928See os.waitid for more information.");
1929
1930static PyStructSequence_Field waitid_result_fields[] = {
1931 {"si_pid", },
1932 {"si_uid", },
1933 {"si_signo", },
1934 {"si_status", },
1935 {"si_code", },
1936 {0}
1937};
1938
1939static PyStructSequence_Desc waitid_result_desc = {
1940 "waitid_result", /* name */
1941 waitid_result__doc__, /* doc */
1942 waitid_result_fields,
1943 5
1944};
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001945static PyTypeObject* WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +02001946#endif
1947
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001948static int initialized;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001949static PyTypeObject* StatResultType;
1950static PyTypeObject* StatVFSResultType;
William Orr81574b82018-10-01 22:19:56 -07001951#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08001952static PyTypeObject* SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001953#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001954static newfunc structseq_new;
1955
1956static PyObject *
1957statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1958{
Victor Stinner8c62be82010-05-06 00:08:46 +00001959 PyStructSequence *result;
1960 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001961
Victor Stinner8c62be82010-05-06 00:08:46 +00001962 result = (PyStructSequence*)structseq_new(type, args, kwds);
1963 if (!result)
1964 return NULL;
1965 /* If we have been initialized from a tuple,
1966 st_?time might be set to None. Initialize it
1967 from the int slots. */
1968 for (i = 7; i <= 9; i++) {
1969 if (result->ob_item[i+3] == Py_None) {
1970 Py_DECREF(Py_None);
1971 Py_INCREF(result->ob_item[i]);
1972 result->ob_item[i+3] = result->ob_item[i];
1973 }
1974 }
1975 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001976}
1977
1978
Larry Hastings6fe20b32012-04-19 15:07:49 -07001979static PyObject *billion = NULL;
1980
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001981static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01001982fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00001983{
Larry Hastings6fe20b32012-04-19 15:07:49 -07001984 PyObject *s = _PyLong_FromTime_t(sec);
1985 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
1986 PyObject *s_in_ns = NULL;
1987 PyObject *ns_total = NULL;
1988 PyObject *float_s = NULL;
1989
1990 if (!(s && ns_fractional))
1991 goto exit;
1992
1993 s_in_ns = PyNumber_Multiply(s, billion);
1994 if (!s_in_ns)
1995 goto exit;
1996
1997 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
1998 if (!ns_total)
1999 goto exit;
2000
Victor Stinner01b5aab2017-10-24 02:02:00 -07002001 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2002 if (!float_s) {
2003 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002004 }
2005
2006 PyStructSequence_SET_ITEM(v, index, s);
2007 PyStructSequence_SET_ITEM(v, index+3, float_s);
2008 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2009 s = NULL;
2010 float_s = NULL;
2011 ns_total = NULL;
2012exit:
2013 Py_XDECREF(s);
2014 Py_XDECREF(ns_fractional);
2015 Py_XDECREF(s_in_ns);
2016 Py_XDECREF(ns_total);
2017 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002018}
2019
Tim Peters5aa91602002-01-30 05:46:57 +00002020/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002021 (used by posix_stat() and posix_fstat()) */
2022static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002023_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002024{
Victor Stinner8c62be82010-05-06 00:08:46 +00002025 unsigned long ansec, mnsec, cnsec;
Eddie Elizondo474eedf2018-11-13 04:09:31 -08002026 PyObject *v = PyStructSequence_New(StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002027 if (v == NULL)
2028 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002029
Victor Stinner8c62be82010-05-06 00:08:46 +00002030 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002031 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002032 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002033#ifdef MS_WINDOWS
2034 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002035#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002036 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002037#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002038 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002039#if defined(MS_WINDOWS)
2040 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2041 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2042#else
2043 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2044 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2045#endif
xdegaye50e86032017-05-22 11:15:08 +02002046 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2047 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002048
Martin v. Löwis14694662006-02-03 12:54:16 +00002049#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002050 ansec = st->st_atim.tv_nsec;
2051 mnsec = st->st_mtim.tv_nsec;
2052 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002053#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002054 ansec = st->st_atimespec.tv_nsec;
2055 mnsec = st->st_mtimespec.tv_nsec;
2056 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002057#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002058 ansec = st->st_atime_nsec;
2059 mnsec = st->st_mtime_nsec;
2060 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002061#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002062 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002063#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002064 fill_time(v, 7, st->st_atime, ansec);
2065 fill_time(v, 8, st->st_mtime, mnsec);
2066 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002067
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002068#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002069 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2070 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002071#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002072#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002073 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2074 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002075#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002076#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002077 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2078 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002079#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002080#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002081 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2082 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002083#endif
2084#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002086 PyObject *val;
2087 unsigned long bsec,bnsec;
2088 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002089#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002090 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002091#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002092 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002093#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002094 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002095 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2096 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002098#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002099#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002100 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2101 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002102#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002103#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2104 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2105 PyLong_FromUnsignedLong(st->st_file_attributes));
2106#endif
jcea6c51d512018-01-28 14:00:08 +01002107#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2108 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2109 PyUnicode_FromString(st->st_fstype));
2110#endif
Fred Drake699f3522000-06-29 21:12:41 +00002111
Victor Stinner8c62be82010-05-06 00:08:46 +00002112 if (PyErr_Occurred()) {
2113 Py_DECREF(v);
2114 return NULL;
2115 }
Fred Drake699f3522000-06-29 21:12:41 +00002116
Victor Stinner8c62be82010-05-06 00:08:46 +00002117 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002118}
2119
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002120/* POSIX methods */
2121
Guido van Rossum94f6f721999-01-06 18:42:14 +00002122
2123static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002124posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002125 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002126{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002127 STRUCT_STAT st;
2128 int result;
2129
2130#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2131 if (follow_symlinks_specified(function_name, follow_symlinks))
2132 return NULL;
2133#endif
2134
2135 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2136 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2137 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2138 return NULL;
2139
2140 Py_BEGIN_ALLOW_THREADS
2141 if (path->fd != -1)
2142 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002143#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002144 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002145 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002146 else
Steve Dowercc16be82016-09-08 10:35:16 -07002147 result = win32_lstat(path->wide, &st);
2148#else
2149 else
2150#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002151 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2152 result = LSTAT(path->narrow, &st);
2153 else
Steve Dowercc16be82016-09-08 10:35:16 -07002154#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002155#ifdef HAVE_FSTATAT
2156 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2157 result = fstatat(dir_fd, path->narrow, &st,
2158 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2159 else
Steve Dowercc16be82016-09-08 10:35:16 -07002160#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002161 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002162#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002163 Py_END_ALLOW_THREADS
2164
Victor Stinner292c8352012-10-30 02:17:38 +01002165 if (result != 0) {
2166 return path_error(path);
2167 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002168
2169 return _pystat_fromstructstat(&st);
2170}
2171
Larry Hastings2f936352014-08-05 14:04:04 +10002172/*[python input]
2173
2174for s in """
2175
2176FACCESSAT
2177FCHMODAT
2178FCHOWNAT
2179FSTATAT
2180LINKAT
2181MKDIRAT
2182MKFIFOAT
2183MKNODAT
2184OPENAT
2185READLINKAT
2186SYMLINKAT
2187UNLINKAT
2188
2189""".strip().split():
2190 s = s.strip()
2191 print("""
2192#ifdef HAVE_{s}
2193 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002194#else
Larry Hastings2f936352014-08-05 14:04:04 +10002195 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002196#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002197""".rstrip().format(s=s))
2198
2199for s in """
2200
2201FCHDIR
2202FCHMOD
2203FCHOWN
2204FDOPENDIR
2205FEXECVE
2206FPATHCONF
2207FSTATVFS
2208FTRUNCATE
2209
2210""".strip().split():
2211 s = s.strip()
2212 print("""
2213#ifdef HAVE_{s}
2214 #define PATH_HAVE_{s} 1
2215#else
2216 #define PATH_HAVE_{s} 0
2217#endif
2218
2219""".rstrip().format(s=s))
2220[python start generated code]*/
2221
2222#ifdef HAVE_FACCESSAT
2223 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2224#else
2225 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2226#endif
2227
2228#ifdef HAVE_FCHMODAT
2229 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2230#else
2231 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2232#endif
2233
2234#ifdef HAVE_FCHOWNAT
2235 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2236#else
2237 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2238#endif
2239
2240#ifdef HAVE_FSTATAT
2241 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2242#else
2243 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2244#endif
2245
2246#ifdef HAVE_LINKAT
2247 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2248#else
2249 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2250#endif
2251
2252#ifdef HAVE_MKDIRAT
2253 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2254#else
2255 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2256#endif
2257
2258#ifdef HAVE_MKFIFOAT
2259 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2260#else
2261 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2262#endif
2263
2264#ifdef HAVE_MKNODAT
2265 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2266#else
2267 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2268#endif
2269
2270#ifdef HAVE_OPENAT
2271 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2272#else
2273 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2274#endif
2275
2276#ifdef HAVE_READLINKAT
2277 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2278#else
2279 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2280#endif
2281
2282#ifdef HAVE_SYMLINKAT
2283 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2284#else
2285 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2286#endif
2287
2288#ifdef HAVE_UNLINKAT
2289 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2290#else
2291 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2292#endif
2293
2294#ifdef HAVE_FCHDIR
2295 #define PATH_HAVE_FCHDIR 1
2296#else
2297 #define PATH_HAVE_FCHDIR 0
2298#endif
2299
2300#ifdef HAVE_FCHMOD
2301 #define PATH_HAVE_FCHMOD 1
2302#else
2303 #define PATH_HAVE_FCHMOD 0
2304#endif
2305
2306#ifdef HAVE_FCHOWN
2307 #define PATH_HAVE_FCHOWN 1
2308#else
2309 #define PATH_HAVE_FCHOWN 0
2310#endif
2311
2312#ifdef HAVE_FDOPENDIR
2313 #define PATH_HAVE_FDOPENDIR 1
2314#else
2315 #define PATH_HAVE_FDOPENDIR 0
2316#endif
2317
2318#ifdef HAVE_FEXECVE
2319 #define PATH_HAVE_FEXECVE 1
2320#else
2321 #define PATH_HAVE_FEXECVE 0
2322#endif
2323
2324#ifdef HAVE_FPATHCONF
2325 #define PATH_HAVE_FPATHCONF 1
2326#else
2327 #define PATH_HAVE_FPATHCONF 0
2328#endif
2329
2330#ifdef HAVE_FSTATVFS
2331 #define PATH_HAVE_FSTATVFS 1
2332#else
2333 #define PATH_HAVE_FSTATVFS 0
2334#endif
2335
2336#ifdef HAVE_FTRUNCATE
2337 #define PATH_HAVE_FTRUNCATE 1
2338#else
2339 #define PATH_HAVE_FTRUNCATE 0
2340#endif
2341/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002342
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002343#ifdef MS_WINDOWS
2344 #undef PATH_HAVE_FTRUNCATE
2345 #define PATH_HAVE_FTRUNCATE 1
2346#endif
Larry Hastings31826802013-10-19 00:09:25 -07002347
Larry Hastings61272b72014-01-07 12:41:53 -08002348/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002349
2350class path_t_converter(CConverter):
2351
2352 type = "path_t"
2353 impl_by_reference = True
2354 parse_by_reference = True
2355
2356 converter = 'path_converter'
2357
2358 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002359 # right now path_t doesn't support default values.
2360 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002361 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002362 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002363
Larry Hastings2f936352014-08-05 14:04:04 +10002364 if self.c_default not in (None, 'Py_None'):
2365 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002366
2367 self.nullable = nullable
2368 self.allow_fd = allow_fd
2369
Larry Hastings7726ac92014-01-31 22:03:12 -08002370 def pre_render(self):
2371 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002372 if isinstance(value, str):
2373 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002374 return str(int(bool(value)))
2375
2376 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002377 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002378 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002379 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002380 strify(self.nullable),
2381 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002382 )
2383
2384 def cleanup(self):
2385 return "path_cleanup(&" + self.name + ");\n"
2386
2387
2388class dir_fd_converter(CConverter):
2389 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002390
Larry Hastings2f936352014-08-05 14:04:04 +10002391 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002392 if self.default in (unspecified, None):
2393 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002394 if isinstance(requires, str):
2395 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2396 else:
2397 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002398
Larry Hastings2f936352014-08-05 14:04:04 +10002399class fildes_converter(CConverter):
2400 type = 'int'
2401 converter = 'fildes_converter'
2402
2403class uid_t_converter(CConverter):
2404 type = "uid_t"
2405 converter = '_Py_Uid_Converter'
2406
2407class gid_t_converter(CConverter):
2408 type = "gid_t"
2409 converter = '_Py_Gid_Converter'
2410
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002411class dev_t_converter(CConverter):
2412 type = 'dev_t'
2413 converter = '_Py_Dev_Converter'
2414
2415class dev_t_return_converter(unsigned_long_return_converter):
2416 type = 'dev_t'
2417 conversion_fn = '_PyLong_FromDev'
2418 unsigned_cast = '(dev_t)'
2419
Larry Hastings2f936352014-08-05 14:04:04 +10002420class FSConverter_converter(CConverter):
2421 type = 'PyObject *'
2422 converter = 'PyUnicode_FSConverter'
2423 def converter_init(self):
2424 if self.default is not unspecified:
2425 fail("FSConverter_converter does not support default values")
2426 self.c_default = 'NULL'
2427
2428 def cleanup(self):
2429 return "Py_XDECREF(" + self.name + ");\n"
2430
2431class pid_t_converter(CConverter):
2432 type = 'pid_t'
2433 format_unit = '" _Py_PARSE_PID "'
2434
2435class idtype_t_converter(int_converter):
2436 type = 'idtype_t'
2437
2438class id_t_converter(CConverter):
2439 type = 'id_t'
2440 format_unit = '" _Py_PARSE_PID "'
2441
Benjamin Petersonca470632016-09-06 13:47:26 -07002442class intptr_t_converter(CConverter):
2443 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002444 format_unit = '" _Py_PARSE_INTPTR "'
2445
2446class Py_off_t_converter(CConverter):
2447 type = 'Py_off_t'
2448 converter = 'Py_off_t_converter'
2449
2450class Py_off_t_return_converter(long_return_converter):
2451 type = 'Py_off_t'
2452 conversion_fn = 'PyLong_FromPy_off_t'
2453
2454class path_confname_converter(CConverter):
2455 type="int"
2456 converter="conv_path_confname"
2457
2458class confstr_confname_converter(path_confname_converter):
2459 converter='conv_confstr_confname'
2460
2461class sysconf_confname_converter(path_confname_converter):
2462 converter="conv_sysconf_confname"
2463
2464class sched_param_converter(CConverter):
2465 type = 'struct sched_param'
2466 converter = 'convert_sched_param'
2467 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002468
Larry Hastings61272b72014-01-07 12:41:53 -08002469[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002470/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002471
Larry Hastings61272b72014-01-07 12:41:53 -08002472/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002473
Larry Hastings2a727912014-01-16 11:32:01 -08002474os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002475
2476 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002477 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002478 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002479
2480 *
2481
Larry Hastings2f936352014-08-05 14:04:04 +10002482 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002483 If not None, it should be a file descriptor open to a directory,
2484 and path should be a relative string; path will then be relative to
2485 that directory.
2486
2487 follow_symlinks: bool = True
2488 If False, and the last element of the path is a symbolic link,
2489 stat will examine the symbolic link itself instead of the file
2490 the link points to.
2491
2492Perform a stat system call on the given path.
2493
2494dir_fd and follow_symlinks may not be implemented
2495 on your platform. If they are unavailable, using them will raise a
2496 NotImplementedError.
2497
2498It's an error to use dir_fd or follow_symlinks when specifying path as
2499 an open file descriptor.
2500
Larry Hastings61272b72014-01-07 12:41:53 -08002501[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002502
Larry Hastings31826802013-10-19 00:09:25 -07002503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002504os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002505/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002506{
2507 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2508}
2509
Larry Hastings2f936352014-08-05 14:04:04 +10002510
2511/*[clinic input]
2512os.lstat
2513
2514 path : path_t
2515
2516 *
2517
2518 dir_fd : dir_fd(requires='fstatat') = None
2519
2520Perform a stat system call on the given path, without following symbolic links.
2521
2522Like stat(), but do not follow symbolic links.
2523Equivalent to stat(path, follow_symlinks=False).
2524[clinic start generated code]*/
2525
Larry Hastings2f936352014-08-05 14:04:04 +10002526static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002527os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2528/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002529{
2530 int follow_symlinks = 0;
2531 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2532}
Larry Hastings31826802013-10-19 00:09:25 -07002533
Larry Hastings2f936352014-08-05 14:04:04 +10002534
Larry Hastings61272b72014-01-07 12:41:53 -08002535/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002536os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002537
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002538 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002539 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002540
2541 mode: int
2542 Operating-system mode bitfield. Can be F_OK to test existence,
2543 or the inclusive-OR of R_OK, W_OK, and X_OK.
2544
2545 *
2546
Larry Hastings2f936352014-08-05 14:04:04 +10002547 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002548 If not None, it should be a file descriptor open to a directory,
2549 and path should be relative; path will then be relative to that
2550 directory.
2551
2552 effective_ids: bool = False
2553 If True, access will use the effective uid/gid instead of
2554 the real uid/gid.
2555
2556 follow_symlinks: bool = True
2557 If False, and the last element of the path is a symbolic link,
2558 access will examine the symbolic link itself instead of the file
2559 the link points to.
2560
2561Use the real uid/gid to test for access to a path.
2562
2563{parameters}
2564dir_fd, effective_ids, and follow_symlinks may not be implemented
2565 on your platform. If they are unavailable, using them will raise a
2566 NotImplementedError.
2567
2568Note that most operations will use the effective uid/gid, therefore this
2569 routine can be used in a suid/sgid environment to test if the invoking user
2570 has the specified access to the path.
2571
Larry Hastings61272b72014-01-07 12:41:53 -08002572[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002573
Larry Hastings2f936352014-08-05 14:04:04 +10002574static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002575os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002576 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002577/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002578{
Larry Hastings2f936352014-08-05 14:04:04 +10002579 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002580
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002581#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002582 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002583#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002584 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002585#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002586
Larry Hastings9cf065c2012-06-22 16:30:09 -07002587#ifndef HAVE_FACCESSAT
2588 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002589 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002590
2591 if (effective_ids) {
2592 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002593 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002594 }
2595#endif
2596
2597#ifdef MS_WINDOWS
2598 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002599 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002600 Py_END_ALLOW_THREADS
2601
2602 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002603 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002604 * * we didn't get a -1, and
2605 * * write access wasn't requested,
2606 * * or the file isn't read-only,
2607 * * or it's a directory.
2608 * (Directories cannot be read-only on Windows.)
2609 */
Larry Hastings2f936352014-08-05 14:04:04 +10002610 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002611 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002612 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002613 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002614#else
2615
2616 Py_BEGIN_ALLOW_THREADS
2617#ifdef HAVE_FACCESSAT
2618 if ((dir_fd != DEFAULT_DIR_FD) ||
2619 effective_ids ||
2620 !follow_symlinks) {
2621 int flags = 0;
2622 if (!follow_symlinks)
2623 flags |= AT_SYMLINK_NOFOLLOW;
2624 if (effective_ids)
2625 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002626 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002627 }
2628 else
2629#endif
Larry Hastings31826802013-10-19 00:09:25 -07002630 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002631 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002632 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002633#endif
2634
Larry Hastings9cf065c2012-06-22 16:30:09 -07002635 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002636}
2637
Guido van Rossumd371ff11999-01-25 16:12:23 +00002638#ifndef F_OK
2639#define F_OK 0
2640#endif
2641#ifndef R_OK
2642#define R_OK 4
2643#endif
2644#ifndef W_OK
2645#define W_OK 2
2646#endif
2647#ifndef X_OK
2648#define X_OK 1
2649#endif
2650
Larry Hastings31826802013-10-19 00:09:25 -07002651
Guido van Rossumd371ff11999-01-25 16:12:23 +00002652#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002653/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002654os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002655
2656 fd: int
2657 Integer file descriptor handle.
2658
2659 /
2660
2661Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002662[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002663
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002664static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002665os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002666/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002667{
2668 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002669
Larry Hastings31826802013-10-19 00:09:25 -07002670 ret = ttyname(fd);
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002671 if (ret == NULL) {
2672 return posix_error();
2673 }
2674 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002675}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002676#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002677
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002678#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002679/*[clinic input]
2680os.ctermid
2681
2682Return the name of the controlling terminal for this process.
2683[clinic start generated code]*/
2684
Larry Hastings2f936352014-08-05 14:04:04 +10002685static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002686os_ctermid_impl(PyObject *module)
2687/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002688{
Victor Stinner8c62be82010-05-06 00:08:46 +00002689 char *ret;
2690 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002691
Greg Wardb48bc172000-03-01 21:51:56 +00002692#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002693 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002694#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002695 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002696#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002697 if (ret == NULL)
2698 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002699 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002700}
Larry Hastings2f936352014-08-05 14:04:04 +10002701#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002702
Larry Hastings2f936352014-08-05 14:04:04 +10002703
2704/*[clinic input]
2705os.chdir
2706
2707 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2708
2709Change the current working directory to the specified path.
2710
2711path may always be specified as a string.
2712On some platforms, path may also be specified as an open file descriptor.
2713 If this functionality is unavailable, using it raises an exception.
2714[clinic start generated code]*/
2715
Larry Hastings2f936352014-08-05 14:04:04 +10002716static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002717os_chdir_impl(PyObject *module, path_t *path)
2718/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002719{
2720 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002721
2722 Py_BEGIN_ALLOW_THREADS
2723#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002724 /* on unix, success = 0, on windows, success = !0 */
2725 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002726#else
2727#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002728 if (path->fd != -1)
2729 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002730 else
2731#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002732 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002733#endif
2734 Py_END_ALLOW_THREADS
2735
2736 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002737 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002738 }
2739
Larry Hastings2f936352014-08-05 14:04:04 +10002740 Py_RETURN_NONE;
2741}
2742
2743
2744#ifdef HAVE_FCHDIR
2745/*[clinic input]
2746os.fchdir
2747
2748 fd: fildes
2749
2750Change to the directory of the given file descriptor.
2751
2752fd must be opened on a directory, not a file.
2753Equivalent to os.chdir(fd).
2754
2755[clinic start generated code]*/
2756
Fred Drake4d1e64b2002-04-15 19:40:07 +00002757static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002758os_fchdir_impl(PyObject *module, int fd)
2759/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002760{
Larry Hastings2f936352014-08-05 14:04:04 +10002761 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002762}
2763#endif /* HAVE_FCHDIR */
2764
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002765
Larry Hastings2f936352014-08-05 14:04:04 +10002766/*[clinic input]
2767os.chmod
2768
2769 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002770 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002771 On some platforms, path may also be specified as an open file descriptor.
2772 If this functionality is unavailable, using it raises an exception.
2773
2774 mode: int
2775 Operating-system mode bitfield.
2776
2777 *
2778
2779 dir_fd : dir_fd(requires='fchmodat') = None
2780 If not None, it should be a file descriptor open to a directory,
2781 and path should be relative; path will then be relative to that
2782 directory.
2783
2784 follow_symlinks: bool = True
2785 If False, and the last element of the path is a symbolic link,
2786 chmod will modify the symbolic link itself instead of the file
2787 the link points to.
2788
2789Change the access permissions of a file.
2790
2791It is an error to use dir_fd or follow_symlinks when specifying path as
2792 an open file descriptor.
2793dir_fd and follow_symlinks may not be implemented on your platform.
2794 If they are unavailable, using them will raise a NotImplementedError.
2795
2796[clinic start generated code]*/
2797
Larry Hastings2f936352014-08-05 14:04:04 +10002798static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002799os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002800 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002801/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002802{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002803 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002804
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002805#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002806 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002807#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002808
Larry Hastings9cf065c2012-06-22 16:30:09 -07002809#ifdef HAVE_FCHMODAT
2810 int fchmodat_nofollow_unsupported = 0;
2811#endif
2812
Larry Hastings9cf065c2012-06-22 16:30:09 -07002813#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2814 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002815 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002816#endif
2817
2818#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002819 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002820 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01002821 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002822 result = 0;
2823 else {
2824 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002825 attr &= ~FILE_ATTRIBUTE_READONLY;
2826 else
2827 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07002828 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002829 }
2830 Py_END_ALLOW_THREADS
2831
2832 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002833 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002834 }
2835#else /* MS_WINDOWS */
2836 Py_BEGIN_ALLOW_THREADS
2837#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002838 if (path->fd != -1)
2839 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002840 else
2841#endif
2842#ifdef HAVE_LCHMOD
2843 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002844 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002845 else
2846#endif
2847#ifdef HAVE_FCHMODAT
2848 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2849 /*
2850 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2851 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002852 * and then says it isn't implemented yet.
2853 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002854 *
2855 * Once it is supported, os.chmod will automatically
2856 * support dir_fd and follow_symlinks=False. (Hopefully.)
2857 * Until then, we need to be careful what exception we raise.
2858 */
Larry Hastings2f936352014-08-05 14:04:04 +10002859 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002860 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2861 /*
2862 * But wait! We can't throw the exception without allowing threads,
2863 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2864 */
2865 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002866 result &&
2867 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2868 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002869 }
2870 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002871#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002872 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002873 Py_END_ALLOW_THREADS
2874
2875 if (result) {
2876#ifdef HAVE_FCHMODAT
2877 if (fchmodat_nofollow_unsupported) {
2878 if (dir_fd != DEFAULT_DIR_FD)
2879 dir_fd_and_follow_symlinks_invalid("chmod",
2880 dir_fd, follow_symlinks);
2881 else
2882 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08002883 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002884 }
2885 else
2886#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002887 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002888 }
2889#endif
2890
Larry Hastings2f936352014-08-05 14:04:04 +10002891 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002892}
2893
Larry Hastings9cf065c2012-06-22 16:30:09 -07002894
Christian Heimes4e30a842007-11-30 22:12:06 +00002895#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002896/*[clinic input]
2897os.fchmod
2898
2899 fd: int
2900 mode: int
2901
2902Change the access permissions of the file given by file descriptor fd.
2903
2904Equivalent to os.chmod(fd, mode).
2905[clinic start generated code]*/
2906
Larry Hastings2f936352014-08-05 14:04:04 +10002907static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002908os_fchmod_impl(PyObject *module, int fd, int mode)
2909/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002910{
2911 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002912 int async_err = 0;
2913
2914 do {
2915 Py_BEGIN_ALLOW_THREADS
2916 res = fchmod(fd, mode);
2917 Py_END_ALLOW_THREADS
2918 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2919 if (res != 0)
2920 return (!async_err) ? posix_error() : NULL;
2921
Victor Stinner8c62be82010-05-06 00:08:46 +00002922 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002923}
2924#endif /* HAVE_FCHMOD */
2925
Larry Hastings2f936352014-08-05 14:04:04 +10002926
Christian Heimes4e30a842007-11-30 22:12:06 +00002927#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002928/*[clinic input]
2929os.lchmod
2930
2931 path: path_t
2932 mode: int
2933
2934Change the access permissions of a file, without following symbolic links.
2935
2936If path is a symlink, this affects the link itself rather than the target.
2937Equivalent to chmod(path, mode, follow_symlinks=False)."
2938[clinic start generated code]*/
2939
Larry Hastings2f936352014-08-05 14:04:04 +10002940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002941os_lchmod_impl(PyObject *module, path_t *path, int mode)
2942/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002943{
Victor Stinner8c62be82010-05-06 00:08:46 +00002944 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00002945 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10002946 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00002947 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002948 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10002949 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01002950 return NULL;
2951 }
Victor Stinner8c62be82010-05-06 00:08:46 +00002952 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002953}
2954#endif /* HAVE_LCHMOD */
2955
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002956
Thomas Wouterscf297e42007-02-23 15:07:44 +00002957#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10002958/*[clinic input]
2959os.chflags
2960
2961 path: path_t
2962 flags: unsigned_long(bitwise=True)
2963 follow_symlinks: bool=True
2964
2965Set file flags.
2966
2967If follow_symlinks is False, and the last element of the path is a symbolic
2968 link, chflags will change flags on the symbolic link itself instead of the
2969 file the link points to.
2970follow_symlinks may not be implemented on your platform. If it is
2971unavailable, using it will raise a NotImplementedError.
2972
2973[clinic start generated code]*/
2974
Larry Hastings2f936352014-08-05 14:04:04 +10002975static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002976os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04002977 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002978/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002979{
2980 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002981
2982#ifndef HAVE_LCHFLAGS
2983 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002984 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002985#endif
2986
Victor Stinner8c62be82010-05-06 00:08:46 +00002987 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002988#ifdef HAVE_LCHFLAGS
2989 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10002990 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002991 else
2992#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002993 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002994 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002995
Larry Hastings2f936352014-08-05 14:04:04 +10002996 if (result)
2997 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002998
Larry Hastings2f936352014-08-05 14:04:04 +10002999 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003000}
3001#endif /* HAVE_CHFLAGS */
3002
Larry Hastings2f936352014-08-05 14:04:04 +10003003
Thomas Wouterscf297e42007-02-23 15:07:44 +00003004#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003005/*[clinic input]
3006os.lchflags
3007
3008 path: path_t
3009 flags: unsigned_long(bitwise=True)
3010
3011Set file flags.
3012
3013This function will not follow symbolic links.
3014Equivalent to chflags(path, flags, follow_symlinks=False).
3015[clinic start generated code]*/
3016
Larry Hastings2f936352014-08-05 14:04:04 +10003017static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003018os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3019/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003020{
Victor Stinner8c62be82010-05-06 00:08:46 +00003021 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003022 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003023 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003024 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003025 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003026 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003027 }
Victor Stinner292c8352012-10-30 02:17:38 +01003028 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003029}
3030#endif /* HAVE_LCHFLAGS */
3031
Larry Hastings2f936352014-08-05 14:04:04 +10003032
Martin v. Löwis244edc82001-10-04 22:44:26 +00003033#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003034/*[clinic input]
3035os.chroot
3036 path: path_t
3037
3038Change root directory to path.
3039
3040[clinic start generated code]*/
3041
Larry Hastings2f936352014-08-05 14:04:04 +10003042static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003043os_chroot_impl(PyObject *module, path_t *path)
3044/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003045{
3046 int res;
3047 Py_BEGIN_ALLOW_THREADS
3048 res = chroot(path->narrow);
3049 Py_END_ALLOW_THREADS
3050 if (res < 0)
3051 return path_error(path);
3052 Py_RETURN_NONE;
3053}
3054#endif /* HAVE_CHROOT */
3055
Martin v. Löwis244edc82001-10-04 22:44:26 +00003056
Guido van Rossum21142a01999-01-08 21:05:37 +00003057#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003058/*[clinic input]
3059os.fsync
3060
3061 fd: fildes
3062
3063Force write of fd to disk.
3064[clinic start generated code]*/
3065
Larry Hastings2f936352014-08-05 14:04:04 +10003066static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003067os_fsync_impl(PyObject *module, int fd)
3068/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003069{
3070 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003071}
3072#endif /* HAVE_FSYNC */
3073
Larry Hastings2f936352014-08-05 14:04:04 +10003074
Ross Lagerwall7807c352011-03-17 20:20:30 +02003075#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003076/*[clinic input]
3077os.sync
3078
3079Force write of everything to disk.
3080[clinic start generated code]*/
3081
Larry Hastings2f936352014-08-05 14:04:04 +10003082static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003083os_sync_impl(PyObject *module)
3084/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003085{
3086 Py_BEGIN_ALLOW_THREADS
3087 sync();
3088 Py_END_ALLOW_THREADS
3089 Py_RETURN_NONE;
3090}
Larry Hastings2f936352014-08-05 14:04:04 +10003091#endif /* HAVE_SYNC */
3092
Ross Lagerwall7807c352011-03-17 20:20:30 +02003093
Guido van Rossum21142a01999-01-08 21:05:37 +00003094#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003095#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003096extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3097#endif
3098
Larry Hastings2f936352014-08-05 14:04:04 +10003099/*[clinic input]
3100os.fdatasync
3101
3102 fd: fildes
3103
3104Force write of fd to disk without forcing update of metadata.
3105[clinic start generated code]*/
3106
Larry Hastings2f936352014-08-05 14:04:04 +10003107static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003108os_fdatasync_impl(PyObject *module, int fd)
3109/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003110{
3111 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003112}
3113#endif /* HAVE_FDATASYNC */
3114
3115
Fredrik Lundh10723342000-07-10 16:38:09 +00003116#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003117/*[clinic input]
3118os.chown
3119
3120 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003121 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003122
3123 uid: uid_t
3124
3125 gid: gid_t
3126
3127 *
3128
3129 dir_fd : dir_fd(requires='fchownat') = None
3130 If not None, it should be a file descriptor open to a directory,
3131 and path should be relative; path will then be relative to that
3132 directory.
3133
3134 follow_symlinks: bool = True
3135 If False, and the last element of the path is a symbolic link,
3136 stat will examine the symbolic link itself instead of the file
3137 the link points to.
3138
3139Change the owner and group id of path to the numeric uid and gid.\
3140
3141path may always be specified as a string.
3142On some platforms, path may also be specified as an open file descriptor.
3143 If this functionality is unavailable, using it raises an exception.
3144If dir_fd is not None, it should be a file descriptor open to a directory,
3145 and path should be relative; path will then be relative to that directory.
3146If follow_symlinks is False, and the last element of the path is a symbolic
3147 link, chown will modify the symbolic link itself instead of the file the
3148 link points to.
3149It is an error to use dir_fd or follow_symlinks when specifying path as
3150 an open file descriptor.
3151dir_fd and follow_symlinks may not be implemented on your platform.
3152 If they are unavailable, using them will raise a NotImplementedError.
3153
3154[clinic start generated code]*/
3155
Larry Hastings2f936352014-08-05 14:04:04 +10003156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003157os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003158 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003159/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003160{
3161 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003162
3163#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3164 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003165 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003166#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003167 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3168 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3169 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003170
3171#ifdef __APPLE__
3172 /*
3173 * This is for Mac OS X 10.3, which doesn't have lchown.
3174 * (But we still have an lchown symbol because of weak-linking.)
3175 * It doesn't have fchownat either. So there's no possibility
3176 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003177 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003178 if ((!follow_symlinks) && (lchown == NULL)) {
3179 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003180 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003181 }
3182#endif
3183
Victor Stinner8c62be82010-05-06 00:08:46 +00003184 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003185#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003186 if (path->fd != -1)
3187 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003188 else
3189#endif
3190#ifdef HAVE_LCHOWN
3191 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003192 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003193 else
3194#endif
3195#ifdef HAVE_FCHOWNAT
3196 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003197 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003198 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3199 else
3200#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003201 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003202 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003203
Larry Hastings2f936352014-08-05 14:04:04 +10003204 if (result)
3205 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003206
Larry Hastings2f936352014-08-05 14:04:04 +10003207 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003208}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003209#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003210
Larry Hastings2f936352014-08-05 14:04:04 +10003211
Christian Heimes4e30a842007-11-30 22:12:06 +00003212#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003213/*[clinic input]
3214os.fchown
3215
3216 fd: int
3217 uid: uid_t
3218 gid: gid_t
3219
3220Change the owner and group id of the file specified by file descriptor.
3221
3222Equivalent to os.chown(fd, uid, gid).
3223
3224[clinic start generated code]*/
3225
Larry Hastings2f936352014-08-05 14:04:04 +10003226static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003227os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3228/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003229{
Victor Stinner8c62be82010-05-06 00:08:46 +00003230 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003231 int async_err = 0;
3232
3233 do {
3234 Py_BEGIN_ALLOW_THREADS
3235 res = fchown(fd, uid, gid);
3236 Py_END_ALLOW_THREADS
3237 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3238 if (res != 0)
3239 return (!async_err) ? posix_error() : NULL;
3240
Victor Stinner8c62be82010-05-06 00:08:46 +00003241 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003242}
3243#endif /* HAVE_FCHOWN */
3244
Larry Hastings2f936352014-08-05 14:04:04 +10003245
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003246#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003247/*[clinic input]
3248os.lchown
3249
3250 path : path_t
3251 uid: uid_t
3252 gid: gid_t
3253
3254Change the owner and group id of path to the numeric uid and gid.
3255
3256This function will not follow symbolic links.
3257Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3258[clinic start generated code]*/
3259
Larry Hastings2f936352014-08-05 14:04:04 +10003260static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003261os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3262/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003263{
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003265 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003266 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003267 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003268 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003269 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003270 }
Larry Hastings2f936352014-08-05 14:04:04 +10003271 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003272}
3273#endif /* HAVE_LCHOWN */
3274
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003275
Barry Warsaw53699e91996-12-10 23:23:01 +00003276static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003277posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003278{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003279 char *buf, *tmpbuf;
3280 char *cwd;
3281 const size_t chunk = 1024;
3282 size_t buflen = 0;
3283 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003284
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003285#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003286 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003287 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003288 wchar_t *wbuf2 = wbuf;
3289 PyObject *resobj;
3290 DWORD len;
3291 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003292 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003293 /* If the buffer is large enough, len does not include the
3294 terminating \0. If the buffer is too small, len includes
3295 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003296 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003297 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 if (wbuf2)
3299 len = GetCurrentDirectoryW(len, wbuf2);
3300 }
3301 Py_END_ALLOW_THREADS
3302 if (!wbuf2) {
3303 PyErr_NoMemory();
3304 return NULL;
3305 }
3306 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003307 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003308 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003309 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 }
3311 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003312 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003313 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003314 return resobj;
3315 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003316
3317 if (win32_warn_bytes_api())
3318 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003319#endif
3320
Victor Stinner4403d7d2015-04-25 00:16:10 +02003321 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003322 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003323 do {
3324 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003325#ifdef MS_WINDOWS
3326 if (buflen > INT_MAX) {
3327 PyErr_NoMemory();
3328 break;
3329 }
3330#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003331 tmpbuf = PyMem_RawRealloc(buf, buflen);
3332 if (tmpbuf == NULL)
3333 break;
3334
3335 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003336#ifdef MS_WINDOWS
3337 cwd = getcwd(buf, (int)buflen);
3338#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003339 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003340#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003341 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003342 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003343
3344 if (cwd == NULL) {
3345 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003346 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003347 }
3348
Victor Stinner8c62be82010-05-06 00:08:46 +00003349 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003350 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3351 else
3352 obj = PyUnicode_DecodeFSDefault(buf);
3353 PyMem_RawFree(buf);
3354
3355 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003356}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003357
Larry Hastings2f936352014-08-05 14:04:04 +10003358
3359/*[clinic input]
3360os.getcwd
3361
3362Return a unicode string representing the current working directory.
3363[clinic start generated code]*/
3364
Larry Hastings2f936352014-08-05 14:04:04 +10003365static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003366os_getcwd_impl(PyObject *module)
3367/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003368{
3369 return posix_getcwd(0);
3370}
3371
Larry Hastings2f936352014-08-05 14:04:04 +10003372
3373/*[clinic input]
3374os.getcwdb
3375
3376Return a bytes string representing the current working directory.
3377[clinic start generated code]*/
3378
Larry Hastings2f936352014-08-05 14:04:04 +10003379static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003380os_getcwdb_impl(PyObject *module)
3381/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003382{
3383 return posix_getcwd(1);
3384}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003385
Larry Hastings2f936352014-08-05 14:04:04 +10003386
Larry Hastings9cf065c2012-06-22 16:30:09 -07003387#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3388#define HAVE_LINK 1
3389#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003390
Guido van Rossumb6775db1994-08-01 11:34:53 +00003391#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003392/*[clinic input]
3393
3394os.link
3395
3396 src : path_t
3397 dst : path_t
3398 *
3399 src_dir_fd : dir_fd = None
3400 dst_dir_fd : dir_fd = None
3401 follow_symlinks: bool = True
3402
3403Create a hard link to a file.
3404
3405If either src_dir_fd or dst_dir_fd is not None, it should be a file
3406 descriptor open to a directory, and the respective path string (src or dst)
3407 should be relative; the path will then be relative to that directory.
3408If follow_symlinks is False, and the last element of src is a symbolic
3409 link, link will create a link to the symbolic link itself instead of the
3410 file the link points to.
3411src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3412 platform. If they are unavailable, using them will raise a
3413 NotImplementedError.
3414[clinic start generated code]*/
3415
Larry Hastings2f936352014-08-05 14:04:04 +10003416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003417os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003418 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003419/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003420{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003421#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003422 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003423#else
3424 int result;
3425#endif
3426
Larry Hastings9cf065c2012-06-22 16:30:09 -07003427#ifndef HAVE_LINKAT
3428 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3429 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003430 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003431 }
3432#endif
3433
Steve Dowercc16be82016-09-08 10:35:16 -07003434#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003435 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003436 PyErr_SetString(PyExc_NotImplementedError,
3437 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003438 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003439 }
Steve Dowercc16be82016-09-08 10:35:16 -07003440#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003441
Brian Curtin1b9df392010-11-24 20:24:31 +00003442#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003443 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003444 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003445 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003446
Larry Hastings2f936352014-08-05 14:04:04 +10003447 if (!result)
3448 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003449#else
3450 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003451#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003452 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3453 (dst_dir_fd != DEFAULT_DIR_FD) ||
3454 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003455 result = linkat(src_dir_fd, src->narrow,
3456 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003457 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3458 else
Steve Dowercc16be82016-09-08 10:35:16 -07003459#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003460 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003461 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003462
Larry Hastings2f936352014-08-05 14:04:04 +10003463 if (result)
3464 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003465#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003466
Larry Hastings2f936352014-08-05 14:04:04 +10003467 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003468}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003469#endif
3470
Brian Curtin1b9df392010-11-24 20:24:31 +00003471
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003472#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003473static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003474_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003475{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003476 PyObject *v;
3477 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3478 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003479 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003480 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003481 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003482 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003483
Steve Dowercc16be82016-09-08 10:35:16 -07003484 WIN32_FIND_DATAW wFileData;
3485 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003486
Steve Dowercc16be82016-09-08 10:35:16 -07003487 if (!path->wide) { /* Default arg: "." */
3488 po_wchars = L".";
3489 len = 1;
3490 } else {
3491 po_wchars = path->wide;
3492 len = wcslen(path->wide);
3493 }
3494 /* The +5 is so we can append "\\*.*\0" */
3495 wnamebuf = PyMem_New(wchar_t, len + 5);
3496 if (!wnamebuf) {
3497 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003498 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003499 }
Steve Dowercc16be82016-09-08 10:35:16 -07003500 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003501 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003502 wchar_t wch = wnamebuf[len-1];
3503 if (wch != SEP && wch != ALTSEP && wch != L':')
3504 wnamebuf[len++] = SEP;
3505 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003506 }
Steve Dowercc16be82016-09-08 10:35:16 -07003507 if ((list = PyList_New(0)) == NULL) {
3508 goto exit;
3509 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003510 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003511 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003512 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003513 if (hFindFile == INVALID_HANDLE_VALUE) {
3514 int error = GetLastError();
3515 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003516 goto exit;
3517 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003518 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003519 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003520 }
3521 do {
3522 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003523 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3524 wcscmp(wFileData.cFileName, L"..") != 0) {
3525 v = PyUnicode_FromWideChar(wFileData.cFileName,
3526 wcslen(wFileData.cFileName));
3527 if (path->narrow && v) {
3528 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3529 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003530 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003531 Py_DECREF(list);
3532 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003533 break;
3534 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003535 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003536 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003537 Py_DECREF(list);
3538 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003539 break;
3540 }
3541 Py_DECREF(v);
3542 }
3543 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003544 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003545 Py_END_ALLOW_THREADS
3546 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3547 it got to the end of the directory. */
3548 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003549 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003550 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003551 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003552 }
3553 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003554
Larry Hastings9cf065c2012-06-22 16:30:09 -07003555exit:
3556 if (hFindFile != INVALID_HANDLE_VALUE) {
3557 if (FindClose(hFindFile) == FALSE) {
3558 if (list != NULL) {
3559 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003560 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003561 }
3562 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003564 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003565
Larry Hastings9cf065c2012-06-22 16:30:09 -07003566 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003567} /* end of _listdir_windows_no_opendir */
3568
3569#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3570
3571static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003572_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003573{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003574 PyObject *v;
3575 DIR *dirp = NULL;
3576 struct dirent *ep;
3577 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003578#ifdef HAVE_FDOPENDIR
3579 int fd = -1;
3580#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003581
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003583#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003584 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003585 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003586 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003587 if (fd == -1)
3588 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003589
Larry Hastingsfdaea062012-06-25 04:42:23 -07003590 return_str = 1;
3591
Larry Hastings9cf065c2012-06-22 16:30:09 -07003592 Py_BEGIN_ALLOW_THREADS
3593 dirp = fdopendir(fd);
3594 Py_END_ALLOW_THREADS
3595 }
3596 else
3597#endif
3598 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003599 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003600 if (path->narrow) {
3601 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003602 /* only return bytes if they specified a bytes-like object */
3603 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003604 }
3605 else {
3606 name = ".";
3607 return_str = 1;
3608 }
3609
Larry Hastings9cf065c2012-06-22 16:30:09 -07003610 Py_BEGIN_ALLOW_THREADS
3611 dirp = opendir(name);
3612 Py_END_ALLOW_THREADS
3613 }
3614
3615 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003616 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003617#ifdef HAVE_FDOPENDIR
3618 if (fd != -1) {
3619 Py_BEGIN_ALLOW_THREADS
3620 close(fd);
3621 Py_END_ALLOW_THREADS
3622 }
3623#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003624 goto exit;
3625 }
3626 if ((list = PyList_New(0)) == NULL) {
3627 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003628 }
3629 for (;;) {
3630 errno = 0;
3631 Py_BEGIN_ALLOW_THREADS
3632 ep = readdir(dirp);
3633 Py_END_ALLOW_THREADS
3634 if (ep == NULL) {
3635 if (errno == 0) {
3636 break;
3637 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003638 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003639 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003640 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003641 }
3642 }
3643 if (ep->d_name[0] == '.' &&
3644 (NAMLEN(ep) == 1 ||
3645 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3646 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003647 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003648 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3649 else
3650 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003651 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003652 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 break;
3654 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003655 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003657 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003658 break;
3659 }
3660 Py_DECREF(v);
3661 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003662
Larry Hastings9cf065c2012-06-22 16:30:09 -07003663exit:
3664 if (dirp != NULL) {
3665 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003666#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003667 if (fd > -1)
3668 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003669#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003670 closedir(dirp);
3671 Py_END_ALLOW_THREADS
3672 }
3673
Larry Hastings9cf065c2012-06-22 16:30:09 -07003674 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003675} /* end of _posix_listdir */
3676#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003677
Larry Hastings2f936352014-08-05 14:04:04 +10003678
3679/*[clinic input]
3680os.listdir
3681
3682 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3683
3684Return a list containing the names of the files in the directory.
3685
BNMetricsb9427072018-11-02 15:20:19 +00003686path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003687 the filenames returned will also be bytes; in all other circumstances
3688 the filenames returned will be str.
3689If path is None, uses the path='.'.
3690On some platforms, path may also be specified as an open file descriptor;\
3691 the file descriptor must refer to a directory.
3692 If this functionality is unavailable, using it raises NotImplementedError.
3693
3694The list is in arbitrary order. It does not include the special
3695entries '.' and '..' even if they are present in the directory.
3696
3697
3698[clinic start generated code]*/
3699
Larry Hastings2f936352014-08-05 14:04:04 +10003700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003701os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003702/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003703{
3704#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3705 return _listdir_windows_no_opendir(path, NULL);
3706#else
3707 return _posix_listdir(path, NULL);
3708#endif
3709}
3710
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003711#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003712/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003713/*[clinic input]
3714os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003715
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003716 path: path_t
3717 /
3718
3719[clinic start generated code]*/
3720
3721static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003722os__getfullpathname_impl(PyObject *module, path_t *path)
3723/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003724{
Steve Dowercc16be82016-09-08 10:35:16 -07003725 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
3726 wchar_t *wtemp;
3727 DWORD result;
3728 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003729
Steve Dowercc16be82016-09-08 10:35:16 -07003730 result = GetFullPathNameW(path->wide,
3731 Py_ARRAY_LENGTH(woutbuf),
3732 woutbuf, &wtemp);
3733 if (result > Py_ARRAY_LENGTH(woutbuf)) {
3734 woutbufp = PyMem_New(wchar_t, result);
3735 if (!woutbufp)
3736 return PyErr_NoMemory();
3737 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003738 }
Steve Dowercc16be82016-09-08 10:35:16 -07003739 if (result) {
3740 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
3741 if (path->narrow)
3742 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3743 } else
3744 v = win32_error_object("GetFullPathNameW", path->object);
3745 if (woutbufp != woutbuf)
3746 PyMem_Free(woutbufp);
3747 return v;
Larry Hastings2f936352014-08-05 14:04:04 +10003748}
Brian Curtind40e6f72010-07-08 21:39:08 +00003749
Brian Curtind25aef52011-06-13 15:16:04 -05003750
Larry Hastings2f936352014-08-05 14:04:04 +10003751/*[clinic input]
3752os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003753
Steve Dower23ad6d02018-02-22 10:39:10 -08003754 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003755 /
3756
3757A helper function for samepath on windows.
3758[clinic start generated code]*/
3759
Larry Hastings2f936352014-08-05 14:04:04 +10003760static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003761os__getfinalpathname_impl(PyObject *module, path_t *path)
3762/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003763{
3764 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003765 wchar_t buf[MAXPATHLEN], *target_path = buf;
3766 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003767 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003768 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003769
Steve Dower23ad6d02018-02-22 10:39:10 -08003770 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003771 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003772 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003773 0, /* desired access */
3774 0, /* share mode */
3775 NULL, /* security attributes */
3776 OPEN_EXISTING,
3777 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3778 FILE_FLAG_BACKUP_SEMANTICS,
3779 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003780 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003781
Steve Dower23ad6d02018-02-22 10:39:10 -08003782 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003783 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003784 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003785
3786 /* We have a good handle to the target, use it to determine the
3787 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003788 while (1) {
3789 Py_BEGIN_ALLOW_THREADS
3790 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3791 buf_size, VOLUME_NAME_DOS);
3792 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003793
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003794 if (!result_length) {
3795 result = win32_error_object("GetFinalPathNameByHandleW",
3796 path->object);
3797 goto cleanup;
3798 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003799
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003800 if (result_length < buf_size) {
3801 break;
3802 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003803
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003804 wchar_t *tmp;
3805 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
3806 result_length * sizeof(*tmp));
3807 if (!tmp) {
3808 result = PyErr_NoMemory();
3809 goto cleanup;
3810 }
3811
3812 buf_size = result_length;
3813 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08003814 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003815
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003816 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dower23ad6d02018-02-22 10:39:10 -08003817 if (path->narrow)
3818 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dower23ad6d02018-02-22 10:39:10 -08003819
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003820cleanup:
3821 if (target_path != buf) {
3822 PyMem_Free(target_path);
3823 }
3824 CloseHandle(hFile);
3825 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003826}
Brian Curtin62857742010-09-06 17:07:27 +00003827
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003828/*[clinic input]
3829os._isdir
3830
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003831 path as arg: object
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003832 /
3833
Serhiy Storchaka579f0382016-11-08 20:21:22 +02003834Return true if the pathname refers to an existing directory.
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003835[clinic start generated code]*/
3836
Brian Curtin9c669cc2011-06-08 18:17:18 -05003837static PyObject *
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003838os__isdir(PyObject *module, PyObject *arg)
3839/*[clinic end generated code: output=404f334d85d4bf25 input=36cb6785874d479e]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003840{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003841 DWORD attributes;
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003842 path_t path = PATH_T_INITIALIZE("_isdir", "path", 0, 0);
3843
3844 if (!path_converter(arg, &path)) {
3845 if (PyErr_ExceptionMatches(PyExc_ValueError)) {
3846 PyErr_Clear();
3847 Py_RETURN_FALSE;
3848 }
3849 return NULL;
3850 }
Brian Curtin9c669cc2011-06-08 18:17:18 -05003851
Steve Dowerb22a6772016-07-17 20:49:38 -07003852 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003853 attributes = GetFileAttributesW(path.wide);
Steve Dowerb22a6772016-07-17 20:49:38 -07003854 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003855
Serhiy Storchaka0185f342018-09-18 11:28:51 +03003856 path_cleanup(&path);
Brian Curtin9c669cc2011-06-08 18:17:18 -05003857 if (attributes == INVALID_FILE_ATTRIBUTES)
3858 Py_RETURN_FALSE;
3859
Brian Curtin9c669cc2011-06-08 18:17:18 -05003860 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3861 Py_RETURN_TRUE;
3862 else
3863 Py_RETURN_FALSE;
3864}
Tim Golden6b528062013-08-01 12:44:00 +01003865
Tim Golden6b528062013-08-01 12:44:00 +01003866
Larry Hastings2f936352014-08-05 14:04:04 +10003867/*[clinic input]
3868os._getvolumepathname
3869
Steve Dower23ad6d02018-02-22 10:39:10 -08003870 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003871
3872A helper function for ismount on Win32.
3873[clinic start generated code]*/
3874
Larry Hastings2f936352014-08-05 14:04:04 +10003875static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003876os__getvolumepathname_impl(PyObject *module, path_t *path)
3877/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003878{
3879 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003880 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003881 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003882 BOOL ret;
3883
Tim Golden6b528062013-08-01 12:44:00 +01003884 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08003885 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01003886
Victor Stinner850a18e2017-10-24 16:53:32 -07003887 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01003888 PyErr_SetString(PyExc_OverflowError, "path too long");
3889 return NULL;
3890 }
3891
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003892 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01003893 if (mountpath == NULL)
3894 return PyErr_NoMemory();
3895
3896 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08003897 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01003898 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01003899 Py_END_ALLOW_THREADS
3900
3901 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08003902 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01003903 goto exit;
3904 }
3905 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08003906 if (path->narrow)
3907 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01003908
3909exit:
3910 PyMem_Free(mountpath);
3911 return result;
3912}
Tim Golden6b528062013-08-01 12:44:00 +01003913
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003914#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003915
Larry Hastings2f936352014-08-05 14:04:04 +10003916
3917/*[clinic input]
3918os.mkdir
3919
3920 path : path_t
3921
3922 mode: int = 0o777
3923
3924 *
3925
3926 dir_fd : dir_fd(requires='mkdirat') = None
3927
3928# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3929
3930Create a directory.
3931
3932If dir_fd is not None, it should be a file descriptor open to a directory,
3933 and path should be relative; path will then be relative to that directory.
3934dir_fd may not be implemented on your platform.
3935 If it is unavailable, using it will raise a NotImplementedError.
3936
3937The mode argument is ignored on Windows.
3938[clinic start generated code]*/
3939
Larry Hastings2f936352014-08-05 14:04:04 +10003940static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003941os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
3942/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003943{
3944 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003945
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003946#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003947 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003948 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003949 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003950
Larry Hastings2f936352014-08-05 14:04:04 +10003951 if (!result)
3952 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003953#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003954 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003955#if HAVE_MKDIRAT
3956 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10003957 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003958 else
3959#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02003960#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10003961 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003962#else
Larry Hastings2f936352014-08-05 14:04:04 +10003963 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003964#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003965 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003966 if (result < 0)
3967 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07003968#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10003969 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003970}
3971
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003972
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003973/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3974#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003975#include <sys/resource.h>
3976#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003977
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003978
3979#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10003980/*[clinic input]
3981os.nice
3982
3983 increment: int
3984 /
3985
3986Add increment to the priority of process and return the new priority.
3987[clinic start generated code]*/
3988
Larry Hastings2f936352014-08-05 14:04:04 +10003989static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003990os_nice_impl(PyObject *module, int increment)
3991/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003992{
3993 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003994
Victor Stinner8c62be82010-05-06 00:08:46 +00003995 /* There are two flavours of 'nice': one that returns the new
3996 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07003997 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00003998 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003999
Victor Stinner8c62be82010-05-06 00:08:46 +00004000 If we are of the nice family that returns the new priority, we
4001 need to clear errno before the call, and check if errno is filled
4002 before calling posix_error() on a returnvalue of -1, because the
4003 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004004
Victor Stinner8c62be82010-05-06 00:08:46 +00004005 errno = 0;
4006 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004007#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004008 if (value == 0)
4009 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004010#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 if (value == -1 && errno != 0)
4012 /* either nice() or getpriority() returned an error */
4013 return posix_error();
4014 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004015}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004016#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004017
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004018
4019#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004020/*[clinic input]
4021os.getpriority
4022
4023 which: int
4024 who: int
4025
4026Return program scheduling priority.
4027[clinic start generated code]*/
4028
Larry Hastings2f936352014-08-05 14:04:04 +10004029static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004030os_getpriority_impl(PyObject *module, int which, int who)
4031/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004032{
4033 int retval;
4034
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004035 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004036 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004037 if (errno != 0)
4038 return posix_error();
4039 return PyLong_FromLong((long)retval);
4040}
4041#endif /* HAVE_GETPRIORITY */
4042
4043
4044#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004045/*[clinic input]
4046os.setpriority
4047
4048 which: int
4049 who: int
4050 priority: int
4051
4052Set program scheduling priority.
4053[clinic start generated code]*/
4054
Larry Hastings2f936352014-08-05 14:04:04 +10004055static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004056os_setpriority_impl(PyObject *module, int which, int who, int priority)
4057/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004058{
4059 int retval;
4060
4061 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004062 if (retval == -1)
4063 return posix_error();
4064 Py_RETURN_NONE;
4065}
4066#endif /* HAVE_SETPRIORITY */
4067
4068
Barry Warsaw53699e91996-12-10 23:23:01 +00004069static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004070internal_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 +00004071{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004072 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004073 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004074
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004075#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004076 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004077 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004078#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004079 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004080#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004081
Larry Hastings9cf065c2012-06-22 16:30:09 -07004082 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4083 (dst_dir_fd != DEFAULT_DIR_FD);
4084#ifndef HAVE_RENAMEAT
4085 if (dir_fd_specified) {
4086 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004087 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004088 }
4089#endif
4090
Larry Hastings9cf065c2012-06-22 16:30:09 -07004091#ifdef MS_WINDOWS
4092 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004093 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004094 Py_END_ALLOW_THREADS
4095
Larry Hastings2f936352014-08-05 14:04:04 +10004096 if (!result)
4097 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004098
4099#else
Steve Dowercc16be82016-09-08 10:35:16 -07004100 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4101 PyErr_Format(PyExc_ValueError,
4102 "%s: src and dst must be the same type", function_name);
4103 return NULL;
4104 }
4105
Larry Hastings9cf065c2012-06-22 16:30:09 -07004106 Py_BEGIN_ALLOW_THREADS
4107#ifdef HAVE_RENAMEAT
4108 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004109 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004110 else
4111#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004112 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004113 Py_END_ALLOW_THREADS
4114
Larry Hastings2f936352014-08-05 14:04:04 +10004115 if (result)
4116 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004117#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004118 Py_RETURN_NONE;
4119}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004120
Larry Hastings2f936352014-08-05 14:04:04 +10004121
4122/*[clinic input]
4123os.rename
4124
4125 src : path_t
4126 dst : path_t
4127 *
4128 src_dir_fd : dir_fd = None
4129 dst_dir_fd : dir_fd = None
4130
4131Rename a file or directory.
4132
4133If either src_dir_fd or dst_dir_fd is not None, it should be a file
4134 descriptor open to a directory, and the respective path string (src or dst)
4135 should be relative; the path will then be relative to that directory.
4136src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4137 If they are unavailable, using them will raise a NotImplementedError.
4138[clinic start generated code]*/
4139
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004140static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004141os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004142 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004143/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004144{
Larry Hastings2f936352014-08-05 14:04:04 +10004145 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004146}
4147
Larry Hastings2f936352014-08-05 14:04:04 +10004148
4149/*[clinic input]
4150os.replace = os.rename
4151
4152Rename a file or directory, overwriting the destination.
4153
4154If either src_dir_fd or dst_dir_fd is not None, it should be a file
4155 descriptor open to a directory, and the respective path string (src or dst)
4156 should be relative; the path will then be relative to that directory.
4157src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4158 If they are unavailable, using them will raise a NotImplementedError."
4159[clinic start generated code]*/
4160
Larry Hastings2f936352014-08-05 14:04:04 +10004161static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004162os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4163 int dst_dir_fd)
4164/*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004165{
4166 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4167}
4168
4169
4170/*[clinic input]
4171os.rmdir
4172
4173 path: path_t
4174 *
4175 dir_fd: dir_fd(requires='unlinkat') = None
4176
4177Remove a directory.
4178
4179If dir_fd is not None, it should be a file descriptor open to a directory,
4180 and path should be relative; path will then be relative to that directory.
4181dir_fd may not be implemented on your platform.
4182 If it is unavailable, using it will raise a NotImplementedError.
4183[clinic start generated code]*/
4184
Larry Hastings2f936352014-08-05 14:04:04 +10004185static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004186os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4187/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004188{
4189 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004190
4191 Py_BEGIN_ALLOW_THREADS
4192#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004193 /* Windows, success=1, UNIX, success=0 */
4194 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004195#else
4196#ifdef HAVE_UNLINKAT
4197 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004198 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004199 else
4200#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004201 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004202#endif
4203 Py_END_ALLOW_THREADS
4204
Larry Hastings2f936352014-08-05 14:04:04 +10004205 if (result)
4206 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004207
Larry Hastings2f936352014-08-05 14:04:04 +10004208 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004209}
4210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004211
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004212#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004213#ifdef MS_WINDOWS
4214/*[clinic input]
4215os.system -> long
4216
4217 command: Py_UNICODE
4218
4219Execute the command in a subshell.
4220[clinic start generated code]*/
4221
Larry Hastings2f936352014-08-05 14:04:04 +10004222static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004223os_system_impl(PyObject *module, const Py_UNICODE *command)
4224/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004225{
4226 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004227 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004228 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004229 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004230 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004231 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004232 return result;
4233}
4234#else /* MS_WINDOWS */
4235/*[clinic input]
4236os.system -> long
4237
4238 command: FSConverter
4239
4240Execute the command in a subshell.
4241[clinic start generated code]*/
4242
Larry Hastings2f936352014-08-05 14:04:04 +10004243static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004244os_system_impl(PyObject *module, PyObject *command)
4245/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004246{
4247 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004248 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004249 Py_BEGIN_ALLOW_THREADS
4250 result = system(bytes);
4251 Py_END_ALLOW_THREADS
4252 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004253}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004254#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004255#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004256
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004257
Larry Hastings2f936352014-08-05 14:04:04 +10004258/*[clinic input]
4259os.umask
4260
4261 mask: int
4262 /
4263
4264Set the current numeric umask and return the previous umask.
4265[clinic start generated code]*/
4266
Larry Hastings2f936352014-08-05 14:04:04 +10004267static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004268os_umask_impl(PyObject *module, int mask)
4269/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004270{
4271 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004272 if (i < 0)
4273 return posix_error();
4274 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004275}
4276
Brian Curtind40e6f72010-07-08 21:39:08 +00004277#ifdef MS_WINDOWS
4278
4279/* override the default DeleteFileW behavior so that directory
4280symlinks can be removed with this function, the same as with
4281Unix symlinks */
4282BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4283{
4284 WIN32_FILE_ATTRIBUTE_DATA info;
4285 WIN32_FIND_DATAW find_data;
4286 HANDLE find_data_handle;
4287 int is_directory = 0;
4288 int is_link = 0;
4289
4290 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4291 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004292
Brian Curtind40e6f72010-07-08 21:39:08 +00004293 /* Get WIN32_FIND_DATA structure for the path to determine if
4294 it is a symlink */
4295 if(is_directory &&
4296 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4297 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4298
4299 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004300 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4301 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4302 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4303 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004304 FindClose(find_data_handle);
4305 }
4306 }
4307 }
4308
4309 if (is_directory && is_link)
4310 return RemoveDirectoryW(lpFileName);
4311
4312 return DeleteFileW(lpFileName);
4313}
4314#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004315
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004316
Larry Hastings2f936352014-08-05 14:04:04 +10004317/*[clinic input]
4318os.unlink
4319
4320 path: path_t
4321 *
4322 dir_fd: dir_fd(requires='unlinkat')=None
4323
4324Remove a file (same as remove()).
4325
4326If dir_fd is not None, it should be a file descriptor open to a directory,
4327 and path should be relative; path will then be relative to that directory.
4328dir_fd may not be implemented on your platform.
4329 If it is unavailable, using it will raise a NotImplementedError.
4330
4331[clinic start generated code]*/
4332
Larry Hastings2f936352014-08-05 14:04:04 +10004333static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004334os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4335/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004336{
4337 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004338
4339 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004340 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004341#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004342 /* Windows, success=1, UNIX, success=0 */
4343 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004344#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004345#ifdef HAVE_UNLINKAT
4346 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004347 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004348 else
4349#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004350 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004351#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004352 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004353 Py_END_ALLOW_THREADS
4354
Larry Hastings2f936352014-08-05 14:04:04 +10004355 if (result)
4356 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004357
Larry Hastings2f936352014-08-05 14:04:04 +10004358 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004359}
4360
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004361
Larry Hastings2f936352014-08-05 14:04:04 +10004362/*[clinic input]
4363os.remove = os.unlink
4364
4365Remove a file (same as unlink()).
4366
4367If dir_fd is not None, it should be a file descriptor open to a directory,
4368 and path should be relative; path will then be relative to that directory.
4369dir_fd may not be implemented on your platform.
4370 If it is unavailable, using it will raise a NotImplementedError.
4371[clinic start generated code]*/
4372
Larry Hastings2f936352014-08-05 14:04:04 +10004373static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004374os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4375/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004376{
4377 return os_unlink_impl(module, path, dir_fd);
4378}
4379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004380
Larry Hastings605a62d2012-06-24 04:33:36 -07004381static PyStructSequence_Field uname_result_fields[] = {
4382 {"sysname", "operating system name"},
4383 {"nodename", "name of machine on network (implementation-defined)"},
4384 {"release", "operating system release"},
4385 {"version", "operating system version"},
4386 {"machine", "hardware identifier"},
4387 {NULL}
4388};
4389
4390PyDoc_STRVAR(uname_result__doc__,
4391"uname_result: Result from os.uname().\n\n\
4392This object may be accessed either as a tuple of\n\
4393 (sysname, nodename, release, version, machine),\n\
4394or via the attributes sysname, nodename, release, version, and machine.\n\
4395\n\
4396See os.uname for more information.");
4397
4398static PyStructSequence_Desc uname_result_desc = {
4399 "uname_result", /* name */
4400 uname_result__doc__, /* doc */
4401 uname_result_fields,
4402 5
4403};
4404
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004405static PyTypeObject* UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07004406
4407
4408#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004409/*[clinic input]
4410os.uname
4411
4412Return an object identifying the current operating system.
4413
4414The object behaves like a named tuple with the following fields:
4415 (sysname, nodename, release, version, machine)
4416
4417[clinic start generated code]*/
4418
Larry Hastings2f936352014-08-05 14:04:04 +10004419static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004420os_uname_impl(PyObject *module)
4421/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004422{
Victor Stinner8c62be82010-05-06 00:08:46 +00004423 struct utsname u;
4424 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004425 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004426
Victor Stinner8c62be82010-05-06 00:08:46 +00004427 Py_BEGIN_ALLOW_THREADS
4428 res = uname(&u);
4429 Py_END_ALLOW_THREADS
4430 if (res < 0)
4431 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004432
Eddie Elizondo474eedf2018-11-13 04:09:31 -08004433 value = PyStructSequence_New(UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004434 if (value == NULL)
4435 return NULL;
4436
4437#define SET(i, field) \
4438 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004439 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004440 if (!o) { \
4441 Py_DECREF(value); \
4442 return NULL; \
4443 } \
4444 PyStructSequence_SET_ITEM(value, i, o); \
4445 } \
4446
4447 SET(0, u.sysname);
4448 SET(1, u.nodename);
4449 SET(2, u.release);
4450 SET(3, u.version);
4451 SET(4, u.machine);
4452
4453#undef SET
4454
4455 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004456}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004457#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004458
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004459
Larry Hastings9cf065c2012-06-22 16:30:09 -07004460
4461typedef struct {
4462 int now;
4463 time_t atime_s;
4464 long atime_ns;
4465 time_t mtime_s;
4466 long mtime_ns;
4467} utime_t;
4468
4469/*
Victor Stinner484df002014-10-09 13:52:31 +02004470 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004471 * they also intentionally leak the declaration of a pointer named "time"
4472 */
4473#define UTIME_TO_TIMESPEC \
4474 struct timespec ts[2]; \
4475 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004476 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004477 time = NULL; \
4478 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004479 ts[0].tv_sec = ut->atime_s; \
4480 ts[0].tv_nsec = ut->atime_ns; \
4481 ts[1].tv_sec = ut->mtime_s; \
4482 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004483 time = ts; \
4484 } \
4485
4486#define UTIME_TO_TIMEVAL \
4487 struct timeval tv[2]; \
4488 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004489 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004490 time = NULL; \
4491 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004492 tv[0].tv_sec = ut->atime_s; \
4493 tv[0].tv_usec = ut->atime_ns / 1000; \
4494 tv[1].tv_sec = ut->mtime_s; \
4495 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004496 time = tv; \
4497 } \
4498
4499#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004500 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004501 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004502 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004503 time = NULL; \
4504 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004505 u.actime = ut->atime_s; \
4506 u.modtime = ut->mtime_s; \
4507 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004508 }
4509
4510#define UTIME_TO_TIME_T \
4511 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004512 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004513 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004514 time = NULL; \
4515 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004516 timet[0] = ut->atime_s; \
4517 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004518 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004519 } \
4520
4521
Victor Stinner528a9ab2015-09-03 21:30:26 +02004522#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004523
4524static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004525utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004526{
4527#ifdef HAVE_UTIMENSAT
4528 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4529 UTIME_TO_TIMESPEC;
4530 return utimensat(dir_fd, path, time, flags);
4531#elif defined(HAVE_FUTIMESAT)
4532 UTIME_TO_TIMEVAL;
4533 /*
4534 * follow_symlinks will never be false here;
4535 * we only allow !follow_symlinks and dir_fd together
4536 * if we have utimensat()
4537 */
4538 assert(follow_symlinks);
4539 return futimesat(dir_fd, path, time);
4540#endif
4541}
4542
Larry Hastings2f936352014-08-05 14:04:04 +10004543 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4544#else
4545 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004546#endif
4547
Victor Stinner528a9ab2015-09-03 21:30:26 +02004548#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004549
4550static int
Victor Stinner484df002014-10-09 13:52:31 +02004551utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004552{
4553#ifdef HAVE_FUTIMENS
4554 UTIME_TO_TIMESPEC;
4555 return futimens(fd, time);
4556#else
4557 UTIME_TO_TIMEVAL;
4558 return futimes(fd, time);
4559#endif
4560}
4561
Larry Hastings2f936352014-08-05 14:04:04 +10004562 #define PATH_UTIME_HAVE_FD 1
4563#else
4564 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004565#endif
4566
Victor Stinner5ebae872015-09-22 01:29:33 +02004567#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4568# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4569#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004570
Victor Stinner4552ced2015-09-21 22:37:15 +02004571#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004572
4573static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004574utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004575{
4576#ifdef HAVE_UTIMENSAT
4577 UTIME_TO_TIMESPEC;
4578 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4579#else
4580 UTIME_TO_TIMEVAL;
4581 return lutimes(path, time);
4582#endif
4583}
4584
4585#endif
4586
4587#ifndef MS_WINDOWS
4588
4589static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004590utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004591{
4592#ifdef HAVE_UTIMENSAT
4593 UTIME_TO_TIMESPEC;
4594 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4595#elif defined(HAVE_UTIMES)
4596 UTIME_TO_TIMEVAL;
4597 return utimes(path, time);
4598#elif defined(HAVE_UTIME_H)
4599 UTIME_TO_UTIMBUF;
4600 return utime(path, time);
4601#else
4602 UTIME_TO_TIME_T;
4603 return utime(path, time);
4604#endif
4605}
4606
4607#endif
4608
Larry Hastings76ad59b2012-05-03 00:30:07 -07004609static int
4610split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4611{
4612 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004613 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004614 divmod = PyNumber_Divmod(py_long, billion);
4615 if (!divmod)
4616 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004617 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4618 PyErr_Format(PyExc_TypeError,
4619 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
4620 Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name);
4621 goto exit;
4622 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004623 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4624 if ((*s == -1) && PyErr_Occurred())
4625 goto exit;
4626 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004627 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004628 goto exit;
4629
4630 result = 1;
4631exit:
4632 Py_XDECREF(divmod);
4633 return result;
4634}
4635
Larry Hastings2f936352014-08-05 14:04:04 +10004636
4637/*[clinic input]
4638os.utime
4639
4640 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4641 times: object = NULL
4642 *
4643 ns: object = NULL
4644 dir_fd: dir_fd(requires='futimensat') = None
4645 follow_symlinks: bool=True
4646
Martin Panter0ff89092015-09-09 01:56:53 +00004647# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004648
4649Set the access and modified time of path.
4650
4651path may always be specified as a string.
4652On some platforms, path may also be specified as an open file descriptor.
4653 If this functionality is unavailable, using it raises an exception.
4654
4655If times is not None, it must be a tuple (atime, mtime);
4656 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004657If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004658 atime_ns and mtime_ns should be expressed as integer nanoseconds
4659 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004660If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004661Specifying tuples for both times and ns is an error.
4662
4663If dir_fd is not None, it should be a file descriptor open to a directory,
4664 and path should be relative; path will then be relative to that directory.
4665If follow_symlinks is False, and the last element of the path is a symbolic
4666 link, utime will modify the symbolic link itself instead of the file the
4667 link points to.
4668It is an error to use dir_fd or follow_symlinks when specifying path
4669 as an open file descriptor.
4670dir_fd and follow_symlinks may not be available on your platform.
4671 If they are unavailable, using them will raise a NotImplementedError.
4672
4673[clinic start generated code]*/
4674
Larry Hastings2f936352014-08-05 14:04:04 +10004675static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004676os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4677 int dir_fd, int follow_symlinks)
4678/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004679{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004680#ifdef MS_WINDOWS
4681 HANDLE hFile;
4682 FILETIME atime, mtime;
4683#else
4684 int result;
4685#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004686
Larry Hastings2f936352014-08-05 14:04:04 +10004687 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004688
Christian Heimesb3c87242013-08-01 00:08:16 +02004689 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004690
Larry Hastings9cf065c2012-06-22 16:30:09 -07004691 if (times && (times != Py_None) && ns) {
4692 PyErr_SetString(PyExc_ValueError,
4693 "utime: you may specify either 'times'"
4694 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004695 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004696 }
4697
4698 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004699 time_t a_sec, m_sec;
4700 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004701 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004702 PyErr_SetString(PyExc_TypeError,
4703 "utime: 'times' must be either"
4704 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004705 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004706 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004707 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004708 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004709 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004710 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004711 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004712 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004713 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004714 utime.atime_s = a_sec;
4715 utime.atime_ns = a_nsec;
4716 utime.mtime_s = m_sec;
4717 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004718 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004719 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004720 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004721 PyErr_SetString(PyExc_TypeError,
4722 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004723 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004724 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004725 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004726 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004727 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004728 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004729 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004730 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004731 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004732 }
4733 else {
4734 /* times and ns are both None/unspecified. use "now". */
4735 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004736 }
4737
Victor Stinner4552ced2015-09-21 22:37:15 +02004738#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004739 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004740 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004741#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004742
Larry Hastings2f936352014-08-05 14:04:04 +10004743 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4744 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4745 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004746 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004747
Larry Hastings9cf065c2012-06-22 16:30:09 -07004748#if !defined(HAVE_UTIMENSAT)
4749 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004750 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004751 "utime: cannot use dir_fd and follow_symlinks "
4752 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004753 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004754 }
4755#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004756
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004757#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004758 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004759 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4760 NULL, OPEN_EXISTING,
4761 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004762 Py_END_ALLOW_THREADS
4763 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004764 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004765 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004766 }
4767
Larry Hastings9cf065c2012-06-22 16:30:09 -07004768 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004769 GetSystemTimeAsFileTime(&mtime);
4770 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004771 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004772 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004773 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4774 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004775 }
4776 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4777 /* Avoid putting the file name into the error here,
4778 as that may confuse the user into believing that
4779 something is wrong with the file, when it also
4780 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004781 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004782 CloseHandle(hFile);
4783 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004784 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004785 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004786#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004787 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004788
Victor Stinner4552ced2015-09-21 22:37:15 +02004789#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004790 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004791 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004792 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004793#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004794
Victor Stinner528a9ab2015-09-03 21:30:26 +02004795#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004796 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004797 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004798 else
4799#endif
4800
Victor Stinner528a9ab2015-09-03 21:30:26 +02004801#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004802 if (path->fd != -1)
4803 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004804 else
4805#endif
4806
Larry Hastings2f936352014-08-05 14:04:04 +10004807 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004808
4809 Py_END_ALLOW_THREADS
4810
4811 if (result < 0) {
4812 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004813 posix_error();
4814 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004815 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004816
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004817#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004818
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004819 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004820}
4821
Guido van Rossum3b066191991-06-04 19:40:25 +00004822/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004823
Larry Hastings2f936352014-08-05 14:04:04 +10004824
4825/*[clinic input]
4826os._exit
4827
4828 status: int
4829
4830Exit to the system with specified status, without normal exit processing.
4831[clinic start generated code]*/
4832
Larry Hastings2f936352014-08-05 14:04:04 +10004833static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004834os__exit_impl(PyObject *module, int status)
4835/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004836{
4837 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004838 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004839}
4840
Steve Dowercc16be82016-09-08 10:35:16 -07004841#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4842#define EXECV_CHAR wchar_t
4843#else
4844#define EXECV_CHAR char
4845#endif
4846
Martin v. Löwis114619e2002-10-07 06:44:21 +00004847#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4848static void
Steve Dowercc16be82016-09-08 10:35:16 -07004849free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004850{
Victor Stinner8c62be82010-05-06 00:08:46 +00004851 Py_ssize_t i;
4852 for (i = 0; i < count; i++)
4853 PyMem_Free(array[i]);
4854 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004855}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004856
Berker Peksag81816462016-09-15 20:19:47 +03004857static int
4858fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00004859{
Victor Stinner8c62be82010-05-06 00:08:46 +00004860 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03004861 PyObject *ub;
4862 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07004863#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03004864 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07004865 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004866 *out = PyUnicode_AsWideCharString(ub, &size);
4867 if (*out)
4868 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07004869#else
Berker Peksag81816462016-09-15 20:19:47 +03004870 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00004871 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03004872 size = PyBytes_GET_SIZE(ub);
4873 *out = PyMem_Malloc(size + 1);
4874 if (*out) {
4875 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
4876 result = 1;
4877 } else
Victor Stinner50abf222013-11-07 23:56:10 +01004878 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07004879#endif
Berker Peksag81816462016-09-15 20:19:47 +03004880 Py_DECREF(ub);
4881 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004882}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004883#endif
4884
Ross Lagerwall7807c352011-03-17 20:20:30 +02004885#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Steve Dowercc16be82016-09-08 10:35:16 -07004886static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00004887parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4888{
Victor Stinner8c62be82010-05-06 00:08:46 +00004889 Py_ssize_t i, pos, envc;
4890 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03004891 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07004892 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004893
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 i = PyMapping_Size(env);
4895 if (i < 0)
4896 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07004897 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00004898 if (envlist == NULL) {
4899 PyErr_NoMemory();
4900 return NULL;
4901 }
4902 envc = 0;
4903 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004904 if (!keys)
4905 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00004906 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01004907 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 goto error;
4909 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4910 PyErr_Format(PyExc_TypeError,
4911 "env.keys() or env.values() is not a list");
4912 goto error;
4913 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004914
Victor Stinner8c62be82010-05-06 00:08:46 +00004915 for (pos = 0; pos < i; pos++) {
4916 key = PyList_GetItem(keys, pos);
4917 val = PyList_GetItem(vals, pos);
4918 if (!key || !val)
4919 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004920
Berker Peksag81816462016-09-15 20:19:47 +03004921#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
4922 if (!PyUnicode_FSDecoder(key, &key2))
4923 goto error;
4924 if (!PyUnicode_FSDecoder(val, &val2)) {
4925 Py_DECREF(key2);
4926 goto error;
4927 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004928 /* Search from index 1 because on Windows starting '=' is allowed for
4929 defining hidden environment variables. */
4930 if (PyUnicode_GET_LENGTH(key2) == 0 ||
4931 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
4932 {
4933 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004934 Py_DECREF(key2);
4935 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004936 goto error;
4937 }
Berker Peksag81816462016-09-15 20:19:47 +03004938 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
4939#else
4940 if (!PyUnicode_FSConverter(key, &key2))
4941 goto error;
4942 if (!PyUnicode_FSConverter(val, &val2)) {
4943 Py_DECREF(key2);
4944 goto error;
4945 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03004946 if (PyBytes_GET_SIZE(key2) == 0 ||
4947 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
4948 {
4949 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04004950 Py_DECREF(key2);
4951 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03004952 goto error;
4953 }
Berker Peksag81816462016-09-15 20:19:47 +03004954 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
4955 PyBytes_AS_STRING(val2));
4956#endif
4957 Py_DECREF(key2);
4958 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07004959 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00004960 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07004961
4962 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
4963 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004964 goto error;
4965 }
Berker Peksag81816462016-09-15 20:19:47 +03004966
Steve Dowercc16be82016-09-08 10:35:16 -07004967 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00004968 }
4969 Py_DECREF(vals);
4970 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004971
Victor Stinner8c62be82010-05-06 00:08:46 +00004972 envlist[envc] = 0;
4973 *envc_ptr = envc;
4974 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004975
4976error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004977 Py_XDECREF(keys);
4978 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07004979 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004981}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004982
Steve Dowercc16be82016-09-08 10:35:16 -07004983static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02004984parse_arglist(PyObject* argv, Py_ssize_t *argc)
4985{
4986 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07004987 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004988 if (argvlist == NULL) {
4989 PyErr_NoMemory();
4990 return NULL;
4991 }
4992 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004993 PyObject* item = PySequence_ITEM(argv, i);
4994 if (item == NULL)
4995 goto fail;
4996 if (!fsconvert_strdup(item, &argvlist[i])) {
4997 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004998 goto fail;
4999 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005000 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005001 }
5002 argvlist[*argc] = NULL;
5003 return argvlist;
5004fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005005 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005006 free_string_array(argvlist, *argc);
5007 return NULL;
5008}
Steve Dowercc16be82016-09-08 10:35:16 -07005009
Ross Lagerwall7807c352011-03-17 20:20:30 +02005010#endif
5011
Larry Hastings2f936352014-08-05 14:04:04 +10005012
Ross Lagerwall7807c352011-03-17 20:20:30 +02005013#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005014/*[clinic input]
5015os.execv
5016
Steve Dowercc16be82016-09-08 10:35:16 -07005017 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005018 Path of executable file.
5019 argv: object
5020 Tuple or list of strings.
5021 /
5022
5023Execute an executable path with arguments, replacing current process.
5024[clinic start generated code]*/
5025
Larry Hastings2f936352014-08-05 14:04:04 +10005026static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005027os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5028/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005029{
Steve Dowercc16be82016-09-08 10:35:16 -07005030 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005031 Py_ssize_t argc;
5032
5033 /* execv has two arguments: (path, argv), where
5034 argv is a list or tuple of strings. */
5035
Ross Lagerwall7807c352011-03-17 20:20:30 +02005036 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5037 PyErr_SetString(PyExc_TypeError,
5038 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005039 return NULL;
5040 }
5041 argc = PySequence_Size(argv);
5042 if (argc < 1) {
5043 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005044 return NULL;
5045 }
5046
5047 argvlist = parse_arglist(argv, &argc);
5048 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005049 return NULL;
5050 }
Steve Dowerbce26262016-11-19 19:17:26 -08005051 if (!argvlist[0][0]) {
5052 PyErr_SetString(PyExc_ValueError,
5053 "execv() arg 2 first element cannot be empty");
5054 free_string_array(argvlist, argc);
5055 return NULL;
5056 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005057
Steve Dowerbce26262016-11-19 19:17:26 -08005058 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005059#ifdef HAVE_WEXECV
5060 _wexecv(path->wide, argvlist);
5061#else
5062 execv(path->narrow, argvlist);
5063#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005064 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005065
5066 /* If we get here it's definitely an error */
5067
5068 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005069 return posix_error();
5070}
5071
Larry Hastings2f936352014-08-05 14:04:04 +10005072
5073/*[clinic input]
5074os.execve
5075
5076 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5077 Path of executable file.
5078 argv: object
5079 Tuple or list of strings.
5080 env: object
5081 Dictionary of strings mapping to strings.
5082
5083Execute an executable path with arguments, replacing current process.
5084[clinic start generated code]*/
5085
Larry Hastings2f936352014-08-05 14:04:04 +10005086static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005087os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5088/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005089{
Steve Dowercc16be82016-09-08 10:35:16 -07005090 EXECV_CHAR **argvlist = NULL;
5091 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005092 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005093
Victor Stinner8c62be82010-05-06 00:08:46 +00005094 /* execve has three arguments: (path, argv, env), where
5095 argv is a list or tuple of strings and env is a dictionary
5096 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005097
Ross Lagerwall7807c352011-03-17 20:20:30 +02005098 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005099 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005100 "execve: argv must be a tuple or list");
5101 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005102 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005103 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005104 if (argc < 1) {
5105 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5106 return NULL;
5107 }
5108
Victor Stinner8c62be82010-05-06 00:08:46 +00005109 if (!PyMapping_Check(env)) {
5110 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005111 "execve: environment must be a mapping object");
5112 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005113 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005114
Ross Lagerwall7807c352011-03-17 20:20:30 +02005115 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005116 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005117 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005118 }
Steve Dowerbce26262016-11-19 19:17:26 -08005119 if (!argvlist[0][0]) {
5120 PyErr_SetString(PyExc_ValueError,
5121 "execve: argv first element cannot be empty");
5122 goto fail;
5123 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005124
Victor Stinner8c62be82010-05-06 00:08:46 +00005125 envlist = parse_envlist(env, &envc);
5126 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005127 goto fail;
5128
Steve Dowerbce26262016-11-19 19:17:26 -08005129 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005130#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005131 if (path->fd > -1)
5132 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005133 else
5134#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005135#ifdef HAVE_WEXECV
5136 _wexecve(path->wide, argvlist, envlist);
5137#else
Larry Hastings2f936352014-08-05 14:04:04 +10005138 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005139#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005140 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005141
5142 /* If we get here it's definitely an error */
5143
Alexey Izbyshev83460312018-10-20 03:28:22 +03005144 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005145
Steve Dowercc16be82016-09-08 10:35:16 -07005146 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005147 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005148 if (argvlist)
5149 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005150 return NULL;
5151}
Steve Dowercc16be82016-09-08 10:35:16 -07005152
Larry Hastings9cf065c2012-06-22 16:30:09 -07005153#endif /* HAVE_EXECV */
5154
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005155#ifdef HAVE_POSIX_SPAWN
5156
5157enum posix_spawn_file_actions_identifier {
5158 POSIX_SPAWN_OPEN,
5159 POSIX_SPAWN_CLOSE,
5160 POSIX_SPAWN_DUP2
5161};
5162
William Orr81574b82018-10-01 22:19:56 -07005163#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005164static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005165convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005166#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005167
5168static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005169parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5170 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005171 PyObject *setsigdef, PyObject *scheduler,
5172 posix_spawnattr_t *attrp)
5173{
5174 long all_flags = 0;
5175
5176 errno = posix_spawnattr_init(attrp);
5177 if (errno) {
5178 posix_error();
5179 return -1;
5180 }
5181
5182 if (setpgroup) {
5183 pid_t pgid = PyLong_AsPid(setpgroup);
5184 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5185 goto fail;
5186 }
5187 errno = posix_spawnattr_setpgroup(attrp, pgid);
5188 if (errno) {
5189 posix_error();
5190 goto fail;
5191 }
5192 all_flags |= POSIX_SPAWN_SETPGROUP;
5193 }
5194
5195 if (resetids) {
5196 all_flags |= POSIX_SPAWN_RESETIDS;
5197 }
5198
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005199 if (setsid) {
5200#ifdef POSIX_SPAWN_SETSID
5201 all_flags |= POSIX_SPAWN_SETSID;
5202#elif defined(POSIX_SPAWN_SETSID_NP)
5203 all_flags |= POSIX_SPAWN_SETSID_NP;
5204#else
5205 argument_unavailable_error(func_name, "setsid");
5206 return -1;
5207#endif
5208 }
5209
Pablo Galindo254a4662018-09-07 16:44:24 +01005210 if (setsigmask) {
5211 sigset_t set;
5212 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5213 goto fail;
5214 }
5215 errno = posix_spawnattr_setsigmask(attrp, &set);
5216 if (errno) {
5217 posix_error();
5218 goto fail;
5219 }
5220 all_flags |= POSIX_SPAWN_SETSIGMASK;
5221 }
5222
5223 if (setsigdef) {
5224 sigset_t set;
5225 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5226 goto fail;
5227 }
5228 errno = posix_spawnattr_setsigdefault(attrp, &set);
5229 if (errno) {
5230 posix_error();
5231 goto fail;
5232 }
5233 all_flags |= POSIX_SPAWN_SETSIGDEF;
5234 }
5235
5236 if (scheduler) {
5237#ifdef POSIX_SPAWN_SETSCHEDULER
5238 PyObject *py_schedpolicy;
5239 struct sched_param schedparam;
5240
5241 if (!PyArg_ParseTuple(scheduler, "OO&"
5242 ";A scheduler tuple must have two elements",
5243 &py_schedpolicy, convert_sched_param, &schedparam)) {
5244 goto fail;
5245 }
5246 if (py_schedpolicy != Py_None) {
5247 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5248
5249 if (schedpolicy == -1 && PyErr_Occurred()) {
5250 goto fail;
5251 }
5252 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5253 if (errno) {
5254 posix_error();
5255 goto fail;
5256 }
5257 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5258 }
5259 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5260 if (errno) {
5261 posix_error();
5262 goto fail;
5263 }
5264 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5265#else
5266 PyErr_SetString(PyExc_NotImplementedError,
5267 "The scheduler option is not supported in this system.");
5268 goto fail;
5269#endif
5270 }
5271
5272 errno = posix_spawnattr_setflags(attrp, all_flags);
5273 if (errno) {
5274 posix_error();
5275 goto fail;
5276 }
5277
5278 return 0;
5279
5280fail:
5281 (void)posix_spawnattr_destroy(attrp);
5282 return -1;
5283}
5284
5285static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005286parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005287 posix_spawn_file_actions_t *file_actionsp,
5288 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005289{
5290 PyObject *seq;
5291 PyObject *file_action = NULL;
5292 PyObject *tag_obj;
5293
5294 seq = PySequence_Fast(file_actions,
5295 "file_actions must be a sequence or None");
5296 if (seq == NULL) {
5297 return -1;
5298 }
5299
5300 errno = posix_spawn_file_actions_init(file_actionsp);
5301 if (errno) {
5302 posix_error();
5303 Py_DECREF(seq);
5304 return -1;
5305 }
5306
5307 for (int i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
5308 file_action = PySequence_Fast_GET_ITEM(seq, i);
5309 Py_INCREF(file_action);
5310 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5311 PyErr_SetString(PyExc_TypeError,
5312 "Each file_actions element must be a non-empty tuple");
5313 goto fail;
5314 }
5315 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5316 if (tag == -1 && PyErr_Occurred()) {
5317 goto fail;
5318 }
5319
5320 /* Populate the file_actions object */
5321 switch (tag) {
5322 case POSIX_SPAWN_OPEN: {
5323 int fd, oflag;
5324 PyObject *path;
5325 unsigned long mode;
5326 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5327 ";A open file_action tuple must have 5 elements",
5328 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5329 &oflag, &mode))
5330 {
5331 goto fail;
5332 }
Pablo Galindocb970732018-06-19 09:19:50 +01005333 if (PyList_Append(temp_buffer, path)) {
5334 Py_DECREF(path);
5335 goto fail;
5336 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005337 errno = posix_spawn_file_actions_addopen(file_actionsp,
5338 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005339 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005340 if (errno) {
5341 posix_error();
5342 goto fail;
5343 }
5344 break;
5345 }
5346 case POSIX_SPAWN_CLOSE: {
5347 int fd;
5348 if (!PyArg_ParseTuple(file_action, "Oi"
5349 ";A close file_action tuple must have 2 elements",
5350 &tag_obj, &fd))
5351 {
5352 goto fail;
5353 }
5354 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5355 if (errno) {
5356 posix_error();
5357 goto fail;
5358 }
5359 break;
5360 }
5361 case POSIX_SPAWN_DUP2: {
5362 int fd1, fd2;
5363 if (!PyArg_ParseTuple(file_action, "Oii"
5364 ";A dup2 file_action tuple must have 3 elements",
5365 &tag_obj, &fd1, &fd2))
5366 {
5367 goto fail;
5368 }
5369 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5370 fd1, fd2);
5371 if (errno) {
5372 posix_error();
5373 goto fail;
5374 }
5375 break;
5376 }
5377 default: {
5378 PyErr_SetString(PyExc_TypeError,
5379 "Unknown file_actions identifier");
5380 goto fail;
5381 }
5382 }
5383 Py_DECREF(file_action);
5384 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005385
Serhiy Storchakaef347532018-05-01 16:45:04 +03005386 Py_DECREF(seq);
5387 return 0;
5388
5389fail:
5390 Py_DECREF(seq);
5391 Py_DECREF(file_action);
5392 (void)posix_spawn_file_actions_destroy(file_actionsp);
5393 return -1;
5394}
5395
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005396
5397static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005398py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5399 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005400 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005401 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005402{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005403 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005404 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005405 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005406 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005407 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005408 posix_spawnattr_t attr;
5409 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005410 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005411 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005412 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005413 pid_t pid;
5414 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005415
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005416 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005417 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005418 like posix.environ. */
5419
Serhiy Storchakaef347532018-05-01 16:45:04 +03005420 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005421 PyErr_Format(PyExc_TypeError,
5422 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005423 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005424 }
5425 argc = PySequence_Size(argv);
5426 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005427 PyErr_Format(PyExc_ValueError,
5428 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005429 return NULL;
5430 }
5431
5432 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005433 PyErr_Format(PyExc_TypeError,
5434 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005435 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005436 }
5437
5438 argvlist = parse_arglist(argv, &argc);
5439 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005440 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005441 }
5442 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005443 PyErr_Format(PyExc_ValueError,
5444 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005445 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005446 }
5447
5448 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005449 if (envlist == NULL) {
5450 goto exit;
5451 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005452
Serhiy Storchakad700f972018-09-08 14:48:18 +03005453 if (file_actions != NULL) {
Pablo Galindocb970732018-06-19 09:19:50 +01005454 /* There is a bug in old versions of glibc that makes some of the
5455 * helper functions for manipulating file actions not copy the provided
5456 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5457 * copy the value of path for some old versions of glibc (<2.20).
5458 * The use of temp_buffer here is a workaround that keeps the
5459 * python objects that own the buffers alive until posix_spawn gets called.
5460 * Check https://bugs.python.org/issue33630 and
5461 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5462 temp_buffer = PyList_New(0);
5463 if (!temp_buffer) {
5464 goto exit;
5465 }
5466 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005467 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005468 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005469 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005470 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005471
Victor Stinner325e4ba2019-02-01 15:47:24 +01005472 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5473 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005474 goto exit;
5475 }
5476 attrp = &attr;
5477
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005478 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005479#ifdef HAVE_POSIX_SPAWNP
5480 if (use_posix_spawnp) {
5481 err_code = posix_spawnp(&pid, path->narrow,
5482 file_actionsp, attrp, argvlist, envlist);
5483 }
5484 else
5485#endif /* HAVE_POSIX_SPAWNP */
5486 {
5487 err_code = posix_spawn(&pid, path->narrow,
5488 file_actionsp, attrp, argvlist, envlist);
5489 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005490 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005491
Serhiy Storchakaef347532018-05-01 16:45:04 +03005492 if (err_code) {
5493 errno = err_code;
5494 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005495 goto exit;
5496 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005497#ifdef _Py_MEMORY_SANITIZER
5498 __msan_unpoison(&pid, sizeof(pid));
5499#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005500 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005501
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005502exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005503 if (file_actionsp) {
5504 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005505 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005506 if (attrp) {
5507 (void)posix_spawnattr_destroy(attrp);
5508 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005509 if (envlist) {
5510 free_string_array(envlist, envc);
5511 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005512 if (argvlist) {
5513 free_string_array(argvlist, argc);
5514 }
Pablo Galindocb970732018-06-19 09:19:50 +01005515 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005516 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005517}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005518
5519
5520/*[clinic input]
5521
5522os.posix_spawn
5523 path: path_t
5524 Path of executable file.
5525 argv: object
5526 Tuple or list of strings.
5527 env: object
5528 Dictionary of strings mapping to strings.
5529 /
5530 *
5531 file_actions: object(c_default='NULL') = ()
5532 A sequence of file action tuples.
5533 setpgroup: object = NULL
5534 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5535 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005536 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5537 setsid: bool(accept={int}) = False
5538 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005539 setsigmask: object(c_default='NULL') = ()
5540 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5541 setsigdef: object(c_default='NULL') = ()
5542 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5543 scheduler: object = NULL
5544 A tuple with the scheduler policy (optional) and parameters.
5545
5546Execute the program specified by path in a new process.
5547[clinic start generated code]*/
5548
5549static PyObject *
5550os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5551 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005552 PyObject *setpgroup, int resetids, int setsid,
5553 PyObject *setsigmask, PyObject *setsigdef,
5554 PyObject *scheduler)
5555/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005556{
5557 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005558 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005559 scheduler);
5560}
5561 #endif /* HAVE_POSIX_SPAWN */
5562
5563
5564
5565#ifdef HAVE_POSIX_SPAWNP
5566/*[clinic input]
5567
5568os.posix_spawnp
5569 path: path_t
5570 Path of executable file.
5571 argv: object
5572 Tuple or list of strings.
5573 env: object
5574 Dictionary of strings mapping to strings.
5575 /
5576 *
5577 file_actions: object(c_default='NULL') = ()
5578 A sequence of file action tuples.
5579 setpgroup: object = NULL
5580 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5581 resetids: bool(accept={int}) = False
5582 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005583 setsid: bool(accept={int}) = False
5584 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005585 setsigmask: object(c_default='NULL') = ()
5586 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5587 setsigdef: object(c_default='NULL') = ()
5588 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5589 scheduler: object = NULL
5590 A tuple with the scheduler policy (optional) and parameters.
5591
5592Execute the program specified by path in a new process.
5593[clinic start generated code]*/
5594
5595static PyObject *
5596os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5597 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005598 PyObject *setpgroup, int resetids, int setsid,
5599 PyObject *setsigmask, PyObject *setsigdef,
5600 PyObject *scheduler)
5601/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005602{
5603 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005604 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005605 scheduler);
5606}
5607#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005608
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005609
Steve Dowercc16be82016-09-08 10:35:16 -07005610#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV)
Larry Hastings2f936352014-08-05 14:04:04 +10005611/*[clinic input]
5612os.spawnv
5613
5614 mode: int
5615 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005616 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005617 Path of executable file.
5618 argv: object
5619 Tuple or list of strings.
5620 /
5621
5622Execute the program specified by path in a new process.
5623[clinic start generated code]*/
5624
Larry Hastings2f936352014-08-05 14:04:04 +10005625static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005626os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5627/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005628{
Steve Dowercc16be82016-09-08 10:35:16 -07005629 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005630 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005631 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005632 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005633 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005634
Victor Stinner8c62be82010-05-06 00:08:46 +00005635 /* spawnv has three arguments: (mode, path, argv), where
5636 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005637
Victor Stinner8c62be82010-05-06 00:08:46 +00005638 if (PyList_Check(argv)) {
5639 argc = PyList_Size(argv);
5640 getitem = PyList_GetItem;
5641 }
5642 else if (PyTuple_Check(argv)) {
5643 argc = PyTuple_Size(argv);
5644 getitem = PyTuple_GetItem;
5645 }
5646 else {
5647 PyErr_SetString(PyExc_TypeError,
5648 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005649 return NULL;
5650 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005651 if (argc == 0) {
5652 PyErr_SetString(PyExc_ValueError,
5653 "spawnv() arg 2 cannot be empty");
5654 return NULL;
5655 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005656
Steve Dowercc16be82016-09-08 10:35:16 -07005657 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005658 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005659 return PyErr_NoMemory();
5660 }
5661 for (i = 0; i < argc; i++) {
5662 if (!fsconvert_strdup((*getitem)(argv, i),
5663 &argvlist[i])) {
5664 free_string_array(argvlist, i);
5665 PyErr_SetString(
5666 PyExc_TypeError,
5667 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005668 return NULL;
5669 }
Steve Dower93ff8722016-11-19 19:03:54 -08005670 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005671 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005672 PyErr_SetString(
5673 PyExc_ValueError,
5674 "spawnv() arg 2 first element cannot be empty");
5675 return NULL;
5676 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005677 }
5678 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005679
Victor Stinner8c62be82010-05-06 00:08:46 +00005680 if (mode == _OLD_P_OVERLAY)
5681 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005682
Victor Stinner8c62be82010-05-06 00:08:46 +00005683 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005684 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005685#ifdef HAVE_WSPAWNV
5686 spawnval = _wspawnv(mode, path->wide, argvlist);
5687#else
5688 spawnval = _spawnv(mode, path->narrow, argvlist);
5689#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005690 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005691 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005692
Victor Stinner8c62be82010-05-06 00:08:46 +00005693 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005694
Victor Stinner8c62be82010-05-06 00:08:46 +00005695 if (spawnval == -1)
5696 return posix_error();
5697 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005698 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005699}
5700
Larry Hastings2f936352014-08-05 14:04:04 +10005701/*[clinic input]
5702os.spawnve
5703
5704 mode: int
5705 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005706 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005707 Path of executable file.
5708 argv: object
5709 Tuple or list of strings.
5710 env: object
5711 Dictionary of strings mapping to strings.
5712 /
5713
5714Execute the program specified by path in a new process.
5715[clinic start generated code]*/
5716
Larry Hastings2f936352014-08-05 14:04:04 +10005717static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005718os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005719 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005720/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005721{
Steve Dowercc16be82016-09-08 10:35:16 -07005722 EXECV_CHAR **argvlist;
5723 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005724 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005725 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005726 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005727 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005728 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005729
Victor Stinner8c62be82010-05-06 00:08:46 +00005730 /* spawnve has four arguments: (mode, path, argv, env), where
5731 argv is a list or tuple of strings and env is a dictionary
5732 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005733
Victor Stinner8c62be82010-05-06 00:08:46 +00005734 if (PyList_Check(argv)) {
5735 argc = PyList_Size(argv);
5736 getitem = PyList_GetItem;
5737 }
5738 else if (PyTuple_Check(argv)) {
5739 argc = PyTuple_Size(argv);
5740 getitem = PyTuple_GetItem;
5741 }
5742 else {
5743 PyErr_SetString(PyExc_TypeError,
5744 "spawnve() arg 2 must be a tuple or list");
5745 goto fail_0;
5746 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005747 if (argc == 0) {
5748 PyErr_SetString(PyExc_ValueError,
5749 "spawnve() arg 2 cannot be empty");
5750 goto fail_0;
5751 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005752 if (!PyMapping_Check(env)) {
5753 PyErr_SetString(PyExc_TypeError,
5754 "spawnve() arg 3 must be a mapping object");
5755 goto fail_0;
5756 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005757
Steve Dowercc16be82016-09-08 10:35:16 -07005758 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005759 if (argvlist == NULL) {
5760 PyErr_NoMemory();
5761 goto fail_0;
5762 }
5763 for (i = 0; i < argc; i++) {
5764 if (!fsconvert_strdup((*getitem)(argv, i),
5765 &argvlist[i]))
5766 {
5767 lastarg = i;
5768 goto fail_1;
5769 }
Steve Dowerbce26262016-11-19 19:17:26 -08005770 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005771 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005772 PyErr_SetString(
5773 PyExc_ValueError,
5774 "spawnv() arg 2 first element cannot be empty");
5775 goto fail_1;
5776 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005777 }
5778 lastarg = argc;
5779 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005780
Victor Stinner8c62be82010-05-06 00:08:46 +00005781 envlist = parse_envlist(env, &envc);
5782 if (envlist == NULL)
5783 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005784
Victor Stinner8c62be82010-05-06 00:08:46 +00005785 if (mode == _OLD_P_OVERLAY)
5786 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005787
Victor Stinner8c62be82010-05-06 00:08:46 +00005788 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005789 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005790#ifdef HAVE_WSPAWNV
5791 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
5792#else
5793 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
5794#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005795 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005796 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005797
Victor Stinner8c62be82010-05-06 00:08:46 +00005798 if (spawnval == -1)
5799 (void) posix_error();
5800 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005801 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005802
Victor Stinner8c62be82010-05-06 00:08:46 +00005803 while (--envc >= 0)
5804 PyMem_DEL(envlist[envc]);
5805 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005806 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005807 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005808 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005809 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005810}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005811
Guido van Rossuma1065681999-01-25 23:20:23 +00005812#endif /* HAVE_SPAWNV */
5813
5814
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005815#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07005816
5817/* Helper function to validate arguments.
5818 Returns 0 on success. non-zero on failure with a TypeError raised.
5819 If obj is non-NULL it must be callable. */
5820static int
5821check_null_or_callable(PyObject *obj, const char* obj_name)
5822{
5823 if (obj && !PyCallable_Check(obj)) {
5824 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
5825 obj_name, Py_TYPE(obj)->tp_name);
5826 return -1;
5827 }
5828 return 0;
5829}
5830
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005831/*[clinic input]
5832os.register_at_fork
5833
Gregory P. Smith163468a2017-05-29 10:03:41 -07005834 *
5835 before: object=NULL
5836 A callable to be called in the parent before the fork() syscall.
5837 after_in_child: object=NULL
5838 A callable to be called in the child after fork().
5839 after_in_parent: object=NULL
5840 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005841
Gregory P. Smith163468a2017-05-29 10:03:41 -07005842Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005843
Gregory P. Smith163468a2017-05-29 10:03:41 -07005844'before' callbacks are called in reverse order.
5845'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005846
5847[clinic start generated code]*/
5848
5849static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07005850os_register_at_fork_impl(PyObject *module, PyObject *before,
5851 PyObject *after_in_child, PyObject *after_in_parent)
5852/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005853{
5854 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005855
Gregory P. Smith163468a2017-05-29 10:03:41 -07005856 if (!before && !after_in_child && !after_in_parent) {
5857 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
5858 return NULL;
5859 }
5860 if (check_null_or_callable(before, "before") ||
5861 check_null_or_callable(after_in_child, "after_in_child") ||
5862 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005863 return NULL;
5864 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02005865 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005866
Gregory P. Smith163468a2017-05-29 10:03:41 -07005867 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005868 return NULL;
5869 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07005870 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005871 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07005872 }
5873 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
5874 return NULL;
5875 }
5876 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005877}
5878#endif /* HAVE_FORK */
5879
5880
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005881#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005882/*[clinic input]
5883os.fork1
5884
5885Fork a child process with a single multiplexed (i.e., not bound) thread.
5886
5887Return 0 to child process and PID of child to parent process.
5888[clinic start generated code]*/
5889
Larry Hastings2f936352014-08-05 14:04:04 +10005890static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005891os_fork1_impl(PyObject *module)
5892/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005893{
Victor Stinner8c62be82010-05-06 00:08:46 +00005894 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005895
Eric Snow59032962018-09-14 14:17:20 -07005896 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
5897 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
5898 return NULL;
5899 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005900 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005901 pid = fork1();
5902 if (pid == 0) {
5903 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005904 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005905 } else {
5906 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005907 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005908 }
5909 if (pid == -1)
5910 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005911 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005912}
Larry Hastings2f936352014-08-05 14:04:04 +10005913#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005914
5915
Guido van Rossumad0ee831995-03-01 10:34:45 +00005916#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005917/*[clinic input]
5918os.fork
5919
5920Fork a child process.
5921
5922Return 0 to child process and PID of child to parent process.
5923[clinic start generated code]*/
5924
Larry Hastings2f936352014-08-05 14:04:04 +10005925static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005926os_fork_impl(PyObject *module)
5927/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005928{
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005930
Eric Snow59032962018-09-14 14:17:20 -07005931 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
5932 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
5933 return NULL;
5934 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005935 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00005936 pid = fork();
5937 if (pid == 0) {
5938 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005939 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00005940 } else {
5941 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02005942 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 }
5944 if (pid == -1)
5945 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005947}
Larry Hastings2f936352014-08-05 14:04:04 +10005948#endif /* HAVE_FORK */
5949
Guido van Rossum85e3b011991-06-03 12:42:10 +00005950
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005951#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005952#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005953/*[clinic input]
5954os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005955
Larry Hastings2f936352014-08-05 14:04:04 +10005956 policy: int
5957
5958Get the maximum scheduling priority for policy.
5959[clinic start generated code]*/
5960
Larry Hastings2f936352014-08-05 14:04:04 +10005961static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005962os_sched_get_priority_max_impl(PyObject *module, int policy)
5963/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005964{
5965 int max;
5966
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005967 max = sched_get_priority_max(policy);
5968 if (max < 0)
5969 return posix_error();
5970 return PyLong_FromLong(max);
5971}
5972
Larry Hastings2f936352014-08-05 14:04:04 +10005973
5974/*[clinic input]
5975os.sched_get_priority_min
5976
5977 policy: int
5978
5979Get the minimum scheduling priority for policy.
5980[clinic start generated code]*/
5981
Larry Hastings2f936352014-08-05 14:04:04 +10005982static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005983os_sched_get_priority_min_impl(PyObject *module, int policy)
5984/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005985{
5986 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005987 if (min < 0)
5988 return posix_error();
5989 return PyLong_FromLong(min);
5990}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005991#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5992
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005993
Larry Hastings2f936352014-08-05 14:04:04 +10005994#ifdef HAVE_SCHED_SETSCHEDULER
5995/*[clinic input]
5996os.sched_getscheduler
5997 pid: pid_t
5998 /
5999
6000Get the scheduling policy for the process identifiedy by pid.
6001
6002Passing 0 for pid returns the scheduling policy for the calling process.
6003[clinic start generated code]*/
6004
Larry Hastings2f936352014-08-05 14:04:04 +10006005static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006006os_sched_getscheduler_impl(PyObject *module, pid_t pid)
6007/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006008{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006009 int policy;
6010
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006011 policy = sched_getscheduler(pid);
6012 if (policy < 0)
6013 return posix_error();
6014 return PyLong_FromLong(policy);
6015}
Larry Hastings2f936352014-08-05 14:04:04 +10006016#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006017
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006018
William Orr81574b82018-10-01 22:19:56 -07006019#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006020/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006021class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006022
6023@classmethod
6024os.sched_param.__new__
6025
6026 sched_priority: object
6027 A scheduling parameter.
6028
6029Current has only one field: sched_priority");
6030[clinic start generated code]*/
6031
Larry Hastings2f936352014-08-05 14:04:04 +10006032static PyObject *
6033os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006034/*[clinic end generated code: output=48f4067d60f48c13 input=ab4de35a9a7811f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006035{
6036 PyObject *res;
6037
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006038 res = PyStructSequence_New(type);
6039 if (!res)
6040 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006041 Py_INCREF(sched_priority);
6042 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006043 return res;
6044}
6045
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006046
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006047PyDoc_VAR(os_sched_param__doc__);
6048
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006049static PyStructSequence_Field sched_param_fields[] = {
6050 {"sched_priority", "the scheduling priority"},
6051 {0}
6052};
6053
6054static PyStructSequence_Desc sched_param_desc = {
6055 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006056 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006057 sched_param_fields,
6058 1
6059};
6060
6061static int
6062convert_sched_param(PyObject *param, struct sched_param *res)
6063{
6064 long priority;
6065
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006066 if (Py_TYPE(param) != SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006067 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6068 return 0;
6069 }
6070 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6071 if (priority == -1 && PyErr_Occurred())
6072 return 0;
6073 if (priority > INT_MAX || priority < INT_MIN) {
6074 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6075 return 0;
6076 }
6077 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6078 return 1;
6079}
William Orr81574b82018-10-01 22:19:56 -07006080#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006081
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006082
6083#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006084/*[clinic input]
6085os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006086
Larry Hastings2f936352014-08-05 14:04:04 +10006087 pid: pid_t
6088 policy: int
6089 param: sched_param
6090 /
6091
6092Set the scheduling policy for the process identified by pid.
6093
6094If pid is 0, the calling process is changed.
6095param is an instance of sched_param.
6096[clinic start generated code]*/
6097
Larry Hastings2f936352014-08-05 14:04:04 +10006098static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006099os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006100 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006101/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006102{
Jesus Cea9c822272011-09-10 01:40:52 +02006103 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006104 ** sched_setscheduler() returns 0 in Linux, but the previous
6105 ** scheduling policy under Solaris/Illumos, and others.
6106 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006107 */
Larry Hastings2f936352014-08-05 14:04:04 +10006108 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006109 return posix_error();
6110 Py_RETURN_NONE;
6111}
Larry Hastings2f936352014-08-05 14:04:04 +10006112#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006113
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006114
6115#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006116/*[clinic input]
6117os.sched_getparam
6118 pid: pid_t
6119 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006120
Larry Hastings2f936352014-08-05 14:04:04 +10006121Returns scheduling parameters for the process identified by pid.
6122
6123If pid is 0, returns parameters for the calling process.
6124Return value is an instance of sched_param.
6125[clinic start generated code]*/
6126
Larry Hastings2f936352014-08-05 14:04:04 +10006127static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006128os_sched_getparam_impl(PyObject *module, pid_t pid)
6129/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006130{
6131 struct sched_param param;
6132 PyObject *result;
6133 PyObject *priority;
6134
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006135 if (sched_getparam(pid, &param))
6136 return posix_error();
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006137 result = PyStructSequence_New(SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006138 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006139 return NULL;
6140 priority = PyLong_FromLong(param.sched_priority);
6141 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006142 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006143 return NULL;
6144 }
Larry Hastings2f936352014-08-05 14:04:04 +10006145 PyStructSequence_SET_ITEM(result, 0, priority);
6146 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006147}
6148
Larry Hastings2f936352014-08-05 14:04:04 +10006149
6150/*[clinic input]
6151os.sched_setparam
6152 pid: pid_t
6153 param: sched_param
6154 /
6155
6156Set scheduling parameters for the process identified by pid.
6157
6158If pid is 0, sets parameters for the calling process.
6159param should be an instance of sched_param.
6160[clinic start generated code]*/
6161
Larry Hastings2f936352014-08-05 14:04:04 +10006162static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006163os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006164 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006165/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006166{
6167 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006168 return posix_error();
6169 Py_RETURN_NONE;
6170}
Larry Hastings2f936352014-08-05 14:04:04 +10006171#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006172
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006173
6174#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006175/*[clinic input]
6176os.sched_rr_get_interval -> double
6177 pid: pid_t
6178 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006179
Larry Hastings2f936352014-08-05 14:04:04 +10006180Return the round-robin quantum for the process identified by pid, in seconds.
6181
6182Value returned is a float.
6183[clinic start generated code]*/
6184
Larry Hastings2f936352014-08-05 14:04:04 +10006185static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006186os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6187/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006188{
6189 struct timespec interval;
6190 if (sched_rr_get_interval(pid, &interval)) {
6191 posix_error();
6192 return -1.0;
6193 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006194#ifdef _Py_MEMORY_SANITIZER
6195 __msan_unpoison(&interval, sizeof(interval));
6196#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006197 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6198}
6199#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006200
Larry Hastings2f936352014-08-05 14:04:04 +10006201
6202/*[clinic input]
6203os.sched_yield
6204
6205Voluntarily relinquish the CPU.
6206[clinic start generated code]*/
6207
Larry Hastings2f936352014-08-05 14:04:04 +10006208static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006209os_sched_yield_impl(PyObject *module)
6210/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006211{
6212 if (sched_yield())
6213 return posix_error();
6214 Py_RETURN_NONE;
6215}
6216
Benjamin Peterson2740af82011-08-02 17:41:34 -05006217#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006218/* The minimum number of CPUs allocated in a cpu_set_t */
6219static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006220
Larry Hastings2f936352014-08-05 14:04:04 +10006221/*[clinic input]
6222os.sched_setaffinity
6223 pid: pid_t
6224 mask : object
6225 /
6226
6227Set the CPU affinity of the process identified by pid to mask.
6228
6229mask should be an iterable of integers identifying CPUs.
6230[clinic start generated code]*/
6231
Larry Hastings2f936352014-08-05 14:04:04 +10006232static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006233os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6234/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006235{
Antoine Pitrou84869872012-08-04 16:16:35 +02006236 int ncpus;
6237 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006238 cpu_set_t *cpu_set = NULL;
6239 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006240
Larry Hastings2f936352014-08-05 14:04:04 +10006241 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006242 if (iterator == NULL)
6243 return NULL;
6244
6245 ncpus = NCPUS_START;
6246 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006247 cpu_set = CPU_ALLOC(ncpus);
6248 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006249 PyErr_NoMemory();
6250 goto error;
6251 }
Larry Hastings2f936352014-08-05 14:04:04 +10006252 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006253
6254 while ((item = PyIter_Next(iterator))) {
6255 long cpu;
6256 if (!PyLong_Check(item)) {
6257 PyErr_Format(PyExc_TypeError,
6258 "expected an iterator of ints, "
6259 "but iterator yielded %R",
6260 Py_TYPE(item));
6261 Py_DECREF(item);
6262 goto error;
6263 }
6264 cpu = PyLong_AsLong(item);
6265 Py_DECREF(item);
6266 if (cpu < 0) {
6267 if (!PyErr_Occurred())
6268 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6269 goto error;
6270 }
6271 if (cpu > INT_MAX - 1) {
6272 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6273 goto error;
6274 }
6275 if (cpu >= ncpus) {
6276 /* Grow CPU mask to fit the CPU number */
6277 int newncpus = ncpus;
6278 cpu_set_t *newmask;
6279 size_t newsetsize;
6280 while (newncpus <= cpu) {
6281 if (newncpus > INT_MAX / 2)
6282 newncpus = cpu + 1;
6283 else
6284 newncpus = newncpus * 2;
6285 }
6286 newmask = CPU_ALLOC(newncpus);
6287 if (newmask == NULL) {
6288 PyErr_NoMemory();
6289 goto error;
6290 }
6291 newsetsize = CPU_ALLOC_SIZE(newncpus);
6292 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006293 memcpy(newmask, cpu_set, setsize);
6294 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006295 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006296 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006297 ncpus = newncpus;
6298 }
Larry Hastings2f936352014-08-05 14:04:04 +10006299 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006300 }
6301 Py_CLEAR(iterator);
6302
Larry Hastings2f936352014-08-05 14:04:04 +10006303 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006304 posix_error();
6305 goto error;
6306 }
Larry Hastings2f936352014-08-05 14:04:04 +10006307 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006308 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006309
6310error:
Larry Hastings2f936352014-08-05 14:04:04 +10006311 if (cpu_set)
6312 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006313 Py_XDECREF(iterator);
6314 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006315}
6316
Larry Hastings2f936352014-08-05 14:04:04 +10006317
6318/*[clinic input]
6319os.sched_getaffinity
6320 pid: pid_t
6321 /
6322
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006323Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006324
6325The affinity is returned as a set of CPU identifiers.
6326[clinic start generated code]*/
6327
Larry Hastings2f936352014-08-05 14:04:04 +10006328static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006329os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006330/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006331{
Antoine Pitrou84869872012-08-04 16:16:35 +02006332 int cpu, ncpus, count;
6333 size_t setsize;
6334 cpu_set_t *mask = NULL;
6335 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006336
Antoine Pitrou84869872012-08-04 16:16:35 +02006337 ncpus = NCPUS_START;
6338 while (1) {
6339 setsize = CPU_ALLOC_SIZE(ncpus);
6340 mask = CPU_ALLOC(ncpus);
6341 if (mask == NULL)
6342 return PyErr_NoMemory();
6343 if (sched_getaffinity(pid, setsize, mask) == 0)
6344 break;
6345 CPU_FREE(mask);
6346 if (errno != EINVAL)
6347 return posix_error();
6348 if (ncpus > INT_MAX / 2) {
6349 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6350 "a large enough CPU set");
6351 return NULL;
6352 }
6353 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006354 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006355
6356 res = PySet_New(NULL);
6357 if (res == NULL)
6358 goto error;
6359 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6360 if (CPU_ISSET_S(cpu, setsize, mask)) {
6361 PyObject *cpu_num = PyLong_FromLong(cpu);
6362 --count;
6363 if (cpu_num == NULL)
6364 goto error;
6365 if (PySet_Add(res, cpu_num)) {
6366 Py_DECREF(cpu_num);
6367 goto error;
6368 }
6369 Py_DECREF(cpu_num);
6370 }
6371 }
6372 CPU_FREE(mask);
6373 return res;
6374
6375error:
6376 if (mask)
6377 CPU_FREE(mask);
6378 Py_XDECREF(res);
6379 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006380}
6381
Benjamin Peterson2740af82011-08-02 17:41:34 -05006382#endif /* HAVE_SCHED_SETAFFINITY */
6383
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006384#endif /* HAVE_SCHED_H */
6385
Larry Hastings2f936352014-08-05 14:04:04 +10006386
Neal Norwitzb59798b2003-03-21 01:43:31 +00006387/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006388/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6389#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006390#define DEV_PTY_FILE "/dev/ptc"
6391#define HAVE_DEV_PTMX
6392#else
6393#define DEV_PTY_FILE "/dev/ptmx"
6394#endif
6395
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006396#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006397#ifdef HAVE_PTY_H
6398#include <pty.h>
6399#else
6400#ifdef HAVE_LIBUTIL_H
6401#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006402#else
6403#ifdef HAVE_UTIL_H
6404#include <util.h>
6405#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006406#endif /* HAVE_LIBUTIL_H */
6407#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006408#ifdef HAVE_STROPTS_H
6409#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006410#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006411#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006412
Larry Hastings2f936352014-08-05 14:04:04 +10006413
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006414#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006415/*[clinic input]
6416os.openpty
6417
6418Open a pseudo-terminal.
6419
6420Return a tuple of (master_fd, slave_fd) containing open file descriptors
6421for both the master and slave ends.
6422[clinic start generated code]*/
6423
Larry Hastings2f936352014-08-05 14:04:04 +10006424static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006425os_openpty_impl(PyObject *module)
6426/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006427{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006428 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006429#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006430 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006431#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006432#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006433 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006434#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006435 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006436#endif
6437#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006438
Thomas Wouters70c21a12000-07-14 14:28:33 +00006439#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006440 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006441 goto posix_error;
6442
6443 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6444 goto error;
6445 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6446 goto error;
6447
Neal Norwitzb59798b2003-03-21 01:43:31 +00006448#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006449 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6450 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006451 goto posix_error;
6452 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6453 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006454
Victor Stinnerdaf45552013-08-28 00:53:59 +02006455 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006456 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006457 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006458
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006459#else
Victor Stinner000de532013-11-25 23:19:58 +01006460 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006462 goto posix_error;
6463
Victor Stinner8c62be82010-05-06 00:08:46 +00006464 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006465
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 /* change permission of slave */
6467 if (grantpt(master_fd) < 0) {
6468 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006469 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006470 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006471
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 /* unlock slave */
6473 if (unlockpt(master_fd) < 0) {
6474 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006475 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006477
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006479
Victor Stinner8c62be82010-05-06 00:08:46 +00006480 slave_name = ptsname(master_fd); /* get name of slave */
6481 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006482 goto posix_error;
6483
6484 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006485 if (slave_fd == -1)
6486 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006487
6488 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6489 goto posix_error;
6490
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006491#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006492 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6493 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006494#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006495 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006496#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006497#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006498#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006499
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006501
Victor Stinnerdaf45552013-08-28 00:53:59 +02006502posix_error:
6503 posix_error();
6504error:
6505 if (master_fd != -1)
6506 close(master_fd);
6507 if (slave_fd != -1)
6508 close(slave_fd);
6509 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006510}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006511#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006512
Larry Hastings2f936352014-08-05 14:04:04 +10006513
Fred Drake8cef4cf2000-06-28 16:40:38 +00006514#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006515/*[clinic input]
6516os.forkpty
6517
6518Fork a new process with a new pseudo-terminal as controlling tty.
6519
6520Returns a tuple of (pid, master_fd).
6521Like fork(), return pid of 0 to the child process,
6522and pid of child to the parent process.
6523To both, return fd of newly opened pseudo-terminal.
6524[clinic start generated code]*/
6525
Larry Hastings2f936352014-08-05 14:04:04 +10006526static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006527os_forkpty_impl(PyObject *module)
6528/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006529{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006530 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006532
Eric Snow59032962018-09-14 14:17:20 -07006533 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6534 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6535 return NULL;
6536 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006537 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006538 pid = forkpty(&master_fd, NULL, NULL, NULL);
6539 if (pid == 0) {
6540 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006541 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006542 } else {
6543 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006544 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006545 }
6546 if (pid == -1)
6547 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006548 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006549}
Larry Hastings2f936352014-08-05 14:04:04 +10006550#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006551
Ross Lagerwall7807c352011-03-17 20:20:30 +02006552
Guido van Rossumad0ee831995-03-01 10:34:45 +00006553#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006554/*[clinic input]
6555os.getegid
6556
6557Return the current process's effective group id.
6558[clinic start generated code]*/
6559
Larry Hastings2f936352014-08-05 14:04:04 +10006560static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006561os_getegid_impl(PyObject *module)
6562/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006563{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006564 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006565}
Larry Hastings2f936352014-08-05 14:04:04 +10006566#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006567
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006568
Guido van Rossumad0ee831995-03-01 10:34:45 +00006569#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006570/*[clinic input]
6571os.geteuid
6572
6573Return the current process's effective user id.
6574[clinic start generated code]*/
6575
Larry Hastings2f936352014-08-05 14:04:04 +10006576static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006577os_geteuid_impl(PyObject *module)
6578/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006579{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006580 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006581}
Larry Hastings2f936352014-08-05 14:04:04 +10006582#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006583
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006584
Guido van Rossumad0ee831995-03-01 10:34:45 +00006585#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006586/*[clinic input]
6587os.getgid
6588
6589Return the current process's group id.
6590[clinic start generated code]*/
6591
Larry Hastings2f936352014-08-05 14:04:04 +10006592static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006593os_getgid_impl(PyObject *module)
6594/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006595{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006596 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006597}
Larry Hastings2f936352014-08-05 14:04:04 +10006598#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006599
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006600
Berker Peksag39404992016-09-15 20:45:16 +03006601#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006602/*[clinic input]
6603os.getpid
6604
6605Return the current process id.
6606[clinic start generated code]*/
6607
Larry Hastings2f936352014-08-05 14:04:04 +10006608static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006609os_getpid_impl(PyObject *module)
6610/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006611{
Victor Stinner8c62be82010-05-06 00:08:46 +00006612 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006613}
Berker Peksag39404992016-09-15 20:45:16 +03006614#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006615
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006616#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006617
6618/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006619PyDoc_STRVAR(posix_getgrouplist__doc__,
6620"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6621Returns a list of groups to which a user belongs.\n\n\
6622 user: username to lookup\n\
6623 group: base group id of the user");
6624
6625static PyObject *
6626posix_getgrouplist(PyObject *self, PyObject *args)
6627{
6628#ifdef NGROUPS_MAX
6629#define MAX_GROUPS NGROUPS_MAX
6630#else
6631 /* defined to be 16 on Solaris7, so this should be a small number */
6632#define MAX_GROUPS 64
6633#endif
6634
6635 const char *user;
6636 int i, ngroups;
6637 PyObject *list;
6638#ifdef __APPLE__
6639 int *groups, basegid;
6640#else
6641 gid_t *groups, basegid;
6642#endif
6643 ngroups = MAX_GROUPS;
6644
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006645#ifdef __APPLE__
6646 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006647 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006648#else
6649 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6650 _Py_Gid_Converter, &basegid))
6651 return NULL;
6652#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006653
6654#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006655 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006656#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006657 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006658#endif
6659 if (groups == NULL)
6660 return PyErr_NoMemory();
6661
6662 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6663 PyMem_Del(groups);
6664 return posix_error();
6665 }
6666
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006667#ifdef _Py_MEMORY_SANITIZER
6668 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6669 __msan_unpoison(&ngroups, sizeof(ngroups));
6670 __msan_unpoison(groups, ngroups*sizeof(*groups));
6671#endif
6672
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006673 list = PyList_New(ngroups);
6674 if (list == NULL) {
6675 PyMem_Del(groups);
6676 return NULL;
6677 }
6678
6679 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006680#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006681 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006682#else
6683 PyObject *o = _PyLong_FromGid(groups[i]);
6684#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006685 if (o == NULL) {
6686 Py_DECREF(list);
6687 PyMem_Del(groups);
6688 return NULL;
6689 }
6690 PyList_SET_ITEM(list, i, o);
6691 }
6692
6693 PyMem_Del(groups);
6694
6695 return list;
6696}
Larry Hastings2f936352014-08-05 14:04:04 +10006697#endif /* HAVE_GETGROUPLIST */
6698
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006699
Fred Drakec9680921999-12-13 16:37:25 +00006700#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006701/*[clinic input]
6702os.getgroups
6703
6704Return list of supplemental group IDs for the process.
6705[clinic start generated code]*/
6706
Larry Hastings2f936352014-08-05 14:04:04 +10006707static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006708os_getgroups_impl(PyObject *module)
6709/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006710{
6711 PyObject *result = NULL;
6712
Fred Drakec9680921999-12-13 16:37:25 +00006713#ifdef NGROUPS_MAX
6714#define MAX_GROUPS NGROUPS_MAX
6715#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006716 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006717#define MAX_GROUPS 64
6718#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006719 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006720
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006721 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006722 * This is a helper variable to store the intermediate result when
6723 * that happens.
6724 *
6725 * To keep the code readable the OSX behaviour is unconditional,
6726 * according to the POSIX spec this should be safe on all unix-y
6727 * systems.
6728 */
6729 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006730 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006731
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006732#ifdef __APPLE__
6733 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6734 * there are more groups than can fit in grouplist. Therefore, on OS X
6735 * always first call getgroups with length 0 to get the actual number
6736 * of groups.
6737 */
6738 n = getgroups(0, NULL);
6739 if (n < 0) {
6740 return posix_error();
6741 } else if (n <= MAX_GROUPS) {
6742 /* groups will fit in existing array */
6743 alt_grouplist = grouplist;
6744 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006745 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006746 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006747 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006748 }
6749 }
6750
6751 n = getgroups(n, alt_grouplist);
6752 if (n == -1) {
6753 if (alt_grouplist != grouplist) {
6754 PyMem_Free(alt_grouplist);
6755 }
6756 return posix_error();
6757 }
6758#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006760 if (n < 0) {
6761 if (errno == EINVAL) {
6762 n = getgroups(0, NULL);
6763 if (n == -1) {
6764 return posix_error();
6765 }
6766 if (n == 0) {
6767 /* Avoid malloc(0) */
6768 alt_grouplist = grouplist;
6769 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006770 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006771 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006772 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006773 }
6774 n = getgroups(n, alt_grouplist);
6775 if (n == -1) {
6776 PyMem_Free(alt_grouplist);
6777 return posix_error();
6778 }
6779 }
6780 } else {
6781 return posix_error();
6782 }
6783 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006784#endif
6785
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006786 result = PyList_New(n);
6787 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 int i;
6789 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006790 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006791 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006792 Py_DECREF(result);
6793 result = NULL;
6794 break;
Fred Drakec9680921999-12-13 16:37:25 +00006795 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006796 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006797 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006798 }
6799
6800 if (alt_grouplist != grouplist) {
6801 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006803
Fred Drakec9680921999-12-13 16:37:25 +00006804 return result;
6805}
Larry Hastings2f936352014-08-05 14:04:04 +10006806#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006807
Antoine Pitroub7572f02009-12-02 20:46:48 +00006808#ifdef HAVE_INITGROUPS
6809PyDoc_STRVAR(posix_initgroups__doc__,
6810"initgroups(username, gid) -> None\n\n\
6811Call the system initgroups() to initialize the group access list with all of\n\
6812the groups of which the specified username is a member, plus the specified\n\
6813group id.");
6814
Larry Hastings2f936352014-08-05 14:04:04 +10006815/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006816static PyObject *
6817posix_initgroups(PyObject *self, PyObject *args)
6818{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006819 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006820 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006821 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006822#ifdef __APPLE__
6823 int gid;
6824#else
6825 gid_t gid;
6826#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006827
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006828#ifdef __APPLE__
6829 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6830 PyUnicode_FSConverter, &oname,
6831 &gid))
6832#else
6833 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6834 PyUnicode_FSConverter, &oname,
6835 _Py_Gid_Converter, &gid))
6836#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006838 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006839
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006840 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006841 Py_DECREF(oname);
6842 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006843 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006844
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006845 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006846}
Larry Hastings2f936352014-08-05 14:04:04 +10006847#endif /* HAVE_INITGROUPS */
6848
Antoine Pitroub7572f02009-12-02 20:46:48 +00006849
Martin v. Löwis606edc12002-06-13 21:09:11 +00006850#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006851/*[clinic input]
6852os.getpgid
6853
6854 pid: pid_t
6855
6856Call the system call getpgid(), and return the result.
6857[clinic start generated code]*/
6858
Larry Hastings2f936352014-08-05 14:04:04 +10006859static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006860os_getpgid_impl(PyObject *module, pid_t pid)
6861/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006862{
6863 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006864 if (pgid < 0)
6865 return posix_error();
6866 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006867}
6868#endif /* HAVE_GETPGID */
6869
6870
Guido van Rossumb6775db1994-08-01 11:34:53 +00006871#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006872/*[clinic input]
6873os.getpgrp
6874
6875Return the current process group id.
6876[clinic start generated code]*/
6877
Larry Hastings2f936352014-08-05 14:04:04 +10006878static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006879os_getpgrp_impl(PyObject *module)
6880/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006881{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006882#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006884#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006885 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006886#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006887}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006888#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006889
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006890
Guido van Rossumb6775db1994-08-01 11:34:53 +00006891#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006892/*[clinic input]
6893os.setpgrp
6894
6895Make the current process the leader of its process group.
6896[clinic start generated code]*/
6897
Larry Hastings2f936352014-08-05 14:04:04 +10006898static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006899os_setpgrp_impl(PyObject *module)
6900/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006901{
Guido van Rossum64933891994-10-20 21:56:42 +00006902#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006904#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006905 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006906#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006907 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02006908 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006909}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006910#endif /* HAVE_SETPGRP */
6911
Guido van Rossumad0ee831995-03-01 10:34:45 +00006912#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006913
6914#ifdef MS_WINDOWS
6915#include <tlhelp32.h>
6916
6917static PyObject*
6918win32_getppid()
6919{
6920 HANDLE snapshot;
6921 pid_t mypid;
6922 PyObject* result = NULL;
6923 BOOL have_record;
6924 PROCESSENTRY32 pe;
6925
6926 mypid = getpid(); /* This function never fails */
6927
6928 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6929 if (snapshot == INVALID_HANDLE_VALUE)
6930 return PyErr_SetFromWindowsErr(GetLastError());
6931
6932 pe.dwSize = sizeof(pe);
6933 have_record = Process32First(snapshot, &pe);
6934 while (have_record) {
6935 if (mypid == (pid_t)pe.th32ProcessID) {
6936 /* We could cache the ulong value in a static variable. */
6937 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6938 break;
6939 }
6940
6941 have_record = Process32Next(snapshot, &pe);
6942 }
6943
6944 /* If our loop exits and our pid was not found (result will be NULL)
6945 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6946 * error anyway, so let's raise it. */
6947 if (!result)
6948 result = PyErr_SetFromWindowsErr(GetLastError());
6949
6950 CloseHandle(snapshot);
6951
6952 return result;
6953}
6954#endif /*MS_WINDOWS*/
6955
Larry Hastings2f936352014-08-05 14:04:04 +10006956
6957/*[clinic input]
6958os.getppid
6959
6960Return the parent's process id.
6961
6962If the parent process has already exited, Windows machines will still
6963return its id; others systems will return the id of the 'init' process (1).
6964[clinic start generated code]*/
6965
Larry Hastings2f936352014-08-05 14:04:04 +10006966static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006967os_getppid_impl(PyObject *module)
6968/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006969{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006970#ifdef MS_WINDOWS
6971 return win32_getppid();
6972#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006973 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006974#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006975}
6976#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006977
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006978
Fred Drake12c6e2d1999-12-14 21:25:03 +00006979#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006980/*[clinic input]
6981os.getlogin
6982
6983Return the actual login name.
6984[clinic start generated code]*/
6985
Larry Hastings2f936352014-08-05 14:04:04 +10006986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006987os_getlogin_impl(PyObject *module)
6988/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006989{
Victor Stinner8c62be82010-05-06 00:08:46 +00006990 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006991#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006992 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006993 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006994
6995 if (GetUserNameW(user_name, &num_chars)) {
6996 /* num_chars is the number of unicode chars plus null terminator */
6997 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006998 }
6999 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007000 result = PyErr_SetFromWindowsErr(GetLastError());
7001#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007002 char *name;
7003 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007004
Victor Stinner8c62be82010-05-06 00:08:46 +00007005 errno = 0;
7006 name = getlogin();
7007 if (name == NULL) {
7008 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007009 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007010 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007011 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007012 }
7013 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007014 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007015 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007016#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007017 return result;
7018}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007019#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007020
Larry Hastings2f936352014-08-05 14:04:04 +10007021
Guido van Rossumad0ee831995-03-01 10:34:45 +00007022#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007023/*[clinic input]
7024os.getuid
7025
7026Return the current process's user id.
7027[clinic start generated code]*/
7028
Larry Hastings2f936352014-08-05 14:04:04 +10007029static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007030os_getuid_impl(PyObject *module)
7031/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007032{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007033 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007034}
Larry Hastings2f936352014-08-05 14:04:04 +10007035#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007036
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007037
Brian Curtineb24d742010-04-12 17:16:38 +00007038#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007039#define HAVE_KILL
7040#endif /* MS_WINDOWS */
7041
7042#ifdef HAVE_KILL
7043/*[clinic input]
7044os.kill
7045
7046 pid: pid_t
7047 signal: Py_ssize_t
7048 /
7049
7050Kill a process with a signal.
7051[clinic start generated code]*/
7052
Larry Hastings2f936352014-08-05 14:04:04 +10007053static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007054os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7055/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007056#ifndef MS_WINDOWS
7057{
7058 if (kill(pid, (int)signal) == -1)
7059 return posix_error();
7060 Py_RETURN_NONE;
7061}
7062#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007063{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007064 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007065 DWORD sig = (DWORD)signal;
7066 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007068
Victor Stinner8c62be82010-05-06 00:08:46 +00007069 /* Console processes which share a common console can be sent CTRL+C or
7070 CTRL+BREAK events, provided they handle said events. */
7071 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007072 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 err = GetLastError();
7074 PyErr_SetFromWindowsErr(err);
7075 }
7076 else
7077 Py_RETURN_NONE;
7078 }
Brian Curtineb24d742010-04-12 17:16:38 +00007079
Victor Stinner8c62be82010-05-06 00:08:46 +00007080 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7081 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007082 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007083 if (handle == NULL) {
7084 err = GetLastError();
7085 return PyErr_SetFromWindowsErr(err);
7086 }
Brian Curtineb24d742010-04-12 17:16:38 +00007087
Victor Stinner8c62be82010-05-06 00:08:46 +00007088 if (TerminateProcess(handle, sig) == 0) {
7089 err = GetLastError();
7090 result = PyErr_SetFromWindowsErr(err);
7091 } else {
7092 Py_INCREF(Py_None);
7093 result = Py_None;
7094 }
Brian Curtineb24d742010-04-12 17:16:38 +00007095
Victor Stinner8c62be82010-05-06 00:08:46 +00007096 CloseHandle(handle);
7097 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007098}
Larry Hastings2f936352014-08-05 14:04:04 +10007099#endif /* !MS_WINDOWS */
7100#endif /* HAVE_KILL */
7101
7102
7103#ifdef HAVE_KILLPG
7104/*[clinic input]
7105os.killpg
7106
7107 pgid: pid_t
7108 signal: int
7109 /
7110
7111Kill a process group with a signal.
7112[clinic start generated code]*/
7113
Larry Hastings2f936352014-08-05 14:04:04 +10007114static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007115os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7116/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007117{
7118 /* XXX some man pages make the `pgid` parameter an int, others
7119 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7120 take the same type. Moreover, pid_t is always at least as wide as
7121 int (else compilation of this module fails), which is safe. */
7122 if (killpg(pgid, signal) == -1)
7123 return posix_error();
7124 Py_RETURN_NONE;
7125}
7126#endif /* HAVE_KILLPG */
7127
Brian Curtineb24d742010-04-12 17:16:38 +00007128
Guido van Rossumc0125471996-06-28 18:55:32 +00007129#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007130#ifdef HAVE_SYS_LOCK_H
7131#include <sys/lock.h>
7132#endif
7133
Larry Hastings2f936352014-08-05 14:04:04 +10007134/*[clinic input]
7135os.plock
7136 op: int
7137 /
7138
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007139Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007140[clinic start generated code]*/
7141
Larry Hastings2f936352014-08-05 14:04:04 +10007142static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007143os_plock_impl(PyObject *module, int op)
7144/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007145{
Victor Stinner8c62be82010-05-06 00:08:46 +00007146 if (plock(op) == -1)
7147 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007148 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007149}
Larry Hastings2f936352014-08-05 14:04:04 +10007150#endif /* HAVE_PLOCK */
7151
Guido van Rossumc0125471996-06-28 18:55:32 +00007152
Guido van Rossumb6775db1994-08-01 11:34:53 +00007153#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007154/*[clinic input]
7155os.setuid
7156
7157 uid: uid_t
7158 /
7159
7160Set the current process's user id.
7161[clinic start generated code]*/
7162
Larry Hastings2f936352014-08-05 14:04:04 +10007163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007164os_setuid_impl(PyObject *module, uid_t uid)
7165/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007166{
Victor Stinner8c62be82010-05-06 00:08:46 +00007167 if (setuid(uid) < 0)
7168 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007169 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007170}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007171#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007172
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007173
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007174#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007175/*[clinic input]
7176os.seteuid
7177
7178 euid: uid_t
7179 /
7180
7181Set the current process's effective user id.
7182[clinic start generated code]*/
7183
Larry Hastings2f936352014-08-05 14:04:04 +10007184static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007185os_seteuid_impl(PyObject *module, uid_t euid)
7186/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007187{
7188 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007189 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007190 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007191}
7192#endif /* HAVE_SETEUID */
7193
Larry Hastings2f936352014-08-05 14:04:04 +10007194
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007195#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007196/*[clinic input]
7197os.setegid
7198
7199 egid: gid_t
7200 /
7201
7202Set the current process's effective group id.
7203[clinic start generated code]*/
7204
Larry Hastings2f936352014-08-05 14:04:04 +10007205static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007206os_setegid_impl(PyObject *module, gid_t egid)
7207/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007208{
7209 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007210 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007211 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007212}
7213#endif /* HAVE_SETEGID */
7214
Larry Hastings2f936352014-08-05 14:04:04 +10007215
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007216#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007217/*[clinic input]
7218os.setreuid
7219
7220 ruid: uid_t
7221 euid: uid_t
7222 /
7223
7224Set the current process's real and effective user ids.
7225[clinic start generated code]*/
7226
Larry Hastings2f936352014-08-05 14:04:04 +10007227static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007228os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7229/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007230{
Victor Stinner8c62be82010-05-06 00:08:46 +00007231 if (setreuid(ruid, euid) < 0) {
7232 return posix_error();
7233 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007234 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007236}
7237#endif /* HAVE_SETREUID */
7238
Larry Hastings2f936352014-08-05 14:04:04 +10007239
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007240#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007241/*[clinic input]
7242os.setregid
7243
7244 rgid: gid_t
7245 egid: gid_t
7246 /
7247
7248Set the current process's real and effective group ids.
7249[clinic start generated code]*/
7250
Larry Hastings2f936352014-08-05 14:04:04 +10007251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007252os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7253/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007254{
7255 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007256 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007257 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007258}
7259#endif /* HAVE_SETREGID */
7260
Larry Hastings2f936352014-08-05 14:04:04 +10007261
Guido van Rossumb6775db1994-08-01 11:34:53 +00007262#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007263/*[clinic input]
7264os.setgid
7265 gid: gid_t
7266 /
7267
7268Set the current process's group id.
7269[clinic start generated code]*/
7270
Larry Hastings2f936352014-08-05 14:04:04 +10007271static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007272os_setgid_impl(PyObject *module, gid_t gid)
7273/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007274{
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 if (setgid(gid) < 0)
7276 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007277 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007278}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007279#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007280
Larry Hastings2f936352014-08-05 14:04:04 +10007281
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007282#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007283/*[clinic input]
7284os.setgroups
7285
7286 groups: object
7287 /
7288
7289Set the groups of the current process to list.
7290[clinic start generated code]*/
7291
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007292static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007293os_setgroups(PyObject *module, PyObject *groups)
7294/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007295{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007296 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007298
Victor Stinner8c62be82010-05-06 00:08:46 +00007299 if (!PySequence_Check(groups)) {
7300 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7301 return NULL;
7302 }
7303 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007304 if (len < 0) {
7305 return NULL;
7306 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007307 if (len > MAX_GROUPS) {
7308 PyErr_SetString(PyExc_ValueError, "too many groups");
7309 return NULL;
7310 }
7311 for(i = 0; i < len; i++) {
7312 PyObject *elem;
7313 elem = PySequence_GetItem(groups, i);
7314 if (!elem)
7315 return NULL;
7316 if (!PyLong_Check(elem)) {
7317 PyErr_SetString(PyExc_TypeError,
7318 "groups must be integers");
7319 Py_DECREF(elem);
7320 return NULL;
7321 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007322 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007323 Py_DECREF(elem);
7324 return NULL;
7325 }
7326 }
7327 Py_DECREF(elem);
7328 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007329
Victor Stinner8c62be82010-05-06 00:08:46 +00007330 if (setgroups(len, grouplist) < 0)
7331 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007332 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007333}
7334#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007335
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007336#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7337static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007338wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007339{
Victor Stinner8c62be82010-05-06 00:08:46 +00007340 PyObject *result;
7341 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02007342 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007343
Victor Stinner8c62be82010-05-06 00:08:46 +00007344 if (pid == -1)
7345 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007346
Victor Stinner8c62be82010-05-06 00:08:46 +00007347 if (struct_rusage == NULL) {
7348 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7349 if (m == NULL)
7350 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02007351 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007352 Py_DECREF(m);
7353 if (struct_rusage == NULL)
7354 return NULL;
7355 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007356
Victor Stinner8c62be82010-05-06 00:08:46 +00007357 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7358 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
7359 if (!result)
7360 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007361
7362#ifndef doubletime
7363#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7364#endif
7365
Victor Stinner8c62be82010-05-06 00:08:46 +00007366 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007367 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007368 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007369 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007370#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7372 SET_INT(result, 2, ru->ru_maxrss);
7373 SET_INT(result, 3, ru->ru_ixrss);
7374 SET_INT(result, 4, ru->ru_idrss);
7375 SET_INT(result, 5, ru->ru_isrss);
7376 SET_INT(result, 6, ru->ru_minflt);
7377 SET_INT(result, 7, ru->ru_majflt);
7378 SET_INT(result, 8, ru->ru_nswap);
7379 SET_INT(result, 9, ru->ru_inblock);
7380 SET_INT(result, 10, ru->ru_oublock);
7381 SET_INT(result, 11, ru->ru_msgsnd);
7382 SET_INT(result, 12, ru->ru_msgrcv);
7383 SET_INT(result, 13, ru->ru_nsignals);
7384 SET_INT(result, 14, ru->ru_nvcsw);
7385 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007386#undef SET_INT
7387
Victor Stinner8c62be82010-05-06 00:08:46 +00007388 if (PyErr_Occurred()) {
7389 Py_DECREF(result);
7390 return NULL;
7391 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007392
Victor Stinner8c62be82010-05-06 00:08:46 +00007393 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007394}
7395#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7396
Larry Hastings2f936352014-08-05 14:04:04 +10007397
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007398#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007399/*[clinic input]
7400os.wait3
7401
7402 options: int
7403Wait for completion of a child process.
7404
7405Returns a tuple of information about the child process:
7406 (pid, status, rusage)
7407[clinic start generated code]*/
7408
Larry Hastings2f936352014-08-05 14:04:04 +10007409static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007410os_wait3_impl(PyObject *module, int options)
7411/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007412{
Victor Stinner8c62be82010-05-06 00:08:46 +00007413 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007414 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007415 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007416 WAIT_TYPE status;
7417 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007418
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007419 do {
7420 Py_BEGIN_ALLOW_THREADS
7421 pid = wait3(&status, options, &ru);
7422 Py_END_ALLOW_THREADS
7423 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7424 if (pid < 0)
7425 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007426
Victor Stinner4195b5c2012-02-08 23:03:19 +01007427 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007428}
7429#endif /* HAVE_WAIT3 */
7430
Larry Hastings2f936352014-08-05 14:04:04 +10007431
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007432#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007433/*[clinic input]
7434
7435os.wait4
7436
7437 pid: pid_t
7438 options: int
7439
7440Wait for completion of a specific child process.
7441
7442Returns a tuple of information about the child process:
7443 (pid, status, rusage)
7444[clinic start generated code]*/
7445
Larry Hastings2f936352014-08-05 14:04:04 +10007446static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007447os_wait4_impl(PyObject *module, pid_t pid, int options)
7448/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007449{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007450 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007451 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007452 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 WAIT_TYPE status;
7454 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007455
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007456 do {
7457 Py_BEGIN_ALLOW_THREADS
7458 res = wait4(pid, &status, options, &ru);
7459 Py_END_ALLOW_THREADS
7460 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7461 if (res < 0)
7462 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007463
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007464 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007465}
7466#endif /* HAVE_WAIT4 */
7467
Larry Hastings2f936352014-08-05 14:04:04 +10007468
Ross Lagerwall7807c352011-03-17 20:20:30 +02007469#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007470/*[clinic input]
7471os.waitid
7472
7473 idtype: idtype_t
7474 Must be one of be P_PID, P_PGID or P_ALL.
7475 id: id_t
7476 The id to wait on.
7477 options: int
7478 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7479 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7480 /
7481
7482Returns the result of waiting for a process or processes.
7483
7484Returns either waitid_result or None if WNOHANG is specified and there are
7485no children in a waitable state.
7486[clinic start generated code]*/
7487
Larry Hastings2f936352014-08-05 14:04:04 +10007488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007489os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7490/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007491{
7492 PyObject *result;
7493 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007494 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007495 siginfo_t si;
7496 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007497
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007498 do {
7499 Py_BEGIN_ALLOW_THREADS
7500 res = waitid(idtype, id, &si, options);
7501 Py_END_ALLOW_THREADS
7502 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7503 if (res < 0)
7504 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007505
7506 if (si.si_pid == 0)
7507 Py_RETURN_NONE;
7508
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007509 result = PyStructSequence_New(WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007510 if (!result)
7511 return NULL;
7512
7513 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007514 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007515 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7516 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7517 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7518 if (PyErr_Occurred()) {
7519 Py_DECREF(result);
7520 return NULL;
7521 }
7522
7523 return result;
7524}
Larry Hastings2f936352014-08-05 14:04:04 +10007525#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007526
Larry Hastings2f936352014-08-05 14:04:04 +10007527
7528#if defined(HAVE_WAITPID)
7529/*[clinic input]
7530os.waitpid
7531 pid: pid_t
7532 options: int
7533 /
7534
7535Wait for completion of a given child process.
7536
7537Returns a tuple of information regarding the child process:
7538 (pid, status)
7539
7540The options argument is ignored on Windows.
7541[clinic start generated code]*/
7542
Larry Hastings2f936352014-08-05 14:04:04 +10007543static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007544os_waitpid_impl(PyObject *module, pid_t pid, int options)
7545/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007546{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007547 pid_t res;
7548 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 WAIT_TYPE status;
7550 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007551
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007552 do {
7553 Py_BEGIN_ALLOW_THREADS
7554 res = waitpid(pid, &status, options);
7555 Py_END_ALLOW_THREADS
7556 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7557 if (res < 0)
7558 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007559
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007560 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007561}
Tim Petersab034fa2002-02-01 11:27:43 +00007562#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007563/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007564/*[clinic input]
7565os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007566 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007567 options: int
7568 /
7569
7570Wait for completion of a given process.
7571
7572Returns a tuple of information regarding the process:
7573 (pid, status << 8)
7574
7575The options argument is ignored on Windows.
7576[clinic start generated code]*/
7577
Larry Hastings2f936352014-08-05 14:04:04 +10007578static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007579os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007580/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007581{
7582 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007583 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007584 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007585
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007586 do {
7587 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007588 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007589 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007590 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007591 Py_END_ALLOW_THREADS
7592 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007593 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007594 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007595
Victor Stinner8c62be82010-05-06 00:08:46 +00007596 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007597 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007598}
Larry Hastings2f936352014-08-05 14:04:04 +10007599#endif
7600
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007601
Guido van Rossumad0ee831995-03-01 10:34:45 +00007602#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007603/*[clinic input]
7604os.wait
7605
7606Wait for completion of a child process.
7607
7608Returns a tuple of information about the child process:
7609 (pid, status)
7610[clinic start generated code]*/
7611
Larry Hastings2f936352014-08-05 14:04:04 +10007612static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007613os_wait_impl(PyObject *module)
7614/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007615{
Victor Stinner8c62be82010-05-06 00:08:46 +00007616 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007617 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 WAIT_TYPE status;
7619 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007620
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007621 do {
7622 Py_BEGIN_ALLOW_THREADS
7623 pid = wait(&status);
7624 Py_END_ALLOW_THREADS
7625 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7626 if (pid < 0)
7627 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007628
Victor Stinner8c62be82010-05-06 00:08:46 +00007629 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007630}
Larry Hastings2f936352014-08-05 14:04:04 +10007631#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007632
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007633
Larry Hastings9cf065c2012-06-22 16:30:09 -07007634#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007635/*[clinic input]
7636os.readlink
7637
7638 path: path_t
7639 *
7640 dir_fd: dir_fd(requires='readlinkat') = None
7641
7642Return a string representing the path to which the symbolic link points.
7643
7644If dir_fd is not None, it should be a file descriptor open to a directory,
7645and path should be relative; path will then be relative to that directory.
7646
7647dir_fd may not be implemented on your platform. If it is unavailable,
7648using it will raise a NotImplementedError.
7649[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007650
Barry Warsaw53699e91996-12-10 23:23:01 +00007651static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007652os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7653/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007654{
Berker Peksage0b5b202018-08-15 13:03:41 +03007655#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007656 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007657 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007658
7659 Py_BEGIN_ALLOW_THREADS
7660#ifdef HAVE_READLINKAT
7661 if (dir_fd != DEFAULT_DIR_FD)
7662 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7663 else
7664#endif
7665 length = readlink(path->narrow, buffer, MAXPATHLEN);
7666 Py_END_ALLOW_THREADS
7667
7668 if (length < 0) {
7669 return path_error(path);
7670 }
7671 buffer[length] = '\0';
7672
7673 if (PyUnicode_Check(path->object))
7674 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7675 else
7676 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007677#elif defined(MS_WINDOWS)
7678 DWORD n_bytes_returned;
7679 DWORD io_result;
7680 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007681 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7682 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
7683 const wchar_t *print_name;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007684 PyObject *result;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007685
Larry Hastings2f936352014-08-05 14:04:04 +10007686 /* First get a handle to the reparse point */
7687 Py_BEGIN_ALLOW_THREADS
7688 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007689 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007690 0,
7691 0,
7692 0,
7693 OPEN_EXISTING,
7694 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7695 0);
7696 Py_END_ALLOW_THREADS
7697
Berker Peksage0b5b202018-08-15 13:03:41 +03007698 if (reparse_point_handle == INVALID_HANDLE_VALUE) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007699 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007700 }
Larry Hastings2f936352014-08-05 14:04:04 +10007701
7702 Py_BEGIN_ALLOW_THREADS
7703 /* New call DeviceIoControl to read the reparse point */
7704 io_result = DeviceIoControl(
7705 reparse_point_handle,
7706 FSCTL_GET_REPARSE_POINT,
7707 0, 0, /* in buffer */
7708 target_buffer, sizeof(target_buffer),
7709 &n_bytes_returned,
7710 0 /* we're not using OVERLAPPED_IO */
7711 );
7712 CloseHandle(reparse_point_handle);
7713 Py_END_ALLOW_THREADS
7714
Berker Peksage0b5b202018-08-15 13:03:41 +03007715 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007716 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007717 }
Larry Hastings2f936352014-08-05 14:04:04 +10007718
7719 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7720 {
7721 PyErr_SetString(PyExc_ValueError,
7722 "not a symbolic link");
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007723 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10007724 }
SSE43c34aad2018-02-13 00:10:35 +07007725 print_name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7726 rdb->SymbolicLinkReparseBuffer.PrintNameOffset);
Larry Hastings2f936352014-08-05 14:04:04 +10007727
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007728 result = PyUnicode_FromWideChar(print_name,
7729 rdb->SymbolicLinkReparseBuffer.PrintNameLength / sizeof(wchar_t));
7730 if (path->narrow) {
7731 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Berker Peksage0b5b202018-08-15 13:03:41 +03007732 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007733 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03007734#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007735}
Berker Peksage0b5b202018-08-15 13:03:41 +03007736#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007737
Larry Hastings9cf065c2012-06-22 16:30:09 -07007738#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007739
7740#if defined(MS_WINDOWS)
7741
7742/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Steve Dower6921e732018-03-05 14:26:08 -08007743static BOOLEAN (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007744
Larry Hastings9cf065c2012-06-22 16:30:09 -07007745static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007746check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007747{
7748 HINSTANCE hKernel32;
7749 /* only recheck */
Steve Dowercc16be82016-09-08 10:35:16 -07007750 if (Py_CreateSymbolicLinkW)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007751 return 1;
7752 hKernel32 = GetModuleHandleW(L"KERNEL32");
7753 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7754 "CreateSymbolicLinkW");
Steve Dowercc16be82016-09-08 10:35:16 -07007755 return Py_CreateSymbolicLinkW != NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007756}
7757
Steve Dower6921e732018-03-05 14:26:08 -08007758/* Remove the last portion of the path - return 0 on success */
7759static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007760_dirnameW(WCHAR *path)
7761{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007762 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08007763 size_t length = wcsnlen_s(path, MAX_PATH);
7764 if (length == MAX_PATH) {
7765 return -1;
7766 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007767
7768 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08007769 for(ptr = path + length; ptr != path; ptr--) {
7770 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007771 break;
Steve Dower6921e732018-03-05 14:26:08 -08007772 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007773 }
7774 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08007775 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007776}
7777
Victor Stinner31b3b922013-06-05 01:49:17 +02007778/* Is this path absolute? */
7779static int
7780_is_absW(const WCHAR *path)
7781{
Steve Dower6921e732018-03-05 14:26:08 -08007782 return path[0] == L'\\' || path[0] == L'/' ||
7783 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04007784}
7785
Steve Dower6921e732018-03-05 14:26:08 -08007786/* join root and rest with a backslash - return 0 on success */
7787static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007788_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7789{
Victor Stinner31b3b922013-06-05 01:49:17 +02007790 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08007791 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007792 }
7793
Steve Dower6921e732018-03-05 14:26:08 -08007794 if (wcscpy_s(dest_path, MAX_PATH, root)) {
7795 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007796 }
Steve Dower6921e732018-03-05 14:26:08 -08007797
7798 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
7799 return -1;
7800 }
7801
7802 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007803}
7804
Victor Stinner31b3b922013-06-05 01:49:17 +02007805/* Return True if the path at src relative to dest is a directory */
7806static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007807_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007808{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007809 WIN32_FILE_ATTRIBUTE_DATA src_info;
7810 WCHAR dest_parent[MAX_PATH];
7811 WCHAR src_resolved[MAX_PATH] = L"";
7812
7813 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08007814 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
7815 _dirnameW(dest_parent)) {
7816 return 0;
7817 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007818 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08007819 if (_joinW(src_resolved, dest_parent, src)) {
7820 return 0;
7821 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04007822 return (
7823 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7824 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7825 );
7826}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007827#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007828
Larry Hastings2f936352014-08-05 14:04:04 +10007829
7830/*[clinic input]
7831os.symlink
7832 src: path_t
7833 dst: path_t
7834 target_is_directory: bool = False
7835 *
7836 dir_fd: dir_fd(requires='symlinkat')=None
7837
7838# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7839
7840Create a symbolic link pointing to src named dst.
7841
7842target_is_directory is required on Windows if the target is to be
7843 interpreted as a directory. (On Windows, symlink requires
7844 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7845 target_is_directory is ignored on non-Windows platforms.
7846
7847If dir_fd is not None, it should be a file descriptor open to a directory,
7848 and path should be relative; path will then be relative to that directory.
7849dir_fd may not be implemented on your platform.
7850 If it is unavailable, using it will raise a NotImplementedError.
7851
7852[clinic start generated code]*/
7853
Larry Hastings2f936352014-08-05 14:04:04 +10007854static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007855os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007856 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007857/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007858{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007859#ifdef MS_WINDOWS
7860 DWORD result;
7861#else
7862 int result;
7863#endif
7864
Larry Hastings9cf065c2012-06-22 16:30:09 -07007865#ifdef MS_WINDOWS
7866 if (!check_CreateSymbolicLink()) {
7867 PyErr_SetString(PyExc_NotImplementedError,
7868 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007869 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007870 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007871 if (!win32_can_symlink) {
7872 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007873 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007874 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007875#endif
7876
Larry Hastings9cf065c2012-06-22 16:30:09 -07007877#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007878
Larry Hastings9cf065c2012-06-22 16:30:09 -07007879 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08007880 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07007881 /* if src is a directory, ensure target_is_directory==1 */
7882 target_is_directory |= _check_dirW(src->wide, dst->wide);
7883 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
7884 target_is_directory);
Steve Dower6921e732018-03-05 14:26:08 -08007885 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07007886 Py_END_ALLOW_THREADS
7887
Larry Hastings2f936352014-08-05 14:04:04 +10007888 if (!result)
7889 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007890
7891#else
7892
Steve Dower6921e732018-03-05 14:26:08 -08007893 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
7894 PyErr_SetString(PyExc_ValueError,
7895 "symlink: src and dst must be the same type");
7896 return NULL;
7897 }
7898
Larry Hastings9cf065c2012-06-22 16:30:09 -07007899 Py_BEGIN_ALLOW_THREADS
7900#if HAVE_SYMLINKAT
7901 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007902 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007903 else
7904#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007905 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007906 Py_END_ALLOW_THREADS
7907
Larry Hastings2f936352014-08-05 14:04:04 +10007908 if (result)
7909 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007910#endif
7911
Larry Hastings2f936352014-08-05 14:04:04 +10007912 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007913}
7914#endif /* HAVE_SYMLINK */
7915
Larry Hastings9cf065c2012-06-22 16:30:09 -07007916
Brian Curtind40e6f72010-07-08 21:39:08 +00007917
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007918
Larry Hastings605a62d2012-06-24 04:33:36 -07007919static PyStructSequence_Field times_result_fields[] = {
7920 {"user", "user time"},
7921 {"system", "system time"},
7922 {"children_user", "user time of children"},
7923 {"children_system", "system time of children"},
7924 {"elapsed", "elapsed time since an arbitrary point in the past"},
7925 {NULL}
7926};
7927
7928PyDoc_STRVAR(times_result__doc__,
7929"times_result: Result from os.times().\n\n\
7930This object may be accessed either as a tuple of\n\
7931 (user, system, children_user, children_system, elapsed),\n\
7932or via the attributes user, system, children_user, children_system,\n\
7933and elapsed.\n\
7934\n\
7935See os.times for more information.");
7936
7937static PyStructSequence_Desc times_result_desc = {
7938 "times_result", /* name */
7939 times_result__doc__, /* doc */
7940 times_result_fields,
7941 5
7942};
7943
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007944static PyTypeObject* TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -07007945
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007946#ifdef MS_WINDOWS
7947#define HAVE_TIMES /* mandatory, for the method table */
7948#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007949
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007950#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007951
7952static PyObject *
7953build_times_result(double user, double system,
7954 double children_user, double children_system,
7955 double elapsed)
7956{
Eddie Elizondo474eedf2018-11-13 04:09:31 -08007957 PyObject *value = PyStructSequence_New(TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07007958 if (value == NULL)
7959 return NULL;
7960
7961#define SET(i, field) \
7962 { \
7963 PyObject *o = PyFloat_FromDouble(field); \
7964 if (!o) { \
7965 Py_DECREF(value); \
7966 return NULL; \
7967 } \
7968 PyStructSequence_SET_ITEM(value, i, o); \
7969 } \
7970
7971 SET(0, user);
7972 SET(1, system);
7973 SET(2, children_user);
7974 SET(3, children_system);
7975 SET(4, elapsed);
7976
7977#undef SET
7978
7979 return value;
7980}
7981
Larry Hastings605a62d2012-06-24 04:33:36 -07007982
Larry Hastings2f936352014-08-05 14:04:04 +10007983#ifndef MS_WINDOWS
7984#define NEED_TICKS_PER_SECOND
7985static long ticks_per_second = -1;
7986#endif /* MS_WINDOWS */
7987
7988/*[clinic input]
7989os.times
7990
7991Return a collection containing process timing information.
7992
7993The object returned behaves like a named tuple with these fields:
7994 (utime, stime, cutime, cstime, elapsed_time)
7995All fields are floating point numbers.
7996[clinic start generated code]*/
7997
Larry Hastings2f936352014-08-05 14:04:04 +10007998static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007999os_times_impl(PyObject *module)
8000/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008001#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008002{
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 FILETIME create, exit, kernel, user;
8004 HANDLE hProc;
8005 hProc = GetCurrentProcess();
8006 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8007 /* The fields of a FILETIME structure are the hi and lo part
8008 of a 64-bit value expressed in 100 nanosecond units.
8009 1e7 is one second in such units; 1e-7 the inverse.
8010 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8011 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008012 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008013 (double)(user.dwHighDateTime*429.4967296 +
8014 user.dwLowDateTime*1e-7),
8015 (double)(kernel.dwHighDateTime*429.4967296 +
8016 kernel.dwLowDateTime*1e-7),
8017 (double)0,
8018 (double)0,
8019 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008020}
Larry Hastings2f936352014-08-05 14:04:04 +10008021#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008022{
Larry Hastings2f936352014-08-05 14:04:04 +10008023
8024
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008025 struct tms t;
8026 clock_t c;
8027 errno = 0;
8028 c = times(&t);
8029 if (c == (clock_t) -1)
8030 return posix_error();
8031 return build_times_result(
8032 (double)t.tms_utime / ticks_per_second,
8033 (double)t.tms_stime / ticks_per_second,
8034 (double)t.tms_cutime / ticks_per_second,
8035 (double)t.tms_cstime / ticks_per_second,
8036 (double)c / ticks_per_second);
8037}
Larry Hastings2f936352014-08-05 14:04:04 +10008038#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008039#endif /* HAVE_TIMES */
8040
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008041
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008042#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008043/*[clinic input]
8044os.getsid
8045
8046 pid: pid_t
8047 /
8048
8049Call the system call getsid(pid) and return the result.
8050[clinic start generated code]*/
8051
Larry Hastings2f936352014-08-05 14:04:04 +10008052static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008053os_getsid_impl(PyObject *module, pid_t pid)
8054/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008055{
Victor Stinner8c62be82010-05-06 00:08:46 +00008056 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008057 sid = getsid(pid);
8058 if (sid < 0)
8059 return posix_error();
8060 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008061}
8062#endif /* HAVE_GETSID */
8063
8064
Guido van Rossumb6775db1994-08-01 11:34:53 +00008065#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008066/*[clinic input]
8067os.setsid
8068
8069Call the system call setsid().
8070[clinic start generated code]*/
8071
Larry Hastings2f936352014-08-05 14:04:04 +10008072static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008073os_setsid_impl(PyObject *module)
8074/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008075{
Victor Stinner8c62be82010-05-06 00:08:46 +00008076 if (setsid() < 0)
8077 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008078 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008079}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008080#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008081
Larry Hastings2f936352014-08-05 14:04:04 +10008082
Guido van Rossumb6775db1994-08-01 11:34:53 +00008083#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008084/*[clinic input]
8085os.setpgid
8086
8087 pid: pid_t
8088 pgrp: pid_t
8089 /
8090
8091Call the system call setpgid(pid, pgrp).
8092[clinic start generated code]*/
8093
Larry Hastings2f936352014-08-05 14:04:04 +10008094static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008095os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8096/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008097{
Victor Stinner8c62be82010-05-06 00:08:46 +00008098 if (setpgid(pid, pgrp) < 0)
8099 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008100 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008101}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008102#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008103
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008104
Guido van Rossumb6775db1994-08-01 11:34:53 +00008105#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008106/*[clinic input]
8107os.tcgetpgrp
8108
8109 fd: int
8110 /
8111
8112Return the process group associated with the terminal specified by fd.
8113[clinic start generated code]*/
8114
Larry Hastings2f936352014-08-05 14:04:04 +10008115static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008116os_tcgetpgrp_impl(PyObject *module, int fd)
8117/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008118{
8119 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 if (pgid < 0)
8121 return posix_error();
8122 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008123}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008124#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008125
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008126
Guido van Rossumb6775db1994-08-01 11:34:53 +00008127#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008128/*[clinic input]
8129os.tcsetpgrp
8130
8131 fd: int
8132 pgid: pid_t
8133 /
8134
8135Set the process group associated with the terminal specified by fd.
8136[clinic start generated code]*/
8137
Larry Hastings2f936352014-08-05 14:04:04 +10008138static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008139os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8140/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008141{
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 if (tcsetpgrp(fd, pgid) < 0)
8143 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008144 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008145}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008146#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008147
Guido van Rossum687dd131993-05-17 08:34:16 +00008148/* Functions acting on file descriptors */
8149
Victor Stinnerdaf45552013-08-28 00:53:59 +02008150#ifdef O_CLOEXEC
8151extern int _Py_open_cloexec_works;
8152#endif
8153
Larry Hastings2f936352014-08-05 14:04:04 +10008154
8155/*[clinic input]
8156os.open -> int
8157 path: path_t
8158 flags: int
8159 mode: int = 0o777
8160 *
8161 dir_fd: dir_fd(requires='openat') = None
8162
8163# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8164
8165Open a file for low level IO. Returns a file descriptor (integer).
8166
8167If dir_fd is not None, it should be a file descriptor open to a directory,
8168 and path should be relative; path will then be relative to that directory.
8169dir_fd may not be implemented on your platform.
8170 If it is unavailable, using it will raise a NotImplementedError.
8171[clinic start generated code]*/
8172
Larry Hastings2f936352014-08-05 14:04:04 +10008173static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008174os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8175/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008176{
8177 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008178 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008179
Victor Stinnerdaf45552013-08-28 00:53:59 +02008180#ifdef O_CLOEXEC
8181 int *atomic_flag_works = &_Py_open_cloexec_works;
8182#elif !defined(MS_WINDOWS)
8183 int *atomic_flag_works = NULL;
8184#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008185
Victor Stinnerdaf45552013-08-28 00:53:59 +02008186#ifdef MS_WINDOWS
8187 flags |= O_NOINHERIT;
8188#elif defined(O_CLOEXEC)
8189 flags |= O_CLOEXEC;
8190#endif
8191
Steve Dower8fc89802015-04-12 00:26:27 -04008192 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008193 do {
8194 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008195#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008196 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008197#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008198#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008199 if (dir_fd != DEFAULT_DIR_FD)
8200 fd = openat(dir_fd, path->narrow, flags, mode);
8201 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008202#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008203 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008204#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008205 Py_END_ALLOW_THREADS
8206 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008207 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008208
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008209 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008210 if (!async_err)
8211 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008212 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008213 }
8214
Victor Stinnerdaf45552013-08-28 00:53:59 +02008215#ifndef MS_WINDOWS
8216 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8217 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008218 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008219 }
8220#endif
8221
Larry Hastings2f936352014-08-05 14:04:04 +10008222 return fd;
8223}
8224
8225
8226/*[clinic input]
8227os.close
8228
8229 fd: int
8230
8231Close a file descriptor.
8232[clinic start generated code]*/
8233
Barry Warsaw53699e91996-12-10 23:23:01 +00008234static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008235os_close_impl(PyObject *module, int fd)
8236/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008237{
Larry Hastings2f936352014-08-05 14:04:04 +10008238 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008239 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8240 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8241 * for more details.
8242 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008243 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008244 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008245 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008246 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008247 Py_END_ALLOW_THREADS
8248 if (res < 0)
8249 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008250 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008251}
8252
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008253
Larry Hastings2f936352014-08-05 14:04:04 +10008254/*[clinic input]
8255os.closerange
8256
8257 fd_low: int
8258 fd_high: int
8259 /
8260
8261Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8262[clinic start generated code]*/
8263
Larry Hastings2f936352014-08-05 14:04:04 +10008264static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008265os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8266/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008267{
8268 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00008269 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008270 _Py_BEGIN_SUPPRESS_IPH
Benjamin Peterson207116b2016-09-08 11:28:06 -07008271 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008272 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04008273 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008274 Py_END_ALLOW_THREADS
8275 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008276}
8277
8278
Larry Hastings2f936352014-08-05 14:04:04 +10008279/*[clinic input]
8280os.dup -> int
8281
8282 fd: int
8283 /
8284
8285Return a duplicate of a file descriptor.
8286[clinic start generated code]*/
8287
Larry Hastings2f936352014-08-05 14:04:04 +10008288static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008289os_dup_impl(PyObject *module, int fd)
8290/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008291{
8292 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008293}
8294
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008295
Larry Hastings2f936352014-08-05 14:04:04 +10008296/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008297os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008298 fd: int
8299 fd2: int
8300 inheritable: bool=True
8301
8302Duplicate file descriptor.
8303[clinic start generated code]*/
8304
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008305static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008306os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008307/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008308{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008309 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008310#if defined(HAVE_DUP3) && \
8311 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8312 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008313 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008314#endif
8315
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008316 if (fd < 0 || fd2 < 0) {
8317 posix_error();
8318 return -1;
8319 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008320
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008321 /* dup2() can fail with EINTR if the target FD is already open, because it
8322 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8323 * upon close(), and therefore below.
8324 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008325#ifdef MS_WINDOWS
8326 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008327 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008328 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008329 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008330 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008331 if (res < 0) {
8332 posix_error();
8333 return -1;
8334 }
8335 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008336
8337 /* Character files like console cannot be make non-inheritable */
8338 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8339 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008340 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008341 }
8342
8343#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8344 Py_BEGIN_ALLOW_THREADS
8345 if (!inheritable)
8346 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8347 else
8348 res = dup2(fd, fd2);
8349 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008350 if (res < 0) {
8351 posix_error();
8352 return -1;
8353 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008354
8355#else
8356
8357#ifdef HAVE_DUP3
8358 if (!inheritable && dup3_works != 0) {
8359 Py_BEGIN_ALLOW_THREADS
8360 res = dup3(fd, fd2, O_CLOEXEC);
8361 Py_END_ALLOW_THREADS
8362 if (res < 0) {
8363 if (dup3_works == -1)
8364 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008365 if (dup3_works) {
8366 posix_error();
8367 return -1;
8368 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008369 }
8370 }
8371
8372 if (inheritable || dup3_works == 0)
8373 {
8374#endif
8375 Py_BEGIN_ALLOW_THREADS
8376 res = dup2(fd, fd2);
8377 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008378 if (res < 0) {
8379 posix_error();
8380 return -1;
8381 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008382
8383 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8384 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008385 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008386 }
8387#ifdef HAVE_DUP3
8388 }
8389#endif
8390
8391#endif
8392
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008393 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008394}
8395
Larry Hastings2f936352014-08-05 14:04:04 +10008396
Ross Lagerwall7807c352011-03-17 20:20:30 +02008397#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008398/*[clinic input]
8399os.lockf
8400
8401 fd: int
8402 An open file descriptor.
8403 command: int
8404 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8405 length: Py_off_t
8406 The number of bytes to lock, starting at the current position.
8407 /
8408
8409Apply, test or remove a POSIX lock on an open file descriptor.
8410
8411[clinic start generated code]*/
8412
Larry Hastings2f936352014-08-05 14:04:04 +10008413static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008414os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8415/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008416{
8417 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008418
8419 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008420 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008421 Py_END_ALLOW_THREADS
8422
8423 if (res < 0)
8424 return posix_error();
8425
8426 Py_RETURN_NONE;
8427}
Larry Hastings2f936352014-08-05 14:04:04 +10008428#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008429
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008430
Larry Hastings2f936352014-08-05 14:04:04 +10008431/*[clinic input]
8432os.lseek -> Py_off_t
8433
8434 fd: int
8435 position: Py_off_t
8436 how: int
8437 /
8438
8439Set the position of a file descriptor. Return the new position.
8440
8441Return the new cursor position in number of bytes
8442relative to the beginning of the file.
8443[clinic start generated code]*/
8444
Larry Hastings2f936352014-08-05 14:04:04 +10008445static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008446os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8447/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008448{
8449 Py_off_t result;
8450
Guido van Rossum687dd131993-05-17 08:34:16 +00008451#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8453 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008454 case 0: how = SEEK_SET; break;
8455 case 1: how = SEEK_CUR; break;
8456 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008457 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008458#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008459
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008461 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008462#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008463 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008464#else
Larry Hastings2f936352014-08-05 14:04:04 +10008465 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008466#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008467 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008468 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008469 if (result < 0)
8470 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008471
Larry Hastings2f936352014-08-05 14:04:04 +10008472 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008473}
8474
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008475
Larry Hastings2f936352014-08-05 14:04:04 +10008476/*[clinic input]
8477os.read
8478 fd: int
8479 length: Py_ssize_t
8480 /
8481
8482Read from a file descriptor. Returns a bytes object.
8483[clinic start generated code]*/
8484
Larry Hastings2f936352014-08-05 14:04:04 +10008485static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008486os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8487/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008488{
Victor Stinner8c62be82010-05-06 00:08:46 +00008489 Py_ssize_t n;
8490 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008491
8492 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008493 errno = EINVAL;
8494 return posix_error();
8495 }
Larry Hastings2f936352014-08-05 14:04:04 +10008496
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008497 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008498
8499 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 if (buffer == NULL)
8501 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008502
Victor Stinner66aab0c2015-03-19 22:53:20 +01008503 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8504 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008505 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008506 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008507 }
Larry Hastings2f936352014-08-05 14:04:04 +10008508
8509 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008510 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008511
Victor Stinner8c62be82010-05-06 00:08:46 +00008512 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008513}
8514
Ross Lagerwall7807c352011-03-17 20:20:30 +02008515#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008516 || defined(__APPLE__))) \
8517 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8518 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8519static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008520iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008521{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008522 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008523
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008524 *iov = PyMem_New(struct iovec, cnt);
8525 if (*iov == NULL) {
8526 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008527 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008528 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008529
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008530 *buf = PyMem_New(Py_buffer, cnt);
8531 if (*buf == NULL) {
8532 PyMem_Del(*iov);
8533 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008534 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008535 }
8536
8537 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008538 PyObject *item = PySequence_GetItem(seq, i);
8539 if (item == NULL)
8540 goto fail;
8541 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8542 Py_DECREF(item);
8543 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008544 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008545 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008546 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008547 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008548 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008549 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008550
8551fail:
8552 PyMem_Del(*iov);
8553 for (j = 0; j < i; j++) {
8554 PyBuffer_Release(&(*buf)[j]);
8555 }
8556 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008557 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008558}
8559
8560static void
8561iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8562{
8563 int i;
8564 PyMem_Del(iov);
8565 for (i = 0; i < cnt; i++) {
8566 PyBuffer_Release(&buf[i]);
8567 }
8568 PyMem_Del(buf);
8569}
8570#endif
8571
Larry Hastings2f936352014-08-05 14:04:04 +10008572
Ross Lagerwall7807c352011-03-17 20:20:30 +02008573#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008574/*[clinic input]
8575os.readv -> Py_ssize_t
8576
8577 fd: int
8578 buffers: object
8579 /
8580
8581Read from a file descriptor fd into an iterable of buffers.
8582
8583The buffers should be mutable buffers accepting bytes.
8584readv will transfer data into each buffer until it is full
8585and then move on to the next buffer in the sequence to hold
8586the rest of the data.
8587
8588readv returns the total number of bytes read,
8589which may be less than the total capacity of all the buffers.
8590[clinic start generated code]*/
8591
Larry Hastings2f936352014-08-05 14:04:04 +10008592static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008593os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8594/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008595{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008596 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008597 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008598 struct iovec *iov;
8599 Py_buffer *buf;
8600
Larry Hastings2f936352014-08-05 14:04:04 +10008601 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008602 PyErr_SetString(PyExc_TypeError,
8603 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008604 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008605 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008606
Larry Hastings2f936352014-08-05 14:04:04 +10008607 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008608 if (cnt < 0)
8609 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008610
8611 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8612 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008613
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008614 do {
8615 Py_BEGIN_ALLOW_THREADS
8616 n = readv(fd, iov, cnt);
8617 Py_END_ALLOW_THREADS
8618 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008619
8620 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008621 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008622 if (!async_err)
8623 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008624 return -1;
8625 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008626
Larry Hastings2f936352014-08-05 14:04:04 +10008627 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008628}
Larry Hastings2f936352014-08-05 14:04:04 +10008629#endif /* HAVE_READV */
8630
Ross Lagerwall7807c352011-03-17 20:20:30 +02008631
8632#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008633/*[clinic input]
8634# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8635os.pread
8636
8637 fd: int
8638 length: int
8639 offset: Py_off_t
8640 /
8641
8642Read a number of bytes from a file descriptor starting at a particular offset.
8643
8644Read length bytes from file descriptor fd, starting at offset bytes from
8645the beginning of the file. The file offset remains unchanged.
8646[clinic start generated code]*/
8647
Larry Hastings2f936352014-08-05 14:04:04 +10008648static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008649os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8650/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008651{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008652 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008653 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008654 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008655
Larry Hastings2f936352014-08-05 14:04:04 +10008656 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008657 errno = EINVAL;
8658 return posix_error();
8659 }
Larry Hastings2f936352014-08-05 14:04:04 +10008660 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008661 if (buffer == NULL)
8662 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008663
8664 do {
8665 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008666 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008667 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008668 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008669 Py_END_ALLOW_THREADS
8670 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8671
Ross Lagerwall7807c352011-03-17 20:20:30 +02008672 if (n < 0) {
8673 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008674 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008675 }
Larry Hastings2f936352014-08-05 14:04:04 +10008676 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008677 _PyBytes_Resize(&buffer, n);
8678 return buffer;
8679}
Larry Hastings2f936352014-08-05 14:04:04 +10008680#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008681
Pablo Galindo4defba32018-01-27 16:16:37 +00008682#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8683/*[clinic input]
8684os.preadv -> Py_ssize_t
8685
8686 fd: int
8687 buffers: object
8688 offset: Py_off_t
8689 flags: int = 0
8690 /
8691
8692Reads from a file descriptor into a number of mutable bytes-like objects.
8693
8694Combines the functionality of readv() and pread(). As readv(), it will
8695transfer data into each buffer until it is full and then move on to the next
8696buffer in the sequence to hold the rest of the data. Its fourth argument,
8697specifies the file offset at which the input operation is to be performed. It
8698will return the total number of bytes read (which can be less than the total
8699capacity of all the objects).
8700
8701The flags argument contains a bitwise OR of zero or more of the following flags:
8702
8703- RWF_HIPRI
8704- RWF_NOWAIT
8705
8706Using non-zero flags requires Linux 4.6 or newer.
8707[clinic start generated code]*/
8708
8709static Py_ssize_t
8710os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
8711 int flags)
8712/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
8713{
8714 Py_ssize_t cnt, n;
8715 int async_err = 0;
8716 struct iovec *iov;
8717 Py_buffer *buf;
8718
8719 if (!PySequence_Check(buffers)) {
8720 PyErr_SetString(PyExc_TypeError,
8721 "preadv2() arg 2 must be a sequence");
8722 return -1;
8723 }
8724
8725 cnt = PySequence_Size(buffers);
8726 if (cnt < 0) {
8727 return -1;
8728 }
8729
8730#ifndef HAVE_PREADV2
8731 if(flags != 0) {
8732 argument_unavailable_error("preadv2", "flags");
8733 return -1;
8734 }
8735#endif
8736
8737 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
8738 return -1;
8739 }
8740#ifdef HAVE_PREADV2
8741 do {
8742 Py_BEGIN_ALLOW_THREADS
8743 _Py_BEGIN_SUPPRESS_IPH
8744 n = preadv2(fd, iov, cnt, offset, flags);
8745 _Py_END_SUPPRESS_IPH
8746 Py_END_ALLOW_THREADS
8747 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8748#else
8749 do {
8750 Py_BEGIN_ALLOW_THREADS
8751 _Py_BEGIN_SUPPRESS_IPH
8752 n = preadv(fd, iov, cnt, offset);
8753 _Py_END_SUPPRESS_IPH
8754 Py_END_ALLOW_THREADS
8755 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8756#endif
8757
8758 iov_cleanup(iov, buf, cnt);
8759 if (n < 0) {
8760 if (!async_err) {
8761 posix_error();
8762 }
8763 return -1;
8764 }
8765
8766 return n;
8767}
8768#endif /* HAVE_PREADV */
8769
Larry Hastings2f936352014-08-05 14:04:04 +10008770
8771/*[clinic input]
8772os.write -> Py_ssize_t
8773
8774 fd: int
8775 data: Py_buffer
8776 /
8777
8778Write a bytes object to a file descriptor.
8779[clinic start generated code]*/
8780
Larry Hastings2f936352014-08-05 14:04:04 +10008781static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008782os_write_impl(PyObject *module, int fd, Py_buffer *data)
8783/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008784{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008785 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008786}
8787
8788#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008789PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008790"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008791sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008792 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008793Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008794
Larry Hastings2f936352014-08-05 14:04:04 +10008795/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008796static PyObject *
8797posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8798{
8799 int in, out;
8800 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008801 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008802 off_t offset;
8803
8804#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8805#ifndef __APPLE__
8806 Py_ssize_t len;
8807#endif
8808 PyObject *headers = NULL, *trailers = NULL;
8809 Py_buffer *hbuf, *tbuf;
8810 off_t sbytes;
8811 struct sf_hdtr sf;
8812 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008813 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008814 static char *keywords[] = {"out", "in",
8815 "offset", "count",
8816 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008817
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008818 sf.headers = NULL;
8819 sf.trailers = NULL;
8820
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008821#ifdef __APPLE__
8822 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008823 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008824#else
8825 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008826 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008827#endif
8828 &headers, &trailers, &flags))
8829 return NULL;
8830 if (headers != NULL) {
8831 if (!PySequence_Check(headers)) {
8832 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008833 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008834 return NULL;
8835 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008836 Py_ssize_t i = PySequence_Size(headers);
8837 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008838 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008839 if (i > INT_MAX) {
8840 PyErr_SetString(PyExc_OverflowError,
8841 "sendfile() header is too large");
8842 return NULL;
8843 }
8844 if (i > 0) {
8845 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008846 if (iov_setup(&(sf.headers), &hbuf,
8847 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008848 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008849#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008850 for (i = 0; i < sf.hdr_cnt; i++) {
8851 Py_ssize_t blen = sf.headers[i].iov_len;
8852# define OFF_T_MAX 0x7fffffffffffffff
8853 if (sbytes >= OFF_T_MAX - blen) {
8854 PyErr_SetString(PyExc_OverflowError,
8855 "sendfile() header is too large");
8856 return NULL;
8857 }
8858 sbytes += blen;
8859 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008860#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008861 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008862 }
8863 }
8864 if (trailers != NULL) {
8865 if (!PySequence_Check(trailers)) {
8866 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008867 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008868 return NULL;
8869 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008870 Py_ssize_t i = PySequence_Size(trailers);
8871 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008872 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008873 if (i > INT_MAX) {
8874 PyErr_SetString(PyExc_OverflowError,
8875 "sendfile() trailer is too large");
8876 return NULL;
8877 }
8878 if (i > 0) {
8879 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008880 if (iov_setup(&(sf.trailers), &tbuf,
8881 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008882 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008883 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008884 }
8885 }
8886
Steve Dower8fc89802015-04-12 00:26:27 -04008887 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008888 do {
8889 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008890#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008891 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008892#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008893 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008894#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008895 Py_END_ALLOW_THREADS
8896 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008897 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008898
8899 if (sf.headers != NULL)
8900 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8901 if (sf.trailers != NULL)
8902 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8903
8904 if (ret < 0) {
8905 if ((errno == EAGAIN) || (errno == EBUSY)) {
8906 if (sbytes != 0) {
8907 // some data has been sent
8908 goto done;
8909 }
8910 else {
8911 // no data has been sent; upper application is supposed
8912 // to retry on EAGAIN or EBUSY
8913 return posix_error();
8914 }
8915 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008916 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008917 }
8918 goto done;
8919
8920done:
8921 #if !defined(HAVE_LARGEFILE_SUPPORT)
8922 return Py_BuildValue("l", sbytes);
8923 #else
8924 return Py_BuildValue("L", sbytes);
8925 #endif
8926
8927#else
8928 Py_ssize_t count;
8929 PyObject *offobj;
8930 static char *keywords[] = {"out", "in",
8931 "offset", "count", NULL};
8932 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8933 keywords, &out, &in, &offobj, &count))
8934 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07008935#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008936 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008937 do {
8938 Py_BEGIN_ALLOW_THREADS
8939 ret = sendfile(out, in, NULL, count);
8940 Py_END_ALLOW_THREADS
8941 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008942 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008943 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008944 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008945 }
8946#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008947 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008948 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008949
8950 do {
8951 Py_BEGIN_ALLOW_THREADS
8952 ret = sendfile(out, in, &offset, count);
8953 Py_END_ALLOW_THREADS
8954 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008955 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008956 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008957 return Py_BuildValue("n", ret);
8958#endif
8959}
Larry Hastings2f936352014-08-05 14:04:04 +10008960#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008961
Larry Hastings2f936352014-08-05 14:04:04 +10008962
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008963#if defined(__APPLE__)
8964/*[clinic input]
8965os._fcopyfile
8966
8967 infd: int
8968 outfd: int
8969 flags: int
8970 /
8971
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07008972Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008973[clinic start generated code]*/
8974
8975static PyObject *
8976os__fcopyfile_impl(PyObject *module, int infd, int outfd, int flags)
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07008977/*[clinic end generated code: output=8e8885c721ec38e3 input=69e0770e600cb44f]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02008978{
8979 int ret;
8980
8981 Py_BEGIN_ALLOW_THREADS
8982 ret = fcopyfile(infd, outfd, NULL, flags);
8983 Py_END_ALLOW_THREADS
8984 if (ret < 0)
8985 return posix_error();
8986 Py_RETURN_NONE;
8987}
8988#endif
8989
8990
Larry Hastings2f936352014-08-05 14:04:04 +10008991/*[clinic input]
8992os.fstat
8993
8994 fd : int
8995
8996Perform a stat system call on the given file descriptor.
8997
8998Like stat(), but for an open file descriptor.
8999Equivalent to os.stat(fd).
9000[clinic start generated code]*/
9001
Larry Hastings2f936352014-08-05 14:04:04 +10009002static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009003os_fstat_impl(PyObject *module, int fd)
9004/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009005{
Victor Stinner8c62be82010-05-06 00:08:46 +00009006 STRUCT_STAT st;
9007 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009008 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009009
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009010 do {
9011 Py_BEGIN_ALLOW_THREADS
9012 res = FSTAT(fd, &st);
9013 Py_END_ALLOW_THREADS
9014 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009015 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009016#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009017 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009018#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009019 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009020#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009021 }
Tim Peters5aa91602002-01-30 05:46:57 +00009022
Victor Stinner4195b5c2012-02-08 23:03:19 +01009023 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009024}
9025
Larry Hastings2f936352014-08-05 14:04:04 +10009026
9027/*[clinic input]
9028os.isatty -> bool
9029 fd: int
9030 /
9031
9032Return True if the fd is connected to a terminal.
9033
9034Return True if the file descriptor is an open file descriptor
9035connected to the slave end of a terminal.
9036[clinic start generated code]*/
9037
Larry Hastings2f936352014-08-05 14:04:04 +10009038static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009039os_isatty_impl(PyObject *module, int fd)
9040/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009041{
Steve Dower8fc89802015-04-12 00:26:27 -04009042 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009043 _Py_BEGIN_SUPPRESS_IPH
9044 return_value = isatty(fd);
9045 _Py_END_SUPPRESS_IPH
9046 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009047}
9048
9049
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009050#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009051/*[clinic input]
9052os.pipe
9053
9054Create a pipe.
9055
9056Returns a tuple of two file descriptors:
9057 (read_fd, write_fd)
9058[clinic start generated code]*/
9059
Larry Hastings2f936352014-08-05 14:04:04 +10009060static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009061os_pipe_impl(PyObject *module)
9062/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009063{
Victor Stinner8c62be82010-05-06 00:08:46 +00009064 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009065#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009066 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009067 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009068 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009069#else
9070 int res;
9071#endif
9072
9073#ifdef MS_WINDOWS
9074 attr.nLength = sizeof(attr);
9075 attr.lpSecurityDescriptor = NULL;
9076 attr.bInheritHandle = FALSE;
9077
9078 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009079 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009080 ok = CreatePipe(&read, &write, &attr, 0);
9081 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009082 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9083 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009084 if (fds[0] == -1 || fds[1] == -1) {
9085 CloseHandle(read);
9086 CloseHandle(write);
9087 ok = 0;
9088 }
9089 }
Steve Dowerc3630612016-11-19 18:41:16 -08009090 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009091 Py_END_ALLOW_THREADS
9092
Victor Stinner8c62be82010-05-06 00:08:46 +00009093 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009094 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009095#else
9096
9097#ifdef HAVE_PIPE2
9098 Py_BEGIN_ALLOW_THREADS
9099 res = pipe2(fds, O_CLOEXEC);
9100 Py_END_ALLOW_THREADS
9101
9102 if (res != 0 && errno == ENOSYS)
9103 {
9104#endif
9105 Py_BEGIN_ALLOW_THREADS
9106 res = pipe(fds);
9107 Py_END_ALLOW_THREADS
9108
9109 if (res == 0) {
9110 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9111 close(fds[0]);
9112 close(fds[1]);
9113 return NULL;
9114 }
9115 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9116 close(fds[0]);
9117 close(fds[1]);
9118 return NULL;
9119 }
9120 }
9121#ifdef HAVE_PIPE2
9122 }
9123#endif
9124
9125 if (res != 0)
9126 return PyErr_SetFromErrno(PyExc_OSError);
9127#endif /* !MS_WINDOWS */
9128 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009129}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009130#endif /* HAVE_PIPE */
9131
Larry Hastings2f936352014-08-05 14:04:04 +10009132
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009133#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009134/*[clinic input]
9135os.pipe2
9136
9137 flags: int
9138 /
9139
9140Create a pipe with flags set atomically.
9141
9142Returns a tuple of two file descriptors:
9143 (read_fd, write_fd)
9144
9145flags can be constructed by ORing together one or more of these values:
9146O_NONBLOCK, O_CLOEXEC.
9147[clinic start generated code]*/
9148
Larry Hastings2f936352014-08-05 14:04:04 +10009149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009150os_pipe2_impl(PyObject *module, int flags)
9151/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009152{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009153 int fds[2];
9154 int res;
9155
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009156 res = pipe2(fds, flags);
9157 if (res != 0)
9158 return posix_error();
9159 return Py_BuildValue("(ii)", fds[0], fds[1]);
9160}
9161#endif /* HAVE_PIPE2 */
9162
Larry Hastings2f936352014-08-05 14:04:04 +10009163
Ross Lagerwall7807c352011-03-17 20:20:30 +02009164#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009165/*[clinic input]
9166os.writev -> Py_ssize_t
9167 fd: int
9168 buffers: object
9169 /
9170
9171Iterate over buffers, and write the contents of each to a file descriptor.
9172
9173Returns the total number of bytes written.
9174buffers must be a sequence of bytes-like objects.
9175[clinic start generated code]*/
9176
Larry Hastings2f936352014-08-05 14:04:04 +10009177static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009178os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9179/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009180{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009181 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009182 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009183 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009184 struct iovec *iov;
9185 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009186
9187 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009188 PyErr_SetString(PyExc_TypeError,
9189 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009190 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009191 }
Larry Hastings2f936352014-08-05 14:04:04 +10009192 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009193 if (cnt < 0)
9194 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009195
Larry Hastings2f936352014-08-05 14:04:04 +10009196 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9197 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009198 }
9199
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009200 do {
9201 Py_BEGIN_ALLOW_THREADS
9202 result = writev(fd, iov, cnt);
9203 Py_END_ALLOW_THREADS
9204 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009205
9206 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009207 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009208 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009209
Georg Brandl306336b2012-06-24 12:55:33 +02009210 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009211}
Larry Hastings2f936352014-08-05 14:04:04 +10009212#endif /* HAVE_WRITEV */
9213
9214
9215#ifdef HAVE_PWRITE
9216/*[clinic input]
9217os.pwrite -> Py_ssize_t
9218
9219 fd: int
9220 buffer: Py_buffer
9221 offset: Py_off_t
9222 /
9223
9224Write bytes to a file descriptor starting at a particular offset.
9225
9226Write buffer to fd, starting at offset bytes from the beginning of
9227the file. Returns the number of bytes writte. Does not change the
9228current file offset.
9229[clinic start generated code]*/
9230
Larry Hastings2f936352014-08-05 14:04:04 +10009231static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009232os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9233/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009234{
9235 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009236 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009237
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009238 do {
9239 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009240 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009241 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009242 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009243 Py_END_ALLOW_THREADS
9244 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009245
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009246 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009247 posix_error();
9248 return size;
9249}
9250#endif /* HAVE_PWRITE */
9251
Pablo Galindo4defba32018-01-27 16:16:37 +00009252#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9253/*[clinic input]
9254os.pwritev -> Py_ssize_t
9255
9256 fd: int
9257 buffers: object
9258 offset: Py_off_t
9259 flags: int = 0
9260 /
9261
9262Writes the contents of bytes-like objects to a file descriptor at a given offset.
9263
9264Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9265of bytes-like objects. Buffers are processed in array order. Entire contents of first
9266buffer is written before proceeding to second, and so on. The operating system may
9267set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9268This function writes the contents of each object to the file descriptor and returns
9269the total number of bytes written.
9270
9271The flags argument contains a bitwise OR of zero or more of the following flags:
9272
9273- RWF_DSYNC
9274- RWF_SYNC
9275
9276Using non-zero flags requires Linux 4.7 or newer.
9277[clinic start generated code]*/
9278
9279static Py_ssize_t
9280os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9281 int flags)
9282/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9283{
9284 Py_ssize_t cnt;
9285 Py_ssize_t result;
9286 int async_err = 0;
9287 struct iovec *iov;
9288 Py_buffer *buf;
9289
9290 if (!PySequence_Check(buffers)) {
9291 PyErr_SetString(PyExc_TypeError,
9292 "pwritev() arg 2 must be a sequence");
9293 return -1;
9294 }
9295
9296 cnt = PySequence_Size(buffers);
9297 if (cnt < 0) {
9298 return -1;
9299 }
9300
9301#ifndef HAVE_PWRITEV2
9302 if(flags != 0) {
9303 argument_unavailable_error("pwritev2", "flags");
9304 return -1;
9305 }
9306#endif
9307
9308 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9309 return -1;
9310 }
9311#ifdef HAVE_PWRITEV2
9312 do {
9313 Py_BEGIN_ALLOW_THREADS
9314 _Py_BEGIN_SUPPRESS_IPH
9315 result = pwritev2(fd, iov, cnt, offset, flags);
9316 _Py_END_SUPPRESS_IPH
9317 Py_END_ALLOW_THREADS
9318 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9319#else
9320 do {
9321 Py_BEGIN_ALLOW_THREADS
9322 _Py_BEGIN_SUPPRESS_IPH
9323 result = pwritev(fd, iov, cnt, offset);
9324 _Py_END_SUPPRESS_IPH
9325 Py_END_ALLOW_THREADS
9326 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9327#endif
9328
9329 iov_cleanup(iov, buf, cnt);
9330 if (result < 0) {
9331 if (!async_err) {
9332 posix_error();
9333 }
9334 return -1;
9335 }
9336
9337 return result;
9338}
9339#endif /* HAVE_PWRITEV */
9340
9341
9342
Larry Hastings2f936352014-08-05 14:04:04 +10009343
9344#ifdef HAVE_MKFIFO
9345/*[clinic input]
9346os.mkfifo
9347
9348 path: path_t
9349 mode: int=0o666
9350 *
9351 dir_fd: dir_fd(requires='mkfifoat')=None
9352
9353Create a "fifo" (a POSIX named pipe).
9354
9355If dir_fd is not None, it should be a file descriptor open to a directory,
9356 and path should be relative; path will then be relative to that directory.
9357dir_fd may not be implemented on your platform.
9358 If it is unavailable, using it will raise a NotImplementedError.
9359[clinic start generated code]*/
9360
Larry Hastings2f936352014-08-05 14:04:04 +10009361static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009362os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9363/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009364{
9365 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009366 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009367
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009368 do {
9369 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009370#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009371 if (dir_fd != DEFAULT_DIR_FD)
9372 result = mkfifoat(dir_fd, path->narrow, mode);
9373 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009374#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009375 result = mkfifo(path->narrow, mode);
9376 Py_END_ALLOW_THREADS
9377 } while (result != 0 && errno == EINTR &&
9378 !(async_err = PyErr_CheckSignals()));
9379 if (result != 0)
9380 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009381
9382 Py_RETURN_NONE;
9383}
9384#endif /* HAVE_MKFIFO */
9385
9386
9387#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9388/*[clinic input]
9389os.mknod
9390
9391 path: path_t
9392 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009393 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009394 *
9395 dir_fd: dir_fd(requires='mknodat')=None
9396
9397Create a node in the file system.
9398
9399Create a node in the file system (file, device special file or named pipe)
9400at path. mode specifies both the permissions to use and the
9401type of node to be created, being combined (bitwise OR) with one of
9402S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9403device defines the newly created device special file (probably using
9404os.makedev()). Otherwise device is ignored.
9405
9406If dir_fd is not None, it should be a file descriptor open to a directory,
9407 and path should be relative; path will then be relative to that directory.
9408dir_fd may not be implemented on your platform.
9409 If it is unavailable, using it will raise a NotImplementedError.
9410[clinic start generated code]*/
9411
Larry Hastings2f936352014-08-05 14:04:04 +10009412static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009413os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009414 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009415/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009416{
9417 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009418 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009419
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009420 do {
9421 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009422#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009423 if (dir_fd != DEFAULT_DIR_FD)
9424 result = mknodat(dir_fd, path->narrow, mode, device);
9425 else
Larry Hastings2f936352014-08-05 14:04:04 +10009426#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009427 result = mknod(path->narrow, mode, device);
9428 Py_END_ALLOW_THREADS
9429 } while (result != 0 && errno == EINTR &&
9430 !(async_err = PyErr_CheckSignals()));
9431 if (result != 0)
9432 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009433
9434 Py_RETURN_NONE;
9435}
9436#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9437
9438
9439#ifdef HAVE_DEVICE_MACROS
9440/*[clinic input]
9441os.major -> unsigned_int
9442
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009443 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009444 /
9445
9446Extracts a device major number from a raw device number.
9447[clinic start generated code]*/
9448
Larry Hastings2f936352014-08-05 14:04:04 +10009449static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009450os_major_impl(PyObject *module, dev_t device)
9451/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009452{
9453 return major(device);
9454}
9455
9456
9457/*[clinic input]
9458os.minor -> unsigned_int
9459
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009460 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009461 /
9462
9463Extracts a device minor number from a raw device number.
9464[clinic start generated code]*/
9465
Larry Hastings2f936352014-08-05 14:04:04 +10009466static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009467os_minor_impl(PyObject *module, dev_t device)
9468/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009469{
9470 return minor(device);
9471}
9472
9473
9474/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009475os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009476
9477 major: int
9478 minor: int
9479 /
9480
9481Composes a raw device number from the major and minor device numbers.
9482[clinic start generated code]*/
9483
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009484static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009485os_makedev_impl(PyObject *module, int major, int minor)
9486/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009487{
9488 return makedev(major, minor);
9489}
9490#endif /* HAVE_DEVICE_MACROS */
9491
9492
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009493#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009494/*[clinic input]
9495os.ftruncate
9496
9497 fd: int
9498 length: Py_off_t
9499 /
9500
9501Truncate a file, specified by file descriptor, to a specific length.
9502[clinic start generated code]*/
9503
Larry Hastings2f936352014-08-05 14:04:04 +10009504static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009505os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9506/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009507{
9508 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009509 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009510
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009511 do {
9512 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009513 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009514#ifdef MS_WINDOWS
9515 result = _chsize_s(fd, length);
9516#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009517 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009518#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009519 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009520 Py_END_ALLOW_THREADS
9521 } while (result != 0 && errno == EINTR &&
9522 !(async_err = PyErr_CheckSignals()));
9523 if (result != 0)
9524 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009525 Py_RETURN_NONE;
9526}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009527#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009528
9529
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009530#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009531/*[clinic input]
9532os.truncate
9533 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9534 length: Py_off_t
9535
9536Truncate a file, specified by path, to a specific length.
9537
9538On some platforms, path may also be specified as an open file descriptor.
9539 If this functionality is unavailable, using it raises an exception.
9540[clinic start generated code]*/
9541
Larry Hastings2f936352014-08-05 14:04:04 +10009542static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009543os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9544/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009545{
9546 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009547#ifdef MS_WINDOWS
9548 int fd;
9549#endif
9550
9551 if (path->fd != -1)
9552 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009553
9554 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009555 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009556#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009557 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009558 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009559 result = -1;
9560 else {
9561 result = _chsize_s(fd, length);
9562 close(fd);
9563 if (result < 0)
9564 errno = result;
9565 }
9566#else
9567 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009568#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009569 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009570 Py_END_ALLOW_THREADS
9571 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009572 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009573
9574 Py_RETURN_NONE;
9575}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009576#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009577
Ross Lagerwall7807c352011-03-17 20:20:30 +02009578
Victor Stinnerd6b17692014-09-30 12:20:05 +02009579/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9580 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9581 defined, which is the case in Python on AIX. AIX bug report:
9582 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9583#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9584# define POSIX_FADVISE_AIX_BUG
9585#endif
9586
Victor Stinnerec39e262014-09-30 12:35:58 +02009587
Victor Stinnerd6b17692014-09-30 12:20:05 +02009588#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009589/*[clinic input]
9590os.posix_fallocate
9591
9592 fd: int
9593 offset: Py_off_t
9594 length: Py_off_t
9595 /
9596
9597Ensure a file has allocated at least a particular number of bytes on disk.
9598
9599Ensure that the file specified by fd encompasses a range of bytes
9600starting at offset bytes from the beginning and continuing for length bytes.
9601[clinic start generated code]*/
9602
Larry Hastings2f936352014-08-05 14:04:04 +10009603static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009604os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009605 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009606/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009607{
9608 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009609 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009610
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009611 do {
9612 Py_BEGIN_ALLOW_THREADS
9613 result = posix_fallocate(fd, offset, length);
9614 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009615 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9616
9617 if (result == 0)
9618 Py_RETURN_NONE;
9619
9620 if (async_err)
9621 return NULL;
9622
9623 errno = result;
9624 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009625}
Victor Stinnerec39e262014-09-30 12:35:58 +02009626#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009627
Ross Lagerwall7807c352011-03-17 20:20:30 +02009628
Victor Stinnerd6b17692014-09-30 12:20:05 +02009629#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009630/*[clinic input]
9631os.posix_fadvise
9632
9633 fd: int
9634 offset: Py_off_t
9635 length: Py_off_t
9636 advice: int
9637 /
9638
9639Announce an intention to access data in a specific pattern.
9640
9641Announce an intention to access data in a specific pattern, thus allowing
9642the kernel to make optimizations.
9643The advice applies to the region of the file specified by fd starting at
9644offset and continuing for length bytes.
9645advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9646POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9647POSIX_FADV_DONTNEED.
9648[clinic start generated code]*/
9649
Larry Hastings2f936352014-08-05 14:04:04 +10009650static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009651os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009652 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009653/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009654{
9655 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009656 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009657
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009658 do {
9659 Py_BEGIN_ALLOW_THREADS
9660 result = posix_fadvise(fd, offset, length, advice);
9661 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009662 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9663
9664 if (result == 0)
9665 Py_RETURN_NONE;
9666
9667 if (async_err)
9668 return NULL;
9669
9670 errno = result;
9671 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +02009672}
Victor Stinnerec39e262014-09-30 12:35:58 +02009673#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009674
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009675#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009676
Fred Drake762e2061999-08-26 17:23:54 +00009677/* Save putenv() parameters as values here, so we can collect them when they
9678 * get re-set with another call for the same key. */
9679static PyObject *posix_putenv_garbage;
9680
Larry Hastings2f936352014-08-05 14:04:04 +10009681static void
9682posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009683{
Larry Hastings2f936352014-08-05 14:04:04 +10009684 /* Install the first arg and newstr in posix_putenv_garbage;
9685 * this will cause previous value to be collected. This has to
9686 * happen after the real putenv() call because the old value
9687 * was still accessible until then. */
9688 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9689 /* really not much we can do; just leak */
9690 PyErr_Clear();
9691 else
9692 Py_DECREF(value);
9693}
9694
9695
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009696#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009697/*[clinic input]
9698os.putenv
9699
9700 name: unicode
9701 value: unicode
9702 /
9703
9704Change or add an environment variable.
9705[clinic start generated code]*/
9706
Larry Hastings2f936352014-08-05 14:04:04 +10009707static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009708os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9709/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009710{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009711 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009712 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +10009713
Serhiy Storchaka77703942017-06-25 07:33:01 +03009714 /* Search from index 1 because on Windows starting '=' is allowed for
9715 defining hidden environment variables. */
9716 if (PyUnicode_GET_LENGTH(name) == 0 ||
9717 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
9718 {
9719 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9720 return NULL;
9721 }
Larry Hastings2f936352014-08-05 14:04:04 +10009722 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9723 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009724 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009725 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009726
9727 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
9728 if (env == NULL)
9729 goto error;
9730 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +01009731 PyErr_Format(PyExc_ValueError,
9732 "the environment variable is longer than %u characters",
9733 _MAX_ENV);
9734 goto error;
9735 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009736 if (wcslen(env) != (size_t)size) {
9737 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +02009738 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +03009739 }
9740
Larry Hastings2f936352014-08-05 14:04:04 +10009741 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009742 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009743 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009744 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009745
Larry Hastings2f936352014-08-05 14:04:04 +10009746 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009747 Py_RETURN_NONE;
9748
9749error:
Larry Hastings2f936352014-08-05 14:04:04 +10009750 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009751 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009752}
Larry Hastings2f936352014-08-05 14:04:04 +10009753#else /* MS_WINDOWS */
9754/*[clinic input]
9755os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009756
Larry Hastings2f936352014-08-05 14:04:04 +10009757 name: FSConverter
9758 value: FSConverter
9759 /
9760
9761Change or add an environment variable.
9762[clinic start generated code]*/
9763
Larry Hastings2f936352014-08-05 14:04:04 +10009764static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009765os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9766/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009767{
9768 PyObject *bytes = NULL;
9769 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +03009770 const char *name_string = PyBytes_AS_STRING(name);
9771 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009772
Serhiy Storchaka77703942017-06-25 07:33:01 +03009773 if (strchr(name_string, '=') != NULL) {
9774 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
9775 return NULL;
9776 }
Larry Hastings2f936352014-08-05 14:04:04 +10009777 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9778 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009779 return NULL;
9780 }
9781
9782 env = PyBytes_AS_STRING(bytes);
9783 if (putenv(env)) {
9784 Py_DECREF(bytes);
9785 return posix_error();
9786 }
9787
9788 posix_putenv_garbage_setitem(name, bytes);
9789 Py_RETURN_NONE;
9790}
9791#endif /* MS_WINDOWS */
9792#endif /* HAVE_PUTENV */
9793
9794
9795#ifdef HAVE_UNSETENV
9796/*[clinic input]
9797os.unsetenv
9798 name: FSConverter
9799 /
9800
9801Delete an environment variable.
9802[clinic start generated code]*/
9803
Larry Hastings2f936352014-08-05 14:04:04 +10009804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009805os_unsetenv_impl(PyObject *module, PyObject *name)
9806/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009807{
Victor Stinner984890f2011-11-24 13:53:38 +01009808#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009809 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009810#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009811
Victor Stinner984890f2011-11-24 13:53:38 +01009812#ifdef HAVE_BROKEN_UNSETENV
9813 unsetenv(PyBytes_AS_STRING(name));
9814#else
Victor Stinner65170952011-11-22 22:16:17 +01009815 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009816 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009817 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009818#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009819
Victor Stinner8c62be82010-05-06 00:08:46 +00009820 /* Remove the key from posix_putenv_garbage;
9821 * this will cause it to be collected. This has to
9822 * happen after the real unsetenv() call because the
9823 * old value was still accessible until then.
9824 */
Victor Stinner65170952011-11-22 22:16:17 +01009825 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009826 /* really not much we can do; just leak */
9827 PyErr_Clear();
9828 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009829 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009830}
Larry Hastings2f936352014-08-05 14:04:04 +10009831#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009832
Larry Hastings2f936352014-08-05 14:04:04 +10009833
9834/*[clinic input]
9835os.strerror
9836
9837 code: int
9838 /
9839
9840Translate an error code to a message string.
9841[clinic start generated code]*/
9842
Larry Hastings2f936352014-08-05 14:04:04 +10009843static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009844os_strerror_impl(PyObject *module, int code)
9845/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009846{
9847 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009848 if (message == NULL) {
9849 PyErr_SetString(PyExc_ValueError,
9850 "strerror() argument out of range");
9851 return NULL;
9852 }
Victor Stinner1b579672011-12-17 05:47:23 +01009853 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009854}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009855
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009856
Guido van Rossumc9641791998-08-04 15:26:23 +00009857#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009858#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009859/*[clinic input]
9860os.WCOREDUMP -> bool
9861
9862 status: int
9863 /
9864
9865Return True if the process returning status was dumped to a core file.
9866[clinic start generated code]*/
9867
Larry Hastings2f936352014-08-05 14:04:04 +10009868static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009869os_WCOREDUMP_impl(PyObject *module, int status)
9870/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009871{
9872 WAIT_TYPE wait_status;
9873 WAIT_STATUS_INT(wait_status) = status;
9874 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009875}
9876#endif /* WCOREDUMP */
9877
Larry Hastings2f936352014-08-05 14:04:04 +10009878
Fred Drake106c1a02002-04-23 15:58:02 +00009879#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009880/*[clinic input]
9881os.WIFCONTINUED -> bool
9882
9883 status: int
9884
9885Return True if a particular process was continued from a job control stop.
9886
9887Return True if the process returning status was continued from a
9888job control stop.
9889[clinic start generated code]*/
9890
Larry Hastings2f936352014-08-05 14:04:04 +10009891static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009892os_WIFCONTINUED_impl(PyObject *module, int status)
9893/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009894{
9895 WAIT_TYPE wait_status;
9896 WAIT_STATUS_INT(wait_status) = status;
9897 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009898}
9899#endif /* WIFCONTINUED */
9900
Larry Hastings2f936352014-08-05 14:04:04 +10009901
Guido van Rossumc9641791998-08-04 15:26:23 +00009902#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009903/*[clinic input]
9904os.WIFSTOPPED -> bool
9905
9906 status: int
9907
9908Return True if the process returning status was stopped.
9909[clinic start generated code]*/
9910
Larry Hastings2f936352014-08-05 14:04:04 +10009911static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009912os_WIFSTOPPED_impl(PyObject *module, int status)
9913/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009914{
9915 WAIT_TYPE wait_status;
9916 WAIT_STATUS_INT(wait_status) = status;
9917 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009918}
9919#endif /* WIFSTOPPED */
9920
Larry Hastings2f936352014-08-05 14:04:04 +10009921
Guido van Rossumc9641791998-08-04 15:26:23 +00009922#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009923/*[clinic input]
9924os.WIFSIGNALED -> bool
9925
9926 status: int
9927
9928Return True if the process returning status was terminated by a signal.
9929[clinic start generated code]*/
9930
Larry Hastings2f936352014-08-05 14:04:04 +10009931static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009932os_WIFSIGNALED_impl(PyObject *module, int status)
9933/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009934{
9935 WAIT_TYPE wait_status;
9936 WAIT_STATUS_INT(wait_status) = status;
9937 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009938}
9939#endif /* WIFSIGNALED */
9940
Larry Hastings2f936352014-08-05 14:04:04 +10009941
Guido van Rossumc9641791998-08-04 15:26:23 +00009942#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009943/*[clinic input]
9944os.WIFEXITED -> bool
9945
9946 status: int
9947
9948Return True if the process returning status exited via the exit() system call.
9949[clinic start generated code]*/
9950
Larry Hastings2f936352014-08-05 14:04:04 +10009951static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009952os_WIFEXITED_impl(PyObject *module, int status)
9953/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009954{
9955 WAIT_TYPE wait_status;
9956 WAIT_STATUS_INT(wait_status) = status;
9957 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009958}
9959#endif /* WIFEXITED */
9960
Larry Hastings2f936352014-08-05 14:04:04 +10009961
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009962#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009963/*[clinic input]
9964os.WEXITSTATUS -> int
9965
9966 status: int
9967
9968Return the process return code from status.
9969[clinic start generated code]*/
9970
Larry Hastings2f936352014-08-05 14:04:04 +10009971static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009972os_WEXITSTATUS_impl(PyObject *module, int status)
9973/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009974{
9975 WAIT_TYPE wait_status;
9976 WAIT_STATUS_INT(wait_status) = status;
9977 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009978}
9979#endif /* WEXITSTATUS */
9980
Larry Hastings2f936352014-08-05 14:04:04 +10009981
Guido van Rossumc9641791998-08-04 15:26:23 +00009982#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009983/*[clinic input]
9984os.WTERMSIG -> int
9985
9986 status: int
9987
9988Return the signal that terminated the process that provided the status value.
9989[clinic start generated code]*/
9990
Larry Hastings2f936352014-08-05 14:04:04 +10009991static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009992os_WTERMSIG_impl(PyObject *module, int status)
9993/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009994{
9995 WAIT_TYPE wait_status;
9996 WAIT_STATUS_INT(wait_status) = status;
9997 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009998}
9999#endif /* WTERMSIG */
10000
Larry Hastings2f936352014-08-05 14:04:04 +100010001
Guido van Rossumc9641791998-08-04 15:26:23 +000010002#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010003/*[clinic input]
10004os.WSTOPSIG -> int
10005
10006 status: int
10007
10008Return the signal that stopped the process that provided the status value.
10009[clinic start generated code]*/
10010
Larry Hastings2f936352014-08-05 14:04:04 +100010011static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010012os_WSTOPSIG_impl(PyObject *module, int status)
10013/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010014{
10015 WAIT_TYPE wait_status;
10016 WAIT_STATUS_INT(wait_status) = status;
10017 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010018}
10019#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010020#endif /* HAVE_SYS_WAIT_H */
10021
10022
Thomas Wouters477c8d52006-05-27 19:21:47 +000010023#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010024#ifdef _SCO_DS
10025/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10026 needed definitions in sys/statvfs.h */
10027#define _SVID3
10028#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010029#include <sys/statvfs.h>
10030
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010031static PyObject*
10032_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondo474eedf2018-11-13 04:09:31 -080010033 PyObject *v = PyStructSequence_New(StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010034 if (v == NULL)
10035 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010036
10037#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010038 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10039 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10040 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10041 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10042 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10043 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10044 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10045 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10046 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10047 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010048#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010049 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10050 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10051 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010052 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010053 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010054 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010055 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010056 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010057 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010058 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010059 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010060 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010061 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010062 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010063 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#endif
Michael Felt502d5512018-01-05 13:01:58 +010010066/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10067 * (issue #32390). */
10068#if defined(_AIX) && defined(_ALL_SOURCE)
10069 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10070#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010071 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010072#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010073 if (PyErr_Occurred()) {
10074 Py_DECREF(v);
10075 return NULL;
10076 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010077
Victor Stinner8c62be82010-05-06 00:08:46 +000010078 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010079}
10080
Larry Hastings2f936352014-08-05 14:04:04 +100010081
10082/*[clinic input]
10083os.fstatvfs
10084 fd: int
10085 /
10086
10087Perform an fstatvfs system call on the given fd.
10088
10089Equivalent to statvfs(fd).
10090[clinic start generated code]*/
10091
Larry Hastings2f936352014-08-05 14:04:04 +100010092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010093os_fstatvfs_impl(PyObject *module, int fd)
10094/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010095{
10096 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010097 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010098 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010099
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010100 do {
10101 Py_BEGIN_ALLOW_THREADS
10102 result = fstatvfs(fd, &st);
10103 Py_END_ALLOW_THREADS
10104 } while (result != 0 && errno == EINTR &&
10105 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010106 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010107 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010108
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010110}
Larry Hastings2f936352014-08-05 14:04:04 +100010111#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010112
10113
Thomas Wouters477c8d52006-05-27 19:21:47 +000010114#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010115#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010116/*[clinic input]
10117os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010118
Larry Hastings2f936352014-08-05 14:04:04 +100010119 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10120
10121Perform a statvfs system call on the given path.
10122
10123path may always be specified as a string.
10124On some platforms, path may also be specified as an open file descriptor.
10125 If this functionality is unavailable, using it raises an exception.
10126[clinic start generated code]*/
10127
Larry Hastings2f936352014-08-05 14:04:04 +100010128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010129os_statvfs_impl(PyObject *module, path_t *path)
10130/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010131{
10132 int result;
10133 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010134
10135 Py_BEGIN_ALLOW_THREADS
10136#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010137 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010138#ifdef __APPLE__
10139 /* handle weak-linking on Mac OS X 10.3 */
10140 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010141 fd_specified("statvfs", path->fd);
10142 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010143 }
10144#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010145 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010146 }
10147 else
10148#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010149 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010150 Py_END_ALLOW_THREADS
10151
10152 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010153 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010154 }
10155
Larry Hastings2f936352014-08-05 14:04:04 +100010156 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010157}
Larry Hastings2f936352014-08-05 14:04:04 +100010158#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10159
Guido van Rossum94f6f721999-01-06 18:42:14 +000010160
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010161#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010162/*[clinic input]
10163os._getdiskusage
10164
Steve Dower23ad6d02018-02-22 10:39:10 -080010165 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010166
10167Return disk usage statistics about the given path as a (total, free) tuple.
10168[clinic start generated code]*/
10169
Larry Hastings2f936352014-08-05 14:04:04 +100010170static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010171os__getdiskusage_impl(PyObject *module, path_t *path)
10172/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010173{
10174 BOOL retval;
10175 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010176 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010177
10178 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010179 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010180 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010181 if (retval == 0) {
10182 if (GetLastError() == ERROR_DIRECTORY) {
10183 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010184
Joe Pamerc8c02492018-09-25 10:57:36 -040010185 dir_path = PyMem_New(wchar_t, path->length + 1);
10186 if (dir_path == NULL) {
10187 return PyErr_NoMemory();
10188 }
10189
10190 wcscpy_s(dir_path, path->length + 1, path->wide);
10191
10192 if (_dirnameW(dir_path) != -1) {
10193 Py_BEGIN_ALLOW_THREADS
10194 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10195 Py_END_ALLOW_THREADS
10196 }
10197 /* Record the last error in case it's modified by PyMem_Free. */
10198 err = GetLastError();
10199 PyMem_Free(dir_path);
10200 if (retval) {
10201 goto success;
10202 }
10203 }
10204 return PyErr_SetFromWindowsErr(err);
10205 }
10206
10207success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010208 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10209}
Larry Hastings2f936352014-08-05 14:04:04 +100010210#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010211
10212
Fred Drakec9680921999-12-13 16:37:25 +000010213/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10214 * It maps strings representing configuration variable names to
10215 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010216 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010217 * rarely-used constants. There are three separate tables that use
10218 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010219 *
10220 * This code is always included, even if none of the interfaces that
10221 * need it are included. The #if hackery needed to avoid it would be
10222 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010223 */
10224struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010225 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010226 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010227};
10228
Fred Drake12c6e2d1999-12-14 21:25:03 +000010229static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010230conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010231 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010232{
Christian Heimes217cfd12007-12-02 14:31:20 +000010233 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010234 int value = _PyLong_AsInt(arg);
10235 if (value == -1 && PyErr_Occurred())
10236 return 0;
10237 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010238 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010239 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010240 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010241 /* look up the value in the table using a binary search */
10242 size_t lo = 0;
10243 size_t mid;
10244 size_t hi = tablesize;
10245 int cmp;
10246 const char *confname;
10247 if (!PyUnicode_Check(arg)) {
10248 PyErr_SetString(PyExc_TypeError,
10249 "configuration names must be strings or integers");
10250 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010251 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010252 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010253 if (confname == NULL)
10254 return 0;
10255 while (lo < hi) {
10256 mid = (lo + hi) / 2;
10257 cmp = strcmp(confname, table[mid].name);
10258 if (cmp < 0)
10259 hi = mid;
10260 else if (cmp > 0)
10261 lo = mid + 1;
10262 else {
10263 *valuep = table[mid].value;
10264 return 1;
10265 }
10266 }
10267 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10268 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010269 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010270}
10271
10272
10273#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10274static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010275#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010276 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010277#endif
10278#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010279 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010280#endif
Fred Drakec9680921999-12-13 16:37:25 +000010281#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010282 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010283#endif
10284#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010285 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010286#endif
10287#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010288 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010289#endif
10290#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010291 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010292#endif
10293#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010294 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010295#endif
10296#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010297 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010298#endif
10299#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010300 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010301#endif
10302#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010303 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010304#endif
10305#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010306 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010307#endif
10308#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010309 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010310#endif
10311#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010312 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010313#endif
10314#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010315 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010316#endif
10317#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010318 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010319#endif
10320#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010321 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010322#endif
10323#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010324 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010325#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010326#ifdef _PC_ACL_ENABLED
10327 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10328#endif
10329#ifdef _PC_MIN_HOLE_SIZE
10330 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10331#endif
10332#ifdef _PC_ALLOC_SIZE_MIN
10333 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10334#endif
10335#ifdef _PC_REC_INCR_XFER_SIZE
10336 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10337#endif
10338#ifdef _PC_REC_MAX_XFER_SIZE
10339 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10340#endif
10341#ifdef _PC_REC_MIN_XFER_SIZE
10342 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10343#endif
10344#ifdef _PC_REC_XFER_ALIGN
10345 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10346#endif
10347#ifdef _PC_SYMLINK_MAX
10348 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10349#endif
10350#ifdef _PC_XATTR_ENABLED
10351 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10352#endif
10353#ifdef _PC_XATTR_EXISTS
10354 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10355#endif
10356#ifdef _PC_TIMESTAMP_RESOLUTION
10357 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10358#endif
Fred Drakec9680921999-12-13 16:37:25 +000010359};
10360
Fred Drakec9680921999-12-13 16:37:25 +000010361static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010362conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010363{
10364 return conv_confname(arg, valuep, posix_constants_pathconf,
10365 sizeof(posix_constants_pathconf)
10366 / sizeof(struct constdef));
10367}
10368#endif
10369
Larry Hastings2f936352014-08-05 14:04:04 +100010370
Fred Drakec9680921999-12-13 16:37:25 +000010371#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010372/*[clinic input]
10373os.fpathconf -> long
10374
10375 fd: int
10376 name: path_confname
10377 /
10378
10379Return the configuration limit name for the file descriptor fd.
10380
10381If there is no limit, return -1.
10382[clinic start generated code]*/
10383
Larry Hastings2f936352014-08-05 14:04:04 +100010384static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010385os_fpathconf_impl(PyObject *module, int fd, int name)
10386/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010387{
10388 long limit;
10389
10390 errno = 0;
10391 limit = fpathconf(fd, name);
10392 if (limit == -1 && errno != 0)
10393 posix_error();
10394
10395 return limit;
10396}
10397#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010398
10399
10400#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010401/*[clinic input]
10402os.pathconf -> long
10403 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10404 name: path_confname
10405
10406Return the configuration limit name for the file or directory path.
10407
10408If there is no limit, return -1.
10409On some platforms, path may also be specified as an open file descriptor.
10410 If this functionality is unavailable, using it raises an exception.
10411[clinic start generated code]*/
10412
Larry Hastings2f936352014-08-05 14:04:04 +100010413static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010414os_pathconf_impl(PyObject *module, path_t *path, int name)
10415/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010416{
Victor Stinner8c62be82010-05-06 00:08:46 +000010417 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010418
Victor Stinner8c62be82010-05-06 00:08:46 +000010419 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010420#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010421 if (path->fd != -1)
10422 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010423 else
10424#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010425 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010426 if (limit == -1 && errno != 0) {
10427 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010428 /* could be a path or name problem */
10429 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010430 else
Larry Hastings2f936352014-08-05 14:04:04 +100010431 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010432 }
Larry Hastings2f936352014-08-05 14:04:04 +100010433
10434 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010435}
Larry Hastings2f936352014-08-05 14:04:04 +100010436#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010437
10438#ifdef HAVE_CONFSTR
10439static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010440#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010441 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010442#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010443#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010444 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010445#endif
10446#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010447 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010448#endif
Fred Draked86ed291999-12-15 15:34:33 +000010449#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010450 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010451#endif
10452#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010453 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010454#endif
10455#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010456 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010457#endif
10458#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010459 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010460#endif
Fred Drakec9680921999-12-13 16:37:25 +000010461#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010462 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010463#endif
10464#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010465 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010466#endif
10467#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010468 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010469#endif
10470#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010471 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010472#endif
10473#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010474 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010475#endif
10476#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010477 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010478#endif
10479#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010480 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010481#endif
10482#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010483 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010484#endif
Fred Draked86ed291999-12-15 15:34:33 +000010485#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010486 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010487#endif
Fred Drakec9680921999-12-13 16:37:25 +000010488#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010489 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010490#endif
Fred Draked86ed291999-12-15 15:34:33 +000010491#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010492 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010493#endif
10494#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010495 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010496#endif
10497#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010498 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010499#endif
10500#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010501 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010502#endif
Fred Drakec9680921999-12-13 16:37:25 +000010503#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010504 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010505#endif
10506#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010507 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010508#endif
10509#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010510 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010511#endif
10512#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010513 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010514#endif
10515#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010517#endif
10518#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010520#endif
10521#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010523#endif
10524#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010526#endif
10527#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010529#endif
10530#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010531 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010532#endif
10533#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010534 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010535#endif
10536#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010537 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010538#endif
10539#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010540 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010541#endif
10542#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010543 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010544#endif
10545#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010546 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010547#endif
10548#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010549 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010550#endif
Fred Draked86ed291999-12-15 15:34:33 +000010551#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010552 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010553#endif
10554#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010555 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010556#endif
10557#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010558 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010559#endif
10560#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010561 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010562#endif
10563#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010564 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010565#endif
10566#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010567 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010568#endif
10569#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010570 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010571#endif
10572#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010574#endif
10575#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010576 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010577#endif
10578#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010579 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010580#endif
10581#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010583#endif
10584#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010585 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010586#endif
10587#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000010588 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000010589#endif
Fred Drakec9680921999-12-13 16:37:25 +000010590};
10591
10592static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010593conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010594{
10595 return conv_confname(arg, valuep, posix_constants_confstr,
10596 sizeof(posix_constants_confstr)
10597 / sizeof(struct constdef));
10598}
10599
Larry Hastings2f936352014-08-05 14:04:04 +100010600
10601/*[clinic input]
10602os.confstr
10603
10604 name: confstr_confname
10605 /
10606
10607Return a string-valued system configuration variable.
10608[clinic start generated code]*/
10609
Larry Hastings2f936352014-08-05 14:04:04 +100010610static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010611os_confstr_impl(PyObject *module, int name)
10612/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000010613{
10614 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000010615 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010616 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000010617
Victor Stinnercb043522010-09-10 23:49:04 +000010618 errno = 0;
10619 len = confstr(name, buffer, sizeof(buffer));
10620 if (len == 0) {
10621 if (errno) {
10622 posix_error();
10623 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000010624 }
10625 else {
Victor Stinnercb043522010-09-10 23:49:04 +000010626 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000010627 }
10628 }
Victor Stinnercb043522010-09-10 23:49:04 +000010629
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020010630 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010010631 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000010632 char *buf = PyMem_Malloc(len);
10633 if (buf == NULL)
10634 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010010635 len2 = confstr(name, buf, len);
10636 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020010637 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000010638 PyMem_Free(buf);
10639 }
10640 else
10641 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000010642 return result;
10643}
Larry Hastings2f936352014-08-05 14:04:04 +100010644#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000010645
10646
10647#ifdef HAVE_SYSCONF
10648static struct constdef posix_constants_sysconf[] = {
10649#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000010651#endif
10652#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000010654#endif
10655#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010656 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010657#endif
10658#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010660#endif
10661#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010662 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010663#endif
10664#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010665 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010666#endif
10667#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010669#endif
10670#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010672#endif
10673#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010674 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010675#endif
10676#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010677 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010678#endif
Fred Draked86ed291999-12-15 15:34:33 +000010679#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010680 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010681#endif
10682#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010683 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010684#endif
Fred Drakec9680921999-12-13 16:37:25 +000010685#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010687#endif
Fred Drakec9680921999-12-13 16:37:25 +000010688#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010689 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010690#endif
10691#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010692 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010693#endif
10694#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010696#endif
10697#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010698 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010699#endif
10700#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010701 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010702#endif
Fred Draked86ed291999-12-15 15:34:33 +000010703#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010704 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010705#endif
Fred Drakec9680921999-12-13 16:37:25 +000010706#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010707 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010708#endif
10709#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010711#endif
10712#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010714#endif
10715#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010717#endif
10718#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010720#endif
Fred Draked86ed291999-12-15 15:34:33 +000010721#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010723#endif
Fred Drakec9680921999-12-13 16:37:25 +000010724#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010726#endif
10727#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010729#endif
10730#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010732#endif
10733#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010734 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010735#endif
10736#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010737 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010738#endif
10739#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010740 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010741#endif
10742#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010744#endif
10745#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010747#endif
10748#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010750#endif
10751#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010752 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010753#endif
10754#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010755 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010756#endif
10757#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010759#endif
10760#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010761 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010762#endif
10763#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010765#endif
10766#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010767 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010768#endif
10769#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010770 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010771#endif
10772#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010773 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010774#endif
10775#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010776 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010777#endif
10778#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010779 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010780#endif
10781#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010782 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010783#endif
10784#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010785 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010786#endif
10787#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010788 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010789#endif
10790#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010791 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010792#endif
Fred Draked86ed291999-12-15 15:34:33 +000010793#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010794 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010795#endif
Fred Drakec9680921999-12-13 16:37:25 +000010796#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010797 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010798#endif
10799#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010800 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010801#endif
10802#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010804#endif
Fred Draked86ed291999-12-15 15:34:33 +000010805#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010806 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010807#endif
Fred Drakec9680921999-12-13 16:37:25 +000010808#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010809 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010810#endif
Fred Draked86ed291999-12-15 15:34:33 +000010811#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010812 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010813#endif
10814#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010816#endif
Fred Drakec9680921999-12-13 16:37:25 +000010817#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010819#endif
10820#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010821 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010822#endif
10823#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010824 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010825#endif
10826#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010827 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010828#endif
Fred Draked86ed291999-12-15 15:34:33 +000010829#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010830 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010831#endif
Fred Drakec9680921999-12-13 16:37:25 +000010832#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010833 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010834#endif
10835#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010837#endif
10838#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010839 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010840#endif
10841#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010842 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010843#endif
10844#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010846#endif
10847#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010849#endif
10850#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010852#endif
Fred Draked86ed291999-12-15 15:34:33 +000010853#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010855#endif
Fred Drakec9680921999-12-13 16:37:25 +000010856#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010858#endif
10859#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010861#endif
Fred Draked86ed291999-12-15 15:34:33 +000010862#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010864#endif
Fred Drakec9680921999-12-13 16:37:25 +000010865#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010867#endif
10868#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010869 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010870#endif
10871#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010873#endif
10874#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010875 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010876#endif
10877#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010878 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010879#endif
10880#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010882#endif
10883#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010885#endif
10886#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010888#endif
10889#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010891#endif
Fred Draked86ed291999-12-15 15:34:33 +000010892#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010894#endif
10895#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010897#endif
Fred Drakec9680921999-12-13 16:37:25 +000010898#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010900#endif
10901#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010903#endif
10904#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010905 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010906#endif
10907#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010908 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010909#endif
10910#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010911 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010912#endif
10913#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010914 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010915#endif
10916#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010917 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010918#endif
10919#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010920 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010921#endif
10922#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010923 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010924#endif
10925#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010926 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010927#endif
10928#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010929 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010930#endif
10931#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010932 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010933#endif
10934#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010935 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010936#endif
10937#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010938 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010939#endif
10940#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010941 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010942#endif
10943#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010944 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010945#endif
10946#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010947 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010948#endif
10949#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010950 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010951#endif
10952#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010953 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010954#endif
10955#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010956 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010957#endif
10958#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010959 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010960#endif
10961#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010962 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010963#endif
10964#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010965 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010966#endif
10967#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010968 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010969#endif
10970#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010971 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010972#endif
10973#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010975#endif
10976#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010977 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010978#endif
10979#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010980 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010981#endif
10982#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010983 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010984#endif
10985#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010986 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010987#endif
10988#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010990#endif
10991#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010992 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010993#endif
10994#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010995 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010996#endif
10997#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010999#endif
11000#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011002#endif
Fred Draked86ed291999-12-15 15:34:33 +000011003#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011004 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011005#endif
Fred Drakec9680921999-12-13 16:37:25 +000011006#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011007 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011008#endif
11009#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011010 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011011#endif
11012#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011013 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011014#endif
11015#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011017#endif
11018#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011020#endif
11021#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011023#endif
11024#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011025 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011026#endif
11027#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011028 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011029#endif
11030#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011032#endif
11033#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011034 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011035#endif
11036#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011037 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011038#endif
11039#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011041#endif
11042#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011044#endif
11045#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011047#endif
11048#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011050#endif
11051#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011052 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011053#endif
11054#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011055 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011056#endif
11057#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011058 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011059#endif
11060#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011061 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011062#endif
11063#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011064 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011065#endif
11066#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011068#endif
11069#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011071#endif
11072#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011074#endif
11075#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011077#endif
11078#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011080#endif
11081#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011083#endif
11084#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011086#endif
11087#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011089#endif
11090#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011092#endif
11093#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011095#endif
11096#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011098#endif
11099#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011101#endif
11102#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011104#endif
11105#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011107#endif
11108#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011110#endif
11111#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011113#endif
11114#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011116#endif
11117#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011119#endif
11120#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011122#endif
11123#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011125#endif
11126#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011128#endif
11129#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011131#endif
11132#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011133 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011134#endif
11135#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011137#endif
11138#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011140#endif
11141};
11142
11143static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011144conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011145{
11146 return conv_confname(arg, valuep, posix_constants_sysconf,
11147 sizeof(posix_constants_sysconf)
11148 / sizeof(struct constdef));
11149}
11150
Larry Hastings2f936352014-08-05 14:04:04 +100011151
11152/*[clinic input]
11153os.sysconf -> long
11154 name: sysconf_confname
11155 /
11156
11157Return an integer-valued system configuration variable.
11158[clinic start generated code]*/
11159
Larry Hastings2f936352014-08-05 14:04:04 +100011160static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011161os_sysconf_impl(PyObject *module, int name)
11162/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011163{
11164 long value;
11165
11166 errno = 0;
11167 value = sysconf(name);
11168 if (value == -1 && errno != 0)
11169 posix_error();
11170 return value;
11171}
11172#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011173
11174
Fred Drakebec628d1999-12-15 18:31:10 +000011175/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011176 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011177 * the exported dictionaries that are used to publish information about the
11178 * names available on the host platform.
11179 *
11180 * Sorting the table at runtime ensures that the table is properly ordered
11181 * when used, even for platforms we're not able to test on. It also makes
11182 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011183 */
Fred Drakebec628d1999-12-15 18:31:10 +000011184
11185static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011186cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011187{
11188 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011189 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011190 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011191 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011192
11193 return strcmp(c1->name, c2->name);
11194}
11195
11196static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011197setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011198 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011199{
Fred Drakebec628d1999-12-15 18:31:10 +000011200 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011201 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011202
11203 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11204 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011205 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011206 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011207
Barry Warsaw3155db32000-04-13 15:20:40 +000011208 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011209 PyObject *o = PyLong_FromLong(table[i].value);
11210 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11211 Py_XDECREF(o);
11212 Py_DECREF(d);
11213 return -1;
11214 }
11215 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011216 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011217 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011218}
11219
Fred Drakebec628d1999-12-15 18:31:10 +000011220/* Return -1 on failure, 0 on success. */
11221static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011222setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011223{
11224#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011225 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011226 sizeof(posix_constants_pathconf)
11227 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011228 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011229 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011230#endif
11231#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011232 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011233 sizeof(posix_constants_confstr)
11234 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011235 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011236 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011237#endif
11238#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011239 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011240 sizeof(posix_constants_sysconf)
11241 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011242 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011243 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011244#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011245 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011246}
Fred Draked86ed291999-12-15 15:34:33 +000011247
11248
Larry Hastings2f936352014-08-05 14:04:04 +100011249/*[clinic input]
11250os.abort
11251
11252Abort the interpreter immediately.
11253
11254This function 'dumps core' or otherwise fails in the hardest way possible
11255on the hosting operating system. This function never returns.
11256[clinic start generated code]*/
11257
Larry Hastings2f936352014-08-05 14:04:04 +100011258static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011259os_abort_impl(PyObject *module)
11260/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011261{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011262 abort();
11263 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011264#ifndef __clang__
11265 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11266 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11267 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011268 Py_FatalError("abort() called from Python code didn't abort!");
11269 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011270#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011271}
Fred Drakebec628d1999-12-15 18:31:10 +000011272
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011273#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011274/* Grab ShellExecute dynamically from shell32 */
11275static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011276static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11277 LPCWSTR, INT);
11278static int
11279check_ShellExecute()
11280{
11281 HINSTANCE hShell32;
11282
11283 /* only recheck */
11284 if (-1 == has_ShellExecute) {
11285 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011286 /* Security note: this call is not vulnerable to "DLL hijacking".
11287 SHELL32 is part of "KnownDLLs" and so Windows always load
11288 the system SHELL32.DLL, even if there is another SHELL32.DLL
11289 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011290 hShell32 = LoadLibraryW(L"SHELL32");
11291 Py_END_ALLOW_THREADS
11292 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011293 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11294 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011295 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011296 } else {
11297 has_ShellExecute = 0;
11298 }
11299 }
11300 return has_ShellExecute;
11301}
11302
11303
Steve Dowercc16be82016-09-08 10:35:16 -070011304/*[clinic input]
11305os.startfile
11306 filepath: path_t
11307 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011308
Steve Dowercc16be82016-09-08 10:35:16 -070011309startfile(filepath [, operation])
11310
11311Start a file with its associated application.
11312
11313When "operation" is not specified or "open", this acts like
11314double-clicking the file in Explorer, or giving the file name as an
11315argument to the DOS "start" command: the file is opened with whatever
11316application (if any) its extension is associated.
11317When another "operation" is given, it specifies what should be done with
11318the file. A typical operation is "print".
11319
11320startfile returns as soon as the associated application is launched.
11321There is no option to wait for the application to close, and no way
11322to retrieve the application's exit status.
11323
11324The filepath is relative to the current directory. If you want to use
11325an absolute path, make sure the first character is not a slash ("/");
11326the underlying Win32 ShellExecute function doesn't work if it is.
11327[clinic start generated code]*/
11328
11329static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011330os_startfile_impl(PyObject *module, path_t *filepath,
11331 const Py_UNICODE *operation)
11332/*[clinic end generated code: output=66dc311c94d50797 input=63950bf2986380d0]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011333{
11334 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011335
11336 if(!check_ShellExecute()) {
11337 /* If the OS doesn't have ShellExecute, return a
11338 NotImplementedError. */
11339 return PyErr_Format(PyExc_NotImplementedError,
11340 "startfile not available on this platform");
11341 }
11342
Victor Stinner8c62be82010-05-06 00:08:46 +000011343 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011344 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011345 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011346 Py_END_ALLOW_THREADS
11347
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011349 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011350 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 }
Steve Dowercc16be82016-09-08 10:35:16 -070011352 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011353}
Larry Hastings2f936352014-08-05 14:04:04 +100011354#endif /* MS_WINDOWS */
11355
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011356
Martin v. Löwis438b5342002-12-27 10:16:42 +000011357#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011358/*[clinic input]
11359os.getloadavg
11360
11361Return average recent system load information.
11362
11363Return the number of processes in the system run queue averaged over
11364the last 1, 5, and 15 minutes as a tuple of three floats.
11365Raises OSError if the load average was unobtainable.
11366[clinic start generated code]*/
11367
Larry Hastings2f936352014-08-05 14:04:04 +100011368static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011369os_getloadavg_impl(PyObject *module)
11370/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011371{
11372 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011373 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011374 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11375 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011376 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011377 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011378}
Larry Hastings2f936352014-08-05 14:04:04 +100011379#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011380
Larry Hastings2f936352014-08-05 14:04:04 +100011381
11382/*[clinic input]
11383os.device_encoding
11384 fd: int
11385
11386Return a string describing the encoding of a terminal's file descriptor.
11387
11388The file descriptor must be attached to a terminal.
11389If the device is not a terminal, return None.
11390[clinic start generated code]*/
11391
Larry Hastings2f936352014-08-05 14:04:04 +100011392static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011393os_device_encoding_impl(PyObject *module, int fd)
11394/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011395{
Brett Cannonefb00c02012-02-29 18:31:31 -050011396 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011397}
11398
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011399
Larry Hastings2f936352014-08-05 14:04:04 +100011400#ifdef HAVE_SETRESUID
11401/*[clinic input]
11402os.setresuid
11403
11404 ruid: uid_t
11405 euid: uid_t
11406 suid: uid_t
11407 /
11408
11409Set the current process's real, effective, and saved user ids.
11410[clinic start generated code]*/
11411
Larry Hastings2f936352014-08-05 14:04:04 +100011412static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011413os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11414/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011415{
Victor Stinner8c62be82010-05-06 00:08:46 +000011416 if (setresuid(ruid, euid, suid) < 0)
11417 return posix_error();
11418 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011419}
Larry Hastings2f936352014-08-05 14:04:04 +100011420#endif /* HAVE_SETRESUID */
11421
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011422
11423#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011424/*[clinic input]
11425os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011426
Larry Hastings2f936352014-08-05 14:04:04 +100011427 rgid: gid_t
11428 egid: gid_t
11429 sgid: gid_t
11430 /
11431
11432Set the current process's real, effective, and saved group ids.
11433[clinic start generated code]*/
11434
Larry Hastings2f936352014-08-05 14:04:04 +100011435static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011436os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11437/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011438{
Victor Stinner8c62be82010-05-06 00:08:46 +000011439 if (setresgid(rgid, egid, sgid) < 0)
11440 return posix_error();
11441 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011442}
Larry Hastings2f936352014-08-05 14:04:04 +100011443#endif /* HAVE_SETRESGID */
11444
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011445
11446#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011447/*[clinic input]
11448os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011449
Larry Hastings2f936352014-08-05 14:04:04 +100011450Return a tuple of the current process's real, effective, and saved user ids.
11451[clinic start generated code]*/
11452
Larry Hastings2f936352014-08-05 14:04:04 +100011453static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011454os_getresuid_impl(PyObject *module)
11455/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011456{
Victor Stinner8c62be82010-05-06 00:08:46 +000011457 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011458 if (getresuid(&ruid, &euid, &suid) < 0)
11459 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011460 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11461 _PyLong_FromUid(euid),
11462 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011463}
Larry Hastings2f936352014-08-05 14:04:04 +100011464#endif /* HAVE_GETRESUID */
11465
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011466
11467#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011468/*[clinic input]
11469os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011470
Larry Hastings2f936352014-08-05 14:04:04 +100011471Return a tuple of the current process's real, effective, and saved group ids.
11472[clinic start generated code]*/
11473
Larry Hastings2f936352014-08-05 14:04:04 +100011474static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011475os_getresgid_impl(PyObject *module)
11476/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011477{
11478 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011479 if (getresgid(&rgid, &egid, &sgid) < 0)
11480 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011481 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11482 _PyLong_FromGid(egid),
11483 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011484}
Larry Hastings2f936352014-08-05 14:04:04 +100011485#endif /* HAVE_GETRESGID */
11486
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011487
Benjamin Peterson9428d532011-09-14 11:45:52 -040011488#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011489/*[clinic input]
11490os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011491
Larry Hastings2f936352014-08-05 14:04:04 +100011492 path: path_t(allow_fd=True)
11493 attribute: path_t
11494 *
11495 follow_symlinks: bool = True
11496
11497Return the value of extended attribute attribute on path.
11498
BNMetricsb9427072018-11-02 15:20:19 +000011499path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011500If follow_symlinks is False, and the last element of the path is a symbolic
11501 link, getxattr will examine the symbolic link itself instead of the file
11502 the link points to.
11503
11504[clinic start generated code]*/
11505
Larry Hastings2f936352014-08-05 14:04:04 +100011506static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011507os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011508 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011509/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011510{
11511 Py_ssize_t i;
11512 PyObject *buffer = NULL;
11513
11514 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11515 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011516
Larry Hastings9cf065c2012-06-22 16:30:09 -070011517 for (i = 0; ; i++) {
11518 void *ptr;
11519 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011520 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011521 Py_ssize_t buffer_size = buffer_sizes[i];
11522 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011523 path_error(path);
11524 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011525 }
11526 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11527 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011528 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011529 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011530
Larry Hastings9cf065c2012-06-22 16:30:09 -070011531 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011532 if (path->fd >= 0)
11533 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011534 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011535 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011536 else
Larry Hastings2f936352014-08-05 14:04:04 +100011537 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011538 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011539
Larry Hastings9cf065c2012-06-22 16:30:09 -070011540 if (result < 0) {
11541 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011542 if (errno == ERANGE)
11543 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011544 path_error(path);
11545 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011546 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011547
Larry Hastings9cf065c2012-06-22 16:30:09 -070011548 if (result != buffer_size) {
11549 /* Can only shrink. */
11550 _PyBytes_Resize(&buffer, result);
11551 }
11552 break;
11553 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011554
Larry Hastings9cf065c2012-06-22 16:30:09 -070011555 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011556}
11557
Larry Hastings2f936352014-08-05 14:04:04 +100011558
11559/*[clinic input]
11560os.setxattr
11561
11562 path: path_t(allow_fd=True)
11563 attribute: path_t
11564 value: Py_buffer
11565 flags: int = 0
11566 *
11567 follow_symlinks: bool = True
11568
11569Set extended attribute attribute on path to value.
11570
BNMetricsb9427072018-11-02 15:20:19 +000011571path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011572If follow_symlinks is False, and the last element of the path is a symbolic
11573 link, setxattr will modify the symbolic link itself instead of the file
11574 the link points to.
11575
11576[clinic start generated code]*/
11577
Benjamin Peterson799bd802011-08-31 22:15:17 -040011578static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011579os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011580 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011581/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011582{
Larry Hastings2f936352014-08-05 14:04:04 +100011583 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011584
Larry Hastings2f936352014-08-05 14:04:04 +100011585 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040011586 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011587
Benjamin Peterson799bd802011-08-31 22:15:17 -040011588 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011589 if (path->fd > -1)
11590 result = fsetxattr(path->fd, attribute->narrow,
11591 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011592 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011593 result = setxattr(path->narrow, attribute->narrow,
11594 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011595 else
Larry Hastings2f936352014-08-05 14:04:04 +100011596 result = lsetxattr(path->narrow, attribute->narrow,
11597 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011598 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011599
Larry Hastings9cf065c2012-06-22 16:30:09 -070011600 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011601 path_error(path);
11602 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011603 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011604
Larry Hastings2f936352014-08-05 14:04:04 +100011605 Py_RETURN_NONE;
11606}
11607
11608
11609/*[clinic input]
11610os.removexattr
11611
11612 path: path_t(allow_fd=True)
11613 attribute: path_t
11614 *
11615 follow_symlinks: bool = True
11616
11617Remove extended attribute attribute on path.
11618
BNMetricsb9427072018-11-02 15:20:19 +000011619path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011620If follow_symlinks is False, and the last element of the path is a symbolic
11621 link, removexattr will modify the symbolic link itself instead of the file
11622 the link points to.
11623
11624[clinic start generated code]*/
11625
Larry Hastings2f936352014-08-05 14:04:04 +100011626static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011627os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011628 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011629/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011630{
11631 ssize_t result;
11632
11633 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11634 return NULL;
11635
11636 Py_BEGIN_ALLOW_THREADS;
11637 if (path->fd > -1)
11638 result = fremovexattr(path->fd, attribute->narrow);
11639 else if (follow_symlinks)
11640 result = removexattr(path->narrow, attribute->narrow);
11641 else
11642 result = lremovexattr(path->narrow, attribute->narrow);
11643 Py_END_ALLOW_THREADS;
11644
11645 if (result) {
11646 return path_error(path);
11647 }
11648
11649 Py_RETURN_NONE;
11650}
11651
11652
11653/*[clinic input]
11654os.listxattr
11655
11656 path: path_t(allow_fd=True, nullable=True) = None
11657 *
11658 follow_symlinks: bool = True
11659
11660Return a list of extended attributes on path.
11661
BNMetricsb9427072018-11-02 15:20:19 +000011662path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011663if path is None, listxattr will examine the current directory.
11664If follow_symlinks is False, and the last element of the path is a symbolic
11665 link, listxattr will examine the symbolic link itself instead of the file
11666 the link points to.
11667[clinic start generated code]*/
11668
Larry Hastings2f936352014-08-05 14:04:04 +100011669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011670os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011671/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011672{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011673 Py_ssize_t i;
11674 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011675 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011676 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011677
Larry Hastings2f936352014-08-05 14:04:04 +100011678 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011679 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011680
Larry Hastings2f936352014-08-05 14:04:04 +100011681 name = path->narrow ? path->narrow : ".";
11682
Larry Hastings9cf065c2012-06-22 16:30:09 -070011683 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011684 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011685 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011686 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011687 Py_ssize_t buffer_size = buffer_sizes[i];
11688 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011689 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011690 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011691 break;
11692 }
11693 buffer = PyMem_MALLOC(buffer_size);
11694 if (!buffer) {
11695 PyErr_NoMemory();
11696 break;
11697 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011698
Larry Hastings9cf065c2012-06-22 16:30:09 -070011699 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011700 if (path->fd > -1)
11701 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011702 else if (follow_symlinks)
11703 length = listxattr(name, buffer, buffer_size);
11704 else
11705 length = llistxattr(name, buffer, buffer_size);
11706 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011707
Larry Hastings9cf065c2012-06-22 16:30:09 -070011708 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011709 if (errno == ERANGE) {
11710 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011711 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011712 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011713 }
Larry Hastings2f936352014-08-05 14:04:04 +100011714 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011715 break;
11716 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011717
Larry Hastings9cf065c2012-06-22 16:30:09 -070011718 result = PyList_New(0);
11719 if (!result) {
11720 goto exit;
11721 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011722
Larry Hastings9cf065c2012-06-22 16:30:09 -070011723 end = buffer + length;
11724 for (trace = start = buffer; trace != end; trace++) {
11725 if (!*trace) {
11726 int error;
11727 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11728 trace - start);
11729 if (!attribute) {
11730 Py_DECREF(result);
11731 result = NULL;
11732 goto exit;
11733 }
11734 error = PyList_Append(result, attribute);
11735 Py_DECREF(attribute);
11736 if (error) {
11737 Py_DECREF(result);
11738 result = NULL;
11739 goto exit;
11740 }
11741 start = trace + 1;
11742 }
11743 }
11744 break;
11745 }
11746exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011747 if (buffer)
11748 PyMem_FREE(buffer);
11749 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011750}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011751#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011752
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011753
Larry Hastings2f936352014-08-05 14:04:04 +100011754/*[clinic input]
11755os.urandom
11756
11757 size: Py_ssize_t
11758 /
11759
11760Return a bytes object containing random bytes suitable for cryptographic use.
11761[clinic start generated code]*/
11762
Larry Hastings2f936352014-08-05 14:04:04 +100011763static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011764os_urandom_impl(PyObject *module, Py_ssize_t size)
11765/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011766{
11767 PyObject *bytes;
11768 int result;
11769
Georg Brandl2fb477c2012-02-21 00:33:36 +010011770 if (size < 0)
11771 return PyErr_Format(PyExc_ValueError,
11772 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011773 bytes = PyBytes_FromStringAndSize(NULL, size);
11774 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011775 return NULL;
11776
Victor Stinnere66987e2016-09-06 16:33:52 -070011777 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100011778 if (result == -1) {
11779 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011780 return NULL;
11781 }
Larry Hastings2f936352014-08-05 14:04:04 +100011782 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011783}
11784
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011785/* Terminal size querying */
11786
Eddie Elizondo474eedf2018-11-13 04:09:31 -080011787static PyTypeObject* TerminalSizeType;
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011788
11789PyDoc_STRVAR(TerminalSize_docstring,
11790 "A tuple of (columns, lines) for holding terminal window size");
11791
11792static PyStructSequence_Field TerminalSize_fields[] = {
11793 {"columns", "width of the terminal window in characters"},
11794 {"lines", "height of the terminal window in characters"},
11795 {NULL, NULL}
11796};
11797
11798static PyStructSequence_Desc TerminalSize_desc = {
11799 "os.terminal_size",
11800 TerminalSize_docstring,
11801 TerminalSize_fields,
11802 2,
11803};
11804
11805#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011806/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011807PyDoc_STRVAR(termsize__doc__,
11808 "Return the size of the terminal window as (columns, lines).\n" \
11809 "\n" \
11810 "The optional argument fd (default standard output) specifies\n" \
11811 "which file descriptor should be queried.\n" \
11812 "\n" \
11813 "If the file descriptor is not connected to a terminal, an OSError\n" \
11814 "is thrown.\n" \
11815 "\n" \
11816 "This function will only be defined if an implementation is\n" \
11817 "available for this system.\n" \
11818 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080011819 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011820 "normally be used, os.get_terminal_size is the low-level implementation.");
11821
11822static PyObject*
11823get_terminal_size(PyObject *self, PyObject *args)
11824{
11825 int columns, lines;
11826 PyObject *termsize;
11827
11828 int fd = fileno(stdout);
11829 /* Under some conditions stdout may not be connected and
11830 * fileno(stdout) may point to an invalid file descriptor. For example
11831 * GUI apps don't have valid standard streams by default.
11832 *
11833 * If this happens, and the optional fd argument is not present,
11834 * the ioctl below will fail returning EBADF. This is what we want.
11835 */
11836
11837 if (!PyArg_ParseTuple(args, "|i", &fd))
11838 return NULL;
11839
11840#ifdef TERMSIZE_USE_IOCTL
11841 {
11842 struct winsize w;
11843 if (ioctl(fd, TIOCGWINSZ, &w))
11844 return PyErr_SetFromErrno(PyExc_OSError);
11845 columns = w.ws_col;
11846 lines = w.ws_row;
11847 }
11848#endif /* TERMSIZE_USE_IOCTL */
11849
11850#ifdef TERMSIZE_USE_CONIO
11851 {
11852 DWORD nhandle;
11853 HANDLE handle;
11854 CONSOLE_SCREEN_BUFFER_INFO csbi;
11855 switch (fd) {
11856 case 0: nhandle = STD_INPUT_HANDLE;
11857 break;
11858 case 1: nhandle = STD_OUTPUT_HANDLE;
11859 break;
11860 case 2: nhandle = STD_ERROR_HANDLE;
11861 break;
11862 default:
11863 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11864 }
11865 handle = GetStdHandle(nhandle);
11866 if (handle == NULL)
11867 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11868 if (handle == INVALID_HANDLE_VALUE)
11869 return PyErr_SetFromWindowsErr(0);
11870
11871 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11872 return PyErr_SetFromWindowsErr(0);
11873
11874 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11875 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11876 }
11877#endif /* TERMSIZE_USE_CONIO */
11878
Eddie Elizondo474eedf2018-11-13 04:09:31 -080011879 termsize = PyStructSequence_New(TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011880 if (termsize == NULL)
11881 return NULL;
11882 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11883 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11884 if (PyErr_Occurred()) {
11885 Py_DECREF(termsize);
11886 return NULL;
11887 }
11888 return termsize;
11889}
11890#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11891
Larry Hastings2f936352014-08-05 14:04:04 +100011892
11893/*[clinic input]
11894os.cpu_count
11895
Charles-François Natali80d62e62015-08-13 20:37:08 +010011896Return the number of CPUs in the system; return None if indeterminable.
11897
11898This number is not equivalent to the number of CPUs the current process can
11899use. The number of usable CPUs can be obtained with
11900``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100011901[clinic start generated code]*/
11902
Larry Hastings2f936352014-08-05 14:04:04 +100011903static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011904os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030011905/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011906{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011907 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011908#ifdef MS_WINDOWS
Christopher Wilcoxc67bae02017-08-30 05:01:08 -040011909 /* Vista is supported and the GetMaximumProcessorCount API is Win7+
11910 Need to fallback to Vista behavior if this call isn't present */
11911 HINSTANCE hKernel32;
11912 hKernel32 = GetModuleHandleW(L"KERNEL32");
11913
11914 static DWORD(CALLBACK *_GetMaximumProcessorCount)(WORD) = NULL;
11915 *(FARPROC*)&_GetMaximumProcessorCount = GetProcAddress(hKernel32,
11916 "GetMaximumProcessorCount");
11917 if (_GetMaximumProcessorCount != NULL) {
11918 ncpu = _GetMaximumProcessorCount(ALL_PROCESSOR_GROUPS);
11919 }
11920 else {
11921 SYSTEM_INFO sysinfo;
11922 GetSystemInfo(&sysinfo);
11923 ncpu = sysinfo.dwNumberOfProcessors;
11924 }
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011925#elif defined(__hpux)
11926 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11927#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11928 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011929#elif defined(__DragonFly__) || \
11930 defined(__OpenBSD__) || \
11931 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011932 defined(__NetBSD__) || \
11933 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011934 int mib[2];
11935 size_t len = sizeof(ncpu);
11936 mib[0] = CTL_HW;
11937 mib[1] = HW_NCPU;
11938 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11939 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011940#endif
11941 if (ncpu >= 1)
11942 return PyLong_FromLong(ncpu);
11943 else
11944 Py_RETURN_NONE;
11945}
11946
Victor Stinnerdaf45552013-08-28 00:53:59 +020011947
Larry Hastings2f936352014-08-05 14:04:04 +100011948/*[clinic input]
11949os.get_inheritable -> bool
11950
11951 fd: int
11952 /
11953
11954Get the close-on-exe flag of the specified file descriptor.
11955[clinic start generated code]*/
11956
Larry Hastings2f936352014-08-05 14:04:04 +100011957static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011958os_get_inheritable_impl(PyObject *module, int fd)
11959/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011960{
Steve Dower8fc89802015-04-12 00:26:27 -040011961 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040011962 _Py_BEGIN_SUPPRESS_IPH
11963 return_value = _Py_get_inheritable(fd);
11964 _Py_END_SUPPRESS_IPH
11965 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011966}
11967
11968
11969/*[clinic input]
11970os.set_inheritable
11971 fd: int
11972 inheritable: int
11973 /
11974
11975Set the inheritable flag of the specified file descriptor.
11976[clinic start generated code]*/
11977
Larry Hastings2f936352014-08-05 14:04:04 +100011978static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011979os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11980/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011981{
Steve Dower8fc89802015-04-12 00:26:27 -040011982 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011983
Steve Dower8fc89802015-04-12 00:26:27 -040011984 _Py_BEGIN_SUPPRESS_IPH
11985 result = _Py_set_inheritable(fd, inheritable, NULL);
11986 _Py_END_SUPPRESS_IPH
11987 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020011988 return NULL;
11989 Py_RETURN_NONE;
11990}
11991
11992
11993#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011994/*[clinic input]
11995os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070011996 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100011997 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011998
Larry Hastings2f936352014-08-05 14:04:04 +100011999Get the close-on-exe flag of the specified file descriptor.
12000[clinic start generated code]*/
12001
Larry Hastings2f936352014-08-05 14:04:04 +100012002static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012003os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012004/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012005{
12006 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012007
12008 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12009 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012010 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012011 }
12012
Larry Hastings2f936352014-08-05 14:04:04 +100012013 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012014}
12015
Victor Stinnerdaf45552013-08-28 00:53:59 +020012016
Larry Hastings2f936352014-08-05 14:04:04 +100012017/*[clinic input]
12018os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012019 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012020 inheritable: bool
12021 /
12022
12023Set the inheritable flag of the specified handle.
12024[clinic start generated code]*/
12025
Larry Hastings2f936352014-08-05 14:04:04 +100012026static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012027os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012028 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012029/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012030{
12031 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012032 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12033 PyErr_SetFromWindowsErr(0);
12034 return NULL;
12035 }
12036 Py_RETURN_NONE;
12037}
Larry Hastings2f936352014-08-05 14:04:04 +100012038#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012039
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012040#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012041/*[clinic input]
12042os.get_blocking -> bool
12043 fd: int
12044 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012045
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012046Get the blocking mode of the file descriptor.
12047
12048Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12049[clinic start generated code]*/
12050
12051static int
12052os_get_blocking_impl(PyObject *module, int fd)
12053/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012054{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012055 int blocking;
12056
Steve Dower8fc89802015-04-12 00:26:27 -040012057 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012058 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012059 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012060 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012061}
12062
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012063/*[clinic input]
12064os.set_blocking
12065 fd: int
12066 blocking: bool(accept={int})
12067 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012068
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012069Set the blocking mode of the specified file descriptor.
12070
12071Set the O_NONBLOCK flag if blocking is False,
12072clear the O_NONBLOCK flag otherwise.
12073[clinic start generated code]*/
12074
12075static PyObject *
12076os_set_blocking_impl(PyObject *module, int fd, int blocking)
12077/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012078{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012079 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012080
Steve Dower8fc89802015-04-12 00:26:27 -040012081 _Py_BEGIN_SUPPRESS_IPH
12082 result = _Py_set_blocking(fd, blocking);
12083 _Py_END_SUPPRESS_IPH
12084 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012085 return NULL;
12086 Py_RETURN_NONE;
12087}
12088#endif /* !MS_WINDOWS */
12089
12090
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012091/*[clinic input]
12092class os.DirEntry "DirEntry *" "&DirEntryType"
12093[clinic start generated code]*/
12094/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3138f09f7c683f1d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012095
12096typedef struct {
12097 PyObject_HEAD
12098 PyObject *name;
12099 PyObject *path;
12100 PyObject *stat;
12101 PyObject *lstat;
12102#ifdef MS_WINDOWS
12103 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012104 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012105 int got_file_index;
12106#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012107#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012108 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012109#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012110 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012111 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012112#endif
12113} DirEntry;
12114
12115static void
12116DirEntry_dealloc(DirEntry *entry)
12117{
12118 Py_XDECREF(entry->name);
12119 Py_XDECREF(entry->path);
12120 Py_XDECREF(entry->stat);
12121 Py_XDECREF(entry->lstat);
12122 Py_TYPE(entry)->tp_free((PyObject *)entry);
12123}
12124
12125/* Forward reference */
12126static int
12127DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12128
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012129/*[clinic input]
12130os.DirEntry.is_symlink -> bool
12131
12132Return True if the entry is a symbolic link; cached per entry.
12133[clinic start generated code]*/
12134
Victor Stinner6036e442015-03-08 01:58:04 +010012135static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012136os_DirEntry_is_symlink_impl(DirEntry *self)
12137/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012138{
12139#ifdef MS_WINDOWS
12140 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012141#elif defined(HAVE_DIRENT_D_TYPE)
12142 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012143 if (self->d_type != DT_UNKNOWN)
12144 return self->d_type == DT_LNK;
12145 else
12146 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012147#else
12148 /* POSIX without d_type */
12149 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012150#endif
12151}
12152
12153static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012154DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12155{
12156 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012157 STRUCT_STAT st;
12158 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012159
12160#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012161 if (!PyUnicode_FSDecoder(self->path, &ub))
12162 return NULL;
12163 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012164#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012165 if (!PyUnicode_FSConverter(self->path, &ub))
12166 return NULL;
12167 const char *path = PyBytes_AS_STRING(ub);
12168 if (self->dir_fd != DEFAULT_DIR_FD) {
12169#ifdef HAVE_FSTATAT
12170 result = fstatat(self->dir_fd, path, &st,
12171 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12172#else
12173 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12174 return NULL;
12175#endif /* HAVE_FSTATAT */
12176 }
12177 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012178#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012179 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012180 if (follow_symlinks)
12181 result = STAT(path, &st);
12182 else
12183 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012184 }
12185 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012186
12187 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012188 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012189
12190 return _pystat_fromstructstat(&st);
12191}
12192
12193static PyObject *
12194DirEntry_get_lstat(DirEntry *self)
12195{
12196 if (!self->lstat) {
12197#ifdef MS_WINDOWS
12198 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12199#else /* POSIX */
12200 self->lstat = DirEntry_fetch_stat(self, 0);
12201#endif
12202 }
12203 Py_XINCREF(self->lstat);
12204 return self->lstat;
12205}
12206
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012207/*[clinic input]
12208os.DirEntry.stat
12209 *
12210 follow_symlinks: bool = True
12211
12212Return stat_result object for the entry; cached per entry.
12213[clinic start generated code]*/
12214
Victor Stinner6036e442015-03-08 01:58:04 +010012215static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012216os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12217/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012218{
12219 if (!follow_symlinks)
12220 return DirEntry_get_lstat(self);
12221
12222 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012223 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012224 if (result == -1)
12225 return NULL;
12226 else if (result)
12227 self->stat = DirEntry_fetch_stat(self, 1);
12228 else
12229 self->stat = DirEntry_get_lstat(self);
12230 }
12231
12232 Py_XINCREF(self->stat);
12233 return self->stat;
12234}
12235
Victor Stinner6036e442015-03-08 01:58:04 +010012236/* Set exception and return -1 on error, 0 for False, 1 for True */
12237static int
12238DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12239{
12240 PyObject *stat = NULL;
12241 PyObject *st_mode = NULL;
12242 long mode;
12243 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012244#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012245 int is_symlink;
12246 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012247#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012248#ifdef MS_WINDOWS
12249 unsigned long dir_bits;
12250#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010012251 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012252
12253#ifdef MS_WINDOWS
12254 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12255 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012256#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012257 is_symlink = self->d_type == DT_LNK;
12258 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12259#endif
12260
Victor Stinner35a97c02015-03-08 02:59:09 +010012261#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012262 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012263#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012264 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012265 if (!stat) {
12266 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12267 /* If file doesn't exist (anymore), then return False
12268 (i.e., say it's not a file/directory) */
12269 PyErr_Clear();
12270 return 0;
12271 }
12272 goto error;
12273 }
12274 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
12275 if (!st_mode)
12276 goto error;
12277
12278 mode = PyLong_AsLong(st_mode);
12279 if (mode == -1 && PyErr_Occurred())
12280 goto error;
12281 Py_CLEAR(st_mode);
12282 Py_CLEAR(stat);
12283 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012284#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012285 }
12286 else if (is_symlink) {
12287 assert(mode_bits != S_IFLNK);
12288 result = 0;
12289 }
12290 else {
12291 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12292#ifdef MS_WINDOWS
12293 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12294 if (mode_bits == S_IFDIR)
12295 result = dir_bits != 0;
12296 else
12297 result = dir_bits == 0;
12298#else /* POSIX */
12299 if (mode_bits == S_IFDIR)
12300 result = self->d_type == DT_DIR;
12301 else
12302 result = self->d_type == DT_REG;
12303#endif
12304 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012305#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012306
12307 return result;
12308
12309error:
12310 Py_XDECREF(st_mode);
12311 Py_XDECREF(stat);
12312 return -1;
12313}
12314
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012315/*[clinic input]
12316os.DirEntry.is_dir -> bool
12317 *
12318 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012319
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012320Return True if the entry is a directory; cached per entry.
12321[clinic start generated code]*/
12322
12323static int
12324os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12325/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12326{
12327 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012328}
12329
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012330/*[clinic input]
12331os.DirEntry.is_file -> bool
12332 *
12333 follow_symlinks: bool = True
12334
12335Return True if the entry is a file; cached per entry.
12336[clinic start generated code]*/
12337
12338static int
12339os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12340/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012341{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012342 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012343}
12344
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012345/*[clinic input]
12346os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012347
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012348Return inode of the entry; cached per entry.
12349[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012350
12351static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012352os_DirEntry_inode_impl(DirEntry *self)
12353/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012354{
12355#ifdef MS_WINDOWS
12356 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012357 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012358 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012359 STRUCT_STAT stat;
12360 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012361
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012362 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012363 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012364 path = PyUnicode_AsUnicode(unicode);
12365 result = LSTAT(path, &stat);
12366 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012367
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012368 if (result != 0)
12369 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012370
12371 self->win32_file_index = stat.st_ino;
12372 self->got_file_index = 1;
12373 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012374 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12375 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012376#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012377 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12378 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012379#endif
12380}
12381
12382static PyObject *
12383DirEntry_repr(DirEntry *self)
12384{
12385 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12386}
12387
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012388/*[clinic input]
12389os.DirEntry.__fspath__
12390
12391Returns the path for the entry.
12392[clinic start generated code]*/
12393
Brett Cannon96881cd2016-06-10 14:37:21 -070012394static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012395os_DirEntry___fspath___impl(DirEntry *self)
12396/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012397{
12398 Py_INCREF(self->path);
12399 return self->path;
12400}
12401
Victor Stinner6036e442015-03-08 01:58:04 +010012402static PyMemberDef DirEntry_members[] = {
12403 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12404 "the entry's base filename, relative to scandir() \"path\" argument"},
12405 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12406 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12407 {NULL}
12408};
12409
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012410#include "clinic/posixmodule.c.h"
12411
Victor Stinner6036e442015-03-08 01:58:04 +010012412static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012413 OS_DIRENTRY_IS_DIR_METHODDEF
12414 OS_DIRENTRY_IS_FILE_METHODDEF
12415 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12416 OS_DIRENTRY_STAT_METHODDEF
12417 OS_DIRENTRY_INODE_METHODDEF
12418 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012419 {NULL}
12420};
12421
Benjamin Peterson5646de42015-04-12 17:56:34 -040012422static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012423 PyVarObject_HEAD_INIT(NULL, 0)
12424 MODNAME ".DirEntry", /* tp_name */
12425 sizeof(DirEntry), /* tp_basicsize */
12426 0, /* tp_itemsize */
12427 /* methods */
12428 (destructor)DirEntry_dealloc, /* tp_dealloc */
12429 0, /* tp_print */
12430 0, /* tp_getattr */
12431 0, /* tp_setattr */
12432 0, /* tp_compare */
12433 (reprfunc)DirEntry_repr, /* tp_repr */
12434 0, /* tp_as_number */
12435 0, /* tp_as_sequence */
12436 0, /* tp_as_mapping */
12437 0, /* tp_hash */
12438 0, /* tp_call */
12439 0, /* tp_str */
12440 0, /* tp_getattro */
12441 0, /* tp_setattro */
12442 0, /* tp_as_buffer */
12443 Py_TPFLAGS_DEFAULT, /* tp_flags */
12444 0, /* tp_doc */
12445 0, /* tp_traverse */
12446 0, /* tp_clear */
12447 0, /* tp_richcompare */
12448 0, /* tp_weaklistoffset */
12449 0, /* tp_iter */
12450 0, /* tp_iternext */
12451 DirEntry_methods, /* tp_methods */
12452 DirEntry_members, /* tp_members */
12453};
12454
12455#ifdef MS_WINDOWS
12456
12457static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012458join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012459{
12460 Py_ssize_t path_len;
12461 Py_ssize_t size;
12462 wchar_t *result;
12463 wchar_t ch;
12464
12465 if (!path_wide) { /* Default arg: "." */
12466 path_wide = L".";
12467 path_len = 1;
12468 }
12469 else {
12470 path_len = wcslen(path_wide);
12471 }
12472
12473 /* The +1's are for the path separator and the NUL */
12474 size = path_len + 1 + wcslen(filename) + 1;
12475 result = PyMem_New(wchar_t, size);
12476 if (!result) {
12477 PyErr_NoMemory();
12478 return NULL;
12479 }
12480 wcscpy(result, path_wide);
12481 if (path_len > 0) {
12482 ch = result[path_len - 1];
12483 if (ch != SEP && ch != ALTSEP && ch != L':')
12484 result[path_len++] = SEP;
12485 wcscpy(result + path_len, filename);
12486 }
12487 return result;
12488}
12489
12490static PyObject *
12491DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12492{
12493 DirEntry *entry;
12494 BY_HANDLE_FILE_INFORMATION file_info;
12495 ULONG reparse_tag;
12496 wchar_t *joined_path;
12497
12498 entry = PyObject_New(DirEntry, &DirEntryType);
12499 if (!entry)
12500 return NULL;
12501 entry->name = NULL;
12502 entry->path = NULL;
12503 entry->stat = NULL;
12504 entry->lstat = NULL;
12505 entry->got_file_index = 0;
12506
12507 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12508 if (!entry->name)
12509 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012510 if (path->narrow) {
12511 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12512 if (!entry->name)
12513 goto error;
12514 }
Victor Stinner6036e442015-03-08 01:58:04 +010012515
12516 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12517 if (!joined_path)
12518 goto error;
12519
12520 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12521 PyMem_Free(joined_path);
12522 if (!entry->path)
12523 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012524 if (path->narrow) {
12525 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12526 if (!entry->path)
12527 goto error;
12528 }
Victor Stinner6036e442015-03-08 01:58:04 +010012529
Steve Dowercc16be82016-09-08 10:35:16 -070012530 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012531 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12532
12533 return (PyObject *)entry;
12534
12535error:
12536 Py_DECREF(entry);
12537 return NULL;
12538}
12539
12540#else /* POSIX */
12541
12542static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012543join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012544{
12545 Py_ssize_t path_len;
12546 Py_ssize_t size;
12547 char *result;
12548
12549 if (!path_narrow) { /* Default arg: "." */
12550 path_narrow = ".";
12551 path_len = 1;
12552 }
12553 else {
12554 path_len = strlen(path_narrow);
12555 }
12556
12557 if (filename_len == -1)
12558 filename_len = strlen(filename);
12559
12560 /* The +1's are for the path separator and the NUL */
12561 size = path_len + 1 + filename_len + 1;
12562 result = PyMem_New(char, size);
12563 if (!result) {
12564 PyErr_NoMemory();
12565 return NULL;
12566 }
12567 strcpy(result, path_narrow);
12568 if (path_len > 0 && result[path_len - 1] != '/')
12569 result[path_len++] = '/';
12570 strcpy(result + path_len, filename);
12571 return result;
12572}
12573
12574static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012575DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012576 ino_t d_ino
12577#ifdef HAVE_DIRENT_D_TYPE
12578 , unsigned char d_type
12579#endif
12580 )
Victor Stinner6036e442015-03-08 01:58:04 +010012581{
12582 DirEntry *entry;
12583 char *joined_path;
12584
12585 entry = PyObject_New(DirEntry, &DirEntryType);
12586 if (!entry)
12587 return NULL;
12588 entry->name = NULL;
12589 entry->path = NULL;
12590 entry->stat = NULL;
12591 entry->lstat = NULL;
12592
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012593 if (path->fd != -1) {
12594 entry->dir_fd = path->fd;
12595 joined_path = NULL;
12596 }
12597 else {
12598 entry->dir_fd = DEFAULT_DIR_FD;
12599 joined_path = join_path_filename(path->narrow, name, name_len);
12600 if (!joined_path)
12601 goto error;
12602 }
Victor Stinner6036e442015-03-08 01:58:04 +010012603
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030012604 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010012605 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012606 if (joined_path)
12607 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012608 }
12609 else {
12610 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012611 if (joined_path)
12612 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010012613 }
12614 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012615 if (!entry->name)
12616 goto error;
12617
12618 if (path->fd != -1) {
12619 entry->path = entry->name;
12620 Py_INCREF(entry->path);
12621 }
12622 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010012623 goto error;
12624
Victor Stinner35a97c02015-03-08 02:59:09 +010012625#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012626 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012627#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012628 entry->d_ino = d_ino;
12629
12630 return (PyObject *)entry;
12631
12632error:
12633 Py_XDECREF(entry);
12634 return NULL;
12635}
12636
12637#endif
12638
12639
12640typedef struct {
12641 PyObject_HEAD
12642 path_t path;
12643#ifdef MS_WINDOWS
12644 HANDLE handle;
12645 WIN32_FIND_DATAW file_data;
12646 int first_time;
12647#else /* POSIX */
12648 DIR *dirp;
12649#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012650#ifdef HAVE_FDOPENDIR
12651 int fd;
12652#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012653} ScandirIterator;
12654
12655#ifdef MS_WINDOWS
12656
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012657static int
12658ScandirIterator_is_closed(ScandirIterator *iterator)
12659{
12660 return iterator->handle == INVALID_HANDLE_VALUE;
12661}
12662
Victor Stinner6036e442015-03-08 01:58:04 +010012663static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012664ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012665{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012666 HANDLE handle = iterator->handle;
12667
12668 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012669 return;
12670
Victor Stinner6036e442015-03-08 01:58:04 +010012671 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012672 Py_BEGIN_ALLOW_THREADS
12673 FindClose(handle);
12674 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012675}
12676
12677static PyObject *
12678ScandirIterator_iternext(ScandirIterator *iterator)
12679{
12680 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12681 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012682 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012683
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012684 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012685 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012686 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012687
12688 while (1) {
12689 if (!iterator->first_time) {
12690 Py_BEGIN_ALLOW_THREADS
12691 success = FindNextFileW(iterator->handle, file_data);
12692 Py_END_ALLOW_THREADS
12693 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012694 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012695 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012696 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012697 break;
12698 }
12699 }
12700 iterator->first_time = 0;
12701
12702 /* Skip over . and .. */
12703 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012704 wcscmp(file_data->cFileName, L"..") != 0) {
12705 entry = DirEntry_from_find_data(&iterator->path, file_data);
12706 if (!entry)
12707 break;
12708 return entry;
12709 }
Victor Stinner6036e442015-03-08 01:58:04 +010012710
12711 /* Loop till we get a non-dot directory or finish iterating */
12712 }
12713
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012714 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012715 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012716 return NULL;
12717}
12718
12719#else /* POSIX */
12720
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012721static int
12722ScandirIterator_is_closed(ScandirIterator *iterator)
12723{
12724 return !iterator->dirp;
12725}
12726
Victor Stinner6036e442015-03-08 01:58:04 +010012727static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012728ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012729{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012730 DIR *dirp = iterator->dirp;
12731
12732 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012733 return;
12734
Victor Stinner6036e442015-03-08 01:58:04 +010012735 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012736 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012737#ifdef HAVE_FDOPENDIR
12738 if (iterator->path.fd != -1)
12739 rewinddir(dirp);
12740#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012741 closedir(dirp);
12742 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012743 return;
12744}
12745
12746static PyObject *
12747ScandirIterator_iternext(ScandirIterator *iterator)
12748{
12749 struct dirent *direntp;
12750 Py_ssize_t name_len;
12751 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012752 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012753
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012754 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012755 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012756 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012757
12758 while (1) {
12759 errno = 0;
12760 Py_BEGIN_ALLOW_THREADS
12761 direntp = readdir(iterator->dirp);
12762 Py_END_ALLOW_THREADS
12763
12764 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012765 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012766 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012767 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012768 break;
12769 }
12770
12771 /* Skip over . and .. */
12772 name_len = NAMLEN(direntp);
12773 is_dot = direntp->d_name[0] == '.' &&
12774 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12775 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012776 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012777 name_len, direntp->d_ino
12778#ifdef HAVE_DIRENT_D_TYPE
12779 , direntp->d_type
12780#endif
12781 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012782 if (!entry)
12783 break;
12784 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012785 }
12786
12787 /* Loop till we get a non-dot directory or finish iterating */
12788 }
12789
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012790 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012791 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012792 return NULL;
12793}
12794
12795#endif
12796
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012797static PyObject *
12798ScandirIterator_close(ScandirIterator *self, PyObject *args)
12799{
12800 ScandirIterator_closedir(self);
12801 Py_RETURN_NONE;
12802}
12803
12804static PyObject *
12805ScandirIterator_enter(PyObject *self, PyObject *args)
12806{
12807 Py_INCREF(self);
12808 return self;
12809}
12810
12811static PyObject *
12812ScandirIterator_exit(ScandirIterator *self, PyObject *args)
12813{
12814 ScandirIterator_closedir(self);
12815 Py_RETURN_NONE;
12816}
12817
Victor Stinner6036e442015-03-08 01:58:04 +010012818static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010012819ScandirIterator_finalize(ScandirIterator *iterator)
12820{
12821 PyObject *error_type, *error_value, *error_traceback;
12822
12823 /* Save the current exception, if any. */
12824 PyErr_Fetch(&error_type, &error_value, &error_traceback);
12825
12826 if (!ScandirIterator_is_closed(iterator)) {
12827 ScandirIterator_closedir(iterator);
12828
12829 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
12830 "unclosed scandir iterator %R", iterator)) {
12831 /* Spurious errors can appear at shutdown */
12832 if (PyErr_ExceptionMatches(PyExc_Warning)) {
12833 PyErr_WriteUnraisable((PyObject *) iterator);
12834 }
12835 }
12836 }
12837
Victor Stinner7bfa4092016-03-23 00:43:54 +010012838 path_cleanup(&iterator->path);
12839
12840 /* Restore the saved exception. */
12841 PyErr_Restore(error_type, error_value, error_traceback);
12842}
12843
12844static void
Victor Stinner6036e442015-03-08 01:58:04 +010012845ScandirIterator_dealloc(ScandirIterator *iterator)
12846{
Victor Stinner7bfa4092016-03-23 00:43:54 +010012847 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
12848 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012849
Victor Stinner6036e442015-03-08 01:58:04 +010012850 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12851}
12852
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012853static PyMethodDef ScandirIterator_methods[] = {
12854 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
12855 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
12856 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
12857 {NULL}
12858};
12859
Benjamin Peterson5646de42015-04-12 17:56:34 -040012860static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012861 PyVarObject_HEAD_INIT(NULL, 0)
12862 MODNAME ".ScandirIterator", /* tp_name */
12863 sizeof(ScandirIterator), /* tp_basicsize */
12864 0, /* tp_itemsize */
12865 /* methods */
12866 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12867 0, /* tp_print */
12868 0, /* tp_getattr */
12869 0, /* tp_setattr */
12870 0, /* tp_compare */
12871 0, /* tp_repr */
12872 0, /* tp_as_number */
12873 0, /* tp_as_sequence */
12874 0, /* tp_as_mapping */
12875 0, /* tp_hash */
12876 0, /* tp_call */
12877 0, /* tp_str */
12878 0, /* tp_getattro */
12879 0, /* tp_setattro */
12880 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012881 Py_TPFLAGS_DEFAULT
12882 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010012883 0, /* tp_doc */
12884 0, /* tp_traverse */
12885 0, /* tp_clear */
12886 0, /* tp_richcompare */
12887 0, /* tp_weaklistoffset */
12888 PyObject_SelfIter, /* tp_iter */
12889 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012890 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012891 0, /* tp_members */
12892 0, /* tp_getset */
12893 0, /* tp_base */
12894 0, /* tp_dict */
12895 0, /* tp_descr_get */
12896 0, /* tp_descr_set */
12897 0, /* tp_dictoffset */
12898 0, /* tp_init */
12899 0, /* tp_alloc */
12900 0, /* tp_new */
12901 0, /* tp_free */
12902 0, /* tp_is_gc */
12903 0, /* tp_bases */
12904 0, /* tp_mro */
12905 0, /* tp_cache */
12906 0, /* tp_subclasses */
12907 0, /* tp_weaklist */
12908 0, /* tp_del */
12909 0, /* tp_version_tag */
12910 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010012911};
12912
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012913/*[clinic input]
12914os.scandir
12915
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012916 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012917
12918Return an iterator of DirEntry objects for given path.
12919
BNMetricsb9427072018-11-02 15:20:19 +000012920path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012921is bytes, the names of yielded DirEntry objects will also be bytes; in
12922all other circumstances they will be str.
12923
12924If path is None, uses the path='.'.
12925[clinic start generated code]*/
12926
Victor Stinner6036e442015-03-08 01:58:04 +010012927static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012928os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000012929/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012930{
12931 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010012932#ifdef MS_WINDOWS
12933 wchar_t *path_strW;
12934#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012935 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012936#ifdef HAVE_FDOPENDIR
12937 int fd = -1;
12938#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012939#endif
12940
12941 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12942 if (!iterator)
12943 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012944
12945#ifdef MS_WINDOWS
12946 iterator->handle = INVALID_HANDLE_VALUE;
12947#else
12948 iterator->dirp = NULL;
12949#endif
12950
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012951 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020012952 /* Move the ownership to iterator->path */
12953 path->object = NULL;
12954 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012955
12956#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010012957 iterator->first_time = 1;
12958
12959 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12960 if (!path_strW)
12961 goto error;
12962
12963 Py_BEGIN_ALLOW_THREADS
12964 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12965 Py_END_ALLOW_THREADS
12966
12967 PyMem_Free(path_strW);
12968
12969 if (iterator->handle == INVALID_HANDLE_VALUE) {
12970 path_error(&iterator->path);
12971 goto error;
12972 }
12973#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012974 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012975#ifdef HAVE_FDOPENDIR
12976 if (path->fd != -1) {
12977 /* closedir() closes the FD, so we duplicate it */
12978 fd = _Py_dup(path->fd);
12979 if (fd == -1)
12980 goto error;
12981
12982 Py_BEGIN_ALLOW_THREADS
12983 iterator->dirp = fdopendir(fd);
12984 Py_END_ALLOW_THREADS
12985 }
12986 else
12987#endif
12988 {
12989 if (iterator->path.narrow)
12990 path_str = iterator->path.narrow;
12991 else
12992 path_str = ".";
12993
12994 Py_BEGIN_ALLOW_THREADS
12995 iterator->dirp = opendir(path_str);
12996 Py_END_ALLOW_THREADS
12997 }
Victor Stinner6036e442015-03-08 01:58:04 +010012998
12999 if (!iterator->dirp) {
13000 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013001#ifdef HAVE_FDOPENDIR
13002 if (fd != -1) {
13003 Py_BEGIN_ALLOW_THREADS
13004 close(fd);
13005 Py_END_ALLOW_THREADS
13006 }
13007#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013008 goto error;
13009 }
13010#endif
13011
13012 return (PyObject *)iterator;
13013
13014error:
13015 Py_DECREF(iterator);
13016 return NULL;
13017}
13018
Ethan Furman410ef8e2016-06-04 12:06:26 -070013019/*
13020 Return the file system path representation of the object.
13021
13022 If the object is str or bytes, then allow it to pass through with
13023 an incremented refcount. If the object defines __fspath__(), then
13024 return the result of that method. All other types raise a TypeError.
13025*/
13026PyObject *
13027PyOS_FSPath(PyObject *path)
13028{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013029 /* For error message reasons, this function is manually inlined in
13030 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013031 _Py_IDENTIFIER(__fspath__);
13032 PyObject *func = NULL;
13033 PyObject *path_repr = NULL;
13034
13035 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13036 Py_INCREF(path);
13037 return path;
13038 }
13039
13040 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13041 if (NULL == func) {
13042 return PyErr_Format(PyExc_TypeError,
13043 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013044 "not %.200s",
13045 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013046 }
13047
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013048 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013049 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013050 if (NULL == path_repr) {
13051 return NULL;
13052 }
13053
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013054 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13055 PyErr_Format(PyExc_TypeError,
13056 "expected %.200s.__fspath__() to return str or bytes, "
13057 "not %.200s", Py_TYPE(path)->tp_name,
13058 Py_TYPE(path_repr)->tp_name);
13059 Py_DECREF(path_repr);
13060 return NULL;
13061 }
13062
Ethan Furman410ef8e2016-06-04 12:06:26 -070013063 return path_repr;
13064}
13065
13066/*[clinic input]
13067os.fspath
13068
13069 path: object
13070
13071Return the file system path representation of the object.
13072
Brett Cannonb4f43e92016-06-09 14:32:08 -070013073If the object is str or bytes, then allow it to pass through as-is. If the
13074object defines __fspath__(), then return the result of that method. All other
13075types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013076[clinic start generated code]*/
13077
13078static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013079os_fspath_impl(PyObject *module, PyObject *path)
13080/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013081{
13082 return PyOS_FSPath(path);
13083}
Victor Stinner6036e442015-03-08 01:58:04 +010013084
Victor Stinner9b1f4742016-09-06 16:18:52 -070013085#ifdef HAVE_GETRANDOM_SYSCALL
13086/*[clinic input]
13087os.getrandom
13088
13089 size: Py_ssize_t
13090 flags: int=0
13091
13092Obtain a series of random bytes.
13093[clinic start generated code]*/
13094
13095static PyObject *
13096os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13097/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13098{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013099 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013100 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013101
13102 if (size < 0) {
13103 errno = EINVAL;
13104 return posix_error();
13105 }
13106
Victor Stinnerec2319c2016-09-20 23:00:59 +020013107 bytes = PyBytes_FromStringAndSize(NULL, size);
13108 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013109 PyErr_NoMemory();
13110 return NULL;
13111 }
13112
13113 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013114 n = syscall(SYS_getrandom,
13115 PyBytes_AS_STRING(bytes),
13116 PyBytes_GET_SIZE(bytes),
13117 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013118 if (n < 0 && errno == EINTR) {
13119 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013120 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013121 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013122
13123 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013124 continue;
13125 }
13126 break;
13127 }
13128
13129 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013130 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013131 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013132 }
13133
Victor Stinnerec2319c2016-09-20 23:00:59 +020013134 if (n != size) {
13135 _PyBytes_Resize(&bytes, n);
13136 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013137
13138 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013139
13140error:
13141 Py_DECREF(bytes);
13142 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013143}
13144#endif /* HAVE_GETRANDOM_SYSCALL */
13145
Larry Hastings31826802013-10-19 00:09:25 -070013146
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013147static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013148
13149 OS_STAT_METHODDEF
13150 OS_ACCESS_METHODDEF
13151 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013152 OS_CHDIR_METHODDEF
13153 OS_CHFLAGS_METHODDEF
13154 OS_CHMOD_METHODDEF
13155 OS_FCHMOD_METHODDEF
13156 OS_LCHMOD_METHODDEF
13157 OS_CHOWN_METHODDEF
13158 OS_FCHOWN_METHODDEF
13159 OS_LCHOWN_METHODDEF
13160 OS_LCHFLAGS_METHODDEF
13161 OS_CHROOT_METHODDEF
13162 OS_CTERMID_METHODDEF
13163 OS_GETCWD_METHODDEF
13164 OS_GETCWDB_METHODDEF
13165 OS_LINK_METHODDEF
13166 OS_LISTDIR_METHODDEF
13167 OS_LSTAT_METHODDEF
13168 OS_MKDIR_METHODDEF
13169 OS_NICE_METHODDEF
13170 OS_GETPRIORITY_METHODDEF
13171 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013172 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013173 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013174 OS_READLINK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013175 OS_RENAME_METHODDEF
13176 OS_REPLACE_METHODDEF
13177 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013178 OS_SYMLINK_METHODDEF
13179 OS_SYSTEM_METHODDEF
13180 OS_UMASK_METHODDEF
13181 OS_UNAME_METHODDEF
13182 OS_UNLINK_METHODDEF
13183 OS_REMOVE_METHODDEF
13184 OS_UTIME_METHODDEF
13185 OS_TIMES_METHODDEF
13186 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013187 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013188 OS_EXECV_METHODDEF
13189 OS_EXECVE_METHODDEF
13190 OS_SPAWNV_METHODDEF
13191 OS_SPAWNVE_METHODDEF
13192 OS_FORK1_METHODDEF
13193 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013194 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013195 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13196 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13197 OS_SCHED_GETPARAM_METHODDEF
13198 OS_SCHED_GETSCHEDULER_METHODDEF
13199 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13200 OS_SCHED_SETPARAM_METHODDEF
13201 OS_SCHED_SETSCHEDULER_METHODDEF
13202 OS_SCHED_YIELD_METHODDEF
13203 OS_SCHED_SETAFFINITY_METHODDEF
13204 OS_SCHED_GETAFFINITY_METHODDEF
13205 OS_OPENPTY_METHODDEF
13206 OS_FORKPTY_METHODDEF
13207 OS_GETEGID_METHODDEF
13208 OS_GETEUID_METHODDEF
13209 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013210#ifdef HAVE_GETGROUPLIST
13211 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13212#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013213 OS_GETGROUPS_METHODDEF
13214 OS_GETPID_METHODDEF
13215 OS_GETPGRP_METHODDEF
13216 OS_GETPPID_METHODDEF
13217 OS_GETUID_METHODDEF
13218 OS_GETLOGIN_METHODDEF
13219 OS_KILL_METHODDEF
13220 OS_KILLPG_METHODDEF
13221 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013222#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013223 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013224#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013225 OS_SETUID_METHODDEF
13226 OS_SETEUID_METHODDEF
13227 OS_SETREUID_METHODDEF
13228 OS_SETGID_METHODDEF
13229 OS_SETEGID_METHODDEF
13230 OS_SETREGID_METHODDEF
13231 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013232#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013233 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013234#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013235 OS_GETPGID_METHODDEF
13236 OS_SETPGRP_METHODDEF
13237 OS_WAIT_METHODDEF
13238 OS_WAIT3_METHODDEF
13239 OS_WAIT4_METHODDEF
13240 OS_WAITID_METHODDEF
13241 OS_WAITPID_METHODDEF
13242 OS_GETSID_METHODDEF
13243 OS_SETSID_METHODDEF
13244 OS_SETPGID_METHODDEF
13245 OS_TCGETPGRP_METHODDEF
13246 OS_TCSETPGRP_METHODDEF
13247 OS_OPEN_METHODDEF
13248 OS_CLOSE_METHODDEF
13249 OS_CLOSERANGE_METHODDEF
13250 OS_DEVICE_ENCODING_METHODDEF
13251 OS_DUP_METHODDEF
13252 OS_DUP2_METHODDEF
13253 OS_LOCKF_METHODDEF
13254 OS_LSEEK_METHODDEF
13255 OS_READ_METHODDEF
13256 OS_READV_METHODDEF
13257 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013258 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013259 OS_WRITE_METHODDEF
13260 OS_WRITEV_METHODDEF
13261 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013262 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013263#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013264 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013265 posix_sendfile__doc__},
13266#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013267 OS_FSTAT_METHODDEF
13268 OS_ISATTY_METHODDEF
13269 OS_PIPE_METHODDEF
13270 OS_PIPE2_METHODDEF
13271 OS_MKFIFO_METHODDEF
13272 OS_MKNOD_METHODDEF
13273 OS_MAJOR_METHODDEF
13274 OS_MINOR_METHODDEF
13275 OS_MAKEDEV_METHODDEF
13276 OS_FTRUNCATE_METHODDEF
13277 OS_TRUNCATE_METHODDEF
13278 OS_POSIX_FALLOCATE_METHODDEF
13279 OS_POSIX_FADVISE_METHODDEF
13280 OS_PUTENV_METHODDEF
13281 OS_UNSETENV_METHODDEF
13282 OS_STRERROR_METHODDEF
13283 OS_FCHDIR_METHODDEF
13284 OS_FSYNC_METHODDEF
13285 OS_SYNC_METHODDEF
13286 OS_FDATASYNC_METHODDEF
13287 OS_WCOREDUMP_METHODDEF
13288 OS_WIFCONTINUED_METHODDEF
13289 OS_WIFSTOPPED_METHODDEF
13290 OS_WIFSIGNALED_METHODDEF
13291 OS_WIFEXITED_METHODDEF
13292 OS_WEXITSTATUS_METHODDEF
13293 OS_WTERMSIG_METHODDEF
13294 OS_WSTOPSIG_METHODDEF
13295 OS_FSTATVFS_METHODDEF
13296 OS_STATVFS_METHODDEF
13297 OS_CONFSTR_METHODDEF
13298 OS_SYSCONF_METHODDEF
13299 OS_FPATHCONF_METHODDEF
13300 OS_PATHCONF_METHODDEF
13301 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013302 OS__GETFULLPATHNAME_METHODDEF
13303 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013304 OS__GETDISKUSAGE_METHODDEF
13305 OS__GETFINALPATHNAME_METHODDEF
13306 OS__GETVOLUMEPATHNAME_METHODDEF
13307 OS_GETLOADAVG_METHODDEF
13308 OS_URANDOM_METHODDEF
13309 OS_SETRESUID_METHODDEF
13310 OS_SETRESGID_METHODDEF
13311 OS_GETRESUID_METHODDEF
13312 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013313
Larry Hastings2f936352014-08-05 14:04:04 +100013314 OS_GETXATTR_METHODDEF
13315 OS_SETXATTR_METHODDEF
13316 OS_REMOVEXATTR_METHODDEF
13317 OS_LISTXATTR_METHODDEF
13318
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013319#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13320 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13321#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013322 OS_CPU_COUNT_METHODDEF
13323 OS_GET_INHERITABLE_METHODDEF
13324 OS_SET_INHERITABLE_METHODDEF
13325 OS_GET_HANDLE_INHERITABLE_METHODDEF
13326 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013327#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013328 OS_GET_BLOCKING_METHODDEF
13329 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013330#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013331 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013332 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013333 OS_GETRANDOM_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000013334 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013335};
13336
13337
Brian Curtin52173d42010-12-02 18:29:18 +000013338#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013339static int
Brian Curtin52173d42010-12-02 18:29:18 +000013340enable_symlink()
13341{
13342 HANDLE tok;
13343 TOKEN_PRIVILEGES tok_priv;
13344 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000013345
13346 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013347 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013348
13349 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013350 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013351
13352 tok_priv.PrivilegeCount = 1;
13353 tok_priv.Privileges[0].Luid = luid;
13354 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
13355
13356 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
13357 sizeof(TOKEN_PRIVILEGES),
13358 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000013359 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000013360
Brian Curtin3b4499c2010-12-28 14:31:47 +000013361 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
13362 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000013363}
13364#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
13365
Barry Warsaw4a342091996-12-19 23:50:02 +000013366static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013367all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013368{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013369#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013370 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013371#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013372#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013373 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013374#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013375#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013376 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013377#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013378#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013379 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013380#endif
Fred Drakec9680921999-12-13 16:37:25 +000013381#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013382 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013383#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013384#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013385 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013386#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013387#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013388 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013389#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013390#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013391 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013392#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013393#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013394 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013395#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013396#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013397 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013398#endif
13399#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013400 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013401#endif
13402#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013403 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013404#endif
13405#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013406 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013407#endif
13408#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013409 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013410#endif
13411#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013412 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013413#endif
13414#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013415 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013416#endif
13417#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013418 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013419#endif
13420#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013421 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013422#endif
13423#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013424 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013425#endif
13426#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013427 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013428#endif
13429#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013430 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013431#endif
13432#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013433 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013434#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013435#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013436 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013437#endif
13438#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013439 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013440#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013441#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013442 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013443#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013444#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013445 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013446#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013447#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013448#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013449 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013450#endif
13451#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013452 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013453#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013454#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013455#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013456 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013457#endif
13458#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013459 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013460#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013461#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013462 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013463#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013464#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013465 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013466#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013467#ifdef O_TMPFILE
13468 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13469#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013470#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013471 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013472#endif
13473#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013474 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013475#endif
13476#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013477 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013478#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013479#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013480 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013481#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013482#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013483 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013484#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013485
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013486
Jesus Cea94363612012-06-22 18:32:07 +020013487#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013488 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013489#endif
13490#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013491 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013492#endif
13493
Tim Peters5aa91602002-01-30 05:46:57 +000013494/* MS Windows */
13495#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013496 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013497 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013498#endif
13499#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013500 /* Optimize for short life (keep in memory). */
13501 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013502 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013503#endif
13504#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013505 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013506 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013507#endif
13508#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013509 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013510 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013511#endif
13512#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013513 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013514 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013515#endif
13516
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013517/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013518#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013519 /* Send a SIGIO signal whenever input or output
13520 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013521 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013522#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013523#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000013524 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013525 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013526#endif
13527#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000013528 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013529 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013530#endif
13531#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000013532 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013533 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013534#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013535#ifdef O_NOLINKS
13536 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013537 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013538#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013539#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000013540 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013541 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000013542#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000013543
Victor Stinner8c62be82010-05-06 00:08:46 +000013544 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013545#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013546 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013547#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013548#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013549 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013550#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013551#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013552 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013553#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013554#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013555 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013556#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013557#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013558 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013559#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013560#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013561 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013562#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013563#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013564 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013565#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013566#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013567 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013568#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013569#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013570 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013571#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013572#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013573 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013574#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013575#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013576 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013577#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013578#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013579 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013580#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013581#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013582 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013583#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013584#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013585 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013586#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013587#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013588 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013589#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013590#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013591 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013592#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013593#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013594 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000013595#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000013596
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000013597 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013598#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013599 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013600#endif /* ST_RDONLY */
13601#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013602 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000013603#endif /* ST_NOSUID */
13604
doko@ubuntu.comca616a22013-12-08 15:23:07 +010013605 /* GNU extensions */
13606#ifdef ST_NODEV
13607 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
13608#endif /* ST_NODEV */
13609#ifdef ST_NOEXEC
13610 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
13611#endif /* ST_NOEXEC */
13612#ifdef ST_SYNCHRONOUS
13613 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
13614#endif /* ST_SYNCHRONOUS */
13615#ifdef ST_MANDLOCK
13616 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
13617#endif /* ST_MANDLOCK */
13618#ifdef ST_WRITE
13619 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
13620#endif /* ST_WRITE */
13621#ifdef ST_APPEND
13622 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
13623#endif /* ST_APPEND */
13624#ifdef ST_NOATIME
13625 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
13626#endif /* ST_NOATIME */
13627#ifdef ST_NODIRATIME
13628 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
13629#endif /* ST_NODIRATIME */
13630#ifdef ST_RELATIME
13631 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
13632#endif /* ST_RELATIME */
13633
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013634 /* FreeBSD sendfile() constants */
13635#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013636 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013637#endif
13638#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013639 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013640#endif
13641#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013642 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013643#endif
13644
Ross Lagerwall7807c352011-03-17 20:20:30 +020013645 /* constants for posix_fadvise */
13646#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013647 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013648#endif
13649#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013650 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013651#endif
13652#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013653 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013654#endif
13655#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013656 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013657#endif
13658#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013659 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013660#endif
13661#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013662 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013663#endif
13664
13665 /* constants for waitid */
13666#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013667 if (PyModule_AddIntMacro(m, P_PID)) return -1;
13668 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
13669 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013670#endif
13671#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013672 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013673#endif
13674#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013675 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013676#endif
13677#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013678 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013679#endif
13680#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013681 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013682#endif
13683#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013684 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013685#endif
13686#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013687 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013688#endif
13689#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013690 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013691#endif
13692
13693 /* constants for lockf */
13694#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013695 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013696#endif
13697#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013698 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013699#endif
13700#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013701 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013702#endif
13703#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013704 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013705#endif
13706
Pablo Galindo4defba32018-01-27 16:16:37 +000013707#ifdef RWF_DSYNC
13708 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
13709#endif
13710#ifdef RWF_HIPRI
13711 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
13712#endif
13713#ifdef RWF_SYNC
13714 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
13715#endif
13716#ifdef RWF_NOWAIT
13717 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
13718#endif
13719
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013720/* constants for posix_spawn */
13721#ifdef HAVE_POSIX_SPAWN
13722 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
13723 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
13724 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
13725#endif
13726
Guido van Rossum246bc171999-02-01 23:54:31 +000013727#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013728 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
13729 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
13730 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
13731 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
13732 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000013733#endif
13734
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013735#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013736#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013737 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013738#endif
13739#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013740 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013741#endif
13742#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013743 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013744#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013745#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080013746 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013747#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013748#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013749 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013750#endif
13751#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013752 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013753#endif
13754#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013755 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013756#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013757#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013758 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013759#endif
13760#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013761 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013762#endif
13763#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013764 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013765#endif
13766#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013767 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013768#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013769#endif
13770
Benjamin Peterson9428d532011-09-14 11:45:52 -040013771#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013772 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
13773 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
13774 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040013775#endif
13776
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013777#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013778 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013779#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013780#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013781 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013782#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013783#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013784 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013785#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013786#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013787 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013788#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013789#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013790 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013791#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013792#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013793 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013794#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013795#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013796 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013797#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010013798#if HAVE_DECL_RTLD_MEMBER
13799 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
13800#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020013801
Victor Stinner9b1f4742016-09-06 16:18:52 -070013802#ifdef HAVE_GETRANDOM_SYSCALL
13803 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
13804 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
13805#endif
13806
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013807#if defined(__APPLE__)
13808 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
13809#endif
13810
Victor Stinner8c62be82010-05-06 00:08:46 +000013811 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000013812}
13813
13814
Martin v. Löwis1a214512008-06-11 05:26:20 +000013815static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000013816 PyModuleDef_HEAD_INIT,
13817 MODNAME,
13818 posix__doc__,
13819 -1,
13820 posix_methods,
13821 NULL,
13822 NULL,
13823 NULL,
13824 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000013825};
13826
13827
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013828static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070013829
13830#ifdef HAVE_FACCESSAT
13831 "HAVE_FACCESSAT",
13832#endif
13833
13834#ifdef HAVE_FCHDIR
13835 "HAVE_FCHDIR",
13836#endif
13837
13838#ifdef HAVE_FCHMOD
13839 "HAVE_FCHMOD",
13840#endif
13841
13842#ifdef HAVE_FCHMODAT
13843 "HAVE_FCHMODAT",
13844#endif
13845
13846#ifdef HAVE_FCHOWN
13847 "HAVE_FCHOWN",
13848#endif
13849
Larry Hastings00964ed2013-08-12 13:49:30 -040013850#ifdef HAVE_FCHOWNAT
13851 "HAVE_FCHOWNAT",
13852#endif
13853
Larry Hastings9cf065c2012-06-22 16:30:09 -070013854#ifdef HAVE_FEXECVE
13855 "HAVE_FEXECVE",
13856#endif
13857
13858#ifdef HAVE_FDOPENDIR
13859 "HAVE_FDOPENDIR",
13860#endif
13861
Georg Brandl306336b2012-06-24 12:55:33 +020013862#ifdef HAVE_FPATHCONF
13863 "HAVE_FPATHCONF",
13864#endif
13865
Larry Hastings9cf065c2012-06-22 16:30:09 -070013866#ifdef HAVE_FSTATAT
13867 "HAVE_FSTATAT",
13868#endif
13869
13870#ifdef HAVE_FSTATVFS
13871 "HAVE_FSTATVFS",
13872#endif
13873
Steve Dowerfe0a41a2015-03-20 19:50:46 -070013874#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020013875 "HAVE_FTRUNCATE",
13876#endif
13877
Larry Hastings9cf065c2012-06-22 16:30:09 -070013878#ifdef HAVE_FUTIMENS
13879 "HAVE_FUTIMENS",
13880#endif
13881
13882#ifdef HAVE_FUTIMES
13883 "HAVE_FUTIMES",
13884#endif
13885
13886#ifdef HAVE_FUTIMESAT
13887 "HAVE_FUTIMESAT",
13888#endif
13889
13890#ifdef HAVE_LINKAT
13891 "HAVE_LINKAT",
13892#endif
13893
13894#ifdef HAVE_LCHFLAGS
13895 "HAVE_LCHFLAGS",
13896#endif
13897
13898#ifdef HAVE_LCHMOD
13899 "HAVE_LCHMOD",
13900#endif
13901
13902#ifdef HAVE_LCHOWN
13903 "HAVE_LCHOWN",
13904#endif
13905
13906#ifdef HAVE_LSTAT
13907 "HAVE_LSTAT",
13908#endif
13909
13910#ifdef HAVE_LUTIMES
13911 "HAVE_LUTIMES",
13912#endif
13913
13914#ifdef HAVE_MKDIRAT
13915 "HAVE_MKDIRAT",
13916#endif
13917
13918#ifdef HAVE_MKFIFOAT
13919 "HAVE_MKFIFOAT",
13920#endif
13921
13922#ifdef HAVE_MKNODAT
13923 "HAVE_MKNODAT",
13924#endif
13925
13926#ifdef HAVE_OPENAT
13927 "HAVE_OPENAT",
13928#endif
13929
13930#ifdef HAVE_READLINKAT
13931 "HAVE_READLINKAT",
13932#endif
13933
13934#ifdef HAVE_RENAMEAT
13935 "HAVE_RENAMEAT",
13936#endif
13937
13938#ifdef HAVE_SYMLINKAT
13939 "HAVE_SYMLINKAT",
13940#endif
13941
13942#ifdef HAVE_UNLINKAT
13943 "HAVE_UNLINKAT",
13944#endif
13945
13946#ifdef HAVE_UTIMENSAT
13947 "HAVE_UTIMENSAT",
13948#endif
13949
13950#ifdef MS_WINDOWS
13951 "MS_WINDOWS",
13952#endif
13953
13954 NULL
13955};
13956
13957
Mark Hammondfe51c6d2002-08-02 02:27:13 +000013958PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000013959INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000013960{
Victor Stinner8c62be82010-05-06 00:08:46 +000013961 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070013962 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013963 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000013964
Brian Curtin52173d42010-12-02 18:29:18 +000013965#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013966 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000013967#endif
13968
Victor Stinner8c62be82010-05-06 00:08:46 +000013969 m = PyModule_Create(&posixmodule);
13970 if (m == NULL)
13971 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000013972
Victor Stinner8c62be82010-05-06 00:08:46 +000013973 /* Initialize environ dictionary */
13974 v = convertenviron();
13975 Py_XINCREF(v);
13976 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
13977 return NULL;
13978 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000013979
Victor Stinner8c62be82010-05-06 00:08:46 +000013980 if (all_ins(m))
13981 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000013982
Victor Stinner8c62be82010-05-06 00:08:46 +000013983 if (setup_confname_tables(m))
13984 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000013985
Victor Stinner8c62be82010-05-06 00:08:46 +000013986 Py_INCREF(PyExc_OSError);
13987 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000013988
Guido van Rossumb3d39562000-01-31 18:41:26 +000013989#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000013990 if (posix_putenv_garbage == NULL)
13991 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000013992#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000013993
Victor Stinner8c62be82010-05-06 00:08:46 +000013994 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020013995#if defined(HAVE_WAITID) && !defined(__APPLE__)
13996 waitid_result_desc.name = MODNAME ".waitid_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080013997 WaitidResultType = PyStructSequence_NewType(&waitid_result_desc);
13998 if (WaitidResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020013999 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014000 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014001#endif
14002
Christian Heimes25827622013-10-12 01:27:08 +020014003 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000014004 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14005 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14006 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014007 StatResultType = PyStructSequence_NewType(&stat_result_desc);
14008 if (StatResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014009 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014010 }
14011 structseq_new = StatResultType->tp_new;
14012 StatResultType->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014013
Christian Heimes25827622013-10-12 01:27:08 +020014014 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014015 StatVFSResultType = PyStructSequence_NewType(&statvfs_result_desc);
14016 if (StatVFSResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014017 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014018 }
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014019#ifdef NEED_TICKS_PER_SECOND
14020# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000014021 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014022# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000014023 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014024# else
Victor Stinner8c62be82010-05-06 00:08:46 +000014025 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014026# endif
14027#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014028
William Orr81574b82018-10-01 22:19:56 -070014029#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014030 sched_param_desc.name = MODNAME ".sched_param";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014031 SchedParamType = PyStructSequence_NewType(&sched_param_desc);
14032 if (SchedParamType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014033 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014034 }
14035 SchedParamType->tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014036#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014037
14038 /* initialize TerminalSize_info */
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014039 TerminalSizeType = PyStructSequence_NewType(&TerminalSize_desc);
14040 if (TerminalSizeType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014041 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014042 }
Victor Stinner6036e442015-03-08 01:58:04 +010014043
14044 /* initialize scandir types */
14045 if (PyType_Ready(&ScandirIteratorType) < 0)
14046 return NULL;
14047 if (PyType_Ready(&DirEntryType) < 0)
14048 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014049 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020014050#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014051 Py_INCREF((PyObject*) WaitidResultType);
14052 PyModule_AddObject(m, "waitid_result", (PyObject*) WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +020014053#endif
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014054 Py_INCREF((PyObject*) StatResultType);
14055 PyModule_AddObject(m, "stat_result", (PyObject*) StatResultType);
14056 Py_INCREF((PyObject*) StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000014057 PyModule_AddObject(m, "statvfs_result",
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014058 (PyObject*) StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014059
14060#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014061 Py_INCREF(SchedParamType);
14062 PyModule_AddObject(m, "sched_param", (PyObject *)SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014063#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014064
Larry Hastings605a62d2012-06-24 04:33:36 -070014065 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014066 TimesResultType = PyStructSequence_NewType(&times_result_desc);
14067 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014068 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014069 }
14070 PyModule_AddObject(m, "times_result", (PyObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014071
14072 uname_result_desc.name = MODNAME ".uname_result";
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014073 UnameResultType = PyStructSequence_NewType(&uname_result_desc);
14074 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014075 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014076 }
14077 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -070014078
Thomas Wouters477c8d52006-05-27 19:21:47 +000014079#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014080 /*
14081 * Step 2 of weak-linking support on Mac OS X.
14082 *
14083 * The code below removes functions that are not available on the
14084 * currently active platform.
14085 *
14086 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014087 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014088 * OSX 10.4.
14089 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014090#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014091 if (fstatvfs == NULL) {
14092 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14093 return NULL;
14094 }
14095 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014096#endif /* HAVE_FSTATVFS */
14097
14098#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014099 if (statvfs == NULL) {
14100 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14101 return NULL;
14102 }
14103 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014104#endif /* HAVE_STATVFS */
14105
14106# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014107 if (lchown == NULL) {
14108 if (PyObject_DelAttrString(m, "lchown") == -1) {
14109 return NULL;
14110 }
14111 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014112#endif /* HAVE_LCHOWN */
14113
14114
14115#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014116
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014117 Py_INCREF(TerminalSizeType);
14118 PyModule_AddObject(m, "terminal_size", (PyObject*)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014119
Larry Hastings6fe20b32012-04-19 15:07:49 -070014120 billion = PyLong_FromLong(1000000000);
14121 if (!billion)
14122 return NULL;
14123
Larry Hastings9cf065c2012-06-22 16:30:09 -070014124 /* suppress "function not used" warnings */
14125 {
14126 int ignored;
14127 fd_specified("", -1);
14128 follow_symlinks_specified("", 1);
14129 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14130 dir_fd_converter(Py_None, &ignored);
14131 dir_fd_unavailable(Py_None, &ignored);
14132 }
14133
14134 /*
14135 * provide list of locally available functions
14136 * so os.py can populate support_* lists
14137 */
14138 list = PyList_New(0);
14139 if (!list)
14140 return NULL;
14141 for (trace = have_functions; *trace; trace++) {
14142 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14143 if (!unicode)
14144 return NULL;
14145 if (PyList_Append(list, unicode))
14146 return NULL;
14147 Py_DECREF(unicode);
14148 }
14149 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014150
14151 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070014152 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070014153
14154 initialized = 1;
14155
Victor Stinner8c62be82010-05-06 00:08:46 +000014156 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014157}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014158
14159#ifdef __cplusplus
14160}
14161#endif