blob: d2047d9c2ac1fd3e44903fce47d6d9854380337f [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"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020028#ifdef MS_WINDOWS
29 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
30
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33
34 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
35# include <windows.h>
36#endif
37
38#include "pycore_ceval.h" /* _PyEval_ReInitThreads() */
Victor Stinner01b63ec2019-06-19 00:48:09 +020039#include "pycore_import.h" /* _PyImport_ReInitLock() */
Victor Stinnerd5d9e812019-05-13 12:35:37 +020040#include "pycore_pystate.h" /* _PyRuntime */
Antoine Pitrou346cbd32017-05-27 17:50:54 +020041#include "pythread.h"
Victor Stinner6036e442015-03-08 01:58:04 +010042#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020043#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020044# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010045#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020046# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020047#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000048
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020049/* On android API level 21, 'AT_EACCESS' is not declared although
50 * HAVE_FACCESSAT is defined. */
51#ifdef __ANDROID__
52#undef HAVE_FACCESSAT
53#endif
54
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000055#include <stdio.h> /* needed for ctermid() */
56
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057#ifdef __cplusplus
58extern "C" {
59#endif
60
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000062"This module provides access to operating system functionality that is\n\
63standardized by the C Standard and the POSIX standard (a thinly\n\
64disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000065corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000067
Ross Lagerwall4d076da2011-03-18 06:56:53 +020068#ifdef HAVE_SYS_UIO_H
69#include <sys/uio.h>
70#endif
71
Christian Heimes75b96182017-09-05 15:53:09 +020072#ifdef HAVE_SYS_SYSMACROS_H
73/* GNU C Library: major(), minor(), makedev() */
74#include <sys/sysmacros.h>
75#endif
76
Thomas Wouters0e3f5912006-08-11 14:57:12 +000077#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000078#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079#endif /* HAVE_SYS_TYPES_H */
80
81#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000082#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000083#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000084
Guido van Rossum36bc6801995-06-14 22:54:23 +000085#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000086#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000087#endif
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080088#ifdef HAVE_LINUX_WAIT_H
89#include <linux/wait.h> // For P_PIDFD
90#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000091
Thomas Wouters0e3f5912006-08-11 14:57:12 +000092#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000093#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000094#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000095
Guido van Rossumb6775db1994-08-01 11:34:53 +000096#ifdef HAVE_FCNTL_H
97#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000098#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000099
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000100#ifdef HAVE_GRP_H
101#include <grp.h>
102#endif
103
Barry Warsaw5676bd12003-01-07 20:57:09 +0000104#ifdef HAVE_SYSEXITS_H
105#include <sysexits.h>
106#endif /* HAVE_SYSEXITS_H */
107
Anthony Baxter8a560de2004-10-13 15:30:56 +0000108#ifdef HAVE_SYS_LOADAVG_H
109#include <sys/loadavg.h>
110#endif
111
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000112#ifdef HAVE_SYS_SENDFILE_H
113#include <sys/sendfile.h>
114#endif
115
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200116#if defined(__APPLE__)
117#include <copyfile.h>
118#endif
119
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500120#ifdef HAVE_SCHED_H
121#include <sched.h>
122#endif
123
Pablo Galindoaac4d032019-05-31 19:39:47 +0100124#ifdef HAVE_COPY_FILE_RANGE
125#include <unistd.h>
126#endif
127
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500128#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500129#undef HAVE_SCHED_SETAFFINITY
130#endif
131
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200132#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400133#define USE_XATTRS
134#endif
135
136#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400137#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400138#endif
139
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000140#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
141#ifdef HAVE_SYS_SOCKET_H
142#include <sys/socket.h>
143#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000144#endif
145
Victor Stinner8b905bd2011-10-25 13:34:04 +0200146#ifdef HAVE_DLFCN_H
147#include <dlfcn.h>
148#endif
149
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200150#ifdef __hpux
151#include <sys/mpctl.h>
152#endif
153
154#if defined(__DragonFly__) || \
155 defined(__OpenBSD__) || \
156 defined(__FreeBSD__) || \
157 defined(__NetBSD__) || \
158 defined(__APPLE__)
159#include <sys/sysctl.h>
160#endif
161
Victor Stinner9b1f4742016-09-06 16:18:52 -0700162#ifdef HAVE_LINUX_RANDOM_H
163# include <linux/random.h>
164#endif
165#ifdef HAVE_GETRANDOM_SYSCALL
166# include <sys/syscall.h>
167#endif
168
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100169#if defined(MS_WINDOWS)
170# define TERMSIZE_USE_CONIO
171#elif defined(HAVE_SYS_IOCTL_H)
172# include <sys/ioctl.h>
173# if defined(HAVE_TERMIOS_H)
174# include <termios.h>
175# endif
176# if defined(TIOCGWINSZ)
177# define TERMSIZE_USE_IOCTL
178# endif
179#endif /* MS_WINDOWS */
180
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000182/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000183#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000184#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000185#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000186#include <process.h>
187#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000189#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000190#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000191#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000192#define HAVE_EXECV 1
Steve Dowercc16be82016-09-08 10:35:16 -0700193#define HAVE_WSPAWNV 1
194#define HAVE_WEXECV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000195#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000196#define HAVE_SYSTEM 1
197#define HAVE_CWAIT 1
198#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000199#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000200#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000201/* Unix functions that the configure script doesn't check for */
pxinwrf2d7ac72019-05-21 18:46:37 +0800202#ifndef __VXWORKS__
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000203#define HAVE_EXECV 1
204#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000205#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000206#define HAVE_FORK1 1
207#endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800208#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#define HAVE_GETEGID 1
210#define HAVE_GETEUID 1
211#define HAVE_GETGID 1
212#define HAVE_GETPPID 1
213#define HAVE_GETUID 1
214#define HAVE_KILL 1
215#define HAVE_OPENDIR 1
216#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000217#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000218#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000219#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000220#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000221#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000222
Eddie Elizondob3966632019-11-05 07:16:14 -0800223_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100224
Larry Hastings61272b72014-01-07 12:41:53 -0800225/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000226# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800227module os
Larry Hastings61272b72014-01-07 12:41:53 -0800228[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000229/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100230
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000231#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000232
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000233#if defined(__sgi)&&_COMPILER_VERSION>=700
234/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
235 (default) */
236extern char *ctermid_r(char *);
237#endif
238
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000239#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240
pxinwrf2d7ac72019-05-21 18:46:37 +0800241#if defined(__VXWORKS__)
242#include <vxCpuLib.h>
243#include <rtpLib.h>
244#include <wait.h>
245#include <taskLib.h>
246#ifndef _P_WAIT
247#define _P_WAIT 0
248#define _P_NOWAIT 1
249#define _P_NOWAITO 1
250#endif
251#endif /* __VXWORKS__ */
252
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000253#ifdef HAVE_POSIX_SPAWN
254#include <spawn.h>
255#endif
256
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257#ifdef HAVE_UTIME_H
258#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000259#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000260
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000261#ifdef HAVE_SYS_UTIME_H
262#include <sys/utime.h>
263#define HAVE_UTIME_H /* pretend we do for the rest of this file */
264#endif /* HAVE_SYS_UTIME_H */
265
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266#ifdef HAVE_SYS_TIMES_H
267#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000268#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000269
270#ifdef HAVE_SYS_PARAM_H
271#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000272#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000273
274#ifdef HAVE_SYS_UTSNAME_H
275#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000276#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000277
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000278#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#define NAMLEN(dirent) strlen((dirent)->d_name)
281#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000282#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000283#include <direct.h>
284#define NAMLEN(dirent) strlen((dirent)->d_name)
285#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000288#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000289#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000291#endif
292#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000294#endif
295#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000297#endif
298#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000300#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303#endif
304#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000306#endif
307#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000308#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000310#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000311#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000312#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100313#ifndef IO_REPARSE_TAG_MOUNT_POINT
314#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
315#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000316#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000317#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000319#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000320#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000321#define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000322#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000323
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000325#if defined(PATH_MAX) && PATH_MAX > 1024
326#define MAXPATHLEN PATH_MAX
327#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000328#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000329#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000330#endif /* MAXPATHLEN */
331
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000332#ifdef UNION_WAIT
333/* Emulate some macros on systems that have a union instead of macros */
334
335#ifndef WIFEXITED
336#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
337#endif
338
339#ifndef WEXITSTATUS
340#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
341#endif
342
343#ifndef WTERMSIG
344#define WTERMSIG(u_wait) ((u_wait).w_termsig)
345#endif
346
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000347#define WAIT_TYPE union wait
348#define WAIT_STATUS_INT(s) (s.w_status)
349
350#else /* !UNION_WAIT */
351#define WAIT_TYPE int
352#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000353#endif /* UNION_WAIT */
354
Greg Wardb48bc172000-03-01 21:51:56 +0000355/* Don't use the "_r" form if we don't need it (also, won't have a
356 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200357#if defined(HAVE_CTERMID_R)
Greg Wardb48bc172000-03-01 21:51:56 +0000358#define USE_CTERMID_R
359#endif
360
Fred Drake699f3522000-06-29 21:12:41 +0000361/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000362#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000363#undef FSTAT
364#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200365#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000366# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700367# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200368# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800369# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000370#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000371# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700372# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000373# define FSTAT fstat
374# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000375#endif
376
Tim Peters11b23062003-04-23 02:39:17 +0000377#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000378#include <sys/mkdev.h>
379#else
380#if defined(MAJOR_IN_SYSMACROS)
381#include <sys/sysmacros.h>
382#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000383#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
384#include <sys/mkdev.h>
385#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000386#endif
Fred Drake699f3522000-06-29 21:12:41 +0000387
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200388#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100389#define INITFUNC PyInit_nt
390#define MODNAME "nt"
391#else
392#define INITFUNC PyInit_posix
393#define MODNAME "posix"
394#endif
395
jcea6c51d512018-01-28 14:00:08 +0100396#if defined(__sun)
397/* Something to implement in autoconf, not present in autoconf 2.69 */
398#define HAVE_STRUCT_STAT_ST_FSTYPE 1
399#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200400
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600401/* memfd_create is either defined in sys/mman.h or sys/memfd.h
402 * linux/memfd.h defines additional flags
403 */
404#ifdef HAVE_SYS_MMAN_H
405#include <sys/mman.h>
406#endif
407#ifdef HAVE_SYS_MEMFD_H
408#include <sys/memfd.h>
409#endif
410#ifdef HAVE_LINUX_MEMFD_H
411#include <linux/memfd.h>
412#endif
413
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800414#ifdef _Py_MEMORY_SANITIZER
415# include <sanitizer/msan_interface.h>
416#endif
417
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200418#ifdef HAVE_FORK
419static void
420run_at_forkers(PyObject *lst, int reverse)
421{
422 Py_ssize_t i;
423 PyObject *cpy;
424
425 if (lst != NULL) {
426 assert(PyList_CheckExact(lst));
427
428 /* Use a list copy in case register_at_fork() is called from
429 * one of the callbacks.
430 */
431 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
432 if (cpy == NULL)
433 PyErr_WriteUnraisable(lst);
434 else {
435 if (reverse)
436 PyList_Reverse(cpy);
437 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
438 PyObject *func, *res;
439 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200440 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200441 if (res == NULL)
442 PyErr_WriteUnraisable(func);
443 else
444 Py_DECREF(res);
445 }
446 Py_DECREF(cpy);
447 }
448 }
449}
450
451void
452PyOS_BeforeFork(void)
453{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200454 run_at_forkers(_PyInterpreterState_Get()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200455
456 _PyImport_AcquireLock();
457}
458
459void
460PyOS_AfterFork_Parent(void)
461{
462 if (_PyImport_ReleaseLock() <= 0)
463 Py_FatalError("failed releasing import lock after fork");
464
Victor Stinnercaba55b2018-08-03 15:33:52 +0200465 run_at_forkers(_PyInterpreterState_Get()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200466}
467
468void
469PyOS_AfterFork_Child(void)
470{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200471 _PyRuntimeState *runtime = &_PyRuntime;
472 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200473 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200474 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200475 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200476 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200477 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200478
Victor Stinnercaba55b2018-08-03 15:33:52 +0200479 run_at_forkers(_PyInterpreterState_Get()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200480}
481
482static int
483register_at_forker(PyObject **lst, PyObject *func)
484{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700485 if (func == NULL) /* nothing to register? do nothing. */
486 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200487 if (*lst == NULL) {
488 *lst = PyList_New(0);
489 if (*lst == NULL)
490 return -1;
491 }
492 return PyList_Append(*lst, func);
493}
494#endif
495
496/* Legacy wrapper */
497void
498PyOS_AfterFork(void)
499{
500#ifdef HAVE_FORK
501 PyOS_AfterFork_Child();
502#endif
503}
504
505
Victor Stinner6036e442015-03-08 01:58:04 +0100506#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200507/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700508void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
509void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200510 ULONG, struct _Py_stat_struct *);
511#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700512
Larry Hastings9cf065c2012-06-22 16:30:09 -0700513
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200514#ifndef MS_WINDOWS
515PyObject *
516_PyLong_FromUid(uid_t uid)
517{
518 if (uid == (uid_t)-1)
519 return PyLong_FromLong(-1);
520 return PyLong_FromUnsignedLong(uid);
521}
522
523PyObject *
524_PyLong_FromGid(gid_t gid)
525{
526 if (gid == (gid_t)-1)
527 return PyLong_FromLong(-1);
528 return PyLong_FromUnsignedLong(gid);
529}
530
531int
532_Py_Uid_Converter(PyObject *obj, void *p)
533{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700534 uid_t uid;
535 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200536 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200537 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700538 unsigned long uresult;
539
540 index = PyNumber_Index(obj);
541 if (index == NULL) {
542 PyErr_Format(PyExc_TypeError,
543 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800544 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200545 return 0;
546 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700547
548 /*
549 * Handling uid_t is complicated for two reasons:
550 * * Although uid_t is (always?) unsigned, it still
551 * accepts -1.
552 * * We don't know its size in advance--it may be
553 * bigger than an int, or it may be smaller than
554 * a long.
555 *
556 * So a bit of defensive programming is in order.
557 * Start with interpreting the value passed
558 * in as a signed long and see if it works.
559 */
560
561 result = PyLong_AsLongAndOverflow(index, &overflow);
562
563 if (!overflow) {
564 uid = (uid_t)result;
565
566 if (result == -1) {
567 if (PyErr_Occurred())
568 goto fail;
569 /* It's a legitimate -1, we're done. */
570 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200571 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700572
573 /* Any other negative number is disallowed. */
574 if (result < 0)
575 goto underflow;
576
577 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200578 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700579 (long)uid != result)
580 goto underflow;
581 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200582 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700583
584 if (overflow < 0)
585 goto underflow;
586
587 /*
588 * Okay, the value overflowed a signed long. If it
589 * fits in an *unsigned* long, it may still be okay,
590 * as uid_t may be unsigned long on this platform.
591 */
592 uresult = PyLong_AsUnsignedLong(index);
593 if (PyErr_Occurred()) {
594 if (PyErr_ExceptionMatches(PyExc_OverflowError))
595 goto overflow;
596 goto fail;
597 }
598
599 uid = (uid_t)uresult;
600
601 /*
602 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
603 * but this value would get interpreted as (uid_t)-1 by chown
604 * and its siblings. That's not what the user meant! So we
605 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100606 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700607 */
608 if (uid == (uid_t)-1)
609 goto overflow;
610
611 /* Ensure the value wasn't truncated. */
612 if (sizeof(uid_t) < sizeof(long) &&
613 (unsigned long)uid != uresult)
614 goto overflow;
615 /* fallthrough */
616
617success:
618 Py_DECREF(index);
619 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200620 return 1;
621
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700622underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200623 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700624 "uid is less than minimum");
625 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200626
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700627overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200628 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700629 "uid is greater than maximum");
630 /* fallthrough */
631
632fail:
633 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200634 return 0;
635}
636
637int
638_Py_Gid_Converter(PyObject *obj, void *p)
639{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700640 gid_t gid;
641 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200642 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200643 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700644 unsigned long uresult;
645
646 index = PyNumber_Index(obj);
647 if (index == NULL) {
648 PyErr_Format(PyExc_TypeError,
649 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800650 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200651 return 0;
652 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700653
654 /*
655 * Handling gid_t is complicated for two reasons:
656 * * Although gid_t is (always?) unsigned, it still
657 * accepts -1.
658 * * We don't know its size in advance--it may be
659 * bigger than an int, or it may be smaller than
660 * a long.
661 *
662 * So a bit of defensive programming is in order.
663 * Start with interpreting the value passed
664 * in as a signed long and see if it works.
665 */
666
667 result = PyLong_AsLongAndOverflow(index, &overflow);
668
669 if (!overflow) {
670 gid = (gid_t)result;
671
672 if (result == -1) {
673 if (PyErr_Occurred())
674 goto fail;
675 /* It's a legitimate -1, we're done. */
676 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200677 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700678
679 /* Any other negative number is disallowed. */
680 if (result < 0) {
681 goto underflow;
682 }
683
684 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200685 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700686 (long)gid != result)
687 goto underflow;
688 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200689 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700690
691 if (overflow < 0)
692 goto underflow;
693
694 /*
695 * Okay, the value overflowed a signed long. If it
696 * fits in an *unsigned* long, it may still be okay,
697 * as gid_t may be unsigned long on this platform.
698 */
699 uresult = PyLong_AsUnsignedLong(index);
700 if (PyErr_Occurred()) {
701 if (PyErr_ExceptionMatches(PyExc_OverflowError))
702 goto overflow;
703 goto fail;
704 }
705
706 gid = (gid_t)uresult;
707
708 /*
709 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
710 * but this value would get interpreted as (gid_t)-1 by chown
711 * and its siblings. That's not what the user meant! So we
712 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100713 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700714 */
715 if (gid == (gid_t)-1)
716 goto overflow;
717
718 /* Ensure the value wasn't truncated. */
719 if (sizeof(gid_t) < sizeof(long) &&
720 (unsigned long)gid != uresult)
721 goto overflow;
722 /* fallthrough */
723
724success:
725 Py_DECREF(index);
726 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200727 return 1;
728
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700729underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200730 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700731 "gid is less than minimum");
732 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200733
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700734overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200735 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700736 "gid is greater than maximum");
737 /* fallthrough */
738
739fail:
740 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200741 return 0;
742}
743#endif /* MS_WINDOWS */
744
745
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700746#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800747
748
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200749#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
750static int
751_Py_Dev_Converter(PyObject *obj, void *p)
752{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200753 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200754 if (PyErr_Occurred())
755 return 0;
756 return 1;
757}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800758#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200759
760
Larry Hastings9cf065c2012-06-22 16:30:09 -0700761#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400762/*
763 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
764 * without the int cast, the value gets interpreted as uint (4291925331),
765 * which doesn't play nicely with all the initializer lines in this file that
766 * look like this:
767 * int dir_fd = DEFAULT_DIR_FD;
768 */
769#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700770#else
771#define DEFAULT_DIR_FD (-100)
772#endif
773
774static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300775_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200776{
777 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700778 long long_value;
779
780 PyObject *index = PyNumber_Index(o);
781 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700782 return 0;
783 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700784
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300785 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700786 long_value = PyLong_AsLongAndOverflow(index, &overflow);
787 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300788 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200789 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700790 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700791 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700792 return 0;
793 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200794 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700795 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700796 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700797 return 0;
798 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700799
Larry Hastings9cf065c2012-06-22 16:30:09 -0700800 *p = (int)long_value;
801 return 1;
802}
803
804static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200805dir_fd_converter(PyObject *o, void *p)
806{
807 if (o == Py_None) {
808 *(int *)p = DEFAULT_DIR_FD;
809 return 1;
810 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300811 else if (PyIndex_Check(o)) {
812 return _fd_converter(o, (int *)p);
813 }
814 else {
815 PyErr_Format(PyExc_TypeError,
816 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800817 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300818 return 0;
819 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700820}
821
Eddie Elizondob3966632019-11-05 07:16:14 -0800822typedef struct {
823 PyObject *billion;
824 PyObject *posix_putenv_garbage;
825 PyObject *DirEntryType;
826 PyObject *ScandirIteratorType;
827#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
828 PyObject *SchedParamType;
829#endif
830 PyObject *StatResultType;
831 PyObject *StatVFSResultType;
832 PyObject *TerminalSizeType;
833 PyObject *TimesResultType;
834 PyObject *UnameResultType;
835#if defined(HAVE_WAITID) && !defined(__APPLE__)
836 PyObject *WaitidResultType;
837#endif
838#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
839 PyObject *struct_rusage;
840#endif
841 PyObject *st_mode;
842} _posixstate;
843
844static struct PyModuleDef posixmodule;
845
846#define _posixstate(o) ((_posixstate *)PyModule_GetState(o))
847#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700848
Larry Hastings9cf065c2012-06-22 16:30:09 -0700849/*
850 * A PyArg_ParseTuple "converter" function
851 * that handles filesystem paths in the manner
852 * preferred by the os module.
853 *
854 * path_converter accepts (Unicode) strings and their
855 * subclasses, and bytes and their subclasses. What
856 * it does with the argument depends on the platform:
857 *
858 * * On Windows, if we get a (Unicode) string we
859 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700860 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700861 *
862 * * On all other platforms, strings are encoded
863 * to bytes using PyUnicode_FSConverter, then we
864 * extract the char * from the bytes object and
865 * return that.
866 *
867 * path_converter also optionally accepts signed
868 * integers (representing open file descriptors) instead
869 * of path strings.
870 *
871 * Input fields:
872 * path.nullable
873 * If nonzero, the path is permitted to be None.
874 * path.allow_fd
875 * If nonzero, the path is permitted to be a file handle
876 * (a signed int) instead of a string.
877 * path.function_name
878 * If non-NULL, path_converter will use that as the name
879 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700880 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700881 * path.argument_name
882 * If non-NULL, path_converter will use that as the name
883 * of the parameter in error messages.
884 * (If path.argument_name is NULL it uses "path".)
885 *
886 * Output fields:
887 * path.wide
888 * Points to the path if it was expressed as Unicode
889 * and was not encoded. (Only used on Windows.)
890 * path.narrow
891 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700892 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000893 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700894 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700895 * path.fd
896 * Contains a file descriptor if path.accept_fd was true
897 * and the caller provided a signed integer instead of any
898 * sort of string.
899 *
900 * WARNING: if your "path" parameter is optional, and is
901 * unspecified, path_converter will never get called.
902 * So if you set allow_fd, you *MUST* initialize path.fd = -1
903 * yourself!
904 * path.length
905 * The length of the path in characters, if specified as
906 * a string.
907 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800908 * The original object passed in (if get a PathLike object,
909 * the result of PyOS_FSPath() is treated as the original object).
910 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700911 * path.cleanup
912 * For internal use only. May point to a temporary object.
913 * (Pay no attention to the man behind the curtain.)
914 *
915 * At most one of path.wide or path.narrow will be non-NULL.
916 * If path was None and path.nullable was set,
917 * or if path was an integer and path.allow_fd was set,
918 * both path.wide and path.narrow will be NULL
919 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200920 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700921 * path_converter takes care to not write to the path_t
922 * unless it's successful. However it must reset the
923 * "cleanup" field each time it's called.
924 *
925 * Use as follows:
926 * path_t path;
927 * memset(&path, 0, sizeof(path));
928 * PyArg_ParseTuple(args, "O&", path_converter, &path);
929 * // ... use values from path ...
930 * path_cleanup(&path);
931 *
932 * (Note that if PyArg_Parse fails you don't need to call
933 * path_cleanup(). However it is safe to do so.)
934 */
935typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100936 const char *function_name;
937 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700938 int nullable;
939 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300940 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700941#ifdef MS_WINDOWS
942 BOOL narrow;
943#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300944 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700945#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700946 int fd;
947 Py_ssize_t length;
948 PyObject *object;
949 PyObject *cleanup;
950} path_t;
951
Steve Dowercc16be82016-09-08 10:35:16 -0700952#ifdef MS_WINDOWS
953#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
954 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
955#else
Larry Hastings2f936352014-08-05 14:04:04 +1000956#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
957 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700958#endif
Larry Hastings31826802013-10-19 00:09:25 -0700959
Larry Hastings9cf065c2012-06-22 16:30:09 -0700960static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800961path_cleanup(path_t *path)
962{
963 Py_CLEAR(path->object);
964 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700965}
966
967static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300968path_converter(PyObject *o, void *p)
969{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700970 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800971 PyObject *bytes = NULL;
972 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700973 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300974 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700975#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800976 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700977 const wchar_t *wide;
978#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700979
980#define FORMAT_EXCEPTION(exc, fmt) \
981 PyErr_Format(exc, "%s%s" fmt, \
982 path->function_name ? path->function_name : "", \
983 path->function_name ? ": " : "", \
984 path->argument_name ? path->argument_name : "path")
985
986 /* Py_CLEANUP_SUPPORTED support */
987 if (o == NULL) {
988 path_cleanup(path);
989 return 1;
990 }
991
Brett Cannon3f9183b2016-08-26 14:44:48 -0700992 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800993 path->object = path->cleanup = NULL;
994 /* path->object owns a reference to the original object */
995 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700996
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300997 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700998 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700999#ifdef MS_WINDOWS
1000 path->narrow = FALSE;
1001#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001002 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001003#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001004 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001005 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001006 }
1007
Brett Cannon3f9183b2016-08-26 14:44:48 -07001008 /* Only call this here so that we don't treat the return value of
1009 os.fspath() as an fd or buffer. */
1010 is_index = path->allow_fd && PyIndex_Check(o);
1011 is_buffer = PyObject_CheckBuffer(o);
1012 is_bytes = PyBytes_Check(o);
1013 is_unicode = PyUnicode_Check(o);
1014
1015 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1016 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001017 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001018
1019 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1020 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001021 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001022 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001023 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001024 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001025 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001026 goto error_exit;
1027 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001028 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001029 is_unicode = 1;
1030 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001031 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001032 is_bytes = 1;
1033 }
1034 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001035 PyErr_Format(PyExc_TypeError,
1036 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001037 "not %.200s", _PyType_Name(Py_TYPE(o)),
1038 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001039 Py_DECREF(res);
1040 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001041 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001042
1043 /* still owns a reference to the original object */
1044 Py_DECREF(o);
1045 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001046 }
1047
1048 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001049#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001050 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001051 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001052 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001053 }
Victor Stinner59799a82013-11-13 14:17:30 +01001054 if (length > 32767) {
1055 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001056 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001057 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001058 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001059 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001060 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001061 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001062
1063 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001064 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001065 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001066 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001067#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001068 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001069 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001070 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001071#endif
1072 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001073 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001074 bytes = o;
1075 Py_INCREF(bytes);
1076 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001077 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001078 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001079 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001080 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1081 "%s%s%s should be %s, not %.200s",
1082 path->function_name ? path->function_name : "",
1083 path->function_name ? ": " : "",
1084 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001085 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1086 "integer or None" :
1087 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1088 path->nullable ? "string, bytes, os.PathLike or None" :
1089 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001090 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001091 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001092 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001093 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001094 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001095 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001096 }
1097 }
Steve Dowercc16be82016-09-08 10:35:16 -07001098 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001099 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001100 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001101 }
1102 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001103#ifdef MS_WINDOWS
1104 path->narrow = FALSE;
1105#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001106 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001107#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001108 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001109 }
1110 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001111 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001112 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1113 path->function_name ? path->function_name : "",
1114 path->function_name ? ": " : "",
1115 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001116 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1117 "integer or None" :
1118 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1119 path->nullable ? "string, bytes, os.PathLike or None" :
1120 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001121 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001122 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001123 }
1124
Larry Hastings9cf065c2012-06-22 16:30:09 -07001125 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001126 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001127 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001128 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001129 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001130 }
1131
Steve Dowercc16be82016-09-08 10:35:16 -07001132#ifdef MS_WINDOWS
1133 wo = PyUnicode_DecodeFSDefaultAndSize(
1134 narrow,
1135 length
1136 );
1137 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001138 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001139 }
1140
Xiang Zhang04316c42017-01-08 23:26:57 +08001141 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001142 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001143 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001144 }
1145 if (length > 32767) {
1146 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001147 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001148 }
1149 if (wcslen(wide) != length) {
1150 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001151 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001152 }
1153 path->wide = wide;
1154 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001155 path->cleanup = wo;
1156 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001157#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001158 path->wide = NULL;
1159 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001160 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001161 /* Still a reference owned by path->object, don't have to
1162 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001163 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001164 }
1165 else {
1166 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001167 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001168#endif
1169 path->fd = -1;
1170
1171 success_exit:
1172 path->length = length;
1173 path->object = o;
1174 return Py_CLEANUP_SUPPORTED;
1175
1176 error_exit:
1177 Py_XDECREF(o);
1178 Py_XDECREF(bytes);
1179#ifdef MS_WINDOWS
1180 Py_XDECREF(wo);
1181#endif
1182 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001183}
1184
1185static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001186argument_unavailable_error(const char *function_name, const char *argument_name)
1187{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001188 PyErr_Format(PyExc_NotImplementedError,
1189 "%s%s%s unavailable on this platform",
1190 (function_name != NULL) ? function_name : "",
1191 (function_name != NULL) ? ": ": "",
1192 argument_name);
1193}
1194
1195static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001196dir_fd_unavailable(PyObject *o, void *p)
1197{
1198 int dir_fd;
1199 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001200 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001201 if (dir_fd != DEFAULT_DIR_FD) {
1202 argument_unavailable_error(NULL, "dir_fd");
1203 return 0;
1204 }
1205 *(int *)p = dir_fd;
1206 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207}
1208
1209static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001210fd_specified(const char *function_name, int fd)
1211{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001212 if (fd == -1)
1213 return 0;
1214
1215 argument_unavailable_error(function_name, "fd");
1216 return 1;
1217}
1218
1219static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001220follow_symlinks_specified(const char *function_name, int follow_symlinks)
1221{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001222 if (follow_symlinks)
1223 return 0;
1224
1225 argument_unavailable_error(function_name, "follow_symlinks");
1226 return 1;
1227}
1228
1229static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001230path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1231{
Steve Dowercc16be82016-09-08 10:35:16 -07001232 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1233#ifndef MS_WINDOWS
1234 && !path->narrow
1235#endif
1236 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001237 PyErr_Format(PyExc_ValueError,
1238 "%s: can't specify dir_fd without matching path",
1239 function_name);
1240 return 1;
1241 }
1242 return 0;
1243}
1244
1245static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001246dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1247{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001248 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1249 PyErr_Format(PyExc_ValueError,
1250 "%s: can't specify both dir_fd and fd",
1251 function_name);
1252 return 1;
1253 }
1254 return 0;
1255}
1256
1257static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001258fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1259 int follow_symlinks)
1260{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001261 if ((fd > 0) && (!follow_symlinks)) {
1262 PyErr_Format(PyExc_ValueError,
1263 "%s: cannot use fd and follow_symlinks together",
1264 function_name);
1265 return 1;
1266 }
1267 return 0;
1268}
1269
1270static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001271dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1272 int follow_symlinks)
1273{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001274 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1275 PyErr_Format(PyExc_ValueError,
1276 "%s: cannot use dir_fd and follow_symlinks together",
1277 function_name);
1278 return 1;
1279 }
1280 return 0;
1281}
1282
Larry Hastings2f936352014-08-05 14:04:04 +10001283#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001284 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001285#else
Larry Hastings2f936352014-08-05 14:04:04 +10001286 typedef off_t Py_off_t;
1287#endif
1288
1289static int
1290Py_off_t_converter(PyObject *arg, void *addr)
1291{
1292#ifdef HAVE_LARGEFILE_SUPPORT
1293 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1294#else
1295 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001296#endif
1297 if (PyErr_Occurred())
1298 return 0;
1299 return 1;
1300}
Larry Hastings2f936352014-08-05 14:04:04 +10001301
1302static PyObject *
1303PyLong_FromPy_off_t(Py_off_t offset)
1304{
1305#ifdef HAVE_LARGEFILE_SUPPORT
1306 return PyLong_FromLongLong(offset);
1307#else
1308 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001309#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001310}
1311
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001312#ifdef HAVE_SIGSET_T
1313/* Convert an iterable of integers to a sigset.
1314 Return 1 on success, return 0 and raise an exception on error. */
1315int
1316_Py_Sigset_Converter(PyObject *obj, void *addr)
1317{
1318 sigset_t *mask = (sigset_t *)addr;
1319 PyObject *iterator, *item;
1320 long signum;
1321 int overflow;
1322
Rémi Lapeyref0900192019-05-04 01:30:53 +02001323 // The extra parens suppress the unreachable-code warning with clang on MacOS
1324 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001325 /* Probably only if mask == NULL. */
1326 PyErr_SetFromErrno(PyExc_OSError);
1327 return 0;
1328 }
1329
1330 iterator = PyObject_GetIter(obj);
1331 if (iterator == NULL) {
1332 return 0;
1333 }
1334
1335 while ((item = PyIter_Next(iterator)) != NULL) {
1336 signum = PyLong_AsLongAndOverflow(item, &overflow);
1337 Py_DECREF(item);
1338 if (signum <= 0 || signum >= NSIG) {
1339 if (overflow || signum != -1 || !PyErr_Occurred()) {
1340 PyErr_Format(PyExc_ValueError,
1341 "signal number %ld out of range", signum);
1342 }
1343 goto error;
1344 }
1345 if (sigaddset(mask, (int)signum)) {
1346 if (errno != EINVAL) {
1347 /* Probably impossible */
1348 PyErr_SetFromErrno(PyExc_OSError);
1349 goto error;
1350 }
1351 /* For backwards compatibility, allow idioms such as
1352 * `range(1, NSIG)` but warn about invalid signal numbers
1353 */
1354 const char msg[] =
1355 "invalid signal number %ld, please use valid_signals()";
1356 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1357 goto error;
1358 }
1359 }
1360 }
1361 if (!PyErr_Occurred()) {
1362 Py_DECREF(iterator);
1363 return 1;
1364 }
1365
1366error:
1367 Py_DECREF(iterator);
1368 return 0;
1369}
1370#endif /* HAVE_SIGSET_T */
1371
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001372#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001373
1374static int
Brian Curtind25aef52011-06-13 15:16:04 -05001375win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001376{
Martin Panter70214ad2016-08-04 02:38:59 +00001377 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1378 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001379 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001380
1381 if (0 == DeviceIoControl(
1382 reparse_point_handle,
1383 FSCTL_GET_REPARSE_POINT,
1384 NULL, 0, /* in buffer */
1385 target_buffer, sizeof(target_buffer),
1386 &n_bytes_returned,
1387 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001388 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001389
1390 if (reparse_tag)
1391 *reparse_tag = rdb->ReparseTag;
1392
Brian Curtind25aef52011-06-13 15:16:04 -05001393 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001394}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001395
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001396#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001397
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001399#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001400/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001401** environ directly, we must obtain it with _NSGetEnviron(). See also
1402** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001403*/
1404#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001405#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001406extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001407#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001408
Barry Warsaw53699e91996-12-10 23:23:01 +00001409static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001410convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001411{
Victor Stinner8c62be82010-05-06 00:08:46 +00001412 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001413#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001414 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001415#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001417#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001418
Victor Stinner8c62be82010-05-06 00:08:46 +00001419 d = PyDict_New();
1420 if (d == NULL)
1421 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001422#ifdef MS_WINDOWS
1423 /* _wenviron must be initialized in this way if the program is started
1424 through main() instead of wmain(). */
1425 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001426 e = _wenviron;
Benoit Hudson723f71a2019-12-06 14:15:03 -05001427#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1428 /* environ is not accessible as an extern in a shared object on OSX; use
1429 _NSGetEnviron to resolve it. The value changes if you add environment
1430 variables between calls to Py_Initialize, so don't cache the value. */
1431 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001432#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001433 e = environ;
1434#endif
1435 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001437 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001438 PyObject *k;
1439 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001440#ifdef MS_WINDOWS
1441 const wchar_t *p = wcschr(*e, L'=');
1442#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001443 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001444#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 if (p == NULL)
1446 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001447#ifdef MS_WINDOWS
1448 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1449#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001450 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001451#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001452 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001453 Py_DECREF(d);
1454 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001456#ifdef MS_WINDOWS
1457 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1458#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001459 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001460#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001461 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001463 Py_DECREF(d);
1464 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001465 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001466 if (PyDict_GetItemWithError(d, k) == NULL) {
1467 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1468 Py_DECREF(v);
1469 Py_DECREF(k);
1470 Py_DECREF(d);
1471 return NULL;
1472 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 }
1474 Py_DECREF(k);
1475 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001476 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001477 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001478}
1479
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001480/* Set a POSIX-specific error from errno, and return NULL */
1481
Barry Warsawd58d7641998-07-23 16:14:40 +00001482static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001483posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001484{
Victor Stinner8c62be82010-05-06 00:08:46 +00001485 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001486}
Mark Hammondef8b6542001-05-13 08:04:26 +00001487
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001488#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001489static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001490win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001491{
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 /* XXX We should pass the function name along in the future.
1493 (winreg.c also wants to pass the function name.)
1494 This would however require an additional param to the
1495 Windows error object, which is non-trivial.
1496 */
1497 errno = GetLastError();
1498 if (filename)
1499 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1500 else
1501 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001502}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001503
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001504static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001505win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001506{
1507 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001508 if (filename)
1509 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001510 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001511 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001512 filename);
1513 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001514 return PyErr_SetFromWindowsErr(err);
1515}
1516
1517static PyObject *
1518win32_error_object(const char* function, PyObject* filename)
1519{
1520 errno = GetLastError();
1521 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001522}
1523
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001524#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001525
Larry Hastings9cf065c2012-06-22 16:30:09 -07001526static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001527posix_path_object_error(PyObject *path)
1528{
1529 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1530}
1531
1532static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001533path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001534{
1535#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001536 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1537 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001538#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001539 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001540#endif
1541}
1542
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001543static PyObject *
1544path_object_error2(PyObject *path, PyObject *path2)
1545{
1546#ifdef MS_WINDOWS
1547 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1548 PyExc_OSError, 0, path, path2);
1549#else
1550 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1551#endif
1552}
1553
1554static PyObject *
1555path_error(path_t *path)
1556{
1557 return path_object_error(path->object);
1558}
Larry Hastings31826802013-10-19 00:09:25 -07001559
Larry Hastingsb0827312014-02-09 22:05:19 -08001560static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001561posix_path_error(path_t *path)
1562{
1563 return posix_path_object_error(path->object);
1564}
1565
1566static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001567path_error2(path_t *path, path_t *path2)
1568{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001569 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001570}
1571
1572
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001573/* POSIX generic methods */
1574
Larry Hastings2f936352014-08-05 14:04:04 +10001575static int
1576fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001577{
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001579 int *pointer = (int *)p;
1580 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001581 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001582 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001583 *pointer = fd;
1584 return 1;
1585}
1586
1587static PyObject *
1588posix_fildes_fd(int fd, int (*func)(int))
1589{
1590 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001591 int async_err = 0;
1592
1593 do {
1594 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001595 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001596 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001597 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001598 Py_END_ALLOW_THREADS
1599 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1600 if (res != 0)
1601 return (!async_err) ? posix_error() : NULL;
1602 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001603}
Guido van Rossum21142a01999-01-08 21:05:37 +00001604
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001605
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001606#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001607/* This is a reimplementation of the C library's chdir function,
1608 but one that produces Win32 errors instead of DOS error codes.
1609 chdir is essentially a wrapper around SetCurrentDirectory; however,
1610 it also needs to set "magic" environment variables indicating
1611 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001612static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001613win32_wchdir(LPCWSTR path)
1614{
Victor Stinnered537822015-12-13 21:40:26 +01001615 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 int result;
1617 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 if(!SetCurrentDirectoryW(path))
1620 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001621 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 if (!result)
1623 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001624 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001625 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001626 if (!new_path) {
1627 SetLastError(ERROR_OUTOFMEMORY);
1628 return FALSE;
1629 }
1630 result = GetCurrentDirectoryW(result, new_path);
1631 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001632 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001633 return FALSE;
1634 }
1635 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001636 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1637 wcsncmp(new_path, L"//", 2) == 0);
1638 if (!is_unc_like_path) {
1639 env[1] = new_path[0];
1640 result = SetEnvironmentVariableW(env, new_path);
1641 }
Victor Stinnered537822015-12-13 21:40:26 +01001642 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001643 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001644 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001645}
1646#endif
1647
Martin v. Löwis14694662006-02-03 12:54:16 +00001648#ifdef MS_WINDOWS
1649/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1650 - time stamps are restricted to second resolution
1651 - file modification times suffer from forth-and-back conversions between
1652 UTC and local time
1653 Therefore, we implement our own stat, based on the Win32 API directly.
1654*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001655#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001656#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001657#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001658
Victor Stinner6036e442015-03-08 01:58:04 +01001659static void
Steve Dowercc16be82016-09-08 10:35:16 -07001660find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1661 BY_HANDLE_FILE_INFORMATION *info,
1662 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001663{
1664 memset(info, 0, sizeof(*info));
1665 info->dwFileAttributes = pFileData->dwFileAttributes;
1666 info->ftCreationTime = pFileData->ftCreationTime;
1667 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1668 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1669 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1670 info->nFileSizeLow = pFileData->nFileSizeLow;
1671/* info->nNumberOfLinks = 1; */
1672 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1673 *reparse_tag = pFileData->dwReserved0;
1674 else
1675 *reparse_tag = 0;
1676}
1677
Guido van Rossumd8faa362007-04-27 19:54:29 +00001678static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001679attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001680{
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 HANDLE hFindFile;
1682 WIN32_FIND_DATAW FileData;
1683 hFindFile = FindFirstFileW(pszFile, &FileData);
1684 if (hFindFile == INVALID_HANDLE_VALUE)
1685 return FALSE;
1686 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001687 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001689}
1690
Brian Curtind25aef52011-06-13 15:16:04 -05001691static int
Steve Dowercc16be82016-09-08 10:35:16 -07001692win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001693 BOOL traverse)
1694{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001695 HANDLE hFile;
1696 BY_HANDLE_FILE_INFORMATION fileInfo;
1697 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1698 DWORD fileType, error;
1699 BOOL isUnhandledTag = FALSE;
1700 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001701
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001702 DWORD access = FILE_READ_ATTRIBUTES;
1703 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1704 if (!traverse) {
1705 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1706 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001707
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001708 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001709 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001710 /* Either the path doesn't exist, or the caller lacks access. */
1711 error = GetLastError();
1712 switch (error) {
1713 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1714 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1715 /* Try reading the parent directory. */
1716 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1717 /* Cannot read the parent directory. */
1718 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001719 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001720 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001721 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1722 if (traverse ||
1723 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1724 /* The stat call has to traverse but cannot, so fail. */
1725 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001726 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001727 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001728 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001729 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001730
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001731 case ERROR_INVALID_PARAMETER:
1732 /* \\.\con requires read or write access. */
1733 hFile = CreateFileW(path, access | GENERIC_READ,
1734 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1735 OPEN_EXISTING, flags, NULL);
1736 if (hFile == INVALID_HANDLE_VALUE) {
1737 SetLastError(error);
1738 return -1;
1739 }
1740 break;
1741
1742 case ERROR_CANT_ACCESS_FILE:
1743 /* bpo37834: open unhandled reparse points if traverse fails. */
1744 if (traverse) {
1745 traverse = FALSE;
1746 isUnhandledTag = TRUE;
1747 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1748 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1749 }
1750 if (hFile == INVALID_HANDLE_VALUE) {
1751 SetLastError(error);
1752 return -1;
1753 }
1754 break;
1755
1756 default:
1757 return -1;
1758 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001759 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001760
1761 if (hFile != INVALID_HANDLE_VALUE) {
1762 /* Handle types other than files on disk. */
1763 fileType = GetFileType(hFile);
1764 if (fileType != FILE_TYPE_DISK) {
1765 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1766 retval = -1;
1767 goto cleanup;
1768 }
1769 DWORD fileAttributes = GetFileAttributesW(path);
1770 memset(result, 0, sizeof(*result));
1771 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1772 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1773 /* \\.\pipe\ or \\.\mailslot\ */
1774 result->st_mode = _S_IFDIR;
1775 } else if (fileType == FILE_TYPE_CHAR) {
1776 /* \\.\nul */
1777 result->st_mode = _S_IFCHR;
1778 } else if (fileType == FILE_TYPE_PIPE) {
1779 /* \\.\pipe\spam */
1780 result->st_mode = _S_IFIFO;
1781 }
1782 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1783 goto cleanup;
1784 }
1785
1786 /* Query the reparse tag, and traverse a non-link. */
1787 if (!traverse) {
1788 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1789 &tagInfo, sizeof(tagInfo))) {
1790 /* Allow devices that do not support FileAttributeTagInfo. */
1791 switch (GetLastError()) {
1792 case ERROR_INVALID_PARAMETER:
1793 case ERROR_INVALID_FUNCTION:
1794 case ERROR_NOT_SUPPORTED:
1795 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1796 tagInfo.ReparseTag = 0;
1797 break;
1798 default:
1799 retval = -1;
1800 goto cleanup;
1801 }
1802 } else if (tagInfo.FileAttributes &
1803 FILE_ATTRIBUTE_REPARSE_POINT) {
1804 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1805 if (isUnhandledTag) {
1806 /* Traversing previously failed for either this link
1807 or its target. */
1808 SetLastError(ERROR_CANT_ACCESS_FILE);
1809 retval = -1;
1810 goto cleanup;
1811 }
1812 /* Traverse a non-link, but not if traversing already failed
1813 for an unhandled tag. */
1814 } else if (!isUnhandledTag) {
1815 CloseHandle(hFile);
1816 return win32_xstat_impl(path, result, TRUE);
1817 }
1818 }
1819 }
1820
1821 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1822 switch (GetLastError()) {
1823 case ERROR_INVALID_PARAMETER:
1824 case ERROR_INVALID_FUNCTION:
1825 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001826 /* Volumes and physical disks are block devices, e.g.
1827 \\.\C: and \\.\PhysicalDrive0. */
1828 memset(result, 0, sizeof(*result));
1829 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001830 goto cleanup;
1831 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001832 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001833 goto cleanup;
1834 }
1835 }
1836
1837 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1838
1839 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1840 /* Fix the file execute permissions. This hack sets S_IEXEC if
1841 the filename has an extension that is commonly used by files
1842 that CreateProcessW can execute. A real implementation calls
1843 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1844 AccessCheck to check for generic read, write, and execute
1845 access. */
1846 const wchar_t *fileExtension = wcsrchr(path, '.');
1847 if (fileExtension) {
1848 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1849 _wcsicmp(fileExtension, L".bat") == 0 ||
1850 _wcsicmp(fileExtension, L".cmd") == 0 ||
1851 _wcsicmp(fileExtension, L".com") == 0) {
1852 result->st_mode |= 0111;
1853 }
1854 }
1855 }
1856
1857cleanup:
1858 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07001859 /* Preserve last error if we are failing */
1860 error = retval ? GetLastError() : 0;
1861 if (!CloseHandle(hFile)) {
1862 retval = -1;
1863 } else if (retval) {
1864 /* Restore last error */
1865 SetLastError(error);
1866 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001867 }
1868
1869 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001870}
1871
1872static int
Steve Dowercc16be82016-09-08 10:35:16 -07001873win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001874{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001875 /* Protocol violation: we explicitly clear errno, instead of
1876 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001877 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001878 errno = 0;
1879 return code;
1880}
Brian Curtind25aef52011-06-13 15:16:04 -05001881/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001882
1883 In Posix, stat automatically traverses symlinks and returns the stat
1884 structure for the target. In Windows, the equivalent GetFileAttributes by
1885 default does not traverse symlinks and instead returns attributes for
1886 the symlink.
1887
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001888 Instead, we will open the file (which *does* traverse symlinks by default)
1889 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001890
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001891static int
Steve Dowercc16be82016-09-08 10:35:16 -07001892win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001893{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001894 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001895}
1896
Victor Stinner8c62be82010-05-06 00:08:46 +00001897static int
Steve Dowercc16be82016-09-08 10:35:16 -07001898win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001899{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001900 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001901}
1902
Martin v. Löwis14694662006-02-03 12:54:16 +00001903#endif /* MS_WINDOWS */
1904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001906"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001907This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001908 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001909or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1910\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001911Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1912or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001913\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001915
1916static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 {"st_mode", "protection bits"},
1918 {"st_ino", "inode"},
1919 {"st_dev", "device"},
1920 {"st_nlink", "number of hard links"},
1921 {"st_uid", "user ID of owner"},
1922 {"st_gid", "group ID of owner"},
1923 {"st_size", "total size, in bytes"},
1924 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1925 {NULL, "integer time of last access"},
1926 {NULL, "integer time of last modification"},
1927 {NULL, "integer time of last change"},
1928 {"st_atime", "time of last access"},
1929 {"st_mtime", "time of last modification"},
1930 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001931 {"st_atime_ns", "time of last access in nanoseconds"},
1932 {"st_mtime_ns", "time of last modification in nanoseconds"},
1933 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001934#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001936#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001937#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001939#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001940#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001943#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001945#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001946#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001948#endif
1949#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001950 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001951#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001952#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1953 {"st_file_attributes", "Windows file attribute bits"},
1954#endif
jcea6c51d512018-01-28 14:00:08 +01001955#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1956 {"st_fstype", "Type of filesystem"},
1957#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001958#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1959 {"st_reparse_tag", "Windows reparse tag"},
1960#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001962};
1963
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001964#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001965#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001966#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001967#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001968#endif
1969
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001970#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001971#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1972#else
1973#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1974#endif
1975
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001976#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001977#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1978#else
1979#define ST_RDEV_IDX ST_BLOCKS_IDX
1980#endif
1981
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001982#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1983#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1984#else
1985#define ST_FLAGS_IDX ST_RDEV_IDX
1986#endif
1987
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001988#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001989#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001990#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001991#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001992#endif
1993
1994#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1995#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1996#else
1997#define ST_BIRTHTIME_IDX ST_GEN_IDX
1998#endif
1999
Zachary Ware63f277b2014-06-19 09:46:37 -05002000#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2001#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
2002#else
2003#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2004#endif
2005
jcea6c51d512018-01-28 14:00:08 +01002006#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2007#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2008#else
2009#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2010#endif
2011
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002012#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2013#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2014#else
2015#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2016#endif
2017
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002018static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002019 "stat_result", /* name */
2020 stat_result__doc__, /* doc */
2021 stat_result_fields,
2022 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002023};
2024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002026"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2027This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002028 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002029or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002030\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002032
2033static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 {"f_bsize", },
2035 {"f_frsize", },
2036 {"f_blocks", },
2037 {"f_bfree", },
2038 {"f_bavail", },
2039 {"f_files", },
2040 {"f_ffree", },
2041 {"f_favail", },
2042 {"f_flag", },
2043 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002044 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002045 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002046};
2047
2048static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002049 "statvfs_result", /* name */
2050 statvfs_result__doc__, /* doc */
2051 statvfs_result_fields,
2052 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002053};
2054
Ross Lagerwall7807c352011-03-17 20:20:30 +02002055#if defined(HAVE_WAITID) && !defined(__APPLE__)
2056PyDoc_STRVAR(waitid_result__doc__,
2057"waitid_result: Result from waitid.\n\n\
2058This object may be accessed either as a tuple of\n\
2059 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2060or via the attributes si_pid, si_uid, and so on.\n\
2061\n\
2062See os.waitid for more information.");
2063
2064static PyStructSequence_Field waitid_result_fields[] = {
2065 {"si_pid", },
2066 {"si_uid", },
2067 {"si_signo", },
2068 {"si_status", },
2069 {"si_code", },
2070 {0}
2071};
2072
2073static PyStructSequence_Desc waitid_result_desc = {
2074 "waitid_result", /* name */
2075 waitid_result__doc__, /* doc */
2076 waitid_result_fields,
2077 5
2078};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002079#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002080static newfunc structseq_new;
2081
2082static PyObject *
2083statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2084{
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 PyStructSequence *result;
2086 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002087
Victor Stinner8c62be82010-05-06 00:08:46 +00002088 result = (PyStructSequence*)structseq_new(type, args, kwds);
2089 if (!result)
2090 return NULL;
2091 /* If we have been initialized from a tuple,
2092 st_?time might be set to None. Initialize it
2093 from the int slots. */
2094 for (i = 7; i <= 9; i++) {
2095 if (result->ob_item[i+3] == Py_None) {
2096 Py_DECREF(Py_None);
2097 Py_INCREF(result->ob_item[i]);
2098 result->ob_item[i+3] = result->ob_item[i];
2099 }
2100 }
2101 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002102}
2103
Eddie Elizondob3966632019-11-05 07:16:14 -08002104static int
2105_posix_clear(PyObject *module)
2106{
2107 Py_CLEAR(_posixstate(module)->billion);
2108 Py_CLEAR(_posixstate(module)->posix_putenv_garbage);
2109 Py_CLEAR(_posixstate(module)->DirEntryType);
2110 Py_CLEAR(_posixstate(module)->ScandirIteratorType);
2111#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2112 Py_CLEAR(_posixstate(module)->SchedParamType);
2113#endif
2114 Py_CLEAR(_posixstate(module)->StatResultType);
2115 Py_CLEAR(_posixstate(module)->StatVFSResultType);
2116 Py_CLEAR(_posixstate(module)->TerminalSizeType);
2117 Py_CLEAR(_posixstate(module)->TimesResultType);
2118 Py_CLEAR(_posixstate(module)->UnameResultType);
2119#if defined(HAVE_WAITID) && !defined(__APPLE__)
2120 Py_CLEAR(_posixstate(module)->WaitidResultType);
2121#endif
2122#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
2123 Py_CLEAR(_posixstate(module)->struct_rusage);
2124#endif
2125 Py_CLEAR(_posixstate(module)->st_mode);
2126 return 0;
2127}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002128
Eddie Elizondob3966632019-11-05 07:16:14 -08002129static int
2130_posix_traverse(PyObject *module, visitproc visit, void *arg)
2131{
2132 Py_VISIT(_posixstate(module)->billion);
2133 Py_VISIT(_posixstate(module)->posix_putenv_garbage);
2134 Py_VISIT(_posixstate(module)->DirEntryType);
2135 Py_VISIT(_posixstate(module)->ScandirIteratorType);
2136#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
2137 Py_VISIT(_posixstate(module)->SchedParamType);
2138#endif
2139 Py_VISIT(_posixstate(module)->StatResultType);
2140 Py_VISIT(_posixstate(module)->StatVFSResultType);
2141 Py_VISIT(_posixstate(module)->TerminalSizeType);
2142 Py_VISIT(_posixstate(module)->TimesResultType);
2143 Py_VISIT(_posixstate(module)->UnameResultType);
2144#if defined(HAVE_WAITID) && !defined(__APPLE__)
2145 Py_VISIT(_posixstate(module)->WaitidResultType);
2146#endif
2147#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
2148 Py_VISIT(_posixstate(module)->struct_rusage);
2149#endif
2150 Py_VISIT(_posixstate(module)->st_mode);
2151 return 0;
2152}
2153
2154static void
2155_posix_free(void *module)
2156{
2157 _posix_clear((PyObject *)module);
2158}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002159
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002160static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002161fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002162{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002163 PyObject *s = _PyLong_FromTime_t(sec);
2164 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2165 PyObject *s_in_ns = NULL;
2166 PyObject *ns_total = NULL;
2167 PyObject *float_s = NULL;
2168
2169 if (!(s && ns_fractional))
2170 goto exit;
2171
Eddie Elizondob3966632019-11-05 07:16:14 -08002172 s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002173 if (!s_in_ns)
2174 goto exit;
2175
2176 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2177 if (!ns_total)
2178 goto exit;
2179
Victor Stinner01b5aab2017-10-24 02:02:00 -07002180 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2181 if (!float_s) {
2182 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002183 }
2184
2185 PyStructSequence_SET_ITEM(v, index, s);
2186 PyStructSequence_SET_ITEM(v, index+3, float_s);
2187 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2188 s = NULL;
2189 float_s = NULL;
2190 ns_total = NULL;
2191exit:
2192 Py_XDECREF(s);
2193 Py_XDECREF(ns_fractional);
2194 Py_XDECREF(s_in_ns);
2195 Py_XDECREF(ns_total);
2196 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002197}
2198
Tim Peters5aa91602002-01-30 05:46:57 +00002199/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002200 (used by posix_stat() and posix_fstat()) */
2201static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002202_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002203{
Victor Stinner8c62be82010-05-06 00:08:46 +00002204 unsigned long ansec, mnsec, cnsec;
Eddie Elizondob3966632019-11-05 07:16:14 -08002205 PyObject *StatResultType = _posixstate_global->StatResultType;
2206 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002207 if (v == NULL)
2208 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002209
Victor Stinner8c62be82010-05-06 00:08:46 +00002210 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002211 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002212 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002213#ifdef MS_WINDOWS
2214 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002215#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002216 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002217#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002218 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002219#if defined(MS_WINDOWS)
2220 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2221 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2222#else
2223 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2224 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2225#endif
xdegaye50e86032017-05-22 11:15:08 +02002226 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2227 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002228
Martin v. Löwis14694662006-02-03 12:54:16 +00002229#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002230 ansec = st->st_atim.tv_nsec;
2231 mnsec = st->st_mtim.tv_nsec;
2232 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002233#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002234 ansec = st->st_atimespec.tv_nsec;
2235 mnsec = st->st_mtimespec.tv_nsec;
2236 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002237#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002238 ansec = st->st_atime_nsec;
2239 mnsec = st->st_mtime_nsec;
2240 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002241#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002242 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002243#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002244 fill_time(v, 7, st->st_atime, ansec);
2245 fill_time(v, 8, st->st_mtime, mnsec);
2246 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002247
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002248#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002249 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2250 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002251#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002252#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002253 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2254 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002255#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002256#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002257 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2258 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002259#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002260#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002261 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2262 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002263#endif
2264#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002265 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002266 PyObject *val;
2267 unsigned long bsec,bnsec;
2268 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002269#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002270 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002271#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002272 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002273#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002274 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002275 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2276 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002277 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002278#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002279#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002280 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2281 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002282#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002283#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2284 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2285 PyLong_FromUnsignedLong(st->st_file_attributes));
2286#endif
jcea6c51d512018-01-28 14:00:08 +01002287#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2288 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2289 PyUnicode_FromString(st->st_fstype));
2290#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002291#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2292 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2293 PyLong_FromUnsignedLong(st->st_reparse_tag));
2294#endif
Fred Drake699f3522000-06-29 21:12:41 +00002295
Victor Stinner8c62be82010-05-06 00:08:46 +00002296 if (PyErr_Occurred()) {
2297 Py_DECREF(v);
2298 return NULL;
2299 }
Fred Drake699f3522000-06-29 21:12:41 +00002300
Victor Stinner8c62be82010-05-06 00:08:46 +00002301 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002302}
2303
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002304/* POSIX methods */
2305
Guido van Rossum94f6f721999-01-06 18:42:14 +00002306
2307static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002308posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002309 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002310{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002311 STRUCT_STAT st;
2312 int result;
2313
2314#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2315 if (follow_symlinks_specified(function_name, follow_symlinks))
2316 return NULL;
2317#endif
2318
2319 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2320 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2321 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2322 return NULL;
2323
2324 Py_BEGIN_ALLOW_THREADS
2325 if (path->fd != -1)
2326 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002327#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002328 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002329 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002330 else
Steve Dowercc16be82016-09-08 10:35:16 -07002331 result = win32_lstat(path->wide, &st);
2332#else
2333 else
2334#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002335 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2336 result = LSTAT(path->narrow, &st);
2337 else
Steve Dowercc16be82016-09-08 10:35:16 -07002338#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002339#ifdef HAVE_FSTATAT
2340 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2341 result = fstatat(dir_fd, path->narrow, &st,
2342 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2343 else
Steve Dowercc16be82016-09-08 10:35:16 -07002344#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002345 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002346#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002347 Py_END_ALLOW_THREADS
2348
Victor Stinner292c8352012-10-30 02:17:38 +01002349 if (result != 0) {
2350 return path_error(path);
2351 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002352
2353 return _pystat_fromstructstat(&st);
2354}
2355
Larry Hastings2f936352014-08-05 14:04:04 +10002356/*[python input]
2357
2358for s in """
2359
2360FACCESSAT
2361FCHMODAT
2362FCHOWNAT
2363FSTATAT
2364LINKAT
2365MKDIRAT
2366MKFIFOAT
2367MKNODAT
2368OPENAT
2369READLINKAT
2370SYMLINKAT
2371UNLINKAT
2372
2373""".strip().split():
2374 s = s.strip()
2375 print("""
2376#ifdef HAVE_{s}
2377 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002378#else
Larry Hastings2f936352014-08-05 14:04:04 +10002379 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002380#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002381""".rstrip().format(s=s))
2382
2383for s in """
2384
2385FCHDIR
2386FCHMOD
2387FCHOWN
2388FDOPENDIR
2389FEXECVE
2390FPATHCONF
2391FSTATVFS
2392FTRUNCATE
2393
2394""".strip().split():
2395 s = s.strip()
2396 print("""
2397#ifdef HAVE_{s}
2398 #define PATH_HAVE_{s} 1
2399#else
2400 #define PATH_HAVE_{s} 0
2401#endif
2402
2403""".rstrip().format(s=s))
2404[python start generated code]*/
2405
2406#ifdef HAVE_FACCESSAT
2407 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2408#else
2409 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2410#endif
2411
2412#ifdef HAVE_FCHMODAT
2413 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2414#else
2415 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2416#endif
2417
2418#ifdef HAVE_FCHOWNAT
2419 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2420#else
2421 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2422#endif
2423
2424#ifdef HAVE_FSTATAT
2425 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2426#else
2427 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2428#endif
2429
2430#ifdef HAVE_LINKAT
2431 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2432#else
2433 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2434#endif
2435
2436#ifdef HAVE_MKDIRAT
2437 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2438#else
2439 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2440#endif
2441
2442#ifdef HAVE_MKFIFOAT
2443 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2444#else
2445 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2446#endif
2447
2448#ifdef HAVE_MKNODAT
2449 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2450#else
2451 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2452#endif
2453
2454#ifdef HAVE_OPENAT
2455 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2456#else
2457 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2458#endif
2459
2460#ifdef HAVE_READLINKAT
2461 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2462#else
2463 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2464#endif
2465
2466#ifdef HAVE_SYMLINKAT
2467 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2468#else
2469 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2470#endif
2471
2472#ifdef HAVE_UNLINKAT
2473 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2474#else
2475 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2476#endif
2477
2478#ifdef HAVE_FCHDIR
2479 #define PATH_HAVE_FCHDIR 1
2480#else
2481 #define PATH_HAVE_FCHDIR 0
2482#endif
2483
2484#ifdef HAVE_FCHMOD
2485 #define PATH_HAVE_FCHMOD 1
2486#else
2487 #define PATH_HAVE_FCHMOD 0
2488#endif
2489
2490#ifdef HAVE_FCHOWN
2491 #define PATH_HAVE_FCHOWN 1
2492#else
2493 #define PATH_HAVE_FCHOWN 0
2494#endif
2495
2496#ifdef HAVE_FDOPENDIR
2497 #define PATH_HAVE_FDOPENDIR 1
2498#else
2499 #define PATH_HAVE_FDOPENDIR 0
2500#endif
2501
2502#ifdef HAVE_FEXECVE
2503 #define PATH_HAVE_FEXECVE 1
2504#else
2505 #define PATH_HAVE_FEXECVE 0
2506#endif
2507
2508#ifdef HAVE_FPATHCONF
2509 #define PATH_HAVE_FPATHCONF 1
2510#else
2511 #define PATH_HAVE_FPATHCONF 0
2512#endif
2513
2514#ifdef HAVE_FSTATVFS
2515 #define PATH_HAVE_FSTATVFS 1
2516#else
2517 #define PATH_HAVE_FSTATVFS 0
2518#endif
2519
2520#ifdef HAVE_FTRUNCATE
2521 #define PATH_HAVE_FTRUNCATE 1
2522#else
2523 #define PATH_HAVE_FTRUNCATE 0
2524#endif
2525/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002526
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002527#ifdef MS_WINDOWS
2528 #undef PATH_HAVE_FTRUNCATE
2529 #define PATH_HAVE_FTRUNCATE 1
2530#endif
Larry Hastings31826802013-10-19 00:09:25 -07002531
Larry Hastings61272b72014-01-07 12:41:53 -08002532/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002533
2534class path_t_converter(CConverter):
2535
2536 type = "path_t"
2537 impl_by_reference = True
2538 parse_by_reference = True
2539
2540 converter = 'path_converter'
2541
2542 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002543 # right now path_t doesn't support default values.
2544 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002545 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002546 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002547
Larry Hastings2f936352014-08-05 14:04:04 +10002548 if self.c_default not in (None, 'Py_None'):
2549 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002550
2551 self.nullable = nullable
2552 self.allow_fd = allow_fd
2553
Larry Hastings7726ac92014-01-31 22:03:12 -08002554 def pre_render(self):
2555 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002556 if isinstance(value, str):
2557 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002558 return str(int(bool(value)))
2559
2560 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002561 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002562 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002563 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002564 strify(self.nullable),
2565 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002566 )
2567
2568 def cleanup(self):
2569 return "path_cleanup(&" + self.name + ");\n"
2570
2571
2572class dir_fd_converter(CConverter):
2573 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002574
Larry Hastings2f936352014-08-05 14:04:04 +10002575 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002576 if self.default in (unspecified, None):
2577 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002578 if isinstance(requires, str):
2579 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2580 else:
2581 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002582
Larry Hastings2f936352014-08-05 14:04:04 +10002583class fildes_converter(CConverter):
2584 type = 'int'
2585 converter = 'fildes_converter'
2586
2587class uid_t_converter(CConverter):
2588 type = "uid_t"
2589 converter = '_Py_Uid_Converter'
2590
2591class gid_t_converter(CConverter):
2592 type = "gid_t"
2593 converter = '_Py_Gid_Converter'
2594
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002595class dev_t_converter(CConverter):
2596 type = 'dev_t'
2597 converter = '_Py_Dev_Converter'
2598
2599class dev_t_return_converter(unsigned_long_return_converter):
2600 type = 'dev_t'
2601 conversion_fn = '_PyLong_FromDev'
2602 unsigned_cast = '(dev_t)'
2603
Larry Hastings2f936352014-08-05 14:04:04 +10002604class FSConverter_converter(CConverter):
2605 type = 'PyObject *'
2606 converter = 'PyUnicode_FSConverter'
2607 def converter_init(self):
2608 if self.default is not unspecified:
2609 fail("FSConverter_converter does not support default values")
2610 self.c_default = 'NULL'
2611
2612 def cleanup(self):
2613 return "Py_XDECREF(" + self.name + ");\n"
2614
2615class pid_t_converter(CConverter):
2616 type = 'pid_t'
2617 format_unit = '" _Py_PARSE_PID "'
2618
2619class idtype_t_converter(int_converter):
2620 type = 'idtype_t'
2621
2622class id_t_converter(CConverter):
2623 type = 'id_t'
2624 format_unit = '" _Py_PARSE_PID "'
2625
Benjamin Petersonca470632016-09-06 13:47:26 -07002626class intptr_t_converter(CConverter):
2627 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002628 format_unit = '" _Py_PARSE_INTPTR "'
2629
2630class Py_off_t_converter(CConverter):
2631 type = 'Py_off_t'
2632 converter = 'Py_off_t_converter'
2633
2634class Py_off_t_return_converter(long_return_converter):
2635 type = 'Py_off_t'
2636 conversion_fn = 'PyLong_FromPy_off_t'
2637
2638class path_confname_converter(CConverter):
2639 type="int"
2640 converter="conv_path_confname"
2641
2642class confstr_confname_converter(path_confname_converter):
2643 converter='conv_confstr_confname'
2644
2645class sysconf_confname_converter(path_confname_converter):
2646 converter="conv_sysconf_confname"
2647
2648class sched_param_converter(CConverter):
2649 type = 'struct sched_param'
2650 converter = 'convert_sched_param'
2651 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002652
Larry Hastings61272b72014-01-07 12:41:53 -08002653[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002654/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002655
Larry Hastings61272b72014-01-07 12:41:53 -08002656/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002657
Larry Hastings2a727912014-01-16 11:32:01 -08002658os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002659
2660 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002661 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002662 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002663
2664 *
2665
Larry Hastings2f936352014-08-05 14:04:04 +10002666 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002667 If not None, it should be a file descriptor open to a directory,
2668 and path should be a relative string; path will then be relative to
2669 that directory.
2670
2671 follow_symlinks: bool = True
2672 If False, and the last element of the path is a symbolic link,
2673 stat will examine the symbolic link itself instead of the file
2674 the link points to.
2675
2676Perform a stat system call on the given path.
2677
2678dir_fd and follow_symlinks may not be implemented
2679 on your platform. If they are unavailable, using them will raise a
2680 NotImplementedError.
2681
2682It's an error to use dir_fd or follow_symlinks when specifying path as
2683 an open file descriptor.
2684
Larry Hastings61272b72014-01-07 12:41:53 -08002685[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002686
Larry Hastings31826802013-10-19 00:09:25 -07002687static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002688os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002689/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002690{
2691 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2692}
2693
Larry Hastings2f936352014-08-05 14:04:04 +10002694
2695/*[clinic input]
2696os.lstat
2697
2698 path : path_t
2699
2700 *
2701
2702 dir_fd : dir_fd(requires='fstatat') = None
2703
2704Perform a stat system call on the given path, without following symbolic links.
2705
2706Like stat(), but do not follow symbolic links.
2707Equivalent to stat(path, follow_symlinks=False).
2708[clinic start generated code]*/
2709
Larry Hastings2f936352014-08-05 14:04:04 +10002710static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002711os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2712/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002713{
2714 int follow_symlinks = 0;
2715 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2716}
Larry Hastings31826802013-10-19 00:09:25 -07002717
Larry Hastings2f936352014-08-05 14:04:04 +10002718
Larry Hastings61272b72014-01-07 12:41:53 -08002719/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002720os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002721
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002722 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002723 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002724
2725 mode: int
2726 Operating-system mode bitfield. Can be F_OK to test existence,
2727 or the inclusive-OR of R_OK, W_OK, and X_OK.
2728
2729 *
2730
Larry Hastings2f936352014-08-05 14:04:04 +10002731 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002732 If not None, it should be a file descriptor open to a directory,
2733 and path should be relative; path will then be relative to that
2734 directory.
2735
2736 effective_ids: bool = False
2737 If True, access will use the effective uid/gid instead of
2738 the real uid/gid.
2739
2740 follow_symlinks: bool = True
2741 If False, and the last element of the path is a symbolic link,
2742 access will examine the symbolic link itself instead of the file
2743 the link points to.
2744
2745Use the real uid/gid to test for access to a path.
2746
2747{parameters}
2748dir_fd, effective_ids, and follow_symlinks may not be implemented
2749 on your platform. If they are unavailable, using them will raise a
2750 NotImplementedError.
2751
2752Note that most operations will use the effective uid/gid, therefore this
2753 routine can be used in a suid/sgid environment to test if the invoking user
2754 has the specified access to the path.
2755
Larry Hastings61272b72014-01-07 12:41:53 -08002756[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002757
Larry Hastings2f936352014-08-05 14:04:04 +10002758static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002759os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002760 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002761/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002762{
Larry Hastings2f936352014-08-05 14:04:04 +10002763 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002764
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002765#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002766 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002767#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002768 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002769#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002770
Larry Hastings9cf065c2012-06-22 16:30:09 -07002771#ifndef HAVE_FACCESSAT
2772 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002773 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002774
2775 if (effective_ids) {
2776 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002777 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002778 }
2779#endif
2780
2781#ifdef MS_WINDOWS
2782 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002783 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002784 Py_END_ALLOW_THREADS
2785
2786 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002787 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002788 * * we didn't get a -1, and
2789 * * write access wasn't requested,
2790 * * or the file isn't read-only,
2791 * * or it's a directory.
2792 * (Directories cannot be read-only on Windows.)
2793 */
Larry Hastings2f936352014-08-05 14:04:04 +10002794 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002795 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002796 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002797 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002798#else
2799
2800 Py_BEGIN_ALLOW_THREADS
2801#ifdef HAVE_FACCESSAT
2802 if ((dir_fd != DEFAULT_DIR_FD) ||
2803 effective_ids ||
2804 !follow_symlinks) {
2805 int flags = 0;
2806 if (!follow_symlinks)
2807 flags |= AT_SYMLINK_NOFOLLOW;
2808 if (effective_ids)
2809 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002810 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002811 }
2812 else
2813#endif
Larry Hastings31826802013-10-19 00:09:25 -07002814 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002815 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002816 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002817#endif
2818
Larry Hastings9cf065c2012-06-22 16:30:09 -07002819 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002820}
2821
Guido van Rossumd371ff11999-01-25 16:12:23 +00002822#ifndef F_OK
2823#define F_OK 0
2824#endif
2825#ifndef R_OK
2826#define R_OK 4
2827#endif
2828#ifndef W_OK
2829#define W_OK 2
2830#endif
2831#ifndef X_OK
2832#define X_OK 1
2833#endif
2834
Larry Hastings31826802013-10-19 00:09:25 -07002835
Guido van Rossumd371ff11999-01-25 16:12:23 +00002836#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002837/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002838os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002839
2840 fd: int
2841 Integer file descriptor handle.
2842
2843 /
2844
2845Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002846[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002847
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002848static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002849os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002850/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002851{
Guido van Rossum94f6f721999-01-06 18:42:14 +00002852
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002853 long size = sysconf(_SC_TTY_NAME_MAX);
2854 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002855 return posix_error();
2856 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002857 char *buffer = (char *)PyMem_RawMalloc(size);
2858 if (buffer == NULL) {
2859 return PyErr_NoMemory();
2860 }
2861 int ret = ttyname_r(fd, buffer, size);
2862 if (ret != 0) {
2863 PyMem_RawFree(buffer);
2864 errno = ret;
2865 return posix_error();
2866 }
2867 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
2868 PyMem_RawFree(buffer);
2869 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002870}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002871#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002872
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002873#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002874/*[clinic input]
2875os.ctermid
2876
2877Return the name of the controlling terminal for this process.
2878[clinic start generated code]*/
2879
Larry Hastings2f936352014-08-05 14:04:04 +10002880static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002881os_ctermid_impl(PyObject *module)
2882/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002883{
Victor Stinner8c62be82010-05-06 00:08:46 +00002884 char *ret;
2885 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002886
Greg Wardb48bc172000-03-01 21:51:56 +00002887#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002888 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002889#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002890 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002891#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002892 if (ret == NULL)
2893 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002894 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002895}
Larry Hastings2f936352014-08-05 14:04:04 +10002896#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002897
Larry Hastings2f936352014-08-05 14:04:04 +10002898
2899/*[clinic input]
2900os.chdir
2901
2902 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2903
2904Change the current working directory to the specified path.
2905
2906path may always be specified as a string.
2907On some platforms, path may also be specified as an open file descriptor.
2908 If this functionality is unavailable, using it raises an exception.
2909[clinic start generated code]*/
2910
Larry Hastings2f936352014-08-05 14:04:04 +10002911static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002912os_chdir_impl(PyObject *module, path_t *path)
2913/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002914{
2915 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002916
2917 Py_BEGIN_ALLOW_THREADS
2918#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002919 /* on unix, success = 0, on windows, success = !0 */
2920 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002921#else
2922#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002923 if (path->fd != -1)
2924 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002925 else
2926#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002927 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002928#endif
2929 Py_END_ALLOW_THREADS
2930
2931 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002932 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002933 }
2934
Larry Hastings2f936352014-08-05 14:04:04 +10002935 Py_RETURN_NONE;
2936}
2937
2938
2939#ifdef HAVE_FCHDIR
2940/*[clinic input]
2941os.fchdir
2942
2943 fd: fildes
2944
2945Change to the directory of the given file descriptor.
2946
2947fd must be opened on a directory, not a file.
2948Equivalent to os.chdir(fd).
2949
2950[clinic start generated code]*/
2951
Fred Drake4d1e64b2002-04-15 19:40:07 +00002952static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002953os_fchdir_impl(PyObject *module, int fd)
2954/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002955{
Larry Hastings2f936352014-08-05 14:04:04 +10002956 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002957}
2958#endif /* HAVE_FCHDIR */
2959
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002960
Larry Hastings2f936352014-08-05 14:04:04 +10002961/*[clinic input]
2962os.chmod
2963
2964 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002965 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002966 On some platforms, path may also be specified as an open file descriptor.
2967 If this functionality is unavailable, using it raises an exception.
2968
2969 mode: int
2970 Operating-system mode bitfield.
2971
2972 *
2973
2974 dir_fd : dir_fd(requires='fchmodat') = None
2975 If not None, it should be a file descriptor open to a directory,
2976 and path should be relative; path will then be relative to that
2977 directory.
2978
2979 follow_symlinks: bool = True
2980 If False, and the last element of the path is a symbolic link,
2981 chmod will modify the symbolic link itself instead of the file
2982 the link points to.
2983
2984Change the access permissions of a file.
2985
2986It is an error to use dir_fd or follow_symlinks when specifying path as
2987 an open file descriptor.
2988dir_fd and follow_symlinks may not be implemented on your platform.
2989 If they are unavailable, using them will raise a NotImplementedError.
2990
2991[clinic start generated code]*/
2992
Larry Hastings2f936352014-08-05 14:04:04 +10002993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002994os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002995 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002996/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002997{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002998 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002999
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003000#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003001 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003002#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003003
Larry Hastings9cf065c2012-06-22 16:30:09 -07003004#ifdef HAVE_FCHMODAT
3005 int fchmodat_nofollow_unsupported = 0;
3006#endif
3007
Larry Hastings9cf065c2012-06-22 16:30:09 -07003008#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3009 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003010 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003011#endif
3012
3013#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003014 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003015 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003016 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003017 result = 0;
3018 else {
3019 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003020 attr &= ~FILE_ATTRIBUTE_READONLY;
3021 else
3022 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003023 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003024 }
3025 Py_END_ALLOW_THREADS
3026
3027 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003028 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003029 }
3030#else /* MS_WINDOWS */
3031 Py_BEGIN_ALLOW_THREADS
3032#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003033 if (path->fd != -1)
3034 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003035 else
3036#endif
3037#ifdef HAVE_LCHMOD
3038 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003039 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003040 else
3041#endif
3042#ifdef HAVE_FCHMODAT
3043 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
3044 /*
3045 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3046 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003047 * and then says it isn't implemented yet.
3048 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003049 *
3050 * Once it is supported, os.chmod will automatically
3051 * support dir_fd and follow_symlinks=False. (Hopefully.)
3052 * Until then, we need to be careful what exception we raise.
3053 */
Larry Hastings2f936352014-08-05 14:04:04 +10003054 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003055 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3056 /*
3057 * But wait! We can't throw the exception without allowing threads,
3058 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3059 */
3060 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003061 result &&
3062 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3063 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00003064 }
3065 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00003066#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003067 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003068 Py_END_ALLOW_THREADS
3069
3070 if (result) {
3071#ifdef HAVE_FCHMODAT
3072 if (fchmodat_nofollow_unsupported) {
3073 if (dir_fd != DEFAULT_DIR_FD)
3074 dir_fd_and_follow_symlinks_invalid("chmod",
3075 dir_fd, follow_symlinks);
3076 else
3077 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003078 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003079 }
3080 else
3081#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003082 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003083 }
3084#endif
3085
Larry Hastings2f936352014-08-05 14:04:04 +10003086 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003087}
3088
Larry Hastings9cf065c2012-06-22 16:30:09 -07003089
Christian Heimes4e30a842007-11-30 22:12:06 +00003090#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003091/*[clinic input]
3092os.fchmod
3093
3094 fd: int
3095 mode: int
3096
3097Change the access permissions of the file given by file descriptor fd.
3098
3099Equivalent to os.chmod(fd, mode).
3100[clinic start generated code]*/
3101
Larry Hastings2f936352014-08-05 14:04:04 +10003102static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003103os_fchmod_impl(PyObject *module, int fd, int mode)
3104/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003105{
3106 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003107 int async_err = 0;
3108
3109 do {
3110 Py_BEGIN_ALLOW_THREADS
3111 res = fchmod(fd, mode);
3112 Py_END_ALLOW_THREADS
3113 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3114 if (res != 0)
3115 return (!async_err) ? posix_error() : NULL;
3116
Victor Stinner8c62be82010-05-06 00:08:46 +00003117 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003118}
3119#endif /* HAVE_FCHMOD */
3120
Larry Hastings2f936352014-08-05 14:04:04 +10003121
Christian Heimes4e30a842007-11-30 22:12:06 +00003122#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003123/*[clinic input]
3124os.lchmod
3125
3126 path: path_t
3127 mode: int
3128
3129Change the access permissions of a file, without following symbolic links.
3130
3131If path is a symlink, this affects the link itself rather than the target.
3132Equivalent to chmod(path, mode, follow_symlinks=False)."
3133[clinic start generated code]*/
3134
Larry Hastings2f936352014-08-05 14:04:04 +10003135static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003136os_lchmod_impl(PyObject *module, path_t *path, int mode)
3137/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003138{
Victor Stinner8c62be82010-05-06 00:08:46 +00003139 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003140 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003141 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003142 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003143 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003144 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003145 return NULL;
3146 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003147 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003148}
3149#endif /* HAVE_LCHMOD */
3150
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003151
Thomas Wouterscf297e42007-02-23 15:07:44 +00003152#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003153/*[clinic input]
3154os.chflags
3155
3156 path: path_t
3157 flags: unsigned_long(bitwise=True)
3158 follow_symlinks: bool=True
3159
3160Set file flags.
3161
3162If follow_symlinks is False, and the last element of the path is a symbolic
3163 link, chflags will change flags on the symbolic link itself instead of the
3164 file the link points to.
3165follow_symlinks may not be implemented on your platform. If it is
3166unavailable, using it will raise a NotImplementedError.
3167
3168[clinic start generated code]*/
3169
Larry Hastings2f936352014-08-05 14:04:04 +10003170static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003171os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003172 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003173/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003174{
3175 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003176
3177#ifndef HAVE_LCHFLAGS
3178 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003179 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003180#endif
3181
Victor Stinner8c62be82010-05-06 00:08:46 +00003182 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003183#ifdef HAVE_LCHFLAGS
3184 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003185 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003186 else
3187#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003188 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003189 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003190
Larry Hastings2f936352014-08-05 14:04:04 +10003191 if (result)
3192 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003193
Larry Hastings2f936352014-08-05 14:04:04 +10003194 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003195}
3196#endif /* HAVE_CHFLAGS */
3197
Larry Hastings2f936352014-08-05 14:04:04 +10003198
Thomas Wouterscf297e42007-02-23 15:07:44 +00003199#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003200/*[clinic input]
3201os.lchflags
3202
3203 path: path_t
3204 flags: unsigned_long(bitwise=True)
3205
3206Set file flags.
3207
3208This function will not follow symbolic links.
3209Equivalent to chflags(path, flags, follow_symlinks=False).
3210[clinic start generated code]*/
3211
Larry Hastings2f936352014-08-05 14:04:04 +10003212static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003213os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3214/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003215{
Victor Stinner8c62be82010-05-06 00:08:46 +00003216 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003217 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003218 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003219 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003220 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003221 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003222 }
Victor Stinner292c8352012-10-30 02:17:38 +01003223 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003224}
3225#endif /* HAVE_LCHFLAGS */
3226
Larry Hastings2f936352014-08-05 14:04:04 +10003227
Martin v. Löwis244edc82001-10-04 22:44:26 +00003228#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003229/*[clinic input]
3230os.chroot
3231 path: path_t
3232
3233Change root directory to path.
3234
3235[clinic start generated code]*/
3236
Larry Hastings2f936352014-08-05 14:04:04 +10003237static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003238os_chroot_impl(PyObject *module, path_t *path)
3239/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003240{
3241 int res;
3242 Py_BEGIN_ALLOW_THREADS
3243 res = chroot(path->narrow);
3244 Py_END_ALLOW_THREADS
3245 if (res < 0)
3246 return path_error(path);
3247 Py_RETURN_NONE;
3248}
3249#endif /* HAVE_CHROOT */
3250
Martin v. Löwis244edc82001-10-04 22:44:26 +00003251
Guido van Rossum21142a01999-01-08 21:05:37 +00003252#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003253/*[clinic input]
3254os.fsync
3255
3256 fd: fildes
3257
3258Force write of fd to disk.
3259[clinic start generated code]*/
3260
Larry Hastings2f936352014-08-05 14:04:04 +10003261static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003262os_fsync_impl(PyObject *module, int fd)
3263/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003264{
3265 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003266}
3267#endif /* HAVE_FSYNC */
3268
Larry Hastings2f936352014-08-05 14:04:04 +10003269
Ross Lagerwall7807c352011-03-17 20:20:30 +02003270#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003271/*[clinic input]
3272os.sync
3273
3274Force write of everything to disk.
3275[clinic start generated code]*/
3276
Larry Hastings2f936352014-08-05 14:04:04 +10003277static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003278os_sync_impl(PyObject *module)
3279/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003280{
3281 Py_BEGIN_ALLOW_THREADS
3282 sync();
3283 Py_END_ALLOW_THREADS
3284 Py_RETURN_NONE;
3285}
Larry Hastings2f936352014-08-05 14:04:04 +10003286#endif /* HAVE_SYNC */
3287
Ross Lagerwall7807c352011-03-17 20:20:30 +02003288
Guido van Rossum21142a01999-01-08 21:05:37 +00003289#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003290#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003291extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3292#endif
3293
Larry Hastings2f936352014-08-05 14:04:04 +10003294/*[clinic input]
3295os.fdatasync
3296
3297 fd: fildes
3298
3299Force write of fd to disk without forcing update of metadata.
3300[clinic start generated code]*/
3301
Larry Hastings2f936352014-08-05 14:04:04 +10003302static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003303os_fdatasync_impl(PyObject *module, int fd)
3304/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003305{
3306 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003307}
3308#endif /* HAVE_FDATASYNC */
3309
3310
Fredrik Lundh10723342000-07-10 16:38:09 +00003311#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003312/*[clinic input]
3313os.chown
3314
3315 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003316 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003317
3318 uid: uid_t
3319
3320 gid: gid_t
3321
3322 *
3323
3324 dir_fd : dir_fd(requires='fchownat') = None
3325 If not None, it should be a file descriptor open to a directory,
3326 and path should be relative; path will then be relative to that
3327 directory.
3328
3329 follow_symlinks: bool = True
3330 If False, and the last element of the path is a symbolic link,
3331 stat will examine the symbolic link itself instead of the file
3332 the link points to.
3333
3334Change the owner and group id of path to the numeric uid and gid.\
3335
3336path may always be specified as a string.
3337On some platforms, path may also be specified as an open file descriptor.
3338 If this functionality is unavailable, using it raises an exception.
3339If dir_fd is not None, it should be a file descriptor open to a directory,
3340 and path should be relative; path will then be relative to that directory.
3341If follow_symlinks is False, and the last element of the path is a symbolic
3342 link, chown will modify the symbolic link itself instead of the file the
3343 link points to.
3344It is an error to use dir_fd or follow_symlinks when specifying path as
3345 an open file descriptor.
3346dir_fd and follow_symlinks may not be implemented on your platform.
3347 If they are unavailable, using them will raise a NotImplementedError.
3348
3349[clinic start generated code]*/
3350
Larry Hastings2f936352014-08-05 14:04:04 +10003351static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003352os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003353 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003354/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003355{
3356 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003357
3358#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3359 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003360 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003361#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003362 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3363 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3364 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003365
3366#ifdef __APPLE__
3367 /*
3368 * This is for Mac OS X 10.3, which doesn't have lchown.
3369 * (But we still have an lchown symbol because of weak-linking.)
3370 * It doesn't have fchownat either. So there's no possibility
3371 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003372 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003373 if ((!follow_symlinks) && (lchown == NULL)) {
3374 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003375 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003376 }
3377#endif
3378
Victor Stinner8c62be82010-05-06 00:08:46 +00003379 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003380#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003381 if (path->fd != -1)
3382 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003383 else
3384#endif
3385#ifdef HAVE_LCHOWN
3386 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003387 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003388 else
3389#endif
3390#ifdef HAVE_FCHOWNAT
3391 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003392 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003393 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3394 else
3395#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003396 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003397 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003398
Larry Hastings2f936352014-08-05 14:04:04 +10003399 if (result)
3400 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003401
Larry Hastings2f936352014-08-05 14:04:04 +10003402 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003403}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003404#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003405
Larry Hastings2f936352014-08-05 14:04:04 +10003406
Christian Heimes4e30a842007-11-30 22:12:06 +00003407#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003408/*[clinic input]
3409os.fchown
3410
3411 fd: int
3412 uid: uid_t
3413 gid: gid_t
3414
3415Change the owner and group id of the file specified by file descriptor.
3416
3417Equivalent to os.chown(fd, uid, gid).
3418
3419[clinic start generated code]*/
3420
Larry Hastings2f936352014-08-05 14:04:04 +10003421static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003422os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3423/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003424{
Victor Stinner8c62be82010-05-06 00:08:46 +00003425 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003426 int async_err = 0;
3427
3428 do {
3429 Py_BEGIN_ALLOW_THREADS
3430 res = fchown(fd, uid, gid);
3431 Py_END_ALLOW_THREADS
3432 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3433 if (res != 0)
3434 return (!async_err) ? posix_error() : NULL;
3435
Victor Stinner8c62be82010-05-06 00:08:46 +00003436 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003437}
3438#endif /* HAVE_FCHOWN */
3439
Larry Hastings2f936352014-08-05 14:04:04 +10003440
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003441#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003442/*[clinic input]
3443os.lchown
3444
3445 path : path_t
3446 uid: uid_t
3447 gid: gid_t
3448
3449Change the owner and group id of path to the numeric uid and gid.
3450
3451This function will not follow symbolic links.
3452Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3453[clinic start generated code]*/
3454
Larry Hastings2f936352014-08-05 14:04:04 +10003455static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003456os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3457/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003458{
Victor Stinner8c62be82010-05-06 00:08:46 +00003459 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003460 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003461 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003462 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003463 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003464 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003465 }
Larry Hastings2f936352014-08-05 14:04:04 +10003466 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003467}
3468#endif /* HAVE_LCHOWN */
3469
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003470
Barry Warsaw53699e91996-12-10 23:23:01 +00003471static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003472posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003473{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003474#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003475 wchar_t wbuf[MAXPATHLEN];
3476 wchar_t *wbuf2 = wbuf;
3477 DWORD len;
3478
3479 Py_BEGIN_ALLOW_THREADS
3480 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3481 /* If the buffer is large enough, len does not include the
3482 terminating \0. If the buffer is too small, len includes
3483 the space needed for the terminator. */
3484 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003485 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003486 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003487 }
Victor Stinner689830e2019-06-26 17:31:12 +02003488 else {
3489 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003490 }
Victor Stinner689830e2019-06-26 17:31:12 +02003491 if (wbuf2) {
3492 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003493 }
Victor Stinner689830e2019-06-26 17:31:12 +02003494 }
3495 Py_END_ALLOW_THREADS
3496
3497 if (!wbuf2) {
3498 PyErr_NoMemory();
3499 return NULL;
3500 }
3501 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003502 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003503 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003504 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003506
Victor Stinner689830e2019-06-26 17:31:12 +02003507 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3508 if (wbuf2 != wbuf) {
3509 PyMem_RawFree(wbuf2);
3510 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003511
Victor Stinner689830e2019-06-26 17:31:12 +02003512 if (use_bytes) {
3513 if (resobj == NULL) {
3514 return NULL;
3515 }
3516 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3517 }
3518
3519 return resobj;
3520#else
3521 const size_t chunk = 1024;
3522
3523 char *buf = NULL;
3524 char *cwd = NULL;
3525 size_t buflen = 0;
3526
Victor Stinner8c62be82010-05-06 00:08:46 +00003527 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003528 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003529 char *newbuf;
3530 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3531 buflen += chunk;
3532 newbuf = PyMem_RawRealloc(buf, buflen);
3533 }
3534 else {
3535 newbuf = NULL;
3536 }
3537 if (newbuf == NULL) {
3538 PyMem_RawFree(buf);
3539 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003540 break;
3541 }
Victor Stinner689830e2019-06-26 17:31:12 +02003542 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003543
Victor Stinner4403d7d2015-04-25 00:16:10 +02003544 cwd = getcwd(buf, buflen);
3545 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003546 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003547
Victor Stinner689830e2019-06-26 17:31:12 +02003548 if (buf == NULL) {
3549 return PyErr_NoMemory();
3550 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003551 if (cwd == NULL) {
3552 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003553 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003554 }
3555
Victor Stinner689830e2019-06-26 17:31:12 +02003556 PyObject *obj;
3557 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003558 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003559 }
3560 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003561 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003562 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003563 PyMem_RawFree(buf);
3564
3565 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003566#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003567}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003568
Larry Hastings2f936352014-08-05 14:04:04 +10003569
3570/*[clinic input]
3571os.getcwd
3572
3573Return a unicode string representing the current working directory.
3574[clinic start generated code]*/
3575
Larry Hastings2f936352014-08-05 14:04:04 +10003576static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003577os_getcwd_impl(PyObject *module)
3578/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003579{
3580 return posix_getcwd(0);
3581}
3582
Larry Hastings2f936352014-08-05 14:04:04 +10003583
3584/*[clinic input]
3585os.getcwdb
3586
3587Return a bytes string representing the current working directory.
3588[clinic start generated code]*/
3589
Larry Hastings2f936352014-08-05 14:04:04 +10003590static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003591os_getcwdb_impl(PyObject *module)
3592/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003593{
3594 return posix_getcwd(1);
3595}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003596
Larry Hastings2f936352014-08-05 14:04:04 +10003597
Larry Hastings9cf065c2012-06-22 16:30:09 -07003598#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3599#define HAVE_LINK 1
3600#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003601
Guido van Rossumb6775db1994-08-01 11:34:53 +00003602#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003603/*[clinic input]
3604
3605os.link
3606
3607 src : path_t
3608 dst : path_t
3609 *
3610 src_dir_fd : dir_fd = None
3611 dst_dir_fd : dir_fd = None
3612 follow_symlinks: bool = True
3613
3614Create a hard link to a file.
3615
3616If either src_dir_fd or dst_dir_fd is not None, it should be a file
3617 descriptor open to a directory, and the respective path string (src or dst)
3618 should be relative; the path will then be relative to that directory.
3619If follow_symlinks is False, and the last element of src is a symbolic
3620 link, link will create a link to the symbolic link itself instead of the
3621 file the link points to.
3622src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3623 platform. If they are unavailable, using them will raise a
3624 NotImplementedError.
3625[clinic start generated code]*/
3626
Larry Hastings2f936352014-08-05 14:04:04 +10003627static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003628os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003629 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003630/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003631{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003632#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003633 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003634#else
3635 int result;
3636#endif
3637
Larry Hastings9cf065c2012-06-22 16:30:09 -07003638#ifndef HAVE_LINKAT
3639 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3640 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003641 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003642 }
3643#endif
3644
Steve Dowercc16be82016-09-08 10:35:16 -07003645#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003646 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003647 PyErr_SetString(PyExc_NotImplementedError,
3648 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003649 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003650 }
Steve Dowercc16be82016-09-08 10:35:16 -07003651#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003652
Brian Curtin1b9df392010-11-24 20:24:31 +00003653#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003654 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003655 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003656 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003657
Larry Hastings2f936352014-08-05 14:04:04 +10003658 if (!result)
3659 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003660#else
3661 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003662#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003663 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3664 (dst_dir_fd != DEFAULT_DIR_FD) ||
3665 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003666 result = linkat(src_dir_fd, src->narrow,
3667 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003668 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3669 else
Steve Dowercc16be82016-09-08 10:35:16 -07003670#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003671 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003672 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003673
Larry Hastings2f936352014-08-05 14:04:04 +10003674 if (result)
3675 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003676#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003677
Larry Hastings2f936352014-08-05 14:04:04 +10003678 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003679}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003680#endif
3681
Brian Curtin1b9df392010-11-24 20:24:31 +00003682
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003683#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003684static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003685_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003686{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003687 PyObject *v;
3688 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3689 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003690 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003691 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003692 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003693 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003694
Steve Dowercc16be82016-09-08 10:35:16 -07003695 WIN32_FIND_DATAW wFileData;
3696 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003697
Steve Dowercc16be82016-09-08 10:35:16 -07003698 if (!path->wide) { /* Default arg: "." */
3699 po_wchars = L".";
3700 len = 1;
3701 } else {
3702 po_wchars = path->wide;
3703 len = wcslen(path->wide);
3704 }
3705 /* The +5 is so we can append "\\*.*\0" */
3706 wnamebuf = PyMem_New(wchar_t, len + 5);
3707 if (!wnamebuf) {
3708 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003709 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 }
Steve Dowercc16be82016-09-08 10:35:16 -07003711 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003713 wchar_t wch = wnamebuf[len-1];
3714 if (wch != SEP && wch != ALTSEP && wch != L':')
3715 wnamebuf[len++] = SEP;
3716 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003717 }
Steve Dowercc16be82016-09-08 10:35:16 -07003718 if ((list = PyList_New(0)) == NULL) {
3719 goto exit;
3720 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003721 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003722 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003723 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003724 if (hFindFile == INVALID_HANDLE_VALUE) {
3725 int error = GetLastError();
3726 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003727 goto exit;
3728 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003729 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003730 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003731 }
3732 do {
3733 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003734 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3735 wcscmp(wFileData.cFileName, L"..") != 0) {
3736 v = PyUnicode_FromWideChar(wFileData.cFileName,
3737 wcslen(wFileData.cFileName));
3738 if (path->narrow && v) {
3739 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3740 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003741 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003742 Py_DECREF(list);
3743 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003744 break;
3745 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003746 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003747 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003748 Py_DECREF(list);
3749 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003750 break;
3751 }
3752 Py_DECREF(v);
3753 }
3754 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003755 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003756 Py_END_ALLOW_THREADS
3757 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3758 it got to the end of the directory. */
3759 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003760 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003761 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003762 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003763 }
3764 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003765
Larry Hastings9cf065c2012-06-22 16:30:09 -07003766exit:
3767 if (hFindFile != INVALID_HANDLE_VALUE) {
3768 if (FindClose(hFindFile) == FALSE) {
3769 if (list != NULL) {
3770 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003771 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003772 }
3773 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003774 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003775 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003776
Larry Hastings9cf065c2012-06-22 16:30:09 -07003777 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003778} /* end of _listdir_windows_no_opendir */
3779
3780#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3781
3782static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003783_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003784{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003785 PyObject *v;
3786 DIR *dirp = NULL;
3787 struct dirent *ep;
3788 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003789#ifdef HAVE_FDOPENDIR
3790 int fd = -1;
3791#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003792
Victor Stinner8c62be82010-05-06 00:08:46 +00003793 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003794#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003795 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003796 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003797 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003798 if (fd == -1)
3799 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003800
Larry Hastingsfdaea062012-06-25 04:42:23 -07003801 return_str = 1;
3802
Larry Hastings9cf065c2012-06-22 16:30:09 -07003803 Py_BEGIN_ALLOW_THREADS
3804 dirp = fdopendir(fd);
3805 Py_END_ALLOW_THREADS
3806 }
3807 else
3808#endif
3809 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003810 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003811 if (path->narrow) {
3812 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003813 /* only return bytes if they specified a bytes-like object */
3814 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003815 }
3816 else {
3817 name = ".";
3818 return_str = 1;
3819 }
3820
Larry Hastings9cf065c2012-06-22 16:30:09 -07003821 Py_BEGIN_ALLOW_THREADS
3822 dirp = opendir(name);
3823 Py_END_ALLOW_THREADS
3824 }
3825
3826 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003827 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003828#ifdef HAVE_FDOPENDIR
3829 if (fd != -1) {
3830 Py_BEGIN_ALLOW_THREADS
3831 close(fd);
3832 Py_END_ALLOW_THREADS
3833 }
3834#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003835 goto exit;
3836 }
3837 if ((list = PyList_New(0)) == NULL) {
3838 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003839 }
3840 for (;;) {
3841 errno = 0;
3842 Py_BEGIN_ALLOW_THREADS
3843 ep = readdir(dirp);
3844 Py_END_ALLOW_THREADS
3845 if (ep == NULL) {
3846 if (errno == 0) {
3847 break;
3848 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003849 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003850 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003851 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003852 }
3853 }
3854 if (ep->d_name[0] == '.' &&
3855 (NAMLEN(ep) == 1 ||
3856 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3857 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003858 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003859 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3860 else
3861 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003862 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003863 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 break;
3865 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003866 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003867 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003868 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003869 break;
3870 }
3871 Py_DECREF(v);
3872 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003873
Larry Hastings9cf065c2012-06-22 16:30:09 -07003874exit:
3875 if (dirp != NULL) {
3876 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003877#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003878 if (fd > -1)
3879 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003880#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003881 closedir(dirp);
3882 Py_END_ALLOW_THREADS
3883 }
3884
Larry Hastings9cf065c2012-06-22 16:30:09 -07003885 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003886} /* end of _posix_listdir */
3887#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003888
Larry Hastings2f936352014-08-05 14:04:04 +10003889
3890/*[clinic input]
3891os.listdir
3892
3893 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3894
3895Return a list containing the names of the files in the directory.
3896
BNMetricsb9427072018-11-02 15:20:19 +00003897path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003898 the filenames returned will also be bytes; in all other circumstances
3899 the filenames returned will be str.
3900If path is None, uses the path='.'.
3901On some platforms, path may also be specified as an open file descriptor;\
3902 the file descriptor must refer to a directory.
3903 If this functionality is unavailable, using it raises NotImplementedError.
3904
3905The list is in arbitrary order. It does not include the special
3906entries '.' and '..' even if they are present in the directory.
3907
3908
3909[clinic start generated code]*/
3910
Larry Hastings2f936352014-08-05 14:04:04 +10003911static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003912os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003913/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003914{
Steve Dower60419a72019-06-24 08:42:54 -07003915 if (PySys_Audit("os.listdir", "O",
3916 path->object ? path->object : Py_None) < 0) {
3917 return NULL;
3918 }
Larry Hastings2f936352014-08-05 14:04:04 +10003919#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3920 return _listdir_windows_no_opendir(path, NULL);
3921#else
3922 return _posix_listdir(path, NULL);
3923#endif
3924}
3925
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003926#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003927/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003928/*[clinic input]
3929os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003930
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003931 path: path_t
3932 /
3933
3934[clinic start generated code]*/
3935
3936static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003937os__getfullpathname_impl(PyObject *module, path_t *path)
3938/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003939{
Victor Stinner3939c322019-06-25 15:02:43 +02003940 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003941
Victor Stinner3939c322019-06-25 15:02:43 +02003942 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3943 if (_Py_abspath(path->wide, &abspath) < 0) {
3944 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003945 }
Victor Stinner3939c322019-06-25 15:02:43 +02003946 if (abspath == NULL) {
3947 return PyErr_NoMemory();
3948 }
3949
3950 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3951 PyMem_RawFree(abspath);
3952 if (str == NULL) {
3953 return NULL;
3954 }
3955 if (path->narrow) {
3956 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
3957 }
3958 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10003959}
Brian Curtind40e6f72010-07-08 21:39:08 +00003960
Brian Curtind25aef52011-06-13 15:16:04 -05003961
Larry Hastings2f936352014-08-05 14:04:04 +10003962/*[clinic input]
3963os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003964
Steve Dower23ad6d02018-02-22 10:39:10 -08003965 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10003966 /
3967
3968A helper function for samepath on windows.
3969[clinic start generated code]*/
3970
Larry Hastings2f936352014-08-05 14:04:04 +10003971static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08003972os__getfinalpathname_impl(PyObject *module, path_t *path)
3973/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003974{
3975 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003976 wchar_t buf[MAXPATHLEN], *target_path = buf;
3977 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00003978 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003979 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003980
Steve Dower23ad6d02018-02-22 10:39:10 -08003981 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00003982 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08003983 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00003984 0, /* desired access */
3985 0, /* share mode */
3986 NULL, /* security attributes */
3987 OPEN_EXISTING,
3988 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3989 FILE_FLAG_BACKUP_SEMANTICS,
3990 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003991 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003992
Steve Dower23ad6d02018-02-22 10:39:10 -08003993 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003994 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08003995 }
Brian Curtind40e6f72010-07-08 21:39:08 +00003996
3997 /* We have a good handle to the target, use it to determine the
3998 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03003999 while (1) {
4000 Py_BEGIN_ALLOW_THREADS
4001 result_length = GetFinalPathNameByHandleW(hFile, target_path,
4002 buf_size, VOLUME_NAME_DOS);
4003 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004004
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004005 if (!result_length) {
4006 result = win32_error_object("GetFinalPathNameByHandleW",
4007 path->object);
4008 goto cleanup;
4009 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004010
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004011 if (result_length < buf_size) {
4012 break;
4013 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004014
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004015 wchar_t *tmp;
4016 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4017 result_length * sizeof(*tmp));
4018 if (!tmp) {
4019 result = PyErr_NoMemory();
4020 goto cleanup;
4021 }
4022
4023 buf_size = result_length;
4024 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004025 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004026
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004027 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004028 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004029 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004030 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004031
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004032cleanup:
4033 if (target_path != buf) {
4034 PyMem_Free(target_path);
4035 }
4036 CloseHandle(hFile);
4037 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004038}
Brian Curtin62857742010-09-06 17:07:27 +00004039
Tim Golden6b528062013-08-01 12:44:00 +01004040
Larry Hastings2f936352014-08-05 14:04:04 +10004041/*[clinic input]
4042os._getvolumepathname
4043
Steve Dower23ad6d02018-02-22 10:39:10 -08004044 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004045
4046A helper function for ismount on Win32.
4047[clinic start generated code]*/
4048
Larry Hastings2f936352014-08-05 14:04:04 +10004049static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004050os__getvolumepathname_impl(PyObject *module, path_t *path)
4051/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004052{
4053 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004054 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004055 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004056 BOOL ret;
4057
Tim Golden6b528062013-08-01 12:44:00 +01004058 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004059 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004060
Victor Stinner850a18e2017-10-24 16:53:32 -07004061 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004062 PyErr_SetString(PyExc_OverflowError, "path too long");
4063 return NULL;
4064 }
4065
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004066 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004067 if (mountpath == NULL)
4068 return PyErr_NoMemory();
4069
4070 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004071 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004072 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004073 Py_END_ALLOW_THREADS
4074
4075 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004076 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004077 goto exit;
4078 }
4079 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004080 if (path->narrow)
4081 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004082
4083exit:
4084 PyMem_Free(mountpath);
4085 return result;
4086}
Tim Golden6b528062013-08-01 12:44:00 +01004087
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004088#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004089
Larry Hastings2f936352014-08-05 14:04:04 +10004090
4091/*[clinic input]
4092os.mkdir
4093
4094 path : path_t
4095
4096 mode: int = 0o777
4097
4098 *
4099
4100 dir_fd : dir_fd(requires='mkdirat') = None
4101
4102# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4103
4104Create a directory.
4105
4106If dir_fd is not None, it should be a file descriptor open to a directory,
4107 and path should be relative; path will then be relative to that directory.
4108dir_fd may not be implemented on your platform.
4109 If it is unavailable, using it will raise a NotImplementedError.
4110
4111The mode argument is ignored on Windows.
4112[clinic start generated code]*/
4113
Larry Hastings2f936352014-08-05 14:04:04 +10004114static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004115os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4116/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004117{
4118 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004119
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004120#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004121 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004122 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004123 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004124
Larry Hastings2f936352014-08-05 14:04:04 +10004125 if (!result)
4126 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004127#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004128 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004129#if HAVE_MKDIRAT
4130 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004131 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004132 else
4133#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004134#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004135 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004136#else
Larry Hastings2f936352014-08-05 14:04:04 +10004137 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004138#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004139 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004140 if (result < 0)
4141 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004142#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004143 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004144}
4145
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004146
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004147/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4148#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004149#include <sys/resource.h>
4150#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004152
4153#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004154/*[clinic input]
4155os.nice
4156
4157 increment: int
4158 /
4159
4160Add increment to the priority of process and return the new priority.
4161[clinic start generated code]*/
4162
Larry Hastings2f936352014-08-05 14:04:04 +10004163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004164os_nice_impl(PyObject *module, int increment)
4165/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004166{
4167 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004168
Victor Stinner8c62be82010-05-06 00:08:46 +00004169 /* There are two flavours of 'nice': one that returns the new
4170 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004171 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004172 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004173
Victor Stinner8c62be82010-05-06 00:08:46 +00004174 If we are of the nice family that returns the new priority, we
4175 need to clear errno before the call, and check if errno is filled
4176 before calling posix_error() on a returnvalue of -1, because the
4177 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004178
Victor Stinner8c62be82010-05-06 00:08:46 +00004179 errno = 0;
4180 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004181#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004182 if (value == 0)
4183 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004184#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004185 if (value == -1 && errno != 0)
4186 /* either nice() or getpriority() returned an error */
4187 return posix_error();
4188 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004189}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004190#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004191
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004192
4193#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004194/*[clinic input]
4195os.getpriority
4196
4197 which: int
4198 who: int
4199
4200Return program scheduling priority.
4201[clinic start generated code]*/
4202
Larry Hastings2f936352014-08-05 14:04:04 +10004203static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004204os_getpriority_impl(PyObject *module, int which, int who)
4205/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004206{
4207 int retval;
4208
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004209 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004210 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004211 if (errno != 0)
4212 return posix_error();
4213 return PyLong_FromLong((long)retval);
4214}
4215#endif /* HAVE_GETPRIORITY */
4216
4217
4218#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004219/*[clinic input]
4220os.setpriority
4221
4222 which: int
4223 who: int
4224 priority: int
4225
4226Set program scheduling priority.
4227[clinic start generated code]*/
4228
Larry Hastings2f936352014-08-05 14:04:04 +10004229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004230os_setpriority_impl(PyObject *module, int which, int who, int priority)
4231/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004232{
4233 int retval;
4234
4235 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004236 if (retval == -1)
4237 return posix_error();
4238 Py_RETURN_NONE;
4239}
4240#endif /* HAVE_SETPRIORITY */
4241
4242
Barry Warsaw53699e91996-12-10 23:23:01 +00004243static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004244internal_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 +00004245{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004246 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004247 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004248
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004249#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004250 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004251 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004252#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004253 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004254#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004255
Larry Hastings9cf065c2012-06-22 16:30:09 -07004256 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4257 (dst_dir_fd != DEFAULT_DIR_FD);
4258#ifndef HAVE_RENAMEAT
4259 if (dir_fd_specified) {
4260 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004261 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004262 }
4263#endif
4264
Larry Hastings9cf065c2012-06-22 16:30:09 -07004265#ifdef MS_WINDOWS
4266 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004267 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004268 Py_END_ALLOW_THREADS
4269
Larry Hastings2f936352014-08-05 14:04:04 +10004270 if (!result)
4271 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004272
4273#else
Steve Dowercc16be82016-09-08 10:35:16 -07004274 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4275 PyErr_Format(PyExc_ValueError,
4276 "%s: src and dst must be the same type", function_name);
4277 return NULL;
4278 }
4279
Larry Hastings9cf065c2012-06-22 16:30:09 -07004280 Py_BEGIN_ALLOW_THREADS
4281#ifdef HAVE_RENAMEAT
4282 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004283 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004284 else
4285#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004286 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004287 Py_END_ALLOW_THREADS
4288
Larry Hastings2f936352014-08-05 14:04:04 +10004289 if (result)
4290 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004291#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004292 Py_RETURN_NONE;
4293}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004294
Larry Hastings2f936352014-08-05 14:04:04 +10004295
4296/*[clinic input]
4297os.rename
4298
4299 src : path_t
4300 dst : path_t
4301 *
4302 src_dir_fd : dir_fd = None
4303 dst_dir_fd : dir_fd = None
4304
4305Rename a file or directory.
4306
4307If either src_dir_fd or dst_dir_fd is not None, it should be a file
4308 descriptor open to a directory, and the respective path string (src or dst)
4309 should be relative; the path will then be relative to that directory.
4310src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4311 If they are unavailable, using them will raise a NotImplementedError.
4312[clinic start generated code]*/
4313
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004314static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004315os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004316 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004317/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004318{
Larry Hastings2f936352014-08-05 14:04:04 +10004319 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004320}
4321
Larry Hastings2f936352014-08-05 14:04:04 +10004322
4323/*[clinic input]
4324os.replace = os.rename
4325
4326Rename a file or directory, overwriting the destination.
4327
4328If either src_dir_fd or dst_dir_fd is not None, it should be a file
4329 descriptor open to a directory, and the respective path string (src or dst)
4330 should be relative; the path will then be relative to that directory.
4331src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004332 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004333[clinic start generated code]*/
4334
Larry Hastings2f936352014-08-05 14:04:04 +10004335static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004336os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4337 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004338/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004339{
4340 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4341}
4342
4343
4344/*[clinic input]
4345os.rmdir
4346
4347 path: path_t
4348 *
4349 dir_fd: dir_fd(requires='unlinkat') = None
4350
4351Remove a directory.
4352
4353If dir_fd is not None, it should be a file descriptor open to a directory,
4354 and path should be relative; path will then be relative to that directory.
4355dir_fd may not be implemented on your platform.
4356 If it is unavailable, using it will raise a NotImplementedError.
4357[clinic start generated code]*/
4358
Larry Hastings2f936352014-08-05 14:04:04 +10004359static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004360os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4361/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004362{
4363 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004364
4365 Py_BEGIN_ALLOW_THREADS
4366#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004367 /* Windows, success=1, UNIX, success=0 */
4368 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004369#else
4370#ifdef HAVE_UNLINKAT
4371 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004372 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004373 else
4374#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004375 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004376#endif
4377 Py_END_ALLOW_THREADS
4378
Larry Hastings2f936352014-08-05 14:04:04 +10004379 if (result)
4380 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004381
Larry Hastings2f936352014-08-05 14:04:04 +10004382 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004383}
4384
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004385
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004386#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004387#ifdef MS_WINDOWS
4388/*[clinic input]
4389os.system -> long
4390
4391 command: Py_UNICODE
4392
4393Execute the command in a subshell.
4394[clinic start generated code]*/
4395
Larry Hastings2f936352014-08-05 14:04:04 +10004396static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004397os_system_impl(PyObject *module, const Py_UNICODE *command)
4398/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004399{
4400 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004401
Steve Dowerfbe3c762019-10-18 00:52:15 -07004402 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004403 return -1;
4404 }
4405
Victor Stinner8c62be82010-05-06 00:08:46 +00004406 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004407 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004408 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004409 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004410 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004411 return result;
4412}
4413#else /* MS_WINDOWS */
4414/*[clinic input]
4415os.system -> long
4416
4417 command: FSConverter
4418
4419Execute the command in a subshell.
4420[clinic start generated code]*/
4421
Larry Hastings2f936352014-08-05 14:04:04 +10004422static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004423os_system_impl(PyObject *module, PyObject *command)
4424/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004425{
4426 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004427 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004428
Steve Dowerfbe3c762019-10-18 00:52:15 -07004429 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004430 return -1;
4431 }
4432
Larry Hastings2f936352014-08-05 14:04:04 +10004433 Py_BEGIN_ALLOW_THREADS
4434 result = system(bytes);
4435 Py_END_ALLOW_THREADS
4436 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004437}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004438#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004439#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004440
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004441
Larry Hastings2f936352014-08-05 14:04:04 +10004442/*[clinic input]
4443os.umask
4444
4445 mask: int
4446 /
4447
4448Set the current numeric umask and return the previous umask.
4449[clinic start generated code]*/
4450
Larry Hastings2f936352014-08-05 14:04:04 +10004451static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004452os_umask_impl(PyObject *module, int mask)
4453/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004454{
4455 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004456 if (i < 0)
4457 return posix_error();
4458 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004459}
4460
Brian Curtind40e6f72010-07-08 21:39:08 +00004461#ifdef MS_WINDOWS
4462
4463/* override the default DeleteFileW behavior so that directory
4464symlinks can be removed with this function, the same as with
4465Unix symlinks */
4466BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4467{
4468 WIN32_FILE_ATTRIBUTE_DATA info;
4469 WIN32_FIND_DATAW find_data;
4470 HANDLE find_data_handle;
4471 int is_directory = 0;
4472 int is_link = 0;
4473
4474 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4475 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004476
Brian Curtind40e6f72010-07-08 21:39:08 +00004477 /* Get WIN32_FIND_DATA structure for the path to determine if
4478 it is a symlink */
4479 if(is_directory &&
4480 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4481 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4482
4483 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004484 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4485 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4486 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4487 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004488 FindClose(find_data_handle);
4489 }
4490 }
4491 }
4492
4493 if (is_directory && is_link)
4494 return RemoveDirectoryW(lpFileName);
4495
4496 return DeleteFileW(lpFileName);
4497}
4498#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004499
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004500
Larry Hastings2f936352014-08-05 14:04:04 +10004501/*[clinic input]
4502os.unlink
4503
4504 path: path_t
4505 *
4506 dir_fd: dir_fd(requires='unlinkat')=None
4507
4508Remove a file (same as remove()).
4509
4510If dir_fd is not None, it should be a file descriptor open to a directory,
4511 and path should be relative; path will then be relative to that directory.
4512dir_fd may not be implemented on your platform.
4513 If it is unavailable, using it will raise a NotImplementedError.
4514
4515[clinic start generated code]*/
4516
Larry Hastings2f936352014-08-05 14:04:04 +10004517static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004518os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4519/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004520{
4521 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004522
4523 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004524 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004525#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004526 /* Windows, success=1, UNIX, success=0 */
4527 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004528#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004529#ifdef HAVE_UNLINKAT
4530 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004531 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004532 else
4533#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004534 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004535#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004536 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004537 Py_END_ALLOW_THREADS
4538
Larry Hastings2f936352014-08-05 14:04:04 +10004539 if (result)
4540 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004541
Larry Hastings2f936352014-08-05 14:04:04 +10004542 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004543}
4544
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004545
Larry Hastings2f936352014-08-05 14:04:04 +10004546/*[clinic input]
4547os.remove = os.unlink
4548
4549Remove a file (same as unlink()).
4550
4551If dir_fd is not None, it should be a file descriptor open to a directory,
4552 and path should be relative; path will then be relative to that directory.
4553dir_fd may not be implemented on your platform.
4554 If it is unavailable, using it will raise a NotImplementedError.
4555[clinic start generated code]*/
4556
Larry Hastings2f936352014-08-05 14:04:04 +10004557static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004558os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4559/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004560{
4561 return os_unlink_impl(module, path, dir_fd);
4562}
4563
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004564
Larry Hastings605a62d2012-06-24 04:33:36 -07004565static PyStructSequence_Field uname_result_fields[] = {
4566 {"sysname", "operating system name"},
4567 {"nodename", "name of machine on network (implementation-defined)"},
4568 {"release", "operating system release"},
4569 {"version", "operating system version"},
4570 {"machine", "hardware identifier"},
4571 {NULL}
4572};
4573
4574PyDoc_STRVAR(uname_result__doc__,
4575"uname_result: Result from os.uname().\n\n\
4576This object may be accessed either as a tuple of\n\
4577 (sysname, nodename, release, version, machine),\n\
4578or via the attributes sysname, nodename, release, version, and machine.\n\
4579\n\
4580See os.uname for more information.");
4581
4582static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08004583 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07004584 uname_result__doc__, /* doc */
4585 uname_result_fields,
4586 5
4587};
4588
Larry Hastings605a62d2012-06-24 04:33:36 -07004589#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004590/*[clinic input]
4591os.uname
4592
4593Return an object identifying the current operating system.
4594
4595The object behaves like a named tuple with the following fields:
4596 (sysname, nodename, release, version, machine)
4597
4598[clinic start generated code]*/
4599
Larry Hastings2f936352014-08-05 14:04:04 +10004600static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004601os_uname_impl(PyObject *module)
4602/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004603{
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 struct utsname u;
4605 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004606 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004607
Victor Stinner8c62be82010-05-06 00:08:46 +00004608 Py_BEGIN_ALLOW_THREADS
4609 res = uname(&u);
4610 Py_END_ALLOW_THREADS
4611 if (res < 0)
4612 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004613
Eddie Elizondob3966632019-11-05 07:16:14 -08004614 PyObject *UnameResultType = _posixstate(module)->UnameResultType;
4615 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004616 if (value == NULL)
4617 return NULL;
4618
4619#define SET(i, field) \
4620 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004621 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004622 if (!o) { \
4623 Py_DECREF(value); \
4624 return NULL; \
4625 } \
4626 PyStructSequence_SET_ITEM(value, i, o); \
4627 } \
4628
4629 SET(0, u.sysname);
4630 SET(1, u.nodename);
4631 SET(2, u.release);
4632 SET(3, u.version);
4633 SET(4, u.machine);
4634
4635#undef SET
4636
4637 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004638}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004639#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004640
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004641
Larry Hastings9cf065c2012-06-22 16:30:09 -07004642
4643typedef struct {
4644 int now;
4645 time_t atime_s;
4646 long atime_ns;
4647 time_t mtime_s;
4648 long mtime_ns;
4649} utime_t;
4650
4651/*
Victor Stinner484df002014-10-09 13:52:31 +02004652 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004653 * they also intentionally leak the declaration of a pointer named "time"
4654 */
4655#define UTIME_TO_TIMESPEC \
4656 struct timespec ts[2]; \
4657 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004658 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004659 time = NULL; \
4660 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004661 ts[0].tv_sec = ut->atime_s; \
4662 ts[0].tv_nsec = ut->atime_ns; \
4663 ts[1].tv_sec = ut->mtime_s; \
4664 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004665 time = ts; \
4666 } \
4667
4668#define UTIME_TO_TIMEVAL \
4669 struct timeval tv[2]; \
4670 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004671 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004672 time = NULL; \
4673 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004674 tv[0].tv_sec = ut->atime_s; \
4675 tv[0].tv_usec = ut->atime_ns / 1000; \
4676 tv[1].tv_sec = ut->mtime_s; \
4677 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004678 time = tv; \
4679 } \
4680
4681#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004682 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004683 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004684 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004685 time = NULL; \
4686 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004687 u.actime = ut->atime_s; \
4688 u.modtime = ut->mtime_s; \
4689 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004690 }
4691
4692#define UTIME_TO_TIME_T \
4693 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004694 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004695 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004696 time = NULL; \
4697 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004698 timet[0] = ut->atime_s; \
4699 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004700 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004701 } \
4702
4703
Victor Stinner528a9ab2015-09-03 21:30:26 +02004704#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004705
4706static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004707utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004708{
4709#ifdef HAVE_UTIMENSAT
4710 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4711 UTIME_TO_TIMESPEC;
4712 return utimensat(dir_fd, path, time, flags);
4713#elif defined(HAVE_FUTIMESAT)
4714 UTIME_TO_TIMEVAL;
4715 /*
4716 * follow_symlinks will never be false here;
4717 * we only allow !follow_symlinks and dir_fd together
4718 * if we have utimensat()
4719 */
4720 assert(follow_symlinks);
4721 return futimesat(dir_fd, path, time);
4722#endif
4723}
4724
Larry Hastings2f936352014-08-05 14:04:04 +10004725 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4726#else
4727 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004728#endif
4729
Victor Stinner528a9ab2015-09-03 21:30:26 +02004730#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004731
4732static int
Victor Stinner484df002014-10-09 13:52:31 +02004733utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004734{
4735#ifdef HAVE_FUTIMENS
4736 UTIME_TO_TIMESPEC;
4737 return futimens(fd, time);
4738#else
4739 UTIME_TO_TIMEVAL;
4740 return futimes(fd, time);
4741#endif
4742}
4743
Larry Hastings2f936352014-08-05 14:04:04 +10004744 #define PATH_UTIME_HAVE_FD 1
4745#else
4746 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004747#endif
4748
Victor Stinner5ebae872015-09-22 01:29:33 +02004749#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4750# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4751#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004752
Victor Stinner4552ced2015-09-21 22:37:15 +02004753#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004754
4755static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004756utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004757{
4758#ifdef HAVE_UTIMENSAT
4759 UTIME_TO_TIMESPEC;
4760 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4761#else
4762 UTIME_TO_TIMEVAL;
4763 return lutimes(path, time);
4764#endif
4765}
4766
4767#endif
4768
4769#ifndef MS_WINDOWS
4770
4771static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004772utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004773{
4774#ifdef HAVE_UTIMENSAT
4775 UTIME_TO_TIMESPEC;
4776 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4777#elif defined(HAVE_UTIMES)
4778 UTIME_TO_TIMEVAL;
4779 return utimes(path, time);
4780#elif defined(HAVE_UTIME_H)
4781 UTIME_TO_UTIMBUF;
4782 return utime(path, time);
4783#else
4784 UTIME_TO_TIME_T;
4785 return utime(path, time);
4786#endif
4787}
4788
4789#endif
4790
Larry Hastings76ad59b2012-05-03 00:30:07 -07004791static int
4792split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4793{
4794 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004795 PyObject *divmod;
Eddie Elizondob3966632019-11-05 07:16:14 -08004796 divmod = PyNumber_Divmod(py_long, _posixstate_global->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004797 if (!divmod)
4798 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004799 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4800 PyErr_Format(PyExc_TypeError,
4801 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08004802 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004803 goto exit;
4804 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004805 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4806 if ((*s == -1) && PyErr_Occurred())
4807 goto exit;
4808 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004809 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004810 goto exit;
4811
4812 result = 1;
4813exit:
4814 Py_XDECREF(divmod);
4815 return result;
4816}
4817
Larry Hastings2f936352014-08-05 14:04:04 +10004818
4819/*[clinic input]
4820os.utime
4821
4822 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004823 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004824 *
4825 ns: object = NULL
4826 dir_fd: dir_fd(requires='futimensat') = None
4827 follow_symlinks: bool=True
4828
Martin Panter0ff89092015-09-09 01:56:53 +00004829# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004830
4831Set the access and modified time of path.
4832
4833path may always be specified as a string.
4834On some platforms, path may also be specified as an open file descriptor.
4835 If this functionality is unavailable, using it raises an exception.
4836
4837If times is not None, it must be a tuple (atime, mtime);
4838 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004839If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004840 atime_ns and mtime_ns should be expressed as integer nanoseconds
4841 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004842If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004843Specifying tuples for both times and ns is an error.
4844
4845If dir_fd is not None, it should be a file descriptor open to a directory,
4846 and path should be relative; path will then be relative to that directory.
4847If follow_symlinks is False, and the last element of the path is a symbolic
4848 link, utime will modify the symbolic link itself instead of the file the
4849 link points to.
4850It is an error to use dir_fd or follow_symlinks when specifying path
4851 as an open file descriptor.
4852dir_fd and follow_symlinks may not be available on your platform.
4853 If they are unavailable, using them will raise a NotImplementedError.
4854
4855[clinic start generated code]*/
4856
Larry Hastings2f936352014-08-05 14:04:04 +10004857static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004858os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4859 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004860/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004861{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004862#ifdef MS_WINDOWS
4863 HANDLE hFile;
4864 FILETIME atime, mtime;
4865#else
4866 int result;
4867#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004868
Larry Hastings2f936352014-08-05 14:04:04 +10004869 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004870
Christian Heimesb3c87242013-08-01 00:08:16 +02004871 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004872
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004873 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004874 PyErr_SetString(PyExc_ValueError,
4875 "utime: you may specify either 'times'"
4876 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004877 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004878 }
4879
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004880 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004881 time_t a_sec, m_sec;
4882 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004883 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004884 PyErr_SetString(PyExc_TypeError,
4885 "utime: 'times' must be either"
4886 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004887 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004888 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004889 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004890 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004891 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004892 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004893 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004894 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004895 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004896 utime.atime_s = a_sec;
4897 utime.atime_ns = a_nsec;
4898 utime.mtime_s = m_sec;
4899 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004900 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004901 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004902 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004903 PyErr_SetString(PyExc_TypeError,
4904 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004905 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004906 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004907 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004908 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004909 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004910 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004911 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004912 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004913 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004914 }
4915 else {
4916 /* times and ns are both None/unspecified. use "now". */
4917 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004918 }
4919
Victor Stinner4552ced2015-09-21 22:37:15 +02004920#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004921 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004922 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004923#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004924
Larry Hastings2f936352014-08-05 14:04:04 +10004925 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4926 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4927 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004928 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004929
Larry Hastings9cf065c2012-06-22 16:30:09 -07004930#if !defined(HAVE_UTIMENSAT)
4931 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004932 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004933 "utime: cannot use dir_fd and follow_symlinks "
4934 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004935 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004936 }
4937#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004938
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004939#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004940 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004941 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
4942 NULL, OPEN_EXISTING,
4943 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004944 Py_END_ALLOW_THREADS
4945 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004946 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004947 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004948 }
4949
Larry Hastings9cf065c2012-06-22 16:30:09 -07004950 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004951 GetSystemTimeAsFileTime(&mtime);
4952 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004953 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004955 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4956 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004957 }
4958 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4959 /* Avoid putting the file name into the error here,
4960 as that may confuse the user into believing that
4961 something is wrong with the file, when it also
4962 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004963 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004964 CloseHandle(hFile);
4965 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004966 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004967 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004968#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004969 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004970
Victor Stinner4552ced2015-09-21 22:37:15 +02004971#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004972 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004973 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004974 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004975#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004976
Victor Stinner528a9ab2015-09-03 21:30:26 +02004977#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004978 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004979 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004980 else
4981#endif
4982
Victor Stinner528a9ab2015-09-03 21:30:26 +02004983#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004984 if (path->fd != -1)
4985 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004986 else
4987#endif
4988
Larry Hastings2f936352014-08-05 14:04:04 +10004989 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004990
4991 Py_END_ALLOW_THREADS
4992
4993 if (result < 0) {
4994 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004995 posix_error();
4996 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004997 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004998
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004999#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005000
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005001 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005002}
5003
Guido van Rossum3b066191991-06-04 19:40:25 +00005004/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005005
Larry Hastings2f936352014-08-05 14:04:04 +10005006
5007/*[clinic input]
5008os._exit
5009
5010 status: int
5011
5012Exit to the system with specified status, without normal exit processing.
5013[clinic start generated code]*/
5014
Larry Hastings2f936352014-08-05 14:04:04 +10005015static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005016os__exit_impl(PyObject *module, int status)
5017/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005018{
5019 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005020 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005021}
5022
Steve Dowercc16be82016-09-08 10:35:16 -07005023#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5024#define EXECV_CHAR wchar_t
5025#else
5026#define EXECV_CHAR char
5027#endif
5028
pxinwrf2d7ac72019-05-21 18:46:37 +08005029#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005030static void
Steve Dowercc16be82016-09-08 10:35:16 -07005031free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005032{
Victor Stinner8c62be82010-05-06 00:08:46 +00005033 Py_ssize_t i;
5034 for (i = 0; i < count; i++)
5035 PyMem_Free(array[i]);
5036 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005037}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005038
Berker Peksag81816462016-09-15 20:19:47 +03005039static int
5040fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005041{
Victor Stinner8c62be82010-05-06 00:08:46 +00005042 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005043 PyObject *ub;
5044 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005045#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005046 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005047 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005048 *out = PyUnicode_AsWideCharString(ub, &size);
5049 if (*out)
5050 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005051#else
Berker Peksag81816462016-09-15 20:19:47 +03005052 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005053 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005054 size = PyBytes_GET_SIZE(ub);
5055 *out = PyMem_Malloc(size + 1);
5056 if (*out) {
5057 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5058 result = 1;
5059 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005060 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005061#endif
Berker Peksag81816462016-09-15 20:19:47 +03005062 Py_DECREF(ub);
5063 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005064}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005065#endif
5066
pxinwrf2d7ac72019-05-21 18:46:37 +08005067#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005068static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005069parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5070{
Victor Stinner8c62be82010-05-06 00:08:46 +00005071 Py_ssize_t i, pos, envc;
5072 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005073 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005074 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005075
Victor Stinner8c62be82010-05-06 00:08:46 +00005076 i = PyMapping_Size(env);
5077 if (i < 0)
5078 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005079 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005080 if (envlist == NULL) {
5081 PyErr_NoMemory();
5082 return NULL;
5083 }
5084 envc = 0;
5085 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005086 if (!keys)
5087 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005089 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005090 goto error;
5091 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5092 PyErr_Format(PyExc_TypeError,
5093 "env.keys() or env.values() is not a list");
5094 goto error;
5095 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005096
Victor Stinner8c62be82010-05-06 00:08:46 +00005097 for (pos = 0; pos < i; pos++) {
5098 key = PyList_GetItem(keys, pos);
5099 val = PyList_GetItem(vals, pos);
5100 if (!key || !val)
5101 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005102
Berker Peksag81816462016-09-15 20:19:47 +03005103#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5104 if (!PyUnicode_FSDecoder(key, &key2))
5105 goto error;
5106 if (!PyUnicode_FSDecoder(val, &val2)) {
5107 Py_DECREF(key2);
5108 goto error;
5109 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005110 /* Search from index 1 because on Windows starting '=' is allowed for
5111 defining hidden environment variables. */
5112 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5113 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5114 {
5115 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005116 Py_DECREF(key2);
5117 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005118 goto error;
5119 }
Berker Peksag81816462016-09-15 20:19:47 +03005120 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5121#else
5122 if (!PyUnicode_FSConverter(key, &key2))
5123 goto error;
5124 if (!PyUnicode_FSConverter(val, &val2)) {
5125 Py_DECREF(key2);
5126 goto error;
5127 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005128 if (PyBytes_GET_SIZE(key2) == 0 ||
5129 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5130 {
5131 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005132 Py_DECREF(key2);
5133 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005134 goto error;
5135 }
Berker Peksag81816462016-09-15 20:19:47 +03005136 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5137 PyBytes_AS_STRING(val2));
5138#endif
5139 Py_DECREF(key2);
5140 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005141 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005142 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005143
5144 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5145 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005146 goto error;
5147 }
Berker Peksag81816462016-09-15 20:19:47 +03005148
Steve Dowercc16be82016-09-08 10:35:16 -07005149 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005150 }
5151 Py_DECREF(vals);
5152 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005153
Victor Stinner8c62be82010-05-06 00:08:46 +00005154 envlist[envc] = 0;
5155 *envc_ptr = envc;
5156 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005157
5158error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005159 Py_XDECREF(keys);
5160 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005161 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005162 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005163}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005164
Steve Dowercc16be82016-09-08 10:35:16 -07005165static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005166parse_arglist(PyObject* argv, Py_ssize_t *argc)
5167{
5168 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005169 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005170 if (argvlist == NULL) {
5171 PyErr_NoMemory();
5172 return NULL;
5173 }
5174 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005175 PyObject* item = PySequence_ITEM(argv, i);
5176 if (item == NULL)
5177 goto fail;
5178 if (!fsconvert_strdup(item, &argvlist[i])) {
5179 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005180 goto fail;
5181 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005182 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005183 }
5184 argvlist[*argc] = NULL;
5185 return argvlist;
5186fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005187 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005188 free_string_array(argvlist, *argc);
5189 return NULL;
5190}
Steve Dowercc16be82016-09-08 10:35:16 -07005191
Ross Lagerwall7807c352011-03-17 20:20:30 +02005192#endif
5193
Larry Hastings2f936352014-08-05 14:04:04 +10005194
Ross Lagerwall7807c352011-03-17 20:20:30 +02005195#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005196/*[clinic input]
5197os.execv
5198
Steve Dowercc16be82016-09-08 10:35:16 -07005199 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005200 Path of executable file.
5201 argv: object
5202 Tuple or list of strings.
5203 /
5204
5205Execute an executable path with arguments, replacing current process.
5206[clinic start generated code]*/
5207
Larry Hastings2f936352014-08-05 14:04:04 +10005208static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005209os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5210/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005211{
Steve Dowercc16be82016-09-08 10:35:16 -07005212 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005213 Py_ssize_t argc;
5214
5215 /* execv has two arguments: (path, argv), where
5216 argv is a list or tuple of strings. */
5217
Ross Lagerwall7807c352011-03-17 20:20:30 +02005218 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5219 PyErr_SetString(PyExc_TypeError,
5220 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005221 return NULL;
5222 }
5223 argc = PySequence_Size(argv);
5224 if (argc < 1) {
5225 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005226 return NULL;
5227 }
5228
5229 argvlist = parse_arglist(argv, &argc);
5230 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005231 return NULL;
5232 }
Steve Dowerbce26262016-11-19 19:17:26 -08005233 if (!argvlist[0][0]) {
5234 PyErr_SetString(PyExc_ValueError,
5235 "execv() arg 2 first element cannot be empty");
5236 free_string_array(argvlist, argc);
5237 return NULL;
5238 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005239
Steve Dowerbce26262016-11-19 19:17:26 -08005240 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005241#ifdef HAVE_WEXECV
5242 _wexecv(path->wide, argvlist);
5243#else
5244 execv(path->narrow, argvlist);
5245#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005246 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005247
5248 /* If we get here it's definitely an error */
5249
5250 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005251 return posix_error();
5252}
5253
Larry Hastings2f936352014-08-05 14:04:04 +10005254
5255/*[clinic input]
5256os.execve
5257
5258 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5259 Path of executable file.
5260 argv: object
5261 Tuple or list of strings.
5262 env: object
5263 Dictionary of strings mapping to strings.
5264
5265Execute an executable path with arguments, replacing current process.
5266[clinic start generated code]*/
5267
Larry Hastings2f936352014-08-05 14:04:04 +10005268static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005269os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5270/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005271{
Steve Dowercc16be82016-09-08 10:35:16 -07005272 EXECV_CHAR **argvlist = NULL;
5273 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005274 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005275
Victor Stinner8c62be82010-05-06 00:08:46 +00005276 /* execve has three arguments: (path, argv, env), where
5277 argv is a list or tuple of strings and env is a dictionary
5278 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005279
Ross Lagerwall7807c352011-03-17 20:20:30 +02005280 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005281 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005282 "execve: argv must be a tuple or list");
5283 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005284 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005285 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005286 if (argc < 1) {
5287 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5288 return NULL;
5289 }
5290
Victor Stinner8c62be82010-05-06 00:08:46 +00005291 if (!PyMapping_Check(env)) {
5292 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005293 "execve: environment must be a mapping object");
5294 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005295 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005296
Ross Lagerwall7807c352011-03-17 20:20:30 +02005297 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005298 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005299 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005300 }
Steve Dowerbce26262016-11-19 19:17:26 -08005301 if (!argvlist[0][0]) {
5302 PyErr_SetString(PyExc_ValueError,
5303 "execve: argv first element cannot be empty");
5304 goto fail;
5305 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005306
Victor Stinner8c62be82010-05-06 00:08:46 +00005307 envlist = parse_envlist(env, &envc);
5308 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005309 goto fail;
5310
Steve Dowerbce26262016-11-19 19:17:26 -08005311 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005312#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005313 if (path->fd > -1)
5314 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005315 else
5316#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005317#ifdef HAVE_WEXECV
5318 _wexecve(path->wide, argvlist, envlist);
5319#else
Larry Hastings2f936352014-08-05 14:04:04 +10005320 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005321#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005322 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005323
5324 /* If we get here it's definitely an error */
5325
Alexey Izbyshev83460312018-10-20 03:28:22 +03005326 posix_path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005327
Steve Dowercc16be82016-09-08 10:35:16 -07005328 free_string_array(envlist, envc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005329 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005330 if (argvlist)
5331 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005332 return NULL;
5333}
Steve Dowercc16be82016-09-08 10:35:16 -07005334
Larry Hastings9cf065c2012-06-22 16:30:09 -07005335#endif /* HAVE_EXECV */
5336
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005337#ifdef HAVE_POSIX_SPAWN
5338
5339enum posix_spawn_file_actions_identifier {
5340 POSIX_SPAWN_OPEN,
5341 POSIX_SPAWN_CLOSE,
5342 POSIX_SPAWN_DUP2
5343};
5344
William Orr81574b82018-10-01 22:19:56 -07005345#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005346static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005347convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005348#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005349
5350static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005351parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5352 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005353 PyObject *setsigdef, PyObject *scheduler,
5354 posix_spawnattr_t *attrp)
5355{
5356 long all_flags = 0;
5357
5358 errno = posix_spawnattr_init(attrp);
5359 if (errno) {
5360 posix_error();
5361 return -1;
5362 }
5363
5364 if (setpgroup) {
5365 pid_t pgid = PyLong_AsPid(setpgroup);
5366 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5367 goto fail;
5368 }
5369 errno = posix_spawnattr_setpgroup(attrp, pgid);
5370 if (errno) {
5371 posix_error();
5372 goto fail;
5373 }
5374 all_flags |= POSIX_SPAWN_SETPGROUP;
5375 }
5376
5377 if (resetids) {
5378 all_flags |= POSIX_SPAWN_RESETIDS;
5379 }
5380
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005381 if (setsid) {
5382#ifdef POSIX_SPAWN_SETSID
5383 all_flags |= POSIX_SPAWN_SETSID;
5384#elif defined(POSIX_SPAWN_SETSID_NP)
5385 all_flags |= POSIX_SPAWN_SETSID_NP;
5386#else
5387 argument_unavailable_error(func_name, "setsid");
5388 return -1;
5389#endif
5390 }
5391
Pablo Galindo254a4662018-09-07 16:44:24 +01005392 if (setsigmask) {
5393 sigset_t set;
5394 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5395 goto fail;
5396 }
5397 errno = posix_spawnattr_setsigmask(attrp, &set);
5398 if (errno) {
5399 posix_error();
5400 goto fail;
5401 }
5402 all_flags |= POSIX_SPAWN_SETSIGMASK;
5403 }
5404
5405 if (setsigdef) {
5406 sigset_t set;
5407 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5408 goto fail;
5409 }
5410 errno = posix_spawnattr_setsigdefault(attrp, &set);
5411 if (errno) {
5412 posix_error();
5413 goto fail;
5414 }
5415 all_flags |= POSIX_SPAWN_SETSIGDEF;
5416 }
5417
5418 if (scheduler) {
5419#ifdef POSIX_SPAWN_SETSCHEDULER
5420 PyObject *py_schedpolicy;
5421 struct sched_param schedparam;
5422
5423 if (!PyArg_ParseTuple(scheduler, "OO&"
5424 ";A scheduler tuple must have two elements",
5425 &py_schedpolicy, convert_sched_param, &schedparam)) {
5426 goto fail;
5427 }
5428 if (py_schedpolicy != Py_None) {
5429 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5430
5431 if (schedpolicy == -1 && PyErr_Occurred()) {
5432 goto fail;
5433 }
5434 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5435 if (errno) {
5436 posix_error();
5437 goto fail;
5438 }
5439 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5440 }
5441 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5442 if (errno) {
5443 posix_error();
5444 goto fail;
5445 }
5446 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5447#else
5448 PyErr_SetString(PyExc_NotImplementedError,
5449 "The scheduler option is not supported in this system.");
5450 goto fail;
5451#endif
5452 }
5453
5454 errno = posix_spawnattr_setflags(attrp, all_flags);
5455 if (errno) {
5456 posix_error();
5457 goto fail;
5458 }
5459
5460 return 0;
5461
5462fail:
5463 (void)posix_spawnattr_destroy(attrp);
5464 return -1;
5465}
5466
5467static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005468parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005469 posix_spawn_file_actions_t *file_actionsp,
5470 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005471{
5472 PyObject *seq;
5473 PyObject *file_action = NULL;
5474 PyObject *tag_obj;
5475
5476 seq = PySequence_Fast(file_actions,
5477 "file_actions must be a sequence or None");
5478 if (seq == NULL) {
5479 return -1;
5480 }
5481
5482 errno = posix_spawn_file_actions_init(file_actionsp);
5483 if (errno) {
5484 posix_error();
5485 Py_DECREF(seq);
5486 return -1;
5487 }
5488
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005489 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005490 file_action = PySequence_Fast_GET_ITEM(seq, i);
5491 Py_INCREF(file_action);
5492 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5493 PyErr_SetString(PyExc_TypeError,
5494 "Each file_actions element must be a non-empty tuple");
5495 goto fail;
5496 }
5497 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5498 if (tag == -1 && PyErr_Occurred()) {
5499 goto fail;
5500 }
5501
5502 /* Populate the file_actions object */
5503 switch (tag) {
5504 case POSIX_SPAWN_OPEN: {
5505 int fd, oflag;
5506 PyObject *path;
5507 unsigned long mode;
5508 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5509 ";A open file_action tuple must have 5 elements",
5510 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5511 &oflag, &mode))
5512 {
5513 goto fail;
5514 }
Pablo Galindocb970732018-06-19 09:19:50 +01005515 if (PyList_Append(temp_buffer, path)) {
5516 Py_DECREF(path);
5517 goto fail;
5518 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005519 errno = posix_spawn_file_actions_addopen(file_actionsp,
5520 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005521 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005522 if (errno) {
5523 posix_error();
5524 goto fail;
5525 }
5526 break;
5527 }
5528 case POSIX_SPAWN_CLOSE: {
5529 int fd;
5530 if (!PyArg_ParseTuple(file_action, "Oi"
5531 ";A close file_action tuple must have 2 elements",
5532 &tag_obj, &fd))
5533 {
5534 goto fail;
5535 }
5536 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5537 if (errno) {
5538 posix_error();
5539 goto fail;
5540 }
5541 break;
5542 }
5543 case POSIX_SPAWN_DUP2: {
5544 int fd1, fd2;
5545 if (!PyArg_ParseTuple(file_action, "Oii"
5546 ";A dup2 file_action tuple must have 3 elements",
5547 &tag_obj, &fd1, &fd2))
5548 {
5549 goto fail;
5550 }
5551 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5552 fd1, fd2);
5553 if (errno) {
5554 posix_error();
5555 goto fail;
5556 }
5557 break;
5558 }
5559 default: {
5560 PyErr_SetString(PyExc_TypeError,
5561 "Unknown file_actions identifier");
5562 goto fail;
5563 }
5564 }
5565 Py_DECREF(file_action);
5566 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005567
Serhiy Storchakaef347532018-05-01 16:45:04 +03005568 Py_DECREF(seq);
5569 return 0;
5570
5571fail:
5572 Py_DECREF(seq);
5573 Py_DECREF(file_action);
5574 (void)posix_spawn_file_actions_destroy(file_actionsp);
5575 return -1;
5576}
5577
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005578
5579static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005580py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5581 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005582 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005583 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005584{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005585 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005586 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005587 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005588 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005589 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005590 posix_spawnattr_t attr;
5591 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005592 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005593 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005594 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005595 pid_t pid;
5596 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005597
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005598 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005599 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005600 like posix.environ. */
5601
Serhiy Storchakaef347532018-05-01 16:45:04 +03005602 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005603 PyErr_Format(PyExc_TypeError,
5604 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005605 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005606 }
5607 argc = PySequence_Size(argv);
5608 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005609 PyErr_Format(PyExc_ValueError,
5610 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005611 return NULL;
5612 }
5613
5614 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005615 PyErr_Format(PyExc_TypeError,
5616 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005617 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005618 }
5619
5620 argvlist = parse_arglist(argv, &argc);
5621 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005622 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005623 }
5624 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005625 PyErr_Format(PyExc_ValueError,
5626 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005627 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005628 }
5629
5630 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005631 if (envlist == NULL) {
5632 goto exit;
5633 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005634
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005635 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005636 /* There is a bug in old versions of glibc that makes some of the
5637 * helper functions for manipulating file actions not copy the provided
5638 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5639 * copy the value of path for some old versions of glibc (<2.20).
5640 * The use of temp_buffer here is a workaround that keeps the
5641 * python objects that own the buffers alive until posix_spawn gets called.
5642 * Check https://bugs.python.org/issue33630 and
5643 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5644 temp_buffer = PyList_New(0);
5645 if (!temp_buffer) {
5646 goto exit;
5647 }
5648 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005649 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005650 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005651 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005652 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005653
Victor Stinner325e4ba2019-02-01 15:47:24 +01005654 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5655 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005656 goto exit;
5657 }
5658 attrp = &attr;
5659
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005660 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005661#ifdef HAVE_POSIX_SPAWNP
5662 if (use_posix_spawnp) {
5663 err_code = posix_spawnp(&pid, path->narrow,
5664 file_actionsp, attrp, argvlist, envlist);
5665 }
5666 else
5667#endif /* HAVE_POSIX_SPAWNP */
5668 {
5669 err_code = posix_spawn(&pid, path->narrow,
5670 file_actionsp, attrp, argvlist, envlist);
5671 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005672 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005673
Serhiy Storchakaef347532018-05-01 16:45:04 +03005674 if (err_code) {
5675 errno = err_code;
5676 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005677 goto exit;
5678 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005679#ifdef _Py_MEMORY_SANITIZER
5680 __msan_unpoison(&pid, sizeof(pid));
5681#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005682 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005683
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005684exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005685 if (file_actionsp) {
5686 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005687 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005688 if (attrp) {
5689 (void)posix_spawnattr_destroy(attrp);
5690 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005691 if (envlist) {
5692 free_string_array(envlist, envc);
5693 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005694 if (argvlist) {
5695 free_string_array(argvlist, argc);
5696 }
Pablo Galindocb970732018-06-19 09:19:50 +01005697 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005698 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005699}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005700
5701
5702/*[clinic input]
5703
5704os.posix_spawn
5705 path: path_t
5706 Path of executable file.
5707 argv: object
5708 Tuple or list of strings.
5709 env: object
5710 Dictionary of strings mapping to strings.
5711 /
5712 *
5713 file_actions: object(c_default='NULL') = ()
5714 A sequence of file action tuples.
5715 setpgroup: object = NULL
5716 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5717 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005718 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5719 setsid: bool(accept={int}) = False
5720 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005721 setsigmask: object(c_default='NULL') = ()
5722 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5723 setsigdef: object(c_default='NULL') = ()
5724 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5725 scheduler: object = NULL
5726 A tuple with the scheduler policy (optional) and parameters.
5727
5728Execute the program specified by path in a new process.
5729[clinic start generated code]*/
5730
5731static PyObject *
5732os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5733 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005734 PyObject *setpgroup, int resetids, int setsid,
5735 PyObject *setsigmask, PyObject *setsigdef,
5736 PyObject *scheduler)
5737/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005738{
5739 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005740 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005741 scheduler);
5742}
5743 #endif /* HAVE_POSIX_SPAWN */
5744
5745
5746
5747#ifdef HAVE_POSIX_SPAWNP
5748/*[clinic input]
5749
5750os.posix_spawnp
5751 path: path_t
5752 Path of executable file.
5753 argv: object
5754 Tuple or list of strings.
5755 env: object
5756 Dictionary of strings mapping to strings.
5757 /
5758 *
5759 file_actions: object(c_default='NULL') = ()
5760 A sequence of file action tuples.
5761 setpgroup: object = NULL
5762 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5763 resetids: bool(accept={int}) = False
5764 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005765 setsid: bool(accept={int}) = False
5766 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005767 setsigmask: object(c_default='NULL') = ()
5768 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5769 setsigdef: object(c_default='NULL') = ()
5770 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5771 scheduler: object = NULL
5772 A tuple with the scheduler policy (optional) and parameters.
5773
5774Execute the program specified by path in a new process.
5775[clinic start generated code]*/
5776
5777static PyObject *
5778os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5779 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005780 PyObject *setpgroup, int resetids, int setsid,
5781 PyObject *setsigmask, PyObject *setsigdef,
5782 PyObject *scheduler)
5783/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005784{
5785 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005786 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005787 scheduler);
5788}
5789#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005790
pxinwrf2d7ac72019-05-21 18:46:37 +08005791#ifdef HAVE_RTPSPAWN
5792static intptr_t
5793_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5794 const char *envp[])
5795{
5796 RTP_ID rtpid;
5797 int status;
5798 pid_t res;
5799 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005800
pxinwrf2d7ac72019-05-21 18:46:37 +08005801 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5802 uStackSize=0 cannot be used, the default stack size is too small for
5803 Python. */
5804 if (envp) {
5805 rtpid = rtpSpawn(rtpFileName, argv, envp,
5806 100, 0x1000000, 0, VX_FP_TASK);
5807 }
5808 else {
5809 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5810 100, 0x1000000, 0, VX_FP_TASK);
5811 }
5812 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5813 do {
5814 res = waitpid((pid_t)rtpid, &status, 0);
5815 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5816
5817 if (res < 0)
5818 return RTP_ID_ERROR;
5819 return ((intptr_t)status);
5820 }
5821 return ((intptr_t)rtpid);
5822}
5823#endif
5824
5825#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005826/*[clinic input]
5827os.spawnv
5828
5829 mode: int
5830 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005831 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005832 Path of executable file.
5833 argv: object
5834 Tuple or list of strings.
5835 /
5836
5837Execute the program specified by path in a new process.
5838[clinic start generated code]*/
5839
Larry Hastings2f936352014-08-05 14:04:04 +10005840static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005841os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5842/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005843{
Steve Dowercc16be82016-09-08 10:35:16 -07005844 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005845 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005846 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005847 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005848 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005849
Victor Stinner8c62be82010-05-06 00:08:46 +00005850 /* spawnv has three arguments: (mode, path, argv), where
5851 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005852
Victor Stinner8c62be82010-05-06 00:08:46 +00005853 if (PyList_Check(argv)) {
5854 argc = PyList_Size(argv);
5855 getitem = PyList_GetItem;
5856 }
5857 else if (PyTuple_Check(argv)) {
5858 argc = PyTuple_Size(argv);
5859 getitem = PyTuple_GetItem;
5860 }
5861 else {
5862 PyErr_SetString(PyExc_TypeError,
5863 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005864 return NULL;
5865 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005866 if (argc == 0) {
5867 PyErr_SetString(PyExc_ValueError,
5868 "spawnv() arg 2 cannot be empty");
5869 return NULL;
5870 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005871
Steve Dowercc16be82016-09-08 10:35:16 -07005872 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005873 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005874 return PyErr_NoMemory();
5875 }
5876 for (i = 0; i < argc; i++) {
5877 if (!fsconvert_strdup((*getitem)(argv, i),
5878 &argvlist[i])) {
5879 free_string_array(argvlist, i);
5880 PyErr_SetString(
5881 PyExc_TypeError,
5882 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005883 return NULL;
5884 }
Steve Dower93ff8722016-11-19 19:03:54 -08005885 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005886 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005887 PyErr_SetString(
5888 PyExc_ValueError,
5889 "spawnv() arg 2 first element cannot be empty");
5890 return NULL;
5891 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005892 }
5893 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005894
pxinwrf2d7ac72019-05-21 18:46:37 +08005895#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005896 if (mode == _OLD_P_OVERLAY)
5897 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005898#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005899
Victor Stinner8c62be82010-05-06 00:08:46 +00005900 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005901 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005902#ifdef HAVE_WSPAWNV
5903 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005904#elif defined(HAVE_RTPSPAWN)
5905 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005906#else
5907 spawnval = _spawnv(mode, path->narrow, argvlist);
5908#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005909 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005910 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005911
Victor Stinner8c62be82010-05-06 00:08:46 +00005912 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005913
Victor Stinner8c62be82010-05-06 00:08:46 +00005914 if (spawnval == -1)
5915 return posix_error();
5916 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005917 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005918}
5919
Larry Hastings2f936352014-08-05 14:04:04 +10005920/*[clinic input]
5921os.spawnve
5922
5923 mode: int
5924 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005925 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005926 Path of executable file.
5927 argv: object
5928 Tuple or list of strings.
5929 env: object
5930 Dictionary of strings mapping to strings.
5931 /
5932
5933Execute the program specified by path in a new process.
5934[clinic start generated code]*/
5935
Larry Hastings2f936352014-08-05 14:04:04 +10005936static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005937os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005938 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07005939/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005940{
Steve Dowercc16be82016-09-08 10:35:16 -07005941 EXECV_CHAR **argvlist;
5942 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005943 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005944 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005945 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005946 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005947 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005948
Victor Stinner8c62be82010-05-06 00:08:46 +00005949 /* spawnve has four arguments: (mode, path, argv, env), where
5950 argv is a list or tuple of strings and env is a dictionary
5951 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005952
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 if (PyList_Check(argv)) {
5954 argc = PyList_Size(argv);
5955 getitem = PyList_GetItem;
5956 }
5957 else if (PyTuple_Check(argv)) {
5958 argc = PyTuple_Size(argv);
5959 getitem = PyTuple_GetItem;
5960 }
5961 else {
5962 PyErr_SetString(PyExc_TypeError,
5963 "spawnve() arg 2 must be a tuple or list");
5964 goto fail_0;
5965 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005966 if (argc == 0) {
5967 PyErr_SetString(PyExc_ValueError,
5968 "spawnve() arg 2 cannot be empty");
5969 goto fail_0;
5970 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005971 if (!PyMapping_Check(env)) {
5972 PyErr_SetString(PyExc_TypeError,
5973 "spawnve() arg 3 must be a mapping object");
5974 goto fail_0;
5975 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005976
Steve Dowercc16be82016-09-08 10:35:16 -07005977 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005978 if (argvlist == NULL) {
5979 PyErr_NoMemory();
5980 goto fail_0;
5981 }
5982 for (i = 0; i < argc; i++) {
5983 if (!fsconvert_strdup((*getitem)(argv, i),
5984 &argvlist[i]))
5985 {
5986 lastarg = i;
5987 goto fail_1;
5988 }
Steve Dowerbce26262016-11-19 19:17:26 -08005989 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02005990 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08005991 PyErr_SetString(
5992 PyExc_ValueError,
5993 "spawnv() arg 2 first element cannot be empty");
5994 goto fail_1;
5995 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 }
5997 lastarg = argc;
5998 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005999
Victor Stinner8c62be82010-05-06 00:08:46 +00006000 envlist = parse_envlist(env, &envc);
6001 if (envlist == NULL)
6002 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006003
pxinwrf2d7ac72019-05-21 18:46:37 +08006004#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 if (mode == _OLD_P_OVERLAY)
6006 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006007#endif
Tim Peters25059d32001-12-07 20:35:43 +00006008
Victor Stinner8c62be82010-05-06 00:08:46 +00006009 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006010 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006011#ifdef HAVE_WSPAWNV
6012 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006013#elif defined(HAVE_RTPSPAWN)
6014 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6015 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006016#else
6017 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6018#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006019 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006020 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006021
Victor Stinner8c62be82010-05-06 00:08:46 +00006022 if (spawnval == -1)
6023 (void) posix_error();
6024 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006025 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006026
Victor Stinner8c62be82010-05-06 00:08:46 +00006027 while (--envc >= 0)
6028 PyMem_DEL(envlist[envc]);
6029 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006030 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006031 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006032 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006034}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006035
Guido van Rossuma1065681999-01-25 23:20:23 +00006036#endif /* HAVE_SPAWNV */
6037
6038
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006039#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006040
6041/* Helper function to validate arguments.
6042 Returns 0 on success. non-zero on failure with a TypeError raised.
6043 If obj is non-NULL it must be callable. */
6044static int
6045check_null_or_callable(PyObject *obj, const char* obj_name)
6046{
6047 if (obj && !PyCallable_Check(obj)) {
6048 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006049 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006050 return -1;
6051 }
6052 return 0;
6053}
6054
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006055/*[clinic input]
6056os.register_at_fork
6057
Gregory P. Smith163468a2017-05-29 10:03:41 -07006058 *
6059 before: object=NULL
6060 A callable to be called in the parent before the fork() syscall.
6061 after_in_child: object=NULL
6062 A callable to be called in the child after fork().
6063 after_in_parent: object=NULL
6064 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006065
Gregory P. Smith163468a2017-05-29 10:03:41 -07006066Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006067
Gregory P. Smith163468a2017-05-29 10:03:41 -07006068'before' callbacks are called in reverse order.
6069'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006070
6071[clinic start generated code]*/
6072
6073static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006074os_register_at_fork_impl(PyObject *module, PyObject *before,
6075 PyObject *after_in_child, PyObject *after_in_parent)
6076/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006077{
6078 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006079
Gregory P. Smith163468a2017-05-29 10:03:41 -07006080 if (!before && !after_in_child && !after_in_parent) {
6081 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6082 return NULL;
6083 }
6084 if (check_null_or_callable(before, "before") ||
6085 check_null_or_callable(after_in_child, "after_in_child") ||
6086 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006087 return NULL;
6088 }
Victor Stinnercaba55b2018-08-03 15:33:52 +02006089 interp = _PyInterpreterState_Get();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006090
Gregory P. Smith163468a2017-05-29 10:03:41 -07006091 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006092 return NULL;
6093 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006094 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006095 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006096 }
6097 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6098 return NULL;
6099 }
6100 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006101}
6102#endif /* HAVE_FORK */
6103
6104
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006105#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006106/*[clinic input]
6107os.fork1
6108
6109Fork a child process with a single multiplexed (i.e., not bound) thread.
6110
6111Return 0 to child process and PID of child to parent process.
6112[clinic start generated code]*/
6113
Larry Hastings2f936352014-08-05 14:04:04 +10006114static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006115os_fork1_impl(PyObject *module)
6116/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006117{
Victor Stinner8c62be82010-05-06 00:08:46 +00006118 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006119
Eric Snow59032962018-09-14 14:17:20 -07006120 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6121 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6122 return NULL;
6123 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006124 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006125 pid = fork1();
6126 if (pid == 0) {
6127 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006128 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006129 } else {
6130 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006131 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006132 }
6133 if (pid == -1)
6134 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006135 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006136}
Larry Hastings2f936352014-08-05 14:04:04 +10006137#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006138
6139
Guido van Rossumad0ee831995-03-01 10:34:45 +00006140#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006141/*[clinic input]
6142os.fork
6143
6144Fork a child process.
6145
6146Return 0 to child process and PID of child to parent process.
6147[clinic start generated code]*/
6148
Larry Hastings2f936352014-08-05 14:04:04 +10006149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006150os_fork_impl(PyObject *module)
6151/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006152{
Victor Stinner8c62be82010-05-06 00:08:46 +00006153 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006154
Eric Snow59032962018-09-14 14:17:20 -07006155 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6156 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6157 return NULL;
6158 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006159 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006160 pid = fork();
6161 if (pid == 0) {
6162 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006163 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006164 } else {
6165 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006166 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006167 }
6168 if (pid == -1)
6169 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006170 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006171}
Larry Hastings2f936352014-08-05 14:04:04 +10006172#endif /* HAVE_FORK */
6173
Guido van Rossum85e3b011991-06-03 12:42:10 +00006174
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006175#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006176#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006177/*[clinic input]
6178os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006179
Larry Hastings2f936352014-08-05 14:04:04 +10006180 policy: int
6181
6182Get the maximum scheduling priority for policy.
6183[clinic start generated code]*/
6184
Larry Hastings2f936352014-08-05 14:04:04 +10006185static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006186os_sched_get_priority_max_impl(PyObject *module, int policy)
6187/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006188{
6189 int max;
6190
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006191 max = sched_get_priority_max(policy);
6192 if (max < 0)
6193 return posix_error();
6194 return PyLong_FromLong(max);
6195}
6196
Larry Hastings2f936352014-08-05 14:04:04 +10006197
6198/*[clinic input]
6199os.sched_get_priority_min
6200
6201 policy: int
6202
6203Get the minimum scheduling priority for policy.
6204[clinic start generated code]*/
6205
Larry Hastings2f936352014-08-05 14:04:04 +10006206static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006207os_sched_get_priority_min_impl(PyObject *module, int policy)
6208/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006209{
6210 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006211 if (min < 0)
6212 return posix_error();
6213 return PyLong_FromLong(min);
6214}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006215#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6216
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006217
Larry Hastings2f936352014-08-05 14:04:04 +10006218#ifdef HAVE_SCHED_SETSCHEDULER
6219/*[clinic input]
6220os.sched_getscheduler
6221 pid: pid_t
6222 /
6223
Min ho Kimc4cacc82019-07-31 08:16:13 +10006224Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006225
6226Passing 0 for pid returns the scheduling policy for the calling process.
6227[clinic start generated code]*/
6228
Larry Hastings2f936352014-08-05 14:04:04 +10006229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006230os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006231/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006232{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006233 int policy;
6234
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006235 policy = sched_getscheduler(pid);
6236 if (policy < 0)
6237 return posix_error();
6238 return PyLong_FromLong(policy);
6239}
Larry Hastings2f936352014-08-05 14:04:04 +10006240#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006241
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006242
William Orr81574b82018-10-01 22:19:56 -07006243#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006244/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006245class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006246
6247@classmethod
6248os.sched_param.__new__
6249
6250 sched_priority: object
6251 A scheduling parameter.
6252
Eddie Elizondob3966632019-11-05 07:16:14 -08006253Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006254[clinic start generated code]*/
6255
Larry Hastings2f936352014-08-05 14:04:04 +10006256static PyObject *
6257os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006258/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006259{
6260 PyObject *res;
6261
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006262 res = PyStructSequence_New(type);
6263 if (!res)
6264 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006265 Py_INCREF(sched_priority);
6266 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006267 return res;
6268}
6269
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006270PyDoc_VAR(os_sched_param__doc__);
6271
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006272static PyStructSequence_Field sched_param_fields[] = {
6273 {"sched_priority", "the scheduling priority"},
6274 {0}
6275};
6276
6277static PyStructSequence_Desc sched_param_desc = {
6278 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006279 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006280 sched_param_fields,
6281 1
6282};
6283
6284static int
6285convert_sched_param(PyObject *param, struct sched_param *res)
6286{
6287 long priority;
6288
Eddie Elizondob3966632019-11-05 07:16:14 -08006289 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6290 if (Py_TYPE(param) != (PyTypeObject *)SchedParamType) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006291 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6292 return 0;
6293 }
6294 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6295 if (priority == -1 && PyErr_Occurred())
6296 return 0;
6297 if (priority > INT_MAX || priority < INT_MIN) {
6298 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6299 return 0;
6300 }
6301 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6302 return 1;
6303}
William Orr81574b82018-10-01 22:19:56 -07006304#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006305
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006306
6307#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006308/*[clinic input]
6309os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006310
Larry Hastings2f936352014-08-05 14:04:04 +10006311 pid: pid_t
6312 policy: int
6313 param: sched_param
6314 /
6315
6316Set the scheduling policy for the process identified by pid.
6317
6318If pid is 0, the calling process is changed.
6319param is an instance of sched_param.
6320[clinic start generated code]*/
6321
Larry Hastings2f936352014-08-05 14:04:04 +10006322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006323os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006324 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006325/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006326{
Jesus Cea9c822272011-09-10 01:40:52 +02006327 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006328 ** sched_setscheduler() returns 0 in Linux, but the previous
6329 ** scheduling policy under Solaris/Illumos, and others.
6330 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006331 */
Larry Hastings2f936352014-08-05 14:04:04 +10006332 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006333 return posix_error();
6334 Py_RETURN_NONE;
6335}
Larry Hastings2f936352014-08-05 14:04:04 +10006336#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006337
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006338
6339#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006340/*[clinic input]
6341os.sched_getparam
6342 pid: pid_t
6343 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006344
Larry Hastings2f936352014-08-05 14:04:04 +10006345Returns scheduling parameters for the process identified by pid.
6346
6347If pid is 0, returns parameters for the calling process.
6348Return value is an instance of sched_param.
6349[clinic start generated code]*/
6350
Larry Hastings2f936352014-08-05 14:04:04 +10006351static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006352os_sched_getparam_impl(PyObject *module, pid_t pid)
6353/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006354{
6355 struct sched_param param;
6356 PyObject *result;
6357 PyObject *priority;
6358
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006359 if (sched_getparam(pid, &param))
6360 return posix_error();
Eddie Elizondob3966632019-11-05 07:16:14 -08006361 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6362 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006363 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006364 return NULL;
6365 priority = PyLong_FromLong(param.sched_priority);
6366 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006367 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006368 return NULL;
6369 }
Larry Hastings2f936352014-08-05 14:04:04 +10006370 PyStructSequence_SET_ITEM(result, 0, priority);
6371 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006372}
6373
Larry Hastings2f936352014-08-05 14:04:04 +10006374
6375/*[clinic input]
6376os.sched_setparam
6377 pid: pid_t
6378 param: sched_param
6379 /
6380
6381Set scheduling parameters for the process identified by pid.
6382
6383If pid is 0, sets parameters for the calling process.
6384param should be an instance of sched_param.
6385[clinic start generated code]*/
6386
Larry Hastings2f936352014-08-05 14:04:04 +10006387static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006388os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006389 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006390/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006391{
6392 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006393 return posix_error();
6394 Py_RETURN_NONE;
6395}
Larry Hastings2f936352014-08-05 14:04:04 +10006396#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006397
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006398
6399#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006400/*[clinic input]
6401os.sched_rr_get_interval -> double
6402 pid: pid_t
6403 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006404
Larry Hastings2f936352014-08-05 14:04:04 +10006405Return the round-robin quantum for the process identified by pid, in seconds.
6406
6407Value returned is a float.
6408[clinic start generated code]*/
6409
Larry Hastings2f936352014-08-05 14:04:04 +10006410static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006411os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6412/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006413{
6414 struct timespec interval;
6415 if (sched_rr_get_interval(pid, &interval)) {
6416 posix_error();
6417 return -1.0;
6418 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006419#ifdef _Py_MEMORY_SANITIZER
6420 __msan_unpoison(&interval, sizeof(interval));
6421#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006422 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6423}
6424#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006425
Larry Hastings2f936352014-08-05 14:04:04 +10006426
6427/*[clinic input]
6428os.sched_yield
6429
6430Voluntarily relinquish the CPU.
6431[clinic start generated code]*/
6432
Larry Hastings2f936352014-08-05 14:04:04 +10006433static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006434os_sched_yield_impl(PyObject *module)
6435/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006436{
6437 if (sched_yield())
6438 return posix_error();
6439 Py_RETURN_NONE;
6440}
6441
Benjamin Peterson2740af82011-08-02 17:41:34 -05006442#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006443/* The minimum number of CPUs allocated in a cpu_set_t */
6444static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006445
Larry Hastings2f936352014-08-05 14:04:04 +10006446/*[clinic input]
6447os.sched_setaffinity
6448 pid: pid_t
6449 mask : object
6450 /
6451
6452Set the CPU affinity of the process identified by pid to mask.
6453
6454mask should be an iterable of integers identifying CPUs.
6455[clinic start generated code]*/
6456
Larry Hastings2f936352014-08-05 14:04:04 +10006457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006458os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6459/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006460{
Antoine Pitrou84869872012-08-04 16:16:35 +02006461 int ncpus;
6462 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006463 cpu_set_t *cpu_set = NULL;
6464 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006465
Larry Hastings2f936352014-08-05 14:04:04 +10006466 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006467 if (iterator == NULL)
6468 return NULL;
6469
6470 ncpus = NCPUS_START;
6471 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006472 cpu_set = CPU_ALLOC(ncpus);
6473 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006474 PyErr_NoMemory();
6475 goto error;
6476 }
Larry Hastings2f936352014-08-05 14:04:04 +10006477 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006478
6479 while ((item = PyIter_Next(iterator))) {
6480 long cpu;
6481 if (!PyLong_Check(item)) {
6482 PyErr_Format(PyExc_TypeError,
6483 "expected an iterator of ints, "
6484 "but iterator yielded %R",
6485 Py_TYPE(item));
6486 Py_DECREF(item);
6487 goto error;
6488 }
6489 cpu = PyLong_AsLong(item);
6490 Py_DECREF(item);
6491 if (cpu < 0) {
6492 if (!PyErr_Occurred())
6493 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6494 goto error;
6495 }
6496 if (cpu > INT_MAX - 1) {
6497 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6498 goto error;
6499 }
6500 if (cpu >= ncpus) {
6501 /* Grow CPU mask to fit the CPU number */
6502 int newncpus = ncpus;
6503 cpu_set_t *newmask;
6504 size_t newsetsize;
6505 while (newncpus <= cpu) {
6506 if (newncpus > INT_MAX / 2)
6507 newncpus = cpu + 1;
6508 else
6509 newncpus = newncpus * 2;
6510 }
6511 newmask = CPU_ALLOC(newncpus);
6512 if (newmask == NULL) {
6513 PyErr_NoMemory();
6514 goto error;
6515 }
6516 newsetsize = CPU_ALLOC_SIZE(newncpus);
6517 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006518 memcpy(newmask, cpu_set, setsize);
6519 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006520 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006521 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006522 ncpus = newncpus;
6523 }
Larry Hastings2f936352014-08-05 14:04:04 +10006524 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006525 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006526 if (PyErr_Occurred()) {
6527 goto error;
6528 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006529 Py_CLEAR(iterator);
6530
Larry Hastings2f936352014-08-05 14:04:04 +10006531 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006532 posix_error();
6533 goto error;
6534 }
Larry Hastings2f936352014-08-05 14:04:04 +10006535 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006536 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006537
6538error:
Larry Hastings2f936352014-08-05 14:04:04 +10006539 if (cpu_set)
6540 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006541 Py_XDECREF(iterator);
6542 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006543}
6544
Larry Hastings2f936352014-08-05 14:04:04 +10006545
6546/*[clinic input]
6547os.sched_getaffinity
6548 pid: pid_t
6549 /
6550
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006551Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006552
6553The affinity is returned as a set of CPU identifiers.
6554[clinic start generated code]*/
6555
Larry Hastings2f936352014-08-05 14:04:04 +10006556static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006557os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006558/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006559{
Antoine Pitrou84869872012-08-04 16:16:35 +02006560 int cpu, ncpus, count;
6561 size_t setsize;
6562 cpu_set_t *mask = NULL;
6563 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006564
Antoine Pitrou84869872012-08-04 16:16:35 +02006565 ncpus = NCPUS_START;
6566 while (1) {
6567 setsize = CPU_ALLOC_SIZE(ncpus);
6568 mask = CPU_ALLOC(ncpus);
6569 if (mask == NULL)
6570 return PyErr_NoMemory();
6571 if (sched_getaffinity(pid, setsize, mask) == 0)
6572 break;
6573 CPU_FREE(mask);
6574 if (errno != EINVAL)
6575 return posix_error();
6576 if (ncpus > INT_MAX / 2) {
6577 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6578 "a large enough CPU set");
6579 return NULL;
6580 }
6581 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006582 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006583
6584 res = PySet_New(NULL);
6585 if (res == NULL)
6586 goto error;
6587 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6588 if (CPU_ISSET_S(cpu, setsize, mask)) {
6589 PyObject *cpu_num = PyLong_FromLong(cpu);
6590 --count;
6591 if (cpu_num == NULL)
6592 goto error;
6593 if (PySet_Add(res, cpu_num)) {
6594 Py_DECREF(cpu_num);
6595 goto error;
6596 }
6597 Py_DECREF(cpu_num);
6598 }
6599 }
6600 CPU_FREE(mask);
6601 return res;
6602
6603error:
6604 if (mask)
6605 CPU_FREE(mask);
6606 Py_XDECREF(res);
6607 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006608}
6609
Benjamin Peterson2740af82011-08-02 17:41:34 -05006610#endif /* HAVE_SCHED_SETAFFINITY */
6611
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006612#endif /* HAVE_SCHED_H */
6613
Larry Hastings2f936352014-08-05 14:04:04 +10006614
Neal Norwitzb59798b2003-03-21 01:43:31 +00006615/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006616/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6617#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006618#define DEV_PTY_FILE "/dev/ptc"
6619#define HAVE_DEV_PTMX
6620#else
6621#define DEV_PTY_FILE "/dev/ptmx"
6622#endif
6623
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006624#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006625#ifdef HAVE_PTY_H
6626#include <pty.h>
6627#else
6628#ifdef HAVE_LIBUTIL_H
6629#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006630#else
6631#ifdef HAVE_UTIL_H
6632#include <util.h>
6633#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006634#endif /* HAVE_LIBUTIL_H */
6635#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006636#ifdef HAVE_STROPTS_H
6637#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006638#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006639#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006640
Larry Hastings2f936352014-08-05 14:04:04 +10006641
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006642#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006643/*[clinic input]
6644os.openpty
6645
6646Open a pseudo-terminal.
6647
6648Return a tuple of (master_fd, slave_fd) containing open file descriptors
6649for both the master and slave ends.
6650[clinic start generated code]*/
6651
Larry Hastings2f936352014-08-05 14:04:04 +10006652static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006653os_openpty_impl(PyObject *module)
6654/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006655{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006656 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006657#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006658 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006659#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006660#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006661 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006662#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006663 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006664#endif
6665#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006666
Thomas Wouters70c21a12000-07-14 14:28:33 +00006667#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006668 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006669 goto posix_error;
6670
6671 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6672 goto error;
6673 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6674 goto error;
6675
Neal Norwitzb59798b2003-03-21 01:43:31 +00006676#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6678 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006679 goto posix_error;
6680 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6681 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006682
Victor Stinnerdaf45552013-08-28 00:53:59 +02006683 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006684 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006685 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006686
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006687#else
Victor Stinner000de532013-11-25 23:19:58 +01006688 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006689 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006690 goto posix_error;
6691
Victor Stinner8c62be82010-05-06 00:08:46 +00006692 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006693
Victor Stinner8c62be82010-05-06 00:08:46 +00006694 /* change permission of slave */
6695 if (grantpt(master_fd) < 0) {
6696 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006697 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006698 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006699
Victor Stinner8c62be82010-05-06 00:08:46 +00006700 /* unlock slave */
6701 if (unlockpt(master_fd) < 0) {
6702 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006703 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006704 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006705
Victor Stinner8c62be82010-05-06 00:08:46 +00006706 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006707
Victor Stinner8c62be82010-05-06 00:08:46 +00006708 slave_name = ptsname(master_fd); /* get name of slave */
6709 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006710 goto posix_error;
6711
6712 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006713 if (slave_fd == -1)
6714 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006715
6716 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6717 goto posix_error;
6718
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006719#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006720 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6721 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006722#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006723 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006724#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006725#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006726#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006727
Victor Stinner8c62be82010-05-06 00:08:46 +00006728 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006729
Victor Stinnerdaf45552013-08-28 00:53:59 +02006730posix_error:
6731 posix_error();
6732error:
6733 if (master_fd != -1)
6734 close(master_fd);
6735 if (slave_fd != -1)
6736 close(slave_fd);
6737 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006738}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006739#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006740
Larry Hastings2f936352014-08-05 14:04:04 +10006741
Fred Drake8cef4cf2000-06-28 16:40:38 +00006742#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006743/*[clinic input]
6744os.forkpty
6745
6746Fork a new process with a new pseudo-terminal as controlling tty.
6747
6748Returns a tuple of (pid, master_fd).
6749Like fork(), return pid of 0 to the child process,
6750and pid of child to the parent process.
6751To both, return fd of newly opened pseudo-terminal.
6752[clinic start generated code]*/
6753
Larry Hastings2f936352014-08-05 14:04:04 +10006754static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006755os_forkpty_impl(PyObject *module)
6756/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006757{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006758 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006759 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006760
Eric Snow59032962018-09-14 14:17:20 -07006761 if (_PyInterpreterState_Get() != PyInterpreterState_Main()) {
6762 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6763 return NULL;
6764 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006765 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006766 pid = forkpty(&master_fd, NULL, NULL, NULL);
6767 if (pid == 0) {
6768 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006769 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006770 } else {
6771 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006772 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 }
6774 if (pid == -1)
6775 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006776 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006777}
Larry Hastings2f936352014-08-05 14:04:04 +10006778#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006779
Ross Lagerwall7807c352011-03-17 20:20:30 +02006780
Guido van Rossumad0ee831995-03-01 10:34:45 +00006781#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006782/*[clinic input]
6783os.getegid
6784
6785Return the current process's effective group id.
6786[clinic start generated code]*/
6787
Larry Hastings2f936352014-08-05 14:04:04 +10006788static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006789os_getegid_impl(PyObject *module)
6790/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006791{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006792 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006793}
Larry Hastings2f936352014-08-05 14:04:04 +10006794#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006795
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006796
Guido van Rossumad0ee831995-03-01 10:34:45 +00006797#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006798/*[clinic input]
6799os.geteuid
6800
6801Return the current process's effective user id.
6802[clinic start generated code]*/
6803
Larry Hastings2f936352014-08-05 14:04:04 +10006804static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006805os_geteuid_impl(PyObject *module)
6806/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006807{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006808 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006809}
Larry Hastings2f936352014-08-05 14:04:04 +10006810#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006811
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006812
Guido van Rossumad0ee831995-03-01 10:34:45 +00006813#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006814/*[clinic input]
6815os.getgid
6816
6817Return the current process's group id.
6818[clinic start generated code]*/
6819
Larry Hastings2f936352014-08-05 14:04:04 +10006820static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006821os_getgid_impl(PyObject *module)
6822/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006823{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006824 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006825}
Larry Hastings2f936352014-08-05 14:04:04 +10006826#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006827
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006828
Berker Peksag39404992016-09-15 20:45:16 +03006829#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006830/*[clinic input]
6831os.getpid
6832
6833Return the current process id.
6834[clinic start generated code]*/
6835
Larry Hastings2f936352014-08-05 14:04:04 +10006836static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006837os_getpid_impl(PyObject *module)
6838/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006839{
Victor Stinner8c62be82010-05-06 00:08:46 +00006840 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006841}
Berker Peksag39404992016-09-15 20:45:16 +03006842#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006843
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006844#ifdef NGROUPS_MAX
6845#define MAX_GROUPS NGROUPS_MAX
6846#else
6847 /* defined to be 16 on Solaris7, so this should be a small number */
6848#define MAX_GROUPS 64
6849#endif
6850
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006851#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006852
6853/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006854PyDoc_STRVAR(posix_getgrouplist__doc__,
6855"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6856Returns a list of groups to which a user belongs.\n\n\
6857 user: username to lookup\n\
6858 group: base group id of the user");
6859
6860static PyObject *
6861posix_getgrouplist(PyObject *self, PyObject *args)
6862{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006863 const char *user;
6864 int i, ngroups;
6865 PyObject *list;
6866#ifdef __APPLE__
6867 int *groups, basegid;
6868#else
6869 gid_t *groups, basegid;
6870#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006871
6872 /*
6873 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6874 * number of supplimental groups a users can belong to.
6875 * We have to increment it by one because
6876 * getgrouplist() returns both the supplemental groups
6877 * and the primary group, i.e. all of the groups the
6878 * user belongs to.
6879 */
6880 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006881
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006882#ifdef __APPLE__
6883 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006884 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006885#else
6886 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6887 _Py_Gid_Converter, &basegid))
6888 return NULL;
6889#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006890
6891#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006892 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006893#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006894 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006895#endif
6896 if (groups == NULL)
6897 return PyErr_NoMemory();
6898
6899 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6900 PyMem_Del(groups);
6901 return posix_error();
6902 }
6903
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006904#ifdef _Py_MEMORY_SANITIZER
6905 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
6906 __msan_unpoison(&ngroups, sizeof(ngroups));
6907 __msan_unpoison(groups, ngroups*sizeof(*groups));
6908#endif
6909
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006910 list = PyList_New(ngroups);
6911 if (list == NULL) {
6912 PyMem_Del(groups);
6913 return NULL;
6914 }
6915
6916 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006917#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006918 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006919#else
6920 PyObject *o = _PyLong_FromGid(groups[i]);
6921#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006922 if (o == NULL) {
6923 Py_DECREF(list);
6924 PyMem_Del(groups);
6925 return NULL;
6926 }
6927 PyList_SET_ITEM(list, i, o);
6928 }
6929
6930 PyMem_Del(groups);
6931
6932 return list;
6933}
Larry Hastings2f936352014-08-05 14:04:04 +10006934#endif /* HAVE_GETGROUPLIST */
6935
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006936
Fred Drakec9680921999-12-13 16:37:25 +00006937#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006938/*[clinic input]
6939os.getgroups
6940
6941Return list of supplemental group IDs for the process.
6942[clinic start generated code]*/
6943
Larry Hastings2f936352014-08-05 14:04:04 +10006944static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006945os_getgroups_impl(PyObject *module)
6946/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006947{
6948 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006949 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006950
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006951 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006952 * This is a helper variable to store the intermediate result when
6953 * that happens.
6954 *
6955 * To keep the code readable the OSX behaviour is unconditional,
6956 * according to the POSIX spec this should be safe on all unix-y
6957 * systems.
6958 */
6959 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006960 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006961
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006962#ifdef __APPLE__
6963 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6964 * there are more groups than can fit in grouplist. Therefore, on OS X
6965 * always first call getgroups with length 0 to get the actual number
6966 * of groups.
6967 */
6968 n = getgroups(0, NULL);
6969 if (n < 0) {
6970 return posix_error();
6971 } else if (n <= MAX_GROUPS) {
6972 /* groups will fit in existing array */
6973 alt_grouplist = grouplist;
6974 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006975 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006976 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07006977 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006978 }
6979 }
6980
6981 n = getgroups(n, alt_grouplist);
6982 if (n == -1) {
6983 if (alt_grouplist != grouplist) {
6984 PyMem_Free(alt_grouplist);
6985 }
6986 return posix_error();
6987 }
6988#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006989 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006990 if (n < 0) {
6991 if (errno == EINVAL) {
6992 n = getgroups(0, NULL);
6993 if (n == -1) {
6994 return posix_error();
6995 }
6996 if (n == 0) {
6997 /* Avoid malloc(0) */
6998 alt_grouplist = grouplist;
6999 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007000 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007001 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007002 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007003 }
7004 n = getgroups(n, alt_grouplist);
7005 if (n == -1) {
7006 PyMem_Free(alt_grouplist);
7007 return posix_error();
7008 }
7009 }
7010 } else {
7011 return posix_error();
7012 }
7013 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007014#endif
7015
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007016 result = PyList_New(n);
7017 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007018 int i;
7019 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007020 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007021 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007022 Py_DECREF(result);
7023 result = NULL;
7024 break;
Fred Drakec9680921999-12-13 16:37:25 +00007025 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007026 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007027 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007028 }
7029
7030 if (alt_grouplist != grouplist) {
7031 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007032 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007033
Fred Drakec9680921999-12-13 16:37:25 +00007034 return result;
7035}
Larry Hastings2f936352014-08-05 14:04:04 +10007036#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007037
Antoine Pitroub7572f02009-12-02 20:46:48 +00007038#ifdef HAVE_INITGROUPS
7039PyDoc_STRVAR(posix_initgroups__doc__,
7040"initgroups(username, gid) -> None\n\n\
7041Call the system initgroups() to initialize the group access list with all of\n\
7042the groups of which the specified username is a member, plus the specified\n\
7043group id.");
7044
Larry Hastings2f936352014-08-05 14:04:04 +10007045/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00007046static PyObject *
7047posix_initgroups(PyObject *self, PyObject *args)
7048{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007049 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007050 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007051 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007052#ifdef __APPLE__
7053 int gid;
7054#else
7055 gid_t gid;
7056#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007057
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007058#ifdef __APPLE__
7059 if (!PyArg_ParseTuple(args, "O&i:initgroups",
7060 PyUnicode_FSConverter, &oname,
7061 &gid))
7062#else
7063 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
7064 PyUnicode_FSConverter, &oname,
7065 _Py_Gid_Converter, &gid))
7066#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007067 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007068 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007069
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007070 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00007071 Py_DECREF(oname);
7072 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007074
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007075 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007076}
Larry Hastings2f936352014-08-05 14:04:04 +10007077#endif /* HAVE_INITGROUPS */
7078
Antoine Pitroub7572f02009-12-02 20:46:48 +00007079
Martin v. Löwis606edc12002-06-13 21:09:11 +00007080#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007081/*[clinic input]
7082os.getpgid
7083
7084 pid: pid_t
7085
7086Call the system call getpgid(), and return the result.
7087[clinic start generated code]*/
7088
Larry Hastings2f936352014-08-05 14:04:04 +10007089static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007090os_getpgid_impl(PyObject *module, pid_t pid)
7091/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007092{
7093 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007094 if (pgid < 0)
7095 return posix_error();
7096 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007097}
7098#endif /* HAVE_GETPGID */
7099
7100
Guido van Rossumb6775db1994-08-01 11:34:53 +00007101#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007102/*[clinic input]
7103os.getpgrp
7104
7105Return the current process group id.
7106[clinic start generated code]*/
7107
Larry Hastings2f936352014-08-05 14:04:04 +10007108static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007109os_getpgrp_impl(PyObject *module)
7110/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007111{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007112#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007114#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007115 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007116#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007117}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007118#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007119
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007120
Guido van Rossumb6775db1994-08-01 11:34:53 +00007121#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007122/*[clinic input]
7123os.setpgrp
7124
7125Make the current process the leader of its process group.
7126[clinic start generated code]*/
7127
Larry Hastings2f936352014-08-05 14:04:04 +10007128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007129os_setpgrp_impl(PyObject *module)
7130/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007131{
Guido van Rossum64933891994-10-20 21:56:42 +00007132#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007133 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007134#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007135 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007136#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007137 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007138 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007139}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007140#endif /* HAVE_SETPGRP */
7141
Guido van Rossumad0ee831995-03-01 10:34:45 +00007142#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007143
7144#ifdef MS_WINDOWS
7145#include <tlhelp32.h>
7146
7147static PyObject*
7148win32_getppid()
7149{
7150 HANDLE snapshot;
7151 pid_t mypid;
7152 PyObject* result = NULL;
7153 BOOL have_record;
7154 PROCESSENTRY32 pe;
7155
7156 mypid = getpid(); /* This function never fails */
7157
7158 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7159 if (snapshot == INVALID_HANDLE_VALUE)
7160 return PyErr_SetFromWindowsErr(GetLastError());
7161
7162 pe.dwSize = sizeof(pe);
7163 have_record = Process32First(snapshot, &pe);
7164 while (have_record) {
7165 if (mypid == (pid_t)pe.th32ProcessID) {
7166 /* We could cache the ulong value in a static variable. */
7167 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7168 break;
7169 }
7170
7171 have_record = Process32Next(snapshot, &pe);
7172 }
7173
7174 /* If our loop exits and our pid was not found (result will be NULL)
7175 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7176 * error anyway, so let's raise it. */
7177 if (!result)
7178 result = PyErr_SetFromWindowsErr(GetLastError());
7179
7180 CloseHandle(snapshot);
7181
7182 return result;
7183}
7184#endif /*MS_WINDOWS*/
7185
Larry Hastings2f936352014-08-05 14:04:04 +10007186
7187/*[clinic input]
7188os.getppid
7189
7190Return the parent's process id.
7191
7192If the parent process has already exited, Windows machines will still
7193return its id; others systems will return the id of the 'init' process (1).
7194[clinic start generated code]*/
7195
Larry Hastings2f936352014-08-05 14:04:04 +10007196static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007197os_getppid_impl(PyObject *module)
7198/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007199{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007200#ifdef MS_WINDOWS
7201 return win32_getppid();
7202#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007203 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007204#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007205}
7206#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007207
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007208
Fred Drake12c6e2d1999-12-14 21:25:03 +00007209#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007210/*[clinic input]
7211os.getlogin
7212
7213Return the actual login name.
7214[clinic start generated code]*/
7215
Larry Hastings2f936352014-08-05 14:04:04 +10007216static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007217os_getlogin_impl(PyObject *module)
7218/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007219{
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007221#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007222 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007223 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007224
7225 if (GetUserNameW(user_name, &num_chars)) {
7226 /* num_chars is the number of unicode chars plus null terminator */
7227 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007228 }
7229 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007230 result = PyErr_SetFromWindowsErr(GetLastError());
7231#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007232 char *name;
7233 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007234
Victor Stinner8c62be82010-05-06 00:08:46 +00007235 errno = 0;
7236 name = getlogin();
7237 if (name == NULL) {
7238 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007239 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007240 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007241 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007242 }
7243 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007244 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007245 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007246#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007247 return result;
7248}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007249#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007250
Larry Hastings2f936352014-08-05 14:04:04 +10007251
Guido van Rossumad0ee831995-03-01 10:34:45 +00007252#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007253/*[clinic input]
7254os.getuid
7255
7256Return the current process's user id.
7257[clinic start generated code]*/
7258
Larry Hastings2f936352014-08-05 14:04:04 +10007259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007260os_getuid_impl(PyObject *module)
7261/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007262{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007263 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007264}
Larry Hastings2f936352014-08-05 14:04:04 +10007265#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007266
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007267
Brian Curtineb24d742010-04-12 17:16:38 +00007268#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007269#define HAVE_KILL
7270#endif /* MS_WINDOWS */
7271
7272#ifdef HAVE_KILL
7273/*[clinic input]
7274os.kill
7275
7276 pid: pid_t
7277 signal: Py_ssize_t
7278 /
7279
7280Kill a process with a signal.
7281[clinic start generated code]*/
7282
Larry Hastings2f936352014-08-05 14:04:04 +10007283static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007284os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7285/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007286#ifndef MS_WINDOWS
7287{
7288 if (kill(pid, (int)signal) == -1)
7289 return posix_error();
7290 Py_RETURN_NONE;
7291}
7292#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00007293{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007294 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007295 DWORD sig = (DWORD)signal;
7296 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007297 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007298
Victor Stinner8c62be82010-05-06 00:08:46 +00007299 /* Console processes which share a common console can be sent CTRL+C or
7300 CTRL+BREAK events, provided they handle said events. */
7301 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007302 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007303 err = GetLastError();
7304 PyErr_SetFromWindowsErr(err);
7305 }
7306 else
7307 Py_RETURN_NONE;
7308 }
Brian Curtineb24d742010-04-12 17:16:38 +00007309
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7311 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007312 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007313 if (handle == NULL) {
7314 err = GetLastError();
7315 return PyErr_SetFromWindowsErr(err);
7316 }
Brian Curtineb24d742010-04-12 17:16:38 +00007317
Victor Stinner8c62be82010-05-06 00:08:46 +00007318 if (TerminateProcess(handle, sig) == 0) {
7319 err = GetLastError();
7320 result = PyErr_SetFromWindowsErr(err);
7321 } else {
7322 Py_INCREF(Py_None);
7323 result = Py_None;
7324 }
Brian Curtineb24d742010-04-12 17:16:38 +00007325
Victor Stinner8c62be82010-05-06 00:08:46 +00007326 CloseHandle(handle);
7327 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00007328}
Larry Hastings2f936352014-08-05 14:04:04 +10007329#endif /* !MS_WINDOWS */
7330#endif /* HAVE_KILL */
7331
7332
7333#ifdef HAVE_KILLPG
7334/*[clinic input]
7335os.killpg
7336
7337 pgid: pid_t
7338 signal: int
7339 /
7340
7341Kill a process group with a signal.
7342[clinic start generated code]*/
7343
Larry Hastings2f936352014-08-05 14:04:04 +10007344static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007345os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7346/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007347{
7348 /* XXX some man pages make the `pgid` parameter an int, others
7349 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7350 take the same type. Moreover, pid_t is always at least as wide as
7351 int (else compilation of this module fails), which is safe. */
7352 if (killpg(pgid, signal) == -1)
7353 return posix_error();
7354 Py_RETURN_NONE;
7355}
7356#endif /* HAVE_KILLPG */
7357
Brian Curtineb24d742010-04-12 17:16:38 +00007358
Guido van Rossumc0125471996-06-28 18:55:32 +00007359#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007360#ifdef HAVE_SYS_LOCK_H
7361#include <sys/lock.h>
7362#endif
7363
Larry Hastings2f936352014-08-05 14:04:04 +10007364/*[clinic input]
7365os.plock
7366 op: int
7367 /
7368
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007369Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007370[clinic start generated code]*/
7371
Larry Hastings2f936352014-08-05 14:04:04 +10007372static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007373os_plock_impl(PyObject *module, int op)
7374/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007375{
Victor Stinner8c62be82010-05-06 00:08:46 +00007376 if (plock(op) == -1)
7377 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007378 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007379}
Larry Hastings2f936352014-08-05 14:04:04 +10007380#endif /* HAVE_PLOCK */
7381
Guido van Rossumc0125471996-06-28 18:55:32 +00007382
Guido van Rossumb6775db1994-08-01 11:34:53 +00007383#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007384/*[clinic input]
7385os.setuid
7386
7387 uid: uid_t
7388 /
7389
7390Set the current process's user id.
7391[clinic start generated code]*/
7392
Larry Hastings2f936352014-08-05 14:04:04 +10007393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007394os_setuid_impl(PyObject *module, uid_t uid)
7395/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007396{
Victor Stinner8c62be82010-05-06 00:08:46 +00007397 if (setuid(uid) < 0)
7398 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007399 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007400}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007401#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007402
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007403
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007404#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007405/*[clinic input]
7406os.seteuid
7407
7408 euid: uid_t
7409 /
7410
7411Set the current process's effective user id.
7412[clinic start generated code]*/
7413
Larry Hastings2f936352014-08-05 14:04:04 +10007414static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007415os_seteuid_impl(PyObject *module, uid_t euid)
7416/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007417{
7418 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007419 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007420 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007421}
7422#endif /* HAVE_SETEUID */
7423
Larry Hastings2f936352014-08-05 14:04:04 +10007424
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007425#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007426/*[clinic input]
7427os.setegid
7428
7429 egid: gid_t
7430 /
7431
7432Set the current process's effective group id.
7433[clinic start generated code]*/
7434
Larry Hastings2f936352014-08-05 14:04:04 +10007435static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007436os_setegid_impl(PyObject *module, gid_t egid)
7437/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007438{
7439 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007440 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007441 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007442}
7443#endif /* HAVE_SETEGID */
7444
Larry Hastings2f936352014-08-05 14:04:04 +10007445
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007446#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007447/*[clinic input]
7448os.setreuid
7449
7450 ruid: uid_t
7451 euid: uid_t
7452 /
7453
7454Set the current process's real and effective user ids.
7455[clinic start generated code]*/
7456
Larry Hastings2f936352014-08-05 14:04:04 +10007457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007458os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7459/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007460{
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 if (setreuid(ruid, euid) < 0) {
7462 return posix_error();
7463 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007464 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007465 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007466}
7467#endif /* HAVE_SETREUID */
7468
Larry Hastings2f936352014-08-05 14:04:04 +10007469
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007470#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007471/*[clinic input]
7472os.setregid
7473
7474 rgid: gid_t
7475 egid: gid_t
7476 /
7477
7478Set the current process's real and effective group ids.
7479[clinic start generated code]*/
7480
Larry Hastings2f936352014-08-05 14:04:04 +10007481static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007482os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7483/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007484{
7485 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007486 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007487 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00007488}
7489#endif /* HAVE_SETREGID */
7490
Larry Hastings2f936352014-08-05 14:04:04 +10007491
Guido van Rossumb6775db1994-08-01 11:34:53 +00007492#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007493/*[clinic input]
7494os.setgid
7495 gid: gid_t
7496 /
7497
7498Set the current process's group id.
7499[clinic start generated code]*/
7500
Larry Hastings2f936352014-08-05 14:04:04 +10007501static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007502os_setgid_impl(PyObject *module, gid_t gid)
7503/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007504{
Victor Stinner8c62be82010-05-06 00:08:46 +00007505 if (setgid(gid) < 0)
7506 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007507 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007508}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007509#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007510
Larry Hastings2f936352014-08-05 14:04:04 +10007511
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007512#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007513/*[clinic input]
7514os.setgroups
7515
7516 groups: object
7517 /
7518
7519Set the groups of the current process to list.
7520[clinic start generated code]*/
7521
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007522static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007523os_setgroups(PyObject *module, PyObject *groups)
7524/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007525{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007526 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007527 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007528
Victor Stinner8c62be82010-05-06 00:08:46 +00007529 if (!PySequence_Check(groups)) {
7530 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7531 return NULL;
7532 }
7533 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007534 if (len < 0) {
7535 return NULL;
7536 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007537 if (len > MAX_GROUPS) {
7538 PyErr_SetString(PyExc_ValueError, "too many groups");
7539 return NULL;
7540 }
7541 for(i = 0; i < len; i++) {
7542 PyObject *elem;
7543 elem = PySequence_GetItem(groups, i);
7544 if (!elem)
7545 return NULL;
7546 if (!PyLong_Check(elem)) {
7547 PyErr_SetString(PyExc_TypeError,
7548 "groups must be integers");
7549 Py_DECREF(elem);
7550 return NULL;
7551 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007552 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007553 Py_DECREF(elem);
7554 return NULL;
7555 }
7556 }
7557 Py_DECREF(elem);
7558 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007559
Victor Stinner8c62be82010-05-06 00:08:46 +00007560 if (setgroups(len, grouplist) < 0)
7561 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007562 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007563}
7564#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007565
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007566#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7567static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007568wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007569{
Victor Stinner8c62be82010-05-06 00:08:46 +00007570 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08007571 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007572
Victor Stinner8c62be82010-05-06 00:08:46 +00007573 if (pid == -1)
7574 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007575
Zackery Spytz682107c2019-09-09 09:48:32 -06007576 // If wait succeeded but no child was ready to report status, ru will not
7577 // have been populated.
7578 if (pid == 0) {
7579 memset(ru, 0, sizeof(*ru));
7580 }
7581
Eddie Elizondob3966632019-11-05 07:16:14 -08007582 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7583 if (m == NULL)
7584 return NULL;
7585 struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage);
7586 Py_DECREF(m);
7587 if (struct_rusage == NULL)
7588 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007589
Victor Stinner8c62be82010-05-06 00:08:46 +00007590 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7591 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
Eddie Elizondoe4db1f02019-11-25 19:07:37 -08007592 Py_DECREF(struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007593 if (!result)
7594 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007595
7596#ifndef doubletime
7597#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7598#endif
7599
Victor Stinner8c62be82010-05-06 00:08:46 +00007600 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007601 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007602 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007603 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007604#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7606 SET_INT(result, 2, ru->ru_maxrss);
7607 SET_INT(result, 3, ru->ru_ixrss);
7608 SET_INT(result, 4, ru->ru_idrss);
7609 SET_INT(result, 5, ru->ru_isrss);
7610 SET_INT(result, 6, ru->ru_minflt);
7611 SET_INT(result, 7, ru->ru_majflt);
7612 SET_INT(result, 8, ru->ru_nswap);
7613 SET_INT(result, 9, ru->ru_inblock);
7614 SET_INT(result, 10, ru->ru_oublock);
7615 SET_INT(result, 11, ru->ru_msgsnd);
7616 SET_INT(result, 12, ru->ru_msgrcv);
7617 SET_INT(result, 13, ru->ru_nsignals);
7618 SET_INT(result, 14, ru->ru_nvcsw);
7619 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007620#undef SET_INT
7621
Victor Stinner8c62be82010-05-06 00:08:46 +00007622 if (PyErr_Occurred()) {
7623 Py_DECREF(result);
7624 return NULL;
7625 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007626
Victor Stinner8c62be82010-05-06 00:08:46 +00007627 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007628}
7629#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7630
Larry Hastings2f936352014-08-05 14:04:04 +10007631
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007632#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007633/*[clinic input]
7634os.wait3
7635
7636 options: int
7637Wait for completion of a child process.
7638
7639Returns a tuple of information about the child process:
7640 (pid, status, rusage)
7641[clinic start generated code]*/
7642
Larry Hastings2f936352014-08-05 14:04:04 +10007643static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007644os_wait3_impl(PyObject *module, int options)
7645/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007646{
Victor Stinner8c62be82010-05-06 00:08:46 +00007647 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007648 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007649 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007650 WAIT_TYPE status;
7651 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007652
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007653 do {
7654 Py_BEGIN_ALLOW_THREADS
7655 pid = wait3(&status, options, &ru);
7656 Py_END_ALLOW_THREADS
7657 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7658 if (pid < 0)
7659 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007660
Victor Stinner4195b5c2012-02-08 23:03:19 +01007661 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007662}
7663#endif /* HAVE_WAIT3 */
7664
Larry Hastings2f936352014-08-05 14:04:04 +10007665
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007666#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007667/*[clinic input]
7668
7669os.wait4
7670
7671 pid: pid_t
7672 options: int
7673
7674Wait for completion of a specific child process.
7675
7676Returns a tuple of information about the child process:
7677 (pid, status, rusage)
7678[clinic start generated code]*/
7679
Larry Hastings2f936352014-08-05 14:04:04 +10007680static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007681os_wait4_impl(PyObject *module, pid_t pid, int options)
7682/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007683{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007684 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007685 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007686 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007687 WAIT_TYPE status;
7688 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007689
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007690 do {
7691 Py_BEGIN_ALLOW_THREADS
7692 res = wait4(pid, &status, options, &ru);
7693 Py_END_ALLOW_THREADS
7694 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7695 if (res < 0)
7696 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007697
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007698 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007699}
7700#endif /* HAVE_WAIT4 */
7701
Larry Hastings2f936352014-08-05 14:04:04 +10007702
Ross Lagerwall7807c352011-03-17 20:20:30 +02007703#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007704/*[clinic input]
7705os.waitid
7706
7707 idtype: idtype_t
7708 Must be one of be P_PID, P_PGID or P_ALL.
7709 id: id_t
7710 The id to wait on.
7711 options: int
7712 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7713 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7714 /
7715
7716Returns the result of waiting for a process or processes.
7717
7718Returns either waitid_result or None if WNOHANG is specified and there are
7719no children in a waitable state.
7720[clinic start generated code]*/
7721
Larry Hastings2f936352014-08-05 14:04:04 +10007722static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007723os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7724/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007725{
7726 PyObject *result;
7727 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007728 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007729 siginfo_t si;
7730 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007731
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007732 do {
7733 Py_BEGIN_ALLOW_THREADS
7734 res = waitid(idtype, id, &si, options);
7735 Py_END_ALLOW_THREADS
7736 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7737 if (res < 0)
7738 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007739
7740 if (si.si_pid == 0)
7741 Py_RETURN_NONE;
7742
Eddie Elizondob3966632019-11-05 07:16:14 -08007743 PyObject *WaitidResultType = _posixstate(module)->WaitidResultType;
7744 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007745 if (!result)
7746 return NULL;
7747
7748 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007749 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007750 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7751 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7752 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7753 if (PyErr_Occurred()) {
7754 Py_DECREF(result);
7755 return NULL;
7756 }
7757
7758 return result;
7759}
Larry Hastings2f936352014-08-05 14:04:04 +10007760#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007761
Larry Hastings2f936352014-08-05 14:04:04 +10007762
7763#if defined(HAVE_WAITPID)
7764/*[clinic input]
7765os.waitpid
7766 pid: pid_t
7767 options: int
7768 /
7769
7770Wait for completion of a given child process.
7771
7772Returns a tuple of information regarding the child process:
7773 (pid, status)
7774
7775The options argument is ignored on Windows.
7776[clinic start generated code]*/
7777
Larry Hastings2f936352014-08-05 14:04:04 +10007778static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007779os_waitpid_impl(PyObject *module, pid_t pid, int options)
7780/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007781{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007782 pid_t res;
7783 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007784 WAIT_TYPE status;
7785 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007786
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007787 do {
7788 Py_BEGIN_ALLOW_THREADS
7789 res = waitpid(pid, &status, options);
7790 Py_END_ALLOW_THREADS
7791 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7792 if (res < 0)
7793 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007794
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007795 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007796}
Tim Petersab034fa2002-02-01 11:27:43 +00007797#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007798/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007799/*[clinic input]
7800os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007801 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007802 options: int
7803 /
7804
7805Wait for completion of a given process.
7806
7807Returns a tuple of information regarding the process:
7808 (pid, status << 8)
7809
7810The options argument is ignored on Windows.
7811[clinic start generated code]*/
7812
Larry Hastings2f936352014-08-05 14:04:04 +10007813static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007814os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007815/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007816{
7817 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007818 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007819 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007820
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007821 do {
7822 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007823 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007824 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007825 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007826 Py_END_ALLOW_THREADS
7827 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007828 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007829 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007830
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007832 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007833}
Larry Hastings2f936352014-08-05 14:04:04 +10007834#endif
7835
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007836
Guido van Rossumad0ee831995-03-01 10:34:45 +00007837#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007838/*[clinic input]
7839os.wait
7840
7841Wait for completion of a child process.
7842
7843Returns a tuple of information about the child process:
7844 (pid, status)
7845[clinic start generated code]*/
7846
Larry Hastings2f936352014-08-05 14:04:04 +10007847static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007848os_wait_impl(PyObject *module)
7849/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007850{
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007852 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 WAIT_TYPE status;
7854 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007855
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007856 do {
7857 Py_BEGIN_ALLOW_THREADS
7858 pid = wait(&status);
7859 Py_END_ALLOW_THREADS
7860 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7861 if (pid < 0)
7862 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007863
Victor Stinner8c62be82010-05-06 00:08:46 +00007864 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007865}
Larry Hastings2f936352014-08-05 14:04:04 +10007866#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007867
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08007868#if defined(__linux__) && defined(__NR_pidfd_open)
7869/*[clinic input]
7870os.pidfd_open
7871 pid: pid_t
7872 flags: unsigned_int = 0
7873
7874Return a file descriptor referring to the process *pid*.
7875
7876The descriptor can be used to perform process management without races and
7877signals.
7878[clinic start generated code]*/
7879
7880static PyObject *
7881os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
7882/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
7883{
7884 int fd = syscall(__NR_pidfd_open, pid, flags);
7885 if (fd < 0) {
7886 return posix_error();
7887 }
7888 return PyLong_FromLong(fd);
7889}
7890#endif
7891
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007892
Larry Hastings9cf065c2012-06-22 16:30:09 -07007893#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007894/*[clinic input]
7895os.readlink
7896
7897 path: path_t
7898 *
7899 dir_fd: dir_fd(requires='readlinkat') = None
7900
7901Return a string representing the path to which the symbolic link points.
7902
7903If dir_fd is not None, it should be a file descriptor open to a directory,
7904and path should be relative; path will then be relative to that directory.
7905
7906dir_fd may not be implemented on your platform. If it is unavailable,
7907using it will raise a NotImplementedError.
7908[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007909
Barry Warsaw53699e91996-12-10 23:23:01 +00007910static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007911os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
7912/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007913{
Berker Peksage0b5b202018-08-15 13:03:41 +03007914#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02007915 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07007916 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007917
7918 Py_BEGIN_ALLOW_THREADS
7919#ifdef HAVE_READLINKAT
7920 if (dir_fd != DEFAULT_DIR_FD)
7921 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
7922 else
7923#endif
7924 length = readlink(path->narrow, buffer, MAXPATHLEN);
7925 Py_END_ALLOW_THREADS
7926
7927 if (length < 0) {
7928 return path_error(path);
7929 }
7930 buffer[length] = '\0';
7931
7932 if (PyUnicode_Check(path->object))
7933 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7934 else
7935 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03007936#elif defined(MS_WINDOWS)
7937 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007938 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03007939 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03007940 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7941 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07007942 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007943
Larry Hastings2f936352014-08-05 14:04:04 +10007944 /* First get a handle to the reparse point */
7945 Py_BEGIN_ALLOW_THREADS
7946 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007947 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10007948 0,
7949 0,
7950 0,
7951 OPEN_EXISTING,
7952 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7953 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007954 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
7955 /* New call DeviceIoControl to read the reparse point */
7956 io_result = DeviceIoControl(
7957 reparse_point_handle,
7958 FSCTL_GET_REPARSE_POINT,
7959 0, 0, /* in buffer */
7960 target_buffer, sizeof(target_buffer),
7961 &n_bytes_returned,
7962 0 /* we're not using OVERLAPPED_IO */
7963 );
7964 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03007965 }
Larry Hastings2f936352014-08-05 14:04:04 +10007966 Py_END_ALLOW_THREADS
7967
Berker Peksage0b5b202018-08-15 13:03:41 +03007968 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03007969 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03007970 }
Larry Hastings2f936352014-08-05 14:04:04 +10007971
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007972 wchar_t *name = NULL;
7973 Py_ssize_t nameLen = 0;
7974 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10007975 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007976 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
7977 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
7978 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10007979 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007980 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
7981 {
7982 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
7983 rdb->MountPointReparseBuffer.SubstituteNameOffset);
7984 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
7985 }
7986 else
7987 {
7988 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
7989 }
7990 if (name) {
7991 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
7992 /* Our buffer is mutable, so this is okay */
7993 name[1] = L'\\';
7994 }
7995 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07007996 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07007997 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
7998 }
Berker Peksage0b5b202018-08-15 13:03:41 +03007999 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008000 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03008001#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008002}
Berker Peksage0b5b202018-08-15 13:03:41 +03008003#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008004
Larry Hastings9cf065c2012-06-22 16:30:09 -07008005#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07008006
8007#if defined(MS_WINDOWS)
8008
Steve Dower6921e732018-03-05 14:26:08 -08008009/* Remove the last portion of the path - return 0 on success */
8010static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008011_dirnameW(WCHAR *path)
8012{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008013 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008014 size_t length = wcsnlen_s(path, MAX_PATH);
8015 if (length == MAX_PATH) {
8016 return -1;
8017 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008018
8019 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008020 for(ptr = path + length; ptr != path; ptr--) {
8021 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008022 break;
Steve Dower6921e732018-03-05 14:26:08 -08008023 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008024 }
8025 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008026 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008027}
8028
Victor Stinner31b3b922013-06-05 01:49:17 +02008029/* Is this path absolute? */
8030static int
8031_is_absW(const WCHAR *path)
8032{
Steve Dower6921e732018-03-05 14:26:08 -08008033 return path[0] == L'\\' || path[0] == L'/' ||
8034 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008035}
8036
Steve Dower6921e732018-03-05 14:26:08 -08008037/* join root and rest with a backslash - return 0 on success */
8038static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008039_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8040{
Victor Stinner31b3b922013-06-05 01:49:17 +02008041 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008042 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008043 }
8044
Steve Dower6921e732018-03-05 14:26:08 -08008045 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8046 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008047 }
Steve Dower6921e732018-03-05 14:26:08 -08008048
8049 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8050 return -1;
8051 }
8052
8053 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008054}
8055
Victor Stinner31b3b922013-06-05 01:49:17 +02008056/* Return True if the path at src relative to dest is a directory */
8057static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008058_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008059{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008060 WIN32_FILE_ATTRIBUTE_DATA src_info;
8061 WCHAR dest_parent[MAX_PATH];
8062 WCHAR src_resolved[MAX_PATH] = L"";
8063
8064 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008065 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8066 _dirnameW(dest_parent)) {
8067 return 0;
8068 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008069 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008070 if (_joinW(src_resolved, dest_parent, src)) {
8071 return 0;
8072 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008073 return (
8074 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8075 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8076 );
8077}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008078#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008079
Larry Hastings2f936352014-08-05 14:04:04 +10008080
8081/*[clinic input]
8082os.symlink
8083 src: path_t
8084 dst: path_t
8085 target_is_directory: bool = False
8086 *
8087 dir_fd: dir_fd(requires='symlinkat')=None
8088
8089# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8090
8091Create a symbolic link pointing to src named dst.
8092
8093target_is_directory is required on Windows if the target is to be
8094 interpreted as a directory. (On Windows, symlink requires
8095 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8096 target_is_directory is ignored on non-Windows platforms.
8097
8098If dir_fd is not None, it should be a file descriptor open to a directory,
8099 and path should be relative; path will then be relative to that directory.
8100dir_fd may not be implemented on your platform.
8101 If it is unavailable, using it will raise a NotImplementedError.
8102
8103[clinic start generated code]*/
8104
Larry Hastings2f936352014-08-05 14:04:04 +10008105static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008106os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008107 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008108/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008109{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008110#ifdef MS_WINDOWS
8111 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008112 DWORD flags = 0;
8113
8114 /* Assumed true, set to false if detected to not be available. */
8115 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008116#else
8117 int result;
8118#endif
8119
Larry Hastings9cf065c2012-06-22 16:30:09 -07008120#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008121
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008122 if (windows_has_symlink_unprivileged_flag) {
8123 /* Allow non-admin symlinks if system allows it. */
8124 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8125 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008126
Larry Hastings9cf065c2012-06-22 16:30:09 -07008127 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008128 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008129 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8130 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8131 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8132 }
8133
8134 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008135 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008136 Py_END_ALLOW_THREADS
8137
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008138 if (windows_has_symlink_unprivileged_flag && !result &&
8139 ERROR_INVALID_PARAMETER == GetLastError()) {
8140
8141 Py_BEGIN_ALLOW_THREADS
8142 _Py_BEGIN_SUPPRESS_IPH
8143 /* This error might be caused by
8144 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8145 Try again, and update windows_has_symlink_unprivileged_flag if we
8146 are successful this time.
8147
8148 NOTE: There is a risk of a race condition here if there are other
8149 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8150 another process (or thread) changes that condition in between our
8151 calls to CreateSymbolicLink.
8152 */
8153 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8154 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8155 _Py_END_SUPPRESS_IPH
8156 Py_END_ALLOW_THREADS
8157
8158 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8159 windows_has_symlink_unprivileged_flag = FALSE;
8160 }
8161 }
8162
Larry Hastings2f936352014-08-05 14:04:04 +10008163 if (!result)
8164 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008165
8166#else
8167
Steve Dower6921e732018-03-05 14:26:08 -08008168 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8169 PyErr_SetString(PyExc_ValueError,
8170 "symlink: src and dst must be the same type");
8171 return NULL;
8172 }
8173
Larry Hastings9cf065c2012-06-22 16:30:09 -07008174 Py_BEGIN_ALLOW_THREADS
8175#if HAVE_SYMLINKAT
8176 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008177 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008178 else
8179#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008180 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008181 Py_END_ALLOW_THREADS
8182
Larry Hastings2f936352014-08-05 14:04:04 +10008183 if (result)
8184 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008185#endif
8186
Larry Hastings2f936352014-08-05 14:04:04 +10008187 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008188}
8189#endif /* HAVE_SYMLINK */
8190
Larry Hastings9cf065c2012-06-22 16:30:09 -07008191
Brian Curtind40e6f72010-07-08 21:39:08 +00008192
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008193
Larry Hastings605a62d2012-06-24 04:33:36 -07008194static PyStructSequence_Field times_result_fields[] = {
8195 {"user", "user time"},
8196 {"system", "system time"},
8197 {"children_user", "user time of children"},
8198 {"children_system", "system time of children"},
8199 {"elapsed", "elapsed time since an arbitrary point in the past"},
8200 {NULL}
8201};
8202
8203PyDoc_STRVAR(times_result__doc__,
8204"times_result: Result from os.times().\n\n\
8205This object may be accessed either as a tuple of\n\
8206 (user, system, children_user, children_system, elapsed),\n\
8207or via the attributes user, system, children_user, children_system,\n\
8208and elapsed.\n\
8209\n\
8210See os.times for more information.");
8211
8212static PyStructSequence_Desc times_result_desc = {
8213 "times_result", /* name */
8214 times_result__doc__, /* doc */
8215 times_result_fields,
8216 5
8217};
8218
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008219#ifdef MS_WINDOWS
8220#define HAVE_TIMES /* mandatory, for the method table */
8221#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008222
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008223#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008224
8225static PyObject *
8226build_times_result(double user, double system,
8227 double children_user, double children_system,
8228 double elapsed)
8229{
Eddie Elizondob3966632019-11-05 07:16:14 -08008230 PyObject *TimesResultType = _posixstate_global->TimesResultType;
8231 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008232 if (value == NULL)
8233 return NULL;
8234
8235#define SET(i, field) \
8236 { \
8237 PyObject *o = PyFloat_FromDouble(field); \
8238 if (!o) { \
8239 Py_DECREF(value); \
8240 return NULL; \
8241 } \
8242 PyStructSequence_SET_ITEM(value, i, o); \
8243 } \
8244
8245 SET(0, user);
8246 SET(1, system);
8247 SET(2, children_user);
8248 SET(3, children_system);
8249 SET(4, elapsed);
8250
8251#undef SET
8252
8253 return value;
8254}
8255
Larry Hastings605a62d2012-06-24 04:33:36 -07008256
Larry Hastings2f936352014-08-05 14:04:04 +10008257#ifndef MS_WINDOWS
8258#define NEED_TICKS_PER_SECOND
8259static long ticks_per_second = -1;
8260#endif /* MS_WINDOWS */
8261
8262/*[clinic input]
8263os.times
8264
8265Return a collection containing process timing information.
8266
8267The object returned behaves like a named tuple with these fields:
8268 (utime, stime, cutime, cstime, elapsed_time)
8269All fields are floating point numbers.
8270[clinic start generated code]*/
8271
Larry Hastings2f936352014-08-05 14:04:04 +10008272static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008273os_times_impl(PyObject *module)
8274/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008275#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008276{
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 FILETIME create, exit, kernel, user;
8278 HANDLE hProc;
8279 hProc = GetCurrentProcess();
8280 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8281 /* The fields of a FILETIME structure are the hi and lo part
8282 of a 64-bit value expressed in 100 nanosecond units.
8283 1e7 is one second in such units; 1e-7 the inverse.
8284 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8285 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008286 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008287 (double)(user.dwHighDateTime*429.4967296 +
8288 user.dwLowDateTime*1e-7),
8289 (double)(kernel.dwHighDateTime*429.4967296 +
8290 kernel.dwLowDateTime*1e-7),
8291 (double)0,
8292 (double)0,
8293 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008294}
Larry Hastings2f936352014-08-05 14:04:04 +10008295#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008296{
Larry Hastings2f936352014-08-05 14:04:04 +10008297
8298
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008299 struct tms t;
8300 clock_t c;
8301 errno = 0;
8302 c = times(&t);
8303 if (c == (clock_t) -1)
8304 return posix_error();
8305 return build_times_result(
8306 (double)t.tms_utime / ticks_per_second,
8307 (double)t.tms_stime / ticks_per_second,
8308 (double)t.tms_cutime / ticks_per_second,
8309 (double)t.tms_cstime / ticks_per_second,
8310 (double)c / ticks_per_second);
8311}
Larry Hastings2f936352014-08-05 14:04:04 +10008312#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008313#endif /* HAVE_TIMES */
8314
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008315
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008316#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008317/*[clinic input]
8318os.getsid
8319
8320 pid: pid_t
8321 /
8322
8323Call the system call getsid(pid) and return the result.
8324[clinic start generated code]*/
8325
Larry Hastings2f936352014-08-05 14:04:04 +10008326static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008327os_getsid_impl(PyObject *module, pid_t pid)
8328/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008329{
Victor Stinner8c62be82010-05-06 00:08:46 +00008330 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008331 sid = getsid(pid);
8332 if (sid < 0)
8333 return posix_error();
8334 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008335}
8336#endif /* HAVE_GETSID */
8337
8338
Guido van Rossumb6775db1994-08-01 11:34:53 +00008339#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008340/*[clinic input]
8341os.setsid
8342
8343Call the system call setsid().
8344[clinic start generated code]*/
8345
Larry Hastings2f936352014-08-05 14:04:04 +10008346static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008347os_setsid_impl(PyObject *module)
8348/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008349{
Victor Stinner8c62be82010-05-06 00:08:46 +00008350 if (setsid() < 0)
8351 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008352 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008353}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008354#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008355
Larry Hastings2f936352014-08-05 14:04:04 +10008356
Guido van Rossumb6775db1994-08-01 11:34:53 +00008357#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008358/*[clinic input]
8359os.setpgid
8360
8361 pid: pid_t
8362 pgrp: pid_t
8363 /
8364
8365Call the system call setpgid(pid, pgrp).
8366[clinic start generated code]*/
8367
Larry Hastings2f936352014-08-05 14:04:04 +10008368static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008369os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8370/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008371{
Victor Stinner8c62be82010-05-06 00:08:46 +00008372 if (setpgid(pid, pgrp) < 0)
8373 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008374 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008375}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008376#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008377
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008378
Guido van Rossumb6775db1994-08-01 11:34:53 +00008379#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008380/*[clinic input]
8381os.tcgetpgrp
8382
8383 fd: int
8384 /
8385
8386Return the process group associated with the terminal specified by fd.
8387[clinic start generated code]*/
8388
Larry Hastings2f936352014-08-05 14:04:04 +10008389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008390os_tcgetpgrp_impl(PyObject *module, int fd)
8391/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008392{
8393 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008394 if (pgid < 0)
8395 return posix_error();
8396 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008397}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008398#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008399
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008400
Guido van Rossumb6775db1994-08-01 11:34:53 +00008401#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008402/*[clinic input]
8403os.tcsetpgrp
8404
8405 fd: int
8406 pgid: pid_t
8407 /
8408
8409Set the process group associated with the terminal specified by fd.
8410[clinic start generated code]*/
8411
Larry Hastings2f936352014-08-05 14:04:04 +10008412static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008413os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8414/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008415{
Victor Stinner8c62be82010-05-06 00:08:46 +00008416 if (tcsetpgrp(fd, pgid) < 0)
8417 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008418 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008419}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008420#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008421
Guido van Rossum687dd131993-05-17 08:34:16 +00008422/* Functions acting on file descriptors */
8423
Victor Stinnerdaf45552013-08-28 00:53:59 +02008424#ifdef O_CLOEXEC
8425extern int _Py_open_cloexec_works;
8426#endif
8427
Larry Hastings2f936352014-08-05 14:04:04 +10008428
8429/*[clinic input]
8430os.open -> int
8431 path: path_t
8432 flags: int
8433 mode: int = 0o777
8434 *
8435 dir_fd: dir_fd(requires='openat') = None
8436
8437# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8438
8439Open a file for low level IO. Returns a file descriptor (integer).
8440
8441If dir_fd is not None, it should be a file descriptor open to a directory,
8442 and path should be relative; path will then be relative to that directory.
8443dir_fd may not be implemented on your platform.
8444 If it is unavailable, using it will raise a NotImplementedError.
8445[clinic start generated code]*/
8446
Larry Hastings2f936352014-08-05 14:04:04 +10008447static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008448os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8449/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008450{
8451 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008452 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008453
Victor Stinnerdaf45552013-08-28 00:53:59 +02008454#ifdef O_CLOEXEC
8455 int *atomic_flag_works = &_Py_open_cloexec_works;
8456#elif !defined(MS_WINDOWS)
8457 int *atomic_flag_works = NULL;
8458#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008459
Victor Stinnerdaf45552013-08-28 00:53:59 +02008460#ifdef MS_WINDOWS
8461 flags |= O_NOINHERIT;
8462#elif defined(O_CLOEXEC)
8463 flags |= O_CLOEXEC;
8464#endif
8465
Steve Dowerb82e17e2019-05-23 08:45:22 -07008466 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8467 return -1;
8468 }
8469
Steve Dower8fc89802015-04-12 00:26:27 -04008470 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008471 do {
8472 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008473#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008474 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008475#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008476#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008477 if (dir_fd != DEFAULT_DIR_FD)
8478 fd = openat(dir_fd, path->narrow, flags, mode);
8479 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008480#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008481 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008482#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008483 Py_END_ALLOW_THREADS
8484 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008485 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008486
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008487 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008488 if (!async_err)
8489 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008490 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008491 }
8492
Victor Stinnerdaf45552013-08-28 00:53:59 +02008493#ifndef MS_WINDOWS
8494 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8495 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008496 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008497 }
8498#endif
8499
Larry Hastings2f936352014-08-05 14:04:04 +10008500 return fd;
8501}
8502
8503
8504/*[clinic input]
8505os.close
8506
8507 fd: int
8508
8509Close a file descriptor.
8510[clinic start generated code]*/
8511
Barry Warsaw53699e91996-12-10 23:23:01 +00008512static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008513os_close_impl(PyObject *module, int fd)
8514/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008515{
Larry Hastings2f936352014-08-05 14:04:04 +10008516 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008517 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8518 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8519 * for more details.
8520 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008522 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008523 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008524 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 Py_END_ALLOW_THREADS
8526 if (res < 0)
8527 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008528 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008529}
8530
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008531
Jakub Kulíke20134f2019-09-11 17:11:57 +02008532#ifdef HAVE_FDWALK
8533static int
8534_fdwalk_close_func(void *lohi, int fd)
8535{
8536 int lo = ((int *)lohi)[0];
8537 int hi = ((int *)lohi)[1];
8538
8539 if (fd >= hi)
8540 return 1;
8541 else if (fd >= lo)
8542 close(fd);
8543 return 0;
8544}
8545#endif /* HAVE_FDWALK */
8546
Larry Hastings2f936352014-08-05 14:04:04 +10008547/*[clinic input]
8548os.closerange
8549
8550 fd_low: int
8551 fd_high: int
8552 /
8553
8554Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8555[clinic start generated code]*/
8556
Larry Hastings2f936352014-08-05 14:04:04 +10008557static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008558os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8559/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008560{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008561#ifdef HAVE_FDWALK
8562 int lohi[2];
8563#else
Larry Hastings2f936352014-08-05 14:04:04 +10008564 int i;
Jakub Kulíke20134f2019-09-11 17:11:57 +02008565#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008566 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008567 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008568#ifdef HAVE_FDWALK
8569 lohi[0] = Py_MAX(fd_low, 0);
8570 lohi[1] = fd_high;
8571 fdwalk(_fdwalk_close_func, lohi);
8572#else
Benjamin Peterson207116b2016-09-08 11:28:06 -07008573 for (i = Py_MAX(fd_low, 0); i < fd_high; i++)
Steve Dower940f33a2016-09-08 11:21:54 -07008574 close(i);
Jakub Kulíke20134f2019-09-11 17:11:57 +02008575#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008576 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008577 Py_END_ALLOW_THREADS
8578 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008579}
8580
8581
Larry Hastings2f936352014-08-05 14:04:04 +10008582/*[clinic input]
8583os.dup -> int
8584
8585 fd: int
8586 /
8587
8588Return a duplicate of a file descriptor.
8589[clinic start generated code]*/
8590
Larry Hastings2f936352014-08-05 14:04:04 +10008591static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008592os_dup_impl(PyObject *module, int fd)
8593/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008594{
8595 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008596}
8597
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008598
Larry Hastings2f936352014-08-05 14:04:04 +10008599/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008600os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008601 fd: int
8602 fd2: int
8603 inheritable: bool=True
8604
8605Duplicate file descriptor.
8606[clinic start generated code]*/
8607
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008608static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008609os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008610/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008611{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008612 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008613#if defined(HAVE_DUP3) && \
8614 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8615 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008616 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008617#endif
8618
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008619 if (fd < 0 || fd2 < 0) {
8620 posix_error();
8621 return -1;
8622 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008623
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008624 /* dup2() can fail with EINTR if the target FD is already open, because it
8625 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8626 * upon close(), and therefore below.
8627 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008628#ifdef MS_WINDOWS
8629 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008630 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008631 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008632 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008633 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008634 if (res < 0) {
8635 posix_error();
8636 return -1;
8637 }
8638 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008639
8640 /* Character files like console cannot be make non-inheritable */
8641 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8642 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008643 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008644 }
8645
8646#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8647 Py_BEGIN_ALLOW_THREADS
8648 if (!inheritable)
8649 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8650 else
8651 res = dup2(fd, fd2);
8652 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008653 if (res < 0) {
8654 posix_error();
8655 return -1;
8656 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008657
8658#else
8659
8660#ifdef HAVE_DUP3
8661 if (!inheritable && dup3_works != 0) {
8662 Py_BEGIN_ALLOW_THREADS
8663 res = dup3(fd, fd2, O_CLOEXEC);
8664 Py_END_ALLOW_THREADS
8665 if (res < 0) {
8666 if (dup3_works == -1)
8667 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008668 if (dup3_works) {
8669 posix_error();
8670 return -1;
8671 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008672 }
8673 }
8674
8675 if (inheritable || dup3_works == 0)
8676 {
8677#endif
8678 Py_BEGIN_ALLOW_THREADS
8679 res = dup2(fd, fd2);
8680 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008681 if (res < 0) {
8682 posix_error();
8683 return -1;
8684 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008685
8686 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8687 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008688 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008689 }
8690#ifdef HAVE_DUP3
8691 }
8692#endif
8693
8694#endif
8695
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008696 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008697}
8698
Larry Hastings2f936352014-08-05 14:04:04 +10008699
Ross Lagerwall7807c352011-03-17 20:20:30 +02008700#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008701/*[clinic input]
8702os.lockf
8703
8704 fd: int
8705 An open file descriptor.
8706 command: int
8707 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8708 length: Py_off_t
8709 The number of bytes to lock, starting at the current position.
8710 /
8711
8712Apply, test or remove a POSIX lock on an open file descriptor.
8713
8714[clinic start generated code]*/
8715
Larry Hastings2f936352014-08-05 14:04:04 +10008716static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008717os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8718/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008719{
8720 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008721
8722 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008723 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008724 Py_END_ALLOW_THREADS
8725
8726 if (res < 0)
8727 return posix_error();
8728
8729 Py_RETURN_NONE;
8730}
Larry Hastings2f936352014-08-05 14:04:04 +10008731#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008732
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008733
Larry Hastings2f936352014-08-05 14:04:04 +10008734/*[clinic input]
8735os.lseek -> Py_off_t
8736
8737 fd: int
8738 position: Py_off_t
8739 how: int
8740 /
8741
8742Set the position of a file descriptor. Return the new position.
8743
8744Return the new cursor position in number of bytes
8745relative to the beginning of the file.
8746[clinic start generated code]*/
8747
Larry Hastings2f936352014-08-05 14:04:04 +10008748static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008749os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8750/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008751{
8752 Py_off_t result;
8753
Guido van Rossum687dd131993-05-17 08:34:16 +00008754#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008755 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8756 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008757 case 0: how = SEEK_SET; break;
8758 case 1: how = SEEK_CUR; break;
8759 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008760 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008761#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008762
Victor Stinner8c62be82010-05-06 00:08:46 +00008763 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008764 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008765#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008766 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008767#else
Larry Hastings2f936352014-08-05 14:04:04 +10008768 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008769#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008770 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008771 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008772 if (result < 0)
8773 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008774
Larry Hastings2f936352014-08-05 14:04:04 +10008775 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008776}
8777
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008778
Larry Hastings2f936352014-08-05 14:04:04 +10008779/*[clinic input]
8780os.read
8781 fd: int
8782 length: Py_ssize_t
8783 /
8784
8785Read from a file descriptor. Returns a bytes object.
8786[clinic start generated code]*/
8787
Larry Hastings2f936352014-08-05 14:04:04 +10008788static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008789os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8790/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008791{
Victor Stinner8c62be82010-05-06 00:08:46 +00008792 Py_ssize_t n;
8793 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008794
8795 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008796 errno = EINVAL;
8797 return posix_error();
8798 }
Larry Hastings2f936352014-08-05 14:04:04 +10008799
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008800 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008801
8802 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008803 if (buffer == NULL)
8804 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008805
Victor Stinner66aab0c2015-03-19 22:53:20 +01008806 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8807 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008808 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008809 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 }
Larry Hastings2f936352014-08-05 14:04:04 +10008811
8812 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008814
Victor Stinner8c62be82010-05-06 00:08:46 +00008815 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008816}
8817
Ross Lagerwall7807c352011-03-17 20:20:30 +02008818#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008819 || defined(__APPLE__))) \
8820 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8821 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8822static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008823iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008824{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008825 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008826
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008827 *iov = PyMem_New(struct iovec, cnt);
8828 if (*iov == NULL) {
8829 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008830 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008831 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008832
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008833 *buf = PyMem_New(Py_buffer, cnt);
8834 if (*buf == NULL) {
8835 PyMem_Del(*iov);
8836 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008837 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008838 }
8839
8840 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008841 PyObject *item = PySequence_GetItem(seq, i);
8842 if (item == NULL)
8843 goto fail;
8844 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8845 Py_DECREF(item);
8846 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008847 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008848 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008849 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008850 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008851 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008852 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008853
8854fail:
8855 PyMem_Del(*iov);
8856 for (j = 0; j < i; j++) {
8857 PyBuffer_Release(&(*buf)[j]);
8858 }
8859 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008860 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008861}
8862
8863static void
8864iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8865{
8866 int i;
8867 PyMem_Del(iov);
8868 for (i = 0; i < cnt; i++) {
8869 PyBuffer_Release(&buf[i]);
8870 }
8871 PyMem_Del(buf);
8872}
8873#endif
8874
Larry Hastings2f936352014-08-05 14:04:04 +10008875
Ross Lagerwall7807c352011-03-17 20:20:30 +02008876#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008877/*[clinic input]
8878os.readv -> Py_ssize_t
8879
8880 fd: int
8881 buffers: object
8882 /
8883
8884Read from a file descriptor fd into an iterable of buffers.
8885
8886The buffers should be mutable buffers accepting bytes.
8887readv will transfer data into each buffer until it is full
8888and then move on to the next buffer in the sequence to hold
8889the rest of the data.
8890
8891readv returns the total number of bytes read,
8892which may be less than the total capacity of all the buffers.
8893[clinic start generated code]*/
8894
Larry Hastings2f936352014-08-05 14:04:04 +10008895static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008896os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8897/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008898{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008899 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008900 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008901 struct iovec *iov;
8902 Py_buffer *buf;
8903
Larry Hastings2f936352014-08-05 14:04:04 +10008904 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008905 PyErr_SetString(PyExc_TypeError,
8906 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008907 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008908 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008909
Larry Hastings2f936352014-08-05 14:04:04 +10008910 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008911 if (cnt < 0)
8912 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10008913
8914 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8915 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008916
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008917 do {
8918 Py_BEGIN_ALLOW_THREADS
8919 n = readv(fd, iov, cnt);
8920 Py_END_ALLOW_THREADS
8921 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008922
8923 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008924 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008925 if (!async_err)
8926 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008927 return -1;
8928 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008929
Larry Hastings2f936352014-08-05 14:04:04 +10008930 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008931}
Larry Hastings2f936352014-08-05 14:04:04 +10008932#endif /* HAVE_READV */
8933
Ross Lagerwall7807c352011-03-17 20:20:30 +02008934
8935#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008936/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10008937os.pread
8938
8939 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09008940 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10008941 offset: Py_off_t
8942 /
8943
8944Read a number of bytes from a file descriptor starting at a particular offset.
8945
8946Read length bytes from file descriptor fd, starting at offset bytes from
8947the beginning of the file. The file offset remains unchanged.
8948[clinic start generated code]*/
8949
Larry Hastings2f936352014-08-05 14:04:04 +10008950static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09008951os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
8952/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008953{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008954 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008955 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008956 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008957
Larry Hastings2f936352014-08-05 14:04:04 +10008958 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008959 errno = EINVAL;
8960 return posix_error();
8961 }
Larry Hastings2f936352014-08-05 14:04:04 +10008962 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008963 if (buffer == NULL)
8964 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008965
8966 do {
8967 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008968 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008969 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008970 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008971 Py_END_ALLOW_THREADS
8972 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8973
Ross Lagerwall7807c352011-03-17 20:20:30 +02008974 if (n < 0) {
8975 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008976 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008977 }
Larry Hastings2f936352014-08-05 14:04:04 +10008978 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008979 _PyBytes_Resize(&buffer, n);
8980 return buffer;
8981}
Larry Hastings2f936352014-08-05 14:04:04 +10008982#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008983
Pablo Galindo4defba32018-01-27 16:16:37 +00008984#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
8985/*[clinic input]
8986os.preadv -> Py_ssize_t
8987
8988 fd: int
8989 buffers: object
8990 offset: Py_off_t
8991 flags: int = 0
8992 /
8993
8994Reads from a file descriptor into a number of mutable bytes-like objects.
8995
8996Combines the functionality of readv() and pread(). As readv(), it will
8997transfer data into each buffer until it is full and then move on to the next
8998buffer in the sequence to hold the rest of the data. Its fourth argument,
8999specifies the file offset at which the input operation is to be performed. It
9000will return the total number of bytes read (which can be less than the total
9001capacity of all the objects).
9002
9003The flags argument contains a bitwise OR of zero or more of the following flags:
9004
9005- RWF_HIPRI
9006- RWF_NOWAIT
9007
9008Using non-zero flags requires Linux 4.6 or newer.
9009[clinic start generated code]*/
9010
9011static Py_ssize_t
9012os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9013 int flags)
9014/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9015{
9016 Py_ssize_t cnt, n;
9017 int async_err = 0;
9018 struct iovec *iov;
9019 Py_buffer *buf;
9020
9021 if (!PySequence_Check(buffers)) {
9022 PyErr_SetString(PyExc_TypeError,
9023 "preadv2() arg 2 must be a sequence");
9024 return -1;
9025 }
9026
9027 cnt = PySequence_Size(buffers);
9028 if (cnt < 0) {
9029 return -1;
9030 }
9031
9032#ifndef HAVE_PREADV2
9033 if(flags != 0) {
9034 argument_unavailable_error("preadv2", "flags");
9035 return -1;
9036 }
9037#endif
9038
9039 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9040 return -1;
9041 }
9042#ifdef HAVE_PREADV2
9043 do {
9044 Py_BEGIN_ALLOW_THREADS
9045 _Py_BEGIN_SUPPRESS_IPH
9046 n = preadv2(fd, iov, cnt, offset, flags);
9047 _Py_END_SUPPRESS_IPH
9048 Py_END_ALLOW_THREADS
9049 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9050#else
9051 do {
9052 Py_BEGIN_ALLOW_THREADS
9053 _Py_BEGIN_SUPPRESS_IPH
9054 n = preadv(fd, iov, cnt, offset);
9055 _Py_END_SUPPRESS_IPH
9056 Py_END_ALLOW_THREADS
9057 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9058#endif
9059
9060 iov_cleanup(iov, buf, cnt);
9061 if (n < 0) {
9062 if (!async_err) {
9063 posix_error();
9064 }
9065 return -1;
9066 }
9067
9068 return n;
9069}
9070#endif /* HAVE_PREADV */
9071
Larry Hastings2f936352014-08-05 14:04:04 +10009072
9073/*[clinic input]
9074os.write -> Py_ssize_t
9075
9076 fd: int
9077 data: Py_buffer
9078 /
9079
9080Write a bytes object to a file descriptor.
9081[clinic start generated code]*/
9082
Larry Hastings2f936352014-08-05 14:04:04 +10009083static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009084os_write_impl(PyObject *module, int fd, Py_buffer *data)
9085/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009086{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009087 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009088}
9089
9090#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009091PyDoc_STRVAR(posix_sendfile__doc__,
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009092"sendfile(out_fd, in_fd, offset, count) -> byteswritten\n\
9093sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009094 -> byteswritten\n\
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009095Copy count bytes from file descriptor in_fd to file descriptor out_fd.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009096
Larry Hastings2f936352014-08-05 14:04:04 +10009097/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009098static PyObject *
9099posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
9100{
9101 int in, out;
9102 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009103 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009104 off_t offset;
9105
9106#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9107#ifndef __APPLE__
9108 Py_ssize_t len;
9109#endif
9110 PyObject *headers = NULL, *trailers = NULL;
9111 Py_buffer *hbuf, *tbuf;
9112 off_t sbytes;
9113 struct sf_hdtr sf;
9114 int flags = 0;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009115 static char *keywords[] = {"out_fd", "in_fd",
Benjamin Petersond8a43b42011-02-26 21:35:16 +00009116 "offset", "count",
9117 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009118
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009119 sf.headers = NULL;
9120 sf.trailers = NULL;
9121
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009122#ifdef __APPLE__
9123 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009124 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009125#else
9126 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10009127 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009128#endif
9129 &headers, &trailers, &flags))
9130 return NULL;
9131 if (headers != NULL) {
9132 if (!PySequence_Check(headers)) {
9133 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009134 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009135 return NULL;
9136 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009137 Py_ssize_t i = PySequence_Size(headers);
9138 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009139 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009140 if (i > INT_MAX) {
9141 PyErr_SetString(PyExc_OverflowError,
9142 "sendfile() header is too large");
9143 return NULL;
9144 }
9145 if (i > 0) {
9146 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009147 if (iov_setup(&(sf.headers), &hbuf,
9148 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009149 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009150#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009151 for (i = 0; i < sf.hdr_cnt; i++) {
9152 Py_ssize_t blen = sf.headers[i].iov_len;
9153# define OFF_T_MAX 0x7fffffffffffffff
9154 if (sbytes >= OFF_T_MAX - blen) {
9155 PyErr_SetString(PyExc_OverflowError,
9156 "sendfile() header is too large");
9157 return NULL;
9158 }
9159 sbytes += blen;
9160 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009161#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009162 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009163 }
9164 }
9165 if (trailers != NULL) {
9166 if (!PySequence_Check(trailers)) {
9167 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009168 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009169 return NULL;
9170 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009171 Py_ssize_t i = PySequence_Size(trailers);
9172 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009173 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009174 if (i > INT_MAX) {
9175 PyErr_SetString(PyExc_OverflowError,
9176 "sendfile() trailer is too large");
9177 return NULL;
9178 }
9179 if (i > 0) {
9180 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009181 if (iov_setup(&(sf.trailers), &tbuf,
9182 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009183 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009184 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009185 }
9186 }
9187
Steve Dower8fc89802015-04-12 00:26:27 -04009188 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009189 do {
9190 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009191#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009192 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009193#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009194 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009195#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009196 Py_END_ALLOW_THREADS
9197 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009198 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009199
9200 if (sf.headers != NULL)
9201 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9202 if (sf.trailers != NULL)
9203 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9204
9205 if (ret < 0) {
9206 if ((errno == EAGAIN) || (errno == EBUSY)) {
9207 if (sbytes != 0) {
9208 // some data has been sent
9209 goto done;
9210 }
9211 else {
9212 // no data has been sent; upper application is supposed
9213 // to retry on EAGAIN or EBUSY
9214 return posix_error();
9215 }
9216 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009217 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009218 }
9219 goto done;
9220
9221done:
9222 #if !defined(HAVE_LARGEFILE_SUPPORT)
9223 return Py_BuildValue("l", sbytes);
9224 #else
9225 return Py_BuildValue("L", sbytes);
9226 #endif
9227
9228#else
9229 Py_ssize_t count;
9230 PyObject *offobj;
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009231 static char *keywords[] = {"out_fd", "in_fd",
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009232 "offset", "count", NULL};
9233 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
9234 keywords, &out, &in, &offobj, &count))
9235 return NULL;
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009236#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009237 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009238 do {
9239 Py_BEGIN_ALLOW_THREADS
9240 ret = sendfile(out, in, NULL, count);
9241 Py_END_ALLOW_THREADS
9242 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009243 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009244 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009245 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009246 }
9247#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009248 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009249 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009250
9251 do {
9252 Py_BEGIN_ALLOW_THREADS
9253 ret = sendfile(out, in, &offset, count);
9254 Py_END_ALLOW_THREADS
9255 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009256 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009257 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009258 return Py_BuildValue("n", ret);
9259#endif
9260}
Larry Hastings2f936352014-08-05 14:04:04 +10009261#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009262
Larry Hastings2f936352014-08-05 14:04:04 +10009263
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009264#if defined(__APPLE__)
9265/*[clinic input]
9266os._fcopyfile
9267
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009268 in_fd: int
9269 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009270 flags: int
9271 /
9272
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009273Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009274[clinic start generated code]*/
9275
9276static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009277os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9278/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009279{
9280 int ret;
9281
9282 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009283 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009284 Py_END_ALLOW_THREADS
9285 if (ret < 0)
9286 return posix_error();
9287 Py_RETURN_NONE;
9288}
9289#endif
9290
9291
Larry Hastings2f936352014-08-05 14:04:04 +10009292/*[clinic input]
9293os.fstat
9294
9295 fd : int
9296
9297Perform a stat system call on the given file descriptor.
9298
9299Like stat(), but for an open file descriptor.
9300Equivalent to os.stat(fd).
9301[clinic start generated code]*/
9302
Larry Hastings2f936352014-08-05 14:04:04 +10009303static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009304os_fstat_impl(PyObject *module, int fd)
9305/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009306{
Victor Stinner8c62be82010-05-06 00:08:46 +00009307 STRUCT_STAT st;
9308 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009309 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009310
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009311 do {
9312 Py_BEGIN_ALLOW_THREADS
9313 res = FSTAT(fd, &st);
9314 Py_END_ALLOW_THREADS
9315 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009316 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009317#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009318 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009319#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009320 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009321#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009322 }
Tim Peters5aa91602002-01-30 05:46:57 +00009323
Victor Stinner4195b5c2012-02-08 23:03:19 +01009324 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009325}
9326
Larry Hastings2f936352014-08-05 14:04:04 +10009327
9328/*[clinic input]
9329os.isatty -> bool
9330 fd: int
9331 /
9332
9333Return True if the fd is connected to a terminal.
9334
9335Return True if the file descriptor is an open file descriptor
9336connected to the slave end of a terminal.
9337[clinic start generated code]*/
9338
Larry Hastings2f936352014-08-05 14:04:04 +10009339static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009340os_isatty_impl(PyObject *module, int fd)
9341/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009342{
Steve Dower8fc89802015-04-12 00:26:27 -04009343 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009344 _Py_BEGIN_SUPPRESS_IPH
9345 return_value = isatty(fd);
9346 _Py_END_SUPPRESS_IPH
9347 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009348}
9349
9350
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009351#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009352/*[clinic input]
9353os.pipe
9354
9355Create a pipe.
9356
9357Returns a tuple of two file descriptors:
9358 (read_fd, write_fd)
9359[clinic start generated code]*/
9360
Larry Hastings2f936352014-08-05 14:04:04 +10009361static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009362os_pipe_impl(PyObject *module)
9363/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009364{
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009366#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009367 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009368 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009369 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009370#else
9371 int res;
9372#endif
9373
9374#ifdef MS_WINDOWS
9375 attr.nLength = sizeof(attr);
9376 attr.lpSecurityDescriptor = NULL;
9377 attr.bInheritHandle = FALSE;
9378
9379 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009380 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009381 ok = CreatePipe(&read, &write, &attr, 0);
9382 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009383 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9384 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009385 if (fds[0] == -1 || fds[1] == -1) {
9386 CloseHandle(read);
9387 CloseHandle(write);
9388 ok = 0;
9389 }
9390 }
Steve Dowerc3630612016-11-19 18:41:16 -08009391 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009392 Py_END_ALLOW_THREADS
9393
Victor Stinner8c62be82010-05-06 00:08:46 +00009394 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009395 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009396#else
9397
9398#ifdef HAVE_PIPE2
9399 Py_BEGIN_ALLOW_THREADS
9400 res = pipe2(fds, O_CLOEXEC);
9401 Py_END_ALLOW_THREADS
9402
9403 if (res != 0 && errno == ENOSYS)
9404 {
9405#endif
9406 Py_BEGIN_ALLOW_THREADS
9407 res = pipe(fds);
9408 Py_END_ALLOW_THREADS
9409
9410 if (res == 0) {
9411 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9412 close(fds[0]);
9413 close(fds[1]);
9414 return NULL;
9415 }
9416 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9417 close(fds[0]);
9418 close(fds[1]);
9419 return NULL;
9420 }
9421 }
9422#ifdef HAVE_PIPE2
9423 }
9424#endif
9425
9426 if (res != 0)
9427 return PyErr_SetFromErrno(PyExc_OSError);
9428#endif /* !MS_WINDOWS */
9429 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009430}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009431#endif /* HAVE_PIPE */
9432
Larry Hastings2f936352014-08-05 14:04:04 +10009433
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009434#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009435/*[clinic input]
9436os.pipe2
9437
9438 flags: int
9439 /
9440
9441Create a pipe with flags set atomically.
9442
9443Returns a tuple of two file descriptors:
9444 (read_fd, write_fd)
9445
9446flags can be constructed by ORing together one or more of these values:
9447O_NONBLOCK, O_CLOEXEC.
9448[clinic start generated code]*/
9449
Larry Hastings2f936352014-08-05 14:04:04 +10009450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009451os_pipe2_impl(PyObject *module, int flags)
9452/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009453{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009454 int fds[2];
9455 int res;
9456
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009457 res = pipe2(fds, flags);
9458 if (res != 0)
9459 return posix_error();
9460 return Py_BuildValue("(ii)", fds[0], fds[1]);
9461}
9462#endif /* HAVE_PIPE2 */
9463
Larry Hastings2f936352014-08-05 14:04:04 +10009464
Ross Lagerwall7807c352011-03-17 20:20:30 +02009465#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009466/*[clinic input]
9467os.writev -> Py_ssize_t
9468 fd: int
9469 buffers: object
9470 /
9471
9472Iterate over buffers, and write the contents of each to a file descriptor.
9473
9474Returns the total number of bytes written.
9475buffers must be a sequence of bytes-like objects.
9476[clinic start generated code]*/
9477
Larry Hastings2f936352014-08-05 14:04:04 +10009478static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009479os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9480/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009481{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009482 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009483 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009484 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009485 struct iovec *iov;
9486 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009487
9488 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009489 PyErr_SetString(PyExc_TypeError,
9490 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009491 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009492 }
Larry Hastings2f936352014-08-05 14:04:04 +10009493 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009494 if (cnt < 0)
9495 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009496
Larry Hastings2f936352014-08-05 14:04:04 +10009497 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9498 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009499 }
9500
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009501 do {
9502 Py_BEGIN_ALLOW_THREADS
9503 result = writev(fd, iov, cnt);
9504 Py_END_ALLOW_THREADS
9505 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009506
9507 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009508 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009509 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009510
Georg Brandl306336b2012-06-24 12:55:33 +02009511 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009512}
Larry Hastings2f936352014-08-05 14:04:04 +10009513#endif /* HAVE_WRITEV */
9514
9515
9516#ifdef HAVE_PWRITE
9517/*[clinic input]
9518os.pwrite -> Py_ssize_t
9519
9520 fd: int
9521 buffer: Py_buffer
9522 offset: Py_off_t
9523 /
9524
9525Write bytes to a file descriptor starting at a particular offset.
9526
9527Write buffer to fd, starting at offset bytes from the beginning of
9528the file. Returns the number of bytes writte. Does not change the
9529current file offset.
9530[clinic start generated code]*/
9531
Larry Hastings2f936352014-08-05 14:04:04 +10009532static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009533os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9534/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009535{
9536 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009537 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009538
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009539 do {
9540 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009541 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009542 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009543 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009544 Py_END_ALLOW_THREADS
9545 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009546
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009547 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009548 posix_error();
9549 return size;
9550}
9551#endif /* HAVE_PWRITE */
9552
Pablo Galindo4defba32018-01-27 16:16:37 +00009553#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9554/*[clinic input]
9555os.pwritev -> Py_ssize_t
9556
9557 fd: int
9558 buffers: object
9559 offset: Py_off_t
9560 flags: int = 0
9561 /
9562
9563Writes the contents of bytes-like objects to a file descriptor at a given offset.
9564
9565Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9566of bytes-like objects. Buffers are processed in array order. Entire contents of first
9567buffer is written before proceeding to second, and so on. The operating system may
9568set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9569This function writes the contents of each object to the file descriptor and returns
9570the total number of bytes written.
9571
9572The flags argument contains a bitwise OR of zero or more of the following flags:
9573
9574- RWF_DSYNC
9575- RWF_SYNC
9576
9577Using non-zero flags requires Linux 4.7 or newer.
9578[clinic start generated code]*/
9579
9580static Py_ssize_t
9581os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9582 int flags)
9583/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9584{
9585 Py_ssize_t cnt;
9586 Py_ssize_t result;
9587 int async_err = 0;
9588 struct iovec *iov;
9589 Py_buffer *buf;
9590
9591 if (!PySequence_Check(buffers)) {
9592 PyErr_SetString(PyExc_TypeError,
9593 "pwritev() arg 2 must be a sequence");
9594 return -1;
9595 }
9596
9597 cnt = PySequence_Size(buffers);
9598 if (cnt < 0) {
9599 return -1;
9600 }
9601
9602#ifndef HAVE_PWRITEV2
9603 if(flags != 0) {
9604 argument_unavailable_error("pwritev2", "flags");
9605 return -1;
9606 }
9607#endif
9608
9609 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9610 return -1;
9611 }
9612#ifdef HAVE_PWRITEV2
9613 do {
9614 Py_BEGIN_ALLOW_THREADS
9615 _Py_BEGIN_SUPPRESS_IPH
9616 result = pwritev2(fd, iov, cnt, offset, flags);
9617 _Py_END_SUPPRESS_IPH
9618 Py_END_ALLOW_THREADS
9619 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9620#else
9621 do {
9622 Py_BEGIN_ALLOW_THREADS
9623 _Py_BEGIN_SUPPRESS_IPH
9624 result = pwritev(fd, iov, cnt, offset);
9625 _Py_END_SUPPRESS_IPH
9626 Py_END_ALLOW_THREADS
9627 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9628#endif
9629
9630 iov_cleanup(iov, buf, cnt);
9631 if (result < 0) {
9632 if (!async_err) {
9633 posix_error();
9634 }
9635 return -1;
9636 }
9637
9638 return result;
9639}
9640#endif /* HAVE_PWRITEV */
9641
Pablo Galindoaac4d032019-05-31 19:39:47 +01009642#ifdef HAVE_COPY_FILE_RANGE
9643/*[clinic input]
9644
9645os.copy_file_range
9646 src: int
9647 Source file descriptor.
9648 dst: int
9649 Destination file descriptor.
9650 count: Py_ssize_t
9651 Number of bytes to copy.
9652 offset_src: object = None
9653 Starting offset in src.
9654 offset_dst: object = None
9655 Starting offset in dst.
9656
9657Copy count bytes from one file descriptor to another.
9658
9659If offset_src is None, then src is read from the current position;
9660respectively for offset_dst.
9661[clinic start generated code]*/
9662
9663static PyObject *
9664os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9665 PyObject *offset_src, PyObject *offset_dst)
9666/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9667{
9668 off_t offset_src_val, offset_dst_val;
9669 off_t *p_offset_src = NULL;
9670 off_t *p_offset_dst = NULL;
9671 Py_ssize_t ret;
9672 int async_err = 0;
9673 /* The flags argument is provided to allow
9674 * for future extensions and currently must be to 0. */
9675 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009676
9677
Pablo Galindoaac4d032019-05-31 19:39:47 +01009678 if (count < 0) {
9679 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9680 return NULL;
9681 }
9682
9683 if (offset_src != Py_None) {
9684 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9685 return NULL;
9686 }
9687 p_offset_src = &offset_src_val;
9688 }
9689
9690 if (offset_dst != Py_None) {
9691 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9692 return NULL;
9693 }
9694 p_offset_dst = &offset_dst_val;
9695 }
9696
9697 do {
9698 Py_BEGIN_ALLOW_THREADS
9699 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9700 Py_END_ALLOW_THREADS
9701 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9702
9703 if (ret < 0) {
9704 return (!async_err) ? posix_error() : NULL;
9705 }
9706
9707 return PyLong_FromSsize_t(ret);
9708}
9709#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009710
9711#ifdef HAVE_MKFIFO
9712/*[clinic input]
9713os.mkfifo
9714
9715 path: path_t
9716 mode: int=0o666
9717 *
9718 dir_fd: dir_fd(requires='mkfifoat')=None
9719
9720Create a "fifo" (a POSIX named pipe).
9721
9722If dir_fd is not None, it should be a file descriptor open to a directory,
9723 and path should be relative; path will then be relative to that directory.
9724dir_fd may not be implemented on your platform.
9725 If it is unavailable, using it will raise a NotImplementedError.
9726[clinic start generated code]*/
9727
Larry Hastings2f936352014-08-05 14:04:04 +10009728static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009729os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9730/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009731{
9732 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009733 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009734
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009735 do {
9736 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009737#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009738 if (dir_fd != DEFAULT_DIR_FD)
9739 result = mkfifoat(dir_fd, path->narrow, mode);
9740 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009741#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009742 result = mkfifo(path->narrow, mode);
9743 Py_END_ALLOW_THREADS
9744 } while (result != 0 && errno == EINTR &&
9745 !(async_err = PyErr_CheckSignals()));
9746 if (result != 0)
9747 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009748
9749 Py_RETURN_NONE;
9750}
9751#endif /* HAVE_MKFIFO */
9752
9753
9754#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9755/*[clinic input]
9756os.mknod
9757
9758 path: path_t
9759 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009760 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009761 *
9762 dir_fd: dir_fd(requires='mknodat')=None
9763
9764Create a node in the file system.
9765
9766Create a node in the file system (file, device special file or named pipe)
9767at path. mode specifies both the permissions to use and the
9768type of node to be created, being combined (bitwise OR) with one of
9769S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9770device defines the newly created device special file (probably using
9771os.makedev()). Otherwise device is ignored.
9772
9773If dir_fd is not None, it should be a file descriptor open to a directory,
9774 and path should be relative; path will then be relative to that directory.
9775dir_fd may not be implemented on your platform.
9776 If it is unavailable, using it will raise a NotImplementedError.
9777[clinic start generated code]*/
9778
Larry Hastings2f936352014-08-05 14:04:04 +10009779static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009780os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009781 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009782/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009783{
9784 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009785 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009786
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009787 do {
9788 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009789#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009790 if (dir_fd != DEFAULT_DIR_FD)
9791 result = mknodat(dir_fd, path->narrow, mode, device);
9792 else
Larry Hastings2f936352014-08-05 14:04:04 +10009793#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009794 result = mknod(path->narrow, mode, device);
9795 Py_END_ALLOW_THREADS
9796 } while (result != 0 && errno == EINTR &&
9797 !(async_err = PyErr_CheckSignals()));
9798 if (result != 0)
9799 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009800
9801 Py_RETURN_NONE;
9802}
9803#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9804
9805
9806#ifdef HAVE_DEVICE_MACROS
9807/*[clinic input]
9808os.major -> unsigned_int
9809
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009810 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009811 /
9812
9813Extracts a device major number from a raw device number.
9814[clinic start generated code]*/
9815
Larry Hastings2f936352014-08-05 14:04:04 +10009816static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009817os_major_impl(PyObject *module, dev_t device)
9818/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009819{
9820 return major(device);
9821}
9822
9823
9824/*[clinic input]
9825os.minor -> unsigned_int
9826
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009827 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009828 /
9829
9830Extracts a device minor number from a raw device number.
9831[clinic start generated code]*/
9832
Larry Hastings2f936352014-08-05 14:04:04 +10009833static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009834os_minor_impl(PyObject *module, dev_t device)
9835/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009836{
9837 return minor(device);
9838}
9839
9840
9841/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009842os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10009843
9844 major: int
9845 minor: int
9846 /
9847
9848Composes a raw device number from the major and minor device numbers.
9849[clinic start generated code]*/
9850
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009851static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009852os_makedev_impl(PyObject *module, int major, int minor)
9853/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009854{
9855 return makedev(major, minor);
9856}
9857#endif /* HAVE_DEVICE_MACROS */
9858
9859
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009860#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009861/*[clinic input]
9862os.ftruncate
9863
9864 fd: int
9865 length: Py_off_t
9866 /
9867
9868Truncate a file, specified by file descriptor, to a specific length.
9869[clinic start generated code]*/
9870
Larry Hastings2f936352014-08-05 14:04:04 +10009871static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009872os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
9873/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009874{
9875 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009876 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009877
Steve Dowerb82e17e2019-05-23 08:45:22 -07009878 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
9879 return NULL;
9880 }
9881
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009882 do {
9883 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009884 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009885#ifdef MS_WINDOWS
9886 result = _chsize_s(fd, length);
9887#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009888 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009889#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009890 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009891 Py_END_ALLOW_THREADS
9892 } while (result != 0 && errno == EINTR &&
9893 !(async_err = PyErr_CheckSignals()));
9894 if (result != 0)
9895 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009896 Py_RETURN_NONE;
9897}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009898#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009899
9900
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009901#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009902/*[clinic input]
9903os.truncate
9904 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
9905 length: Py_off_t
9906
9907Truncate a file, specified by path, to a specific length.
9908
9909On some platforms, path may also be specified as an open file descriptor.
9910 If this functionality is unavailable, using it raises an exception.
9911[clinic start generated code]*/
9912
Larry Hastings2f936352014-08-05 14:04:04 +10009913static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009914os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
9915/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009916{
9917 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009918#ifdef MS_WINDOWS
9919 int fd;
9920#endif
9921
9922 if (path->fd != -1)
9923 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009924
Steve Dowerb82e17e2019-05-23 08:45:22 -07009925 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
9926 return NULL;
9927 }
9928
Larry Hastings2f936352014-08-05 14:04:04 +10009929 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04009930 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009931#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009932 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02009933 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009934 result = -1;
9935 else {
9936 result = _chsize_s(fd, length);
9937 close(fd);
9938 if (result < 0)
9939 errno = result;
9940 }
9941#else
9942 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10009943#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04009944 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10009945 Py_END_ALLOW_THREADS
9946 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +03009947 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +10009948
9949 Py_RETURN_NONE;
9950}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07009951#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10009952
Ross Lagerwall7807c352011-03-17 20:20:30 +02009953
Victor Stinnerd6b17692014-09-30 12:20:05 +02009954/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
9955 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
9956 defined, which is the case in Python on AIX. AIX bug report:
9957 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
9958#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
9959# define POSIX_FADVISE_AIX_BUG
9960#endif
9961
Victor Stinnerec39e262014-09-30 12:35:58 +02009962
Victor Stinnerd6b17692014-09-30 12:20:05 +02009963#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009964/*[clinic input]
9965os.posix_fallocate
9966
9967 fd: int
9968 offset: Py_off_t
9969 length: Py_off_t
9970 /
9971
9972Ensure a file has allocated at least a particular number of bytes on disk.
9973
9974Ensure that the file specified by fd encompasses a range of bytes
9975starting at offset bytes from the beginning and continuing for length bytes.
9976[clinic start generated code]*/
9977
Larry Hastings2f936352014-08-05 14:04:04 +10009978static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009979os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009980 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009981/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009982{
9983 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009984 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009985
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009986 do {
9987 Py_BEGIN_ALLOW_THREADS
9988 result = posix_fallocate(fd, offset, length);
9989 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +05009990 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
9991
9992 if (result == 0)
9993 Py_RETURN_NONE;
9994
9995 if (async_err)
9996 return NULL;
9997
9998 errno = result;
9999 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010000}
Victor Stinnerec39e262014-09-30 12:35:58 +020010001#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010002
Ross Lagerwall7807c352011-03-17 20:20:30 +020010003
Victor Stinnerd6b17692014-09-30 12:20:05 +020010004#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010005/*[clinic input]
10006os.posix_fadvise
10007
10008 fd: int
10009 offset: Py_off_t
10010 length: Py_off_t
10011 advice: int
10012 /
10013
10014Announce an intention to access data in a specific pattern.
10015
10016Announce an intention to access data in a specific pattern, thus allowing
10017the kernel to make optimizations.
10018The advice applies to the region of the file specified by fd starting at
10019offset and continuing for length bytes.
10020advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10021POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10022POSIX_FADV_DONTNEED.
10023[clinic start generated code]*/
10024
Larry Hastings2f936352014-08-05 14:04:04 +100010025static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010026os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010027 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010028/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010029{
10030 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010031 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010032
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010033 do {
10034 Py_BEGIN_ALLOW_THREADS
10035 result = posix_fadvise(fd, offset, length, advice);
10036 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010037 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10038
10039 if (result == 0)
10040 Py_RETURN_NONE;
10041
10042 if (async_err)
10043 return NULL;
10044
10045 errno = result;
10046 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010047}
Victor Stinnerec39e262014-09-30 12:35:58 +020010048#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010049
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010050#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010051
Larry Hastings2f936352014-08-05 14:04:04 +100010052static void
10053posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010054{
Larry Hastings2f936352014-08-05 14:04:04 +100010055 /* Install the first arg and newstr in posix_putenv_garbage;
10056 * this will cause previous value to be collected. This has to
10057 * happen after the real putenv() call because the old value
10058 * was still accessible until then. */
Eddie Elizondob3966632019-11-05 07:16:14 -080010059 if (PyDict_SetItem(_posixstate_global->posix_putenv_garbage, name, value))
Larry Hastings2f936352014-08-05 14:04:04 +100010060 /* really not much we can do; just leak */
10061 PyErr_Clear();
10062 else
10063 Py_DECREF(value);
10064}
10065
10066
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010067#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010068/*[clinic input]
10069os.putenv
10070
10071 name: unicode
10072 value: unicode
10073 /
10074
10075Change or add an environment variable.
10076[clinic start generated code]*/
10077
Larry Hastings2f936352014-08-05 14:04:04 +100010078static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010079os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10080/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010081{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010082 const wchar_t *env;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010083 Py_ssize_t size;
Larry Hastings2f936352014-08-05 14:04:04 +100010084
Serhiy Storchaka77703942017-06-25 07:33:01 +030010085 /* Search from index 1 because on Windows starting '=' is allowed for
10086 defining hidden environment variables. */
10087 if (PyUnicode_GET_LENGTH(name) == 0 ||
10088 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10089 {
10090 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10091 return NULL;
10092 }
Larry Hastings2f936352014-08-05 14:04:04 +100010093 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
10094 if (unicode == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010095 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +000010096 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010097
10098 env = PyUnicode_AsUnicodeAndSize(unicode, &size);
10099 if (env == NULL)
10100 goto error;
10101 if (size > _MAX_ENV) {
Victor Stinner65170952011-11-22 22:16:17 +010010102 PyErr_Format(PyExc_ValueError,
10103 "the environment variable is longer than %u characters",
10104 _MAX_ENV);
10105 goto error;
10106 }
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010107 if (wcslen(env) != (size_t)size) {
10108 PyErr_SetString(PyExc_ValueError, "embedded null character");
Victor Stinnereb5657a2011-09-30 01:44:27 +020010109 goto error;
Serhiy Storchakaf7eae0a2017-06-28 08:30:06 +030010110 }
10111
Larry Hastings2f936352014-08-05 14:04:04 +100010112 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010113 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +000010114 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +000010115 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010116
Larry Hastings2f936352014-08-05 14:04:04 +100010117 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010118 Py_RETURN_NONE;
10119
10120error:
Larry Hastings2f936352014-08-05 14:04:04 +100010121 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +000010122 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010123}
Larry Hastings2f936352014-08-05 14:04:04 +100010124#else /* MS_WINDOWS */
10125/*[clinic input]
10126os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010127
Larry Hastings2f936352014-08-05 14:04:04 +100010128 name: FSConverter
10129 value: FSConverter
10130 /
10131
10132Change or add an environment variable.
10133[clinic start generated code]*/
10134
Larry Hastings2f936352014-08-05 14:04:04 +100010135static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010136os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10137/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010138{
10139 PyObject *bytes = NULL;
10140 char *env;
Serhiy Storchaka77703942017-06-25 07:33:01 +030010141 const char *name_string = PyBytes_AS_STRING(name);
10142 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010143
Serhiy Storchaka77703942017-06-25 07:33:01 +030010144 if (strchr(name_string, '=') != NULL) {
10145 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10146 return NULL;
10147 }
Larry Hastings2f936352014-08-05 14:04:04 +100010148 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
10149 if (bytes == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010150 return NULL;
10151 }
10152
10153 env = PyBytes_AS_STRING(bytes);
10154 if (putenv(env)) {
10155 Py_DECREF(bytes);
10156 return posix_error();
10157 }
10158
10159 posix_putenv_garbage_setitem(name, bytes);
10160 Py_RETURN_NONE;
10161}
10162#endif /* MS_WINDOWS */
10163#endif /* HAVE_PUTENV */
10164
10165
Victor Stinner56cd3712020-01-21 16:13:09 +010010166#ifdef MS_WINDOWS
10167/*[clinic input]
10168os.unsetenv
10169 name: unicode
10170 /
10171
10172Delete an environment variable.
10173[clinic start generated code]*/
10174
10175static PyObject *
10176os_unsetenv_impl(PyObject *module, PyObject *name)
10177/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10178{
10179 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10180 wchar_t *name_str = PyUnicode_AsWideCharString(name, NULL);
10181 if (name_str == NULL) {
10182 return NULL;
10183 }
10184
10185 BOOL ok = SetEnvironmentVariableW(name_str, NULL);
10186 PyMem_Free(name_str);
10187
10188 if (!ok) {
10189 return PyErr_SetFromWindowsErr(0);
10190 }
10191
10192 /* Remove the key from posix_putenv_garbage;
10193 * this will cause it to be collected. This has to
10194 * happen after the real unsetenv() call because the
10195 * old value was still accessible until then.
10196 */
10197 if (PyDict_DelItem(_posixstate(module)->posix_putenv_garbage, name)) {
10198 /* really not much we can do; just leak */
10199 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10200 return NULL;
10201 }
10202 PyErr_Clear();
10203 }
10204
10205 Py_RETURN_NONE;
10206}
10207/* repeat !defined(MS_WINDOWS) to workaround an Argument Clinic issue */
10208#elif defined(HAVE_UNSETENV) && !defined(MS_WINDOWS)
Larry Hastings2f936352014-08-05 14:04:04 +100010209/*[clinic input]
10210os.unsetenv
10211 name: FSConverter
10212 /
10213
10214Delete an environment variable.
10215[clinic start generated code]*/
10216
Larry Hastings2f936352014-08-05 14:04:04 +100010217static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010218os_unsetenv_impl(PyObject *module, PyObject *name)
10219/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010220{
Victor Stinner984890f2011-11-24 13:53:38 +010010221#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +010010222 int err;
Victor Stinner984890f2011-11-24 13:53:38 +010010223#endif
Victor Stinner84ae1182010-05-06 22:05:07 +000010224
Victor Stinner984890f2011-11-24 13:53:38 +010010225#ifdef HAVE_BROKEN_UNSETENV
10226 unsetenv(PyBytes_AS_STRING(name));
10227#else
Victor Stinner65170952011-11-22 22:16:17 +010010228 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +100010229 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +010010230 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +010010231#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010232
Victor Stinner8c62be82010-05-06 00:08:46 +000010233 /* Remove the key from posix_putenv_garbage;
10234 * this will cause it to be collected. This has to
10235 * happen after the real unsetenv() call because the
10236 * old value was still accessible until then.
10237 */
Eddie Elizondob3966632019-11-05 07:16:14 -080010238 if (PyDict_DelItem(_posixstate(module)->posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010239 /* really not much we can do; just leak */
Serhiy Storchakaa24107b2019-02-25 17:59:46 +020010240 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
10241 return NULL;
10242 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010243 PyErr_Clear();
10244 }
Victor Stinner84ae1182010-05-06 22:05:07 +000010245 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010246}
Larry Hastings2f936352014-08-05 14:04:04 +100010247#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +000010248
Larry Hastings2f936352014-08-05 14:04:04 +100010249
10250/*[clinic input]
10251os.strerror
10252
10253 code: int
10254 /
10255
10256Translate an error code to a message string.
10257[clinic start generated code]*/
10258
Larry Hastings2f936352014-08-05 14:04:04 +100010259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010260os_strerror_impl(PyObject *module, int code)
10261/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010262{
10263 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010264 if (message == NULL) {
10265 PyErr_SetString(PyExc_ValueError,
10266 "strerror() argument out of range");
10267 return NULL;
10268 }
Victor Stinner1b579672011-12-17 05:47:23 +010010269 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010270}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010271
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010272
Guido van Rossumc9641791998-08-04 15:26:23 +000010273#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010274#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010275/*[clinic input]
10276os.WCOREDUMP -> bool
10277
10278 status: int
10279 /
10280
10281Return True if the process returning status was dumped to a core file.
10282[clinic start generated code]*/
10283
Larry Hastings2f936352014-08-05 14:04:04 +100010284static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010285os_WCOREDUMP_impl(PyObject *module, int status)
10286/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010287{
10288 WAIT_TYPE wait_status;
10289 WAIT_STATUS_INT(wait_status) = status;
10290 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010291}
10292#endif /* WCOREDUMP */
10293
Larry Hastings2f936352014-08-05 14:04:04 +100010294
Fred Drake106c1a02002-04-23 15:58:02 +000010295#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010296/*[clinic input]
10297os.WIFCONTINUED -> bool
10298
10299 status: int
10300
10301Return True if a particular process was continued from a job control stop.
10302
10303Return True if the process returning status was continued from a
10304job control stop.
10305[clinic start generated code]*/
10306
Larry Hastings2f936352014-08-05 14:04:04 +100010307static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010308os_WIFCONTINUED_impl(PyObject *module, int status)
10309/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010310{
10311 WAIT_TYPE wait_status;
10312 WAIT_STATUS_INT(wait_status) = status;
10313 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010314}
10315#endif /* WIFCONTINUED */
10316
Larry Hastings2f936352014-08-05 14:04:04 +100010317
Guido van Rossumc9641791998-08-04 15:26:23 +000010318#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010319/*[clinic input]
10320os.WIFSTOPPED -> bool
10321
10322 status: int
10323
10324Return True if the process returning status was stopped.
10325[clinic start generated code]*/
10326
Larry Hastings2f936352014-08-05 14:04:04 +100010327static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010328os_WIFSTOPPED_impl(PyObject *module, int status)
10329/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010330{
10331 WAIT_TYPE wait_status;
10332 WAIT_STATUS_INT(wait_status) = status;
10333 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010334}
10335#endif /* WIFSTOPPED */
10336
Larry Hastings2f936352014-08-05 14:04:04 +100010337
Guido van Rossumc9641791998-08-04 15:26:23 +000010338#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010339/*[clinic input]
10340os.WIFSIGNALED -> bool
10341
10342 status: int
10343
10344Return True if the process returning status was terminated by a signal.
10345[clinic start generated code]*/
10346
Larry Hastings2f936352014-08-05 14:04:04 +100010347static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010348os_WIFSIGNALED_impl(PyObject *module, int status)
10349/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010350{
10351 WAIT_TYPE wait_status;
10352 WAIT_STATUS_INT(wait_status) = status;
10353 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010354}
10355#endif /* WIFSIGNALED */
10356
Larry Hastings2f936352014-08-05 14:04:04 +100010357
Guido van Rossumc9641791998-08-04 15:26:23 +000010358#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010359/*[clinic input]
10360os.WIFEXITED -> bool
10361
10362 status: int
10363
10364Return True if the process returning status exited via the exit() system call.
10365[clinic start generated code]*/
10366
Larry Hastings2f936352014-08-05 14:04:04 +100010367static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010368os_WIFEXITED_impl(PyObject *module, int status)
10369/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010370{
10371 WAIT_TYPE wait_status;
10372 WAIT_STATUS_INT(wait_status) = status;
10373 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010374}
10375#endif /* WIFEXITED */
10376
Larry Hastings2f936352014-08-05 14:04:04 +100010377
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010378#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010379/*[clinic input]
10380os.WEXITSTATUS -> int
10381
10382 status: int
10383
10384Return the process return code from status.
10385[clinic start generated code]*/
10386
Larry Hastings2f936352014-08-05 14:04:04 +100010387static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010388os_WEXITSTATUS_impl(PyObject *module, int status)
10389/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010390{
10391 WAIT_TYPE wait_status;
10392 WAIT_STATUS_INT(wait_status) = status;
10393 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010394}
10395#endif /* WEXITSTATUS */
10396
Larry Hastings2f936352014-08-05 14:04:04 +100010397
Guido van Rossumc9641791998-08-04 15:26:23 +000010398#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010399/*[clinic input]
10400os.WTERMSIG -> int
10401
10402 status: int
10403
10404Return the signal that terminated the process that provided the status value.
10405[clinic start generated code]*/
10406
Larry Hastings2f936352014-08-05 14:04:04 +100010407static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010408os_WTERMSIG_impl(PyObject *module, int status)
10409/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010410{
10411 WAIT_TYPE wait_status;
10412 WAIT_STATUS_INT(wait_status) = status;
10413 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010414}
10415#endif /* WTERMSIG */
10416
Larry Hastings2f936352014-08-05 14:04:04 +100010417
Guido van Rossumc9641791998-08-04 15:26:23 +000010418#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010419/*[clinic input]
10420os.WSTOPSIG -> int
10421
10422 status: int
10423
10424Return the signal that stopped the process that provided the status value.
10425[clinic start generated code]*/
10426
Larry Hastings2f936352014-08-05 14:04:04 +100010427static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010428os_WSTOPSIG_impl(PyObject *module, int status)
10429/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010430{
10431 WAIT_TYPE wait_status;
10432 WAIT_STATUS_INT(wait_status) = status;
10433 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010434}
10435#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010436#endif /* HAVE_SYS_WAIT_H */
10437
10438
Thomas Wouters477c8d52006-05-27 19:21:47 +000010439#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010440#ifdef _SCO_DS
10441/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10442 needed definitions in sys/statvfs.h */
10443#define _SVID3
10444#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010445#include <sys/statvfs.h>
10446
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010447static PyObject*
10448_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondob3966632019-11-05 07:16:14 -080010449 PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType;
10450 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010451 if (v == NULL)
10452 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010453
10454#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010455 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10456 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10457 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10458 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10459 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10460 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10461 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10462 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10463 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10464 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010465#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010466 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10467 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10468 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010469 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010471 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010472 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010473 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010474 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010475 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010476 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010477 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010478 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010479 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010480 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10481 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010482#endif
Michael Felt502d5512018-01-05 13:01:58 +010010483/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10484 * (issue #32390). */
10485#if defined(_AIX) && defined(_ALL_SOURCE)
10486 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10487#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010488 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010489#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010490 if (PyErr_Occurred()) {
10491 Py_DECREF(v);
10492 return NULL;
10493 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010494
Victor Stinner8c62be82010-05-06 00:08:46 +000010495 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010496}
10497
Larry Hastings2f936352014-08-05 14:04:04 +100010498
10499/*[clinic input]
10500os.fstatvfs
10501 fd: int
10502 /
10503
10504Perform an fstatvfs system call on the given fd.
10505
10506Equivalent to statvfs(fd).
10507[clinic start generated code]*/
10508
Larry Hastings2f936352014-08-05 14:04:04 +100010509static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010510os_fstatvfs_impl(PyObject *module, int fd)
10511/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010512{
10513 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010514 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010515 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010516
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010517 do {
10518 Py_BEGIN_ALLOW_THREADS
10519 result = fstatvfs(fd, &st);
10520 Py_END_ALLOW_THREADS
10521 } while (result != 0 && errno == EINTR &&
10522 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010523 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010524 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010525
Victor Stinner8c62be82010-05-06 00:08:46 +000010526 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010527}
Larry Hastings2f936352014-08-05 14:04:04 +100010528#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010529
10530
Thomas Wouters477c8d52006-05-27 19:21:47 +000010531#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010532#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010533/*[clinic input]
10534os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010535
Larry Hastings2f936352014-08-05 14:04:04 +100010536 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10537
10538Perform a statvfs system call on the given path.
10539
10540path may always be specified as a string.
10541On some platforms, path may also be specified as an open file descriptor.
10542 If this functionality is unavailable, using it raises an exception.
10543[clinic start generated code]*/
10544
Larry Hastings2f936352014-08-05 14:04:04 +100010545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010546os_statvfs_impl(PyObject *module, path_t *path)
10547/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010548{
10549 int result;
10550 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010551
10552 Py_BEGIN_ALLOW_THREADS
10553#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010554 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010555#ifdef __APPLE__
10556 /* handle weak-linking on Mac OS X 10.3 */
10557 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010558 fd_specified("statvfs", path->fd);
10559 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010560 }
10561#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010562 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010563 }
10564 else
10565#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010566 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010567 Py_END_ALLOW_THREADS
10568
10569 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010570 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010571 }
10572
Larry Hastings2f936352014-08-05 14:04:04 +100010573 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010574}
Larry Hastings2f936352014-08-05 14:04:04 +100010575#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10576
Guido van Rossum94f6f721999-01-06 18:42:14 +000010577
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010578#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010579/*[clinic input]
10580os._getdiskusage
10581
Steve Dower23ad6d02018-02-22 10:39:10 -080010582 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010583
10584Return disk usage statistics about the given path as a (total, free) tuple.
10585[clinic start generated code]*/
10586
Larry Hastings2f936352014-08-05 14:04:04 +100010587static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010588os__getdiskusage_impl(PyObject *module, path_t *path)
10589/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010590{
10591 BOOL retval;
10592 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010593 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010594
10595 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010596 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010597 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010598 if (retval == 0) {
10599 if (GetLastError() == ERROR_DIRECTORY) {
10600 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010601
Joe Pamerc8c02492018-09-25 10:57:36 -040010602 dir_path = PyMem_New(wchar_t, path->length + 1);
10603 if (dir_path == NULL) {
10604 return PyErr_NoMemory();
10605 }
10606
10607 wcscpy_s(dir_path, path->length + 1, path->wide);
10608
10609 if (_dirnameW(dir_path) != -1) {
10610 Py_BEGIN_ALLOW_THREADS
10611 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10612 Py_END_ALLOW_THREADS
10613 }
10614 /* Record the last error in case it's modified by PyMem_Free. */
10615 err = GetLastError();
10616 PyMem_Free(dir_path);
10617 if (retval) {
10618 goto success;
10619 }
10620 }
10621 return PyErr_SetFromWindowsErr(err);
10622 }
10623
10624success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010625 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10626}
Larry Hastings2f936352014-08-05 14:04:04 +100010627#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010628
10629
Fred Drakec9680921999-12-13 16:37:25 +000010630/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10631 * It maps strings representing configuration variable names to
10632 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010633 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010634 * rarely-used constants. There are three separate tables that use
10635 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010636 *
10637 * This code is always included, even if none of the interfaces that
10638 * need it are included. The #if hackery needed to avoid it would be
10639 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010640 */
10641struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010642 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010643 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010644};
10645
Fred Drake12c6e2d1999-12-14 21:25:03 +000010646static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010647conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010648 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010649{
Christian Heimes217cfd12007-12-02 14:31:20 +000010650 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010651 int value = _PyLong_AsInt(arg);
10652 if (value == -1 && PyErr_Occurred())
10653 return 0;
10654 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010655 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010656 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010657 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010658 /* look up the value in the table using a binary search */
10659 size_t lo = 0;
10660 size_t mid;
10661 size_t hi = tablesize;
10662 int cmp;
10663 const char *confname;
10664 if (!PyUnicode_Check(arg)) {
10665 PyErr_SetString(PyExc_TypeError,
10666 "configuration names must be strings or integers");
10667 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010668 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010669 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010670 if (confname == NULL)
10671 return 0;
10672 while (lo < hi) {
10673 mid = (lo + hi) / 2;
10674 cmp = strcmp(confname, table[mid].name);
10675 if (cmp < 0)
10676 hi = mid;
10677 else if (cmp > 0)
10678 lo = mid + 1;
10679 else {
10680 *valuep = table[mid].value;
10681 return 1;
10682 }
10683 }
10684 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10685 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010687}
10688
10689
10690#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10691static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010692#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010694#endif
10695#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010696 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010697#endif
Fred Drakec9680921999-12-13 16:37:25 +000010698#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010699 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010700#endif
10701#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010702 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010703#endif
10704#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010705 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010706#endif
10707#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010708 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010709#endif
10710#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010711 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010712#endif
10713#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010715#endif
10716#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010717 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010718#endif
10719#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010720 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010721#endif
10722#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010723 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010724#endif
10725#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010726 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010727#endif
10728#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010729 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010730#endif
10731#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010732 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010733#endif
10734#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010735 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010736#endif
10737#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010738 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010739#endif
10740#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010741 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010742#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010743#ifdef _PC_ACL_ENABLED
10744 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10745#endif
10746#ifdef _PC_MIN_HOLE_SIZE
10747 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10748#endif
10749#ifdef _PC_ALLOC_SIZE_MIN
10750 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10751#endif
10752#ifdef _PC_REC_INCR_XFER_SIZE
10753 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10754#endif
10755#ifdef _PC_REC_MAX_XFER_SIZE
10756 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10757#endif
10758#ifdef _PC_REC_MIN_XFER_SIZE
10759 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10760#endif
10761#ifdef _PC_REC_XFER_ALIGN
10762 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10763#endif
10764#ifdef _PC_SYMLINK_MAX
10765 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10766#endif
10767#ifdef _PC_XATTR_ENABLED
10768 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10769#endif
10770#ifdef _PC_XATTR_EXISTS
10771 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10772#endif
10773#ifdef _PC_TIMESTAMP_RESOLUTION
10774 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10775#endif
Fred Drakec9680921999-12-13 16:37:25 +000010776};
10777
Fred Drakec9680921999-12-13 16:37:25 +000010778static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010779conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010780{
10781 return conv_confname(arg, valuep, posix_constants_pathconf,
10782 sizeof(posix_constants_pathconf)
10783 / sizeof(struct constdef));
10784}
10785#endif
10786
Larry Hastings2f936352014-08-05 14:04:04 +100010787
Fred Drakec9680921999-12-13 16:37:25 +000010788#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010789/*[clinic input]
10790os.fpathconf -> long
10791
10792 fd: int
10793 name: path_confname
10794 /
10795
10796Return the configuration limit name for the file descriptor fd.
10797
10798If there is no limit, return -1.
10799[clinic start generated code]*/
10800
Larry Hastings2f936352014-08-05 14:04:04 +100010801static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010802os_fpathconf_impl(PyObject *module, int fd, int name)
10803/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010804{
10805 long limit;
10806
10807 errno = 0;
10808 limit = fpathconf(fd, name);
10809 if (limit == -1 && errno != 0)
10810 posix_error();
10811
10812 return limit;
10813}
10814#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010815
10816
10817#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010818/*[clinic input]
10819os.pathconf -> long
10820 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10821 name: path_confname
10822
10823Return the configuration limit name for the file or directory path.
10824
10825If there is no limit, return -1.
10826On some platforms, path may also be specified as an open file descriptor.
10827 If this functionality is unavailable, using it raises an exception.
10828[clinic start generated code]*/
10829
Larry Hastings2f936352014-08-05 14:04:04 +100010830static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010831os_pathconf_impl(PyObject *module, path_t *path, int name)
10832/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010833{
Victor Stinner8c62be82010-05-06 00:08:46 +000010834 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010835
Victor Stinner8c62be82010-05-06 00:08:46 +000010836 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010837#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010838 if (path->fd != -1)
10839 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010840 else
10841#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010842 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010843 if (limit == -1 && errno != 0) {
10844 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000010845 /* could be a path or name problem */
10846 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000010847 else
Larry Hastings2f936352014-08-05 14:04:04 +100010848 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000010849 }
Larry Hastings2f936352014-08-05 14:04:04 +100010850
10851 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000010852}
Larry Hastings2f936352014-08-05 14:04:04 +100010853#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010854
10855#ifdef HAVE_CONFSTR
10856static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010857#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000010858 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000010859#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000010860#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010861 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010862#endif
10863#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010864 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000010865#endif
Fred Draked86ed291999-12-15 15:34:33 +000010866#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010867 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010868#endif
10869#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010870 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010871#endif
10872#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010873 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000010874#endif
10875#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010876 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010877#endif
Fred Drakec9680921999-12-13 16:37:25 +000010878#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010879 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010880#endif
10881#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010882 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010883#endif
10884#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010885 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010886#endif
10887#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010888 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010889#endif
10890#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010891 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010892#endif
10893#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010894 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010895#endif
10896#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010897 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010898#endif
10899#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010900 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010901#endif
Fred Draked86ed291999-12-15 15:34:33 +000010902#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000010903 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000010904#endif
Fred Drakec9680921999-12-13 16:37:25 +000010905#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000010906 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000010907#endif
Fred Draked86ed291999-12-15 15:34:33 +000010908#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010909 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000010910#endif
10911#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010912 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000010913#endif
10914#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010915 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000010916#endif
10917#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010918 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000010919#endif
Fred Drakec9680921999-12-13 16:37:25 +000010920#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010921 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010922#endif
10923#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010924 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010925#endif
10926#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010927 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010928#endif
10929#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010930 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010931#endif
10932#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010933 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010934#endif
10935#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010936 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010937#endif
10938#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010939 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010940#endif
10941#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010942 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010943#endif
10944#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010945 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010946#endif
10947#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010948 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010949#endif
10950#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010951 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010952#endif
10953#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010954 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010955#endif
10956#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010957 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010958#endif
10959#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010960 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010961#endif
10962#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000010963 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000010964#endif
10965#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010966 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000010967#endif
Fred Draked86ed291999-12-15 15:34:33 +000010968#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010969 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010970#endif
10971#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000010972 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000010973#endif
10974#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000010975 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000010976#endif
10977#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010978 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010979#endif
10980#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010981 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000010982#endif
10983#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000010984 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000010985#endif
10986#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010987 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000010988#endif
10989#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000010990 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000010991#endif
10992#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010993 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000010994#endif
10995#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000010996 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000010997#endif
10998#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000010999 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011000#endif
11001#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011002 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011003#endif
11004#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011005 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011006#endif
Fred Drakec9680921999-12-13 16:37:25 +000011007};
11008
11009static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011010conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011011{
11012 return conv_confname(arg, valuep, posix_constants_confstr,
11013 sizeof(posix_constants_confstr)
11014 / sizeof(struct constdef));
11015}
11016
Larry Hastings2f936352014-08-05 14:04:04 +100011017
11018/*[clinic input]
11019os.confstr
11020
11021 name: confstr_confname
11022 /
11023
11024Return a string-valued system configuration variable.
11025[clinic start generated code]*/
11026
Larry Hastings2f936352014-08-05 14:04:04 +100011027static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011028os_confstr_impl(PyObject *module, int name)
11029/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011030{
11031 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011032 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011033 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011034
Victor Stinnercb043522010-09-10 23:49:04 +000011035 errno = 0;
11036 len = confstr(name, buffer, sizeof(buffer));
11037 if (len == 0) {
11038 if (errno) {
11039 posix_error();
11040 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011041 }
11042 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011043 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011044 }
11045 }
Victor Stinnercb043522010-09-10 23:49:04 +000011046
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011047 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011048 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011049 char *buf = PyMem_Malloc(len);
11050 if (buf == NULL)
11051 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011052 len2 = confstr(name, buf, len);
11053 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011054 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011055 PyMem_Free(buf);
11056 }
11057 else
11058 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011059 return result;
11060}
Larry Hastings2f936352014-08-05 14:04:04 +100011061#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011062
11063
11064#ifdef HAVE_SYSCONF
11065static struct constdef posix_constants_sysconf[] = {
11066#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011068#endif
11069#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011071#endif
11072#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011074#endif
11075#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011077#endif
11078#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011080#endif
11081#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011083#endif
11084#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011086#endif
11087#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011089#endif
11090#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011092#endif
11093#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011095#endif
Fred Draked86ed291999-12-15 15:34:33 +000011096#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011098#endif
11099#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011101#endif
Fred Drakec9680921999-12-13 16:37:25 +000011102#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011104#endif
Fred Drakec9680921999-12-13 16:37:25 +000011105#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011107#endif
11108#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011110#endif
11111#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011113#endif
11114#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011116#endif
11117#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011119#endif
Fred Draked86ed291999-12-15 15:34:33 +000011120#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011122#endif
Fred Drakec9680921999-12-13 16:37:25 +000011123#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011125#endif
11126#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011128#endif
11129#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011131#endif
11132#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011133 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011134#endif
11135#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011137#endif
Fred Draked86ed291999-12-15 15:34:33 +000011138#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011140#endif
Fred Drakec9680921999-12-13 16:37:25 +000011141#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011142 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011143#endif
11144#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011145 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011146#endif
11147#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011148 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011149#endif
11150#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011151 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011152#endif
11153#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011154 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011155#endif
11156#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011157 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011158#endif
11159#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011160 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011161#endif
11162#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011163 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011164#endif
11165#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011166 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011167#endif
11168#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011169 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011170#endif
11171#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011172 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011173#endif
11174#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011175 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011176#endif
11177#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011178 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011179#endif
11180#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011181 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011182#endif
11183#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011184 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011185#endif
11186#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011187 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011188#endif
11189#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011190 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011191#endif
11192#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011193 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011194#endif
11195#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011196 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011197#endif
11198#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011199 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011200#endif
11201#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011202 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011203#endif
11204#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011205 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011206#endif
11207#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011208 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011209#endif
Fred Draked86ed291999-12-15 15:34:33 +000011210#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011211 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011212#endif
Fred Drakec9680921999-12-13 16:37:25 +000011213#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011214 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011215#endif
11216#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011217 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011218#endif
11219#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011220 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011221#endif
Fred Draked86ed291999-12-15 15:34:33 +000011222#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011223 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011224#endif
Fred Drakec9680921999-12-13 16:37:25 +000011225#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011226 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011227#endif
Fred Draked86ed291999-12-15 15:34:33 +000011228#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011229 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011230#endif
11231#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011232 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011233#endif
Fred Drakec9680921999-12-13 16:37:25 +000011234#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011235 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011236#endif
11237#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011238 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011239#endif
11240#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011241 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011242#endif
11243#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011244 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011245#endif
Fred Draked86ed291999-12-15 15:34:33 +000011246#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011247 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011248#endif
Fred Drakec9680921999-12-13 16:37:25 +000011249#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011250 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011251#endif
11252#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011253 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011254#endif
11255#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011256 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011257#endif
11258#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011259 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011260#endif
11261#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011262 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011263#endif
11264#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011266#endif
11267#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011268 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011269#endif
Fred Draked86ed291999-12-15 15:34:33 +000011270#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011271 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011272#endif
Fred Drakec9680921999-12-13 16:37:25 +000011273#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011274 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011275#endif
11276#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011277 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011278#endif
Fred Draked86ed291999-12-15 15:34:33 +000011279#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011280 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011281#endif
Fred Drakec9680921999-12-13 16:37:25 +000011282#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011283 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011284#endif
11285#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011286 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011287#endif
11288#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011289 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011290#endif
11291#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011292 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011293#endif
11294#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011295 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011296#endif
11297#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011298 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011299#endif
11300#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011301 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011302#endif
11303#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011304 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011305#endif
11306#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011307 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011308#endif
Fred Draked86ed291999-12-15 15:34:33 +000011309#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011310 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011311#endif
11312#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011313 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011314#endif
Fred Drakec9680921999-12-13 16:37:25 +000011315#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011316 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011317#endif
11318#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011319 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011320#endif
11321#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011322 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011323#endif
11324#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011325 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011326#endif
11327#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011328 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011329#endif
11330#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011331 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011332#endif
11333#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011334 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011335#endif
11336#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011337 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011338#endif
11339#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011340 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011341#endif
11342#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011343 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011344#endif
11345#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011346 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011347#endif
11348#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011349 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011350#endif
11351#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011352 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011353#endif
11354#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011355 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011356#endif
11357#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011358 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011359#endif
11360#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011361 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011362#endif
11363#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011364 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011365#endif
11366#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011367 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011368#endif
11369#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011370 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011371#endif
11372#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011373 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011374#endif
11375#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011376 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011377#endif
11378#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011379 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011380#endif
11381#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011382 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011383#endif
11384#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011385 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011386#endif
11387#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011388 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011389#endif
11390#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011391 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011392#endif
11393#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011394 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011395#endif
11396#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011397 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011398#endif
11399#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011400 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011401#endif
11402#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011403 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011404#endif
11405#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011406 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011407#endif
11408#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011409 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011410#endif
11411#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011412 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011413#endif
11414#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011415 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011416#endif
11417#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011418 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011419#endif
Fred Draked86ed291999-12-15 15:34:33 +000011420#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011421 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011422#endif
Fred Drakec9680921999-12-13 16:37:25 +000011423#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011424 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011425#endif
11426#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011427 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011428#endif
11429#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011430 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011431#endif
11432#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011433 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011434#endif
11435#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011436 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011437#endif
11438#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011439 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011440#endif
11441#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011442 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011443#endif
11444#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011445 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011446#endif
11447#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011448 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011449#endif
11450#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011451 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011452#endif
11453#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011454 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011455#endif
11456#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011457 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011458#endif
11459#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011460 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011461#endif
11462#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011463 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011464#endif
11465#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011466 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011467#endif
11468#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011469 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011470#endif
11471#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011472 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011473#endif
11474#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011475 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011476#endif
11477#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011478 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011479#endif
11480#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011481 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011482#endif
11483#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011484 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011485#endif
11486#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011487 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011488#endif
11489#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011490 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011491#endif
11492#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011493 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011494#endif
11495#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011496 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011497#endif
11498#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011499 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011500#endif
11501#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011502 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011503#endif
11504#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011505 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011506#endif
11507#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011508 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011509#endif
11510#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011511 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011512#endif
11513#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011514 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011515#endif
11516#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011517 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011518#endif
11519#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011520 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011521#endif
11522#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011523 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011524#endif
11525#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011526 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011527#endif
11528#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011529 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011530#endif
11531#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011532 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011533#endif
11534#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011535 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011536#endif
11537#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011538 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011539#endif
11540#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011541 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011542#endif
11543#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011544 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011545#endif
11546#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011547 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011548#endif
11549#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011550 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011551#endif
11552#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011553 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011554#endif
11555#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011556 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011557#endif
11558};
11559
11560static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011561conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011562{
11563 return conv_confname(arg, valuep, posix_constants_sysconf,
11564 sizeof(posix_constants_sysconf)
11565 / sizeof(struct constdef));
11566}
11567
Larry Hastings2f936352014-08-05 14:04:04 +100011568
11569/*[clinic input]
11570os.sysconf -> long
11571 name: sysconf_confname
11572 /
11573
11574Return an integer-valued system configuration variable.
11575[clinic start generated code]*/
11576
Larry Hastings2f936352014-08-05 14:04:04 +100011577static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011578os_sysconf_impl(PyObject *module, int name)
11579/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011580{
11581 long value;
11582
11583 errno = 0;
11584 value = sysconf(name);
11585 if (value == -1 && errno != 0)
11586 posix_error();
11587 return value;
11588}
11589#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011590
11591
Fred Drakebec628d1999-12-15 18:31:10 +000011592/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011593 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011594 * the exported dictionaries that are used to publish information about the
11595 * names available on the host platform.
11596 *
11597 * Sorting the table at runtime ensures that the table is properly ordered
11598 * when used, even for platforms we're not able to test on. It also makes
11599 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011600 */
Fred Drakebec628d1999-12-15 18:31:10 +000011601
11602static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011603cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011604{
11605 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011606 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011607 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011608 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011609
11610 return strcmp(c1->name, c2->name);
11611}
11612
11613static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011614setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011615 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011616{
Fred Drakebec628d1999-12-15 18:31:10 +000011617 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011618 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011619
11620 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11621 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011622 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011623 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011624
Barry Warsaw3155db32000-04-13 15:20:40 +000011625 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011626 PyObject *o = PyLong_FromLong(table[i].value);
11627 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11628 Py_XDECREF(o);
11629 Py_DECREF(d);
11630 return -1;
11631 }
11632 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011633 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011634 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011635}
11636
Fred Drakebec628d1999-12-15 18:31:10 +000011637/* Return -1 on failure, 0 on success. */
11638static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011639setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011640{
11641#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011642 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011643 sizeof(posix_constants_pathconf)
11644 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011645 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011646 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011647#endif
11648#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011649 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011650 sizeof(posix_constants_confstr)
11651 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011652 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011653 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011654#endif
11655#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011656 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011657 sizeof(posix_constants_sysconf)
11658 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011659 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011660 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011661#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011662 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011663}
Fred Draked86ed291999-12-15 15:34:33 +000011664
11665
Larry Hastings2f936352014-08-05 14:04:04 +100011666/*[clinic input]
11667os.abort
11668
11669Abort the interpreter immediately.
11670
11671This function 'dumps core' or otherwise fails in the hardest way possible
11672on the hosting operating system. This function never returns.
11673[clinic start generated code]*/
11674
Larry Hastings2f936352014-08-05 14:04:04 +100011675static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011676os_abort_impl(PyObject *module)
11677/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011678{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011679 abort();
11680 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011681#ifndef __clang__
11682 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11683 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11684 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011685 Py_FatalError("abort() called from Python code didn't abort!");
11686 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011687#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011688}
Fred Drakebec628d1999-12-15 18:31:10 +000011689
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011690#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011691/* Grab ShellExecute dynamically from shell32 */
11692static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011693static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11694 LPCWSTR, INT);
11695static int
11696check_ShellExecute()
11697{
11698 HINSTANCE hShell32;
11699
11700 /* only recheck */
11701 if (-1 == has_ShellExecute) {
11702 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011703 /* Security note: this call is not vulnerable to "DLL hijacking".
11704 SHELL32 is part of "KnownDLLs" and so Windows always load
11705 the system SHELL32.DLL, even if there is another SHELL32.DLL
11706 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011707 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011708 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011709 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11710 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011711 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011712 } else {
11713 has_ShellExecute = 0;
11714 }
Tony Roberts4860f012019-02-02 18:16:42 +010011715 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011716 }
11717 return has_ShellExecute;
11718}
11719
11720
Steve Dowercc16be82016-09-08 10:35:16 -070011721/*[clinic input]
11722os.startfile
11723 filepath: path_t
11724 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011725
Steve Dowercc16be82016-09-08 10:35:16 -070011726Start a file with its associated application.
11727
11728When "operation" is not specified or "open", this acts like
11729double-clicking the file in Explorer, or giving the file name as an
11730argument to the DOS "start" command: the file is opened with whatever
11731application (if any) its extension is associated.
11732When another "operation" is given, it specifies what should be done with
11733the file. A typical operation is "print".
11734
11735startfile returns as soon as the associated application is launched.
11736There is no option to wait for the application to close, and no way
11737to retrieve the application's exit status.
11738
11739The filepath is relative to the current directory. If you want to use
11740an absolute path, make sure the first character is not a slash ("/");
11741the underlying Win32 ShellExecute function doesn't work if it is.
11742[clinic start generated code]*/
11743
11744static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011745os_startfile_impl(PyObject *module, path_t *filepath,
11746 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011747/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011748{
11749 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011750
11751 if(!check_ShellExecute()) {
11752 /* If the OS doesn't have ShellExecute, return a
11753 NotImplementedError. */
11754 return PyErr_Format(PyExc_NotImplementedError,
11755 "startfile not available on this platform");
11756 }
11757
Victor Stinner8c62be82010-05-06 00:08:46 +000011758 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011759 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011760 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011761 Py_END_ALLOW_THREADS
11762
Victor Stinner8c62be82010-05-06 00:08:46 +000011763 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011764 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011765 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011766 }
Steve Dowercc16be82016-09-08 10:35:16 -070011767 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011768}
Larry Hastings2f936352014-08-05 14:04:04 +100011769#endif /* MS_WINDOWS */
11770
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011771
Martin v. Löwis438b5342002-12-27 10:16:42 +000011772#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011773/*[clinic input]
11774os.getloadavg
11775
11776Return average recent system load information.
11777
11778Return the number of processes in the system run queue averaged over
11779the last 1, 5, and 15 minutes as a tuple of three floats.
11780Raises OSError if the load average was unobtainable.
11781[clinic start generated code]*/
11782
Larry Hastings2f936352014-08-05 14:04:04 +100011783static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011784os_getloadavg_impl(PyObject *module)
11785/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011786{
11787 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011788 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011789 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11790 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011791 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011792 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011793}
Larry Hastings2f936352014-08-05 14:04:04 +100011794#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011795
Larry Hastings2f936352014-08-05 14:04:04 +100011796
11797/*[clinic input]
11798os.device_encoding
11799 fd: int
11800
11801Return a string describing the encoding of a terminal's file descriptor.
11802
11803The file descriptor must be attached to a terminal.
11804If the device is not a terminal, return None.
11805[clinic start generated code]*/
11806
Larry Hastings2f936352014-08-05 14:04:04 +100011807static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011808os_device_encoding_impl(PyObject *module, int fd)
11809/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011810{
Brett Cannonefb00c02012-02-29 18:31:31 -050011811 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011812}
11813
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011814
Larry Hastings2f936352014-08-05 14:04:04 +100011815#ifdef HAVE_SETRESUID
11816/*[clinic input]
11817os.setresuid
11818
11819 ruid: uid_t
11820 euid: uid_t
11821 suid: uid_t
11822 /
11823
11824Set the current process's real, effective, and saved user ids.
11825[clinic start generated code]*/
11826
Larry Hastings2f936352014-08-05 14:04:04 +100011827static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011828os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11829/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011830{
Victor Stinner8c62be82010-05-06 00:08:46 +000011831 if (setresuid(ruid, euid, suid) < 0)
11832 return posix_error();
11833 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011834}
Larry Hastings2f936352014-08-05 14:04:04 +100011835#endif /* HAVE_SETRESUID */
11836
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011837
11838#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011839/*[clinic input]
11840os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011841
Larry Hastings2f936352014-08-05 14:04:04 +100011842 rgid: gid_t
11843 egid: gid_t
11844 sgid: gid_t
11845 /
11846
11847Set the current process's real, effective, and saved group ids.
11848[clinic start generated code]*/
11849
Larry Hastings2f936352014-08-05 14:04:04 +100011850static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011851os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
11852/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011853{
Victor Stinner8c62be82010-05-06 00:08:46 +000011854 if (setresgid(rgid, egid, sgid) < 0)
11855 return posix_error();
11856 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011857}
Larry Hastings2f936352014-08-05 14:04:04 +100011858#endif /* HAVE_SETRESGID */
11859
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011860
11861#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100011862/*[clinic input]
11863os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011864
Larry Hastings2f936352014-08-05 14:04:04 +100011865Return a tuple of the current process's real, effective, and saved user ids.
11866[clinic start generated code]*/
11867
Larry Hastings2f936352014-08-05 14:04:04 +100011868static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011869os_getresuid_impl(PyObject *module)
11870/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011871{
Victor Stinner8c62be82010-05-06 00:08:46 +000011872 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011873 if (getresuid(&ruid, &euid, &suid) < 0)
11874 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011875 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
11876 _PyLong_FromUid(euid),
11877 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011878}
Larry Hastings2f936352014-08-05 14:04:04 +100011879#endif /* HAVE_GETRESUID */
11880
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011881
11882#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100011883/*[clinic input]
11884os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011885
Larry Hastings2f936352014-08-05 14:04:04 +100011886Return a tuple of the current process's real, effective, and saved group ids.
11887[clinic start generated code]*/
11888
Larry Hastings2f936352014-08-05 14:04:04 +100011889static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011890os_getresgid_impl(PyObject *module)
11891/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011892{
11893 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000011894 if (getresgid(&rgid, &egid, &sgid) < 0)
11895 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020011896 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
11897 _PyLong_FromGid(egid),
11898 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011899}
Larry Hastings2f936352014-08-05 14:04:04 +100011900#endif /* HAVE_GETRESGID */
11901
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011902
Benjamin Peterson9428d532011-09-14 11:45:52 -040011903#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100011904/*[clinic input]
11905os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040011906
Larry Hastings2f936352014-08-05 14:04:04 +100011907 path: path_t(allow_fd=True)
11908 attribute: path_t
11909 *
11910 follow_symlinks: bool = True
11911
11912Return the value of extended attribute attribute on path.
11913
BNMetricsb9427072018-11-02 15:20:19 +000011914path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011915If follow_symlinks is False, and the last element of the path is a symbolic
11916 link, getxattr will examine the symbolic link itself instead of the file
11917 the link points to.
11918
11919[clinic start generated code]*/
11920
Larry Hastings2f936352014-08-05 14:04:04 +100011921static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011922os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011923 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011924/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011925{
11926 Py_ssize_t i;
11927 PyObject *buffer = NULL;
11928
11929 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
11930 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011931
Larry Hastings9cf065c2012-06-22 16:30:09 -070011932 for (i = 0; ; i++) {
11933 void *ptr;
11934 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011935 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070011936 Py_ssize_t buffer_size = buffer_sizes[i];
11937 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100011938 path_error(path);
11939 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011940 }
11941 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
11942 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100011943 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011944 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040011945
Larry Hastings9cf065c2012-06-22 16:30:09 -070011946 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011947 if (path->fd >= 0)
11948 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011949 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100011950 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011951 else
Larry Hastings2f936352014-08-05 14:04:04 +100011952 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011953 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011954
Larry Hastings9cf065c2012-06-22 16:30:09 -070011955 if (result < 0) {
11956 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011957 if (errno == ERANGE)
11958 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100011959 path_error(path);
11960 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011961 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011962
Larry Hastings9cf065c2012-06-22 16:30:09 -070011963 if (result != buffer_size) {
11964 /* Can only shrink. */
11965 _PyBytes_Resize(&buffer, result);
11966 }
11967 break;
11968 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011969
Larry Hastings9cf065c2012-06-22 16:30:09 -070011970 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011971}
11972
Larry Hastings2f936352014-08-05 14:04:04 +100011973
11974/*[clinic input]
11975os.setxattr
11976
11977 path: path_t(allow_fd=True)
11978 attribute: path_t
11979 value: Py_buffer
11980 flags: int = 0
11981 *
11982 follow_symlinks: bool = True
11983
11984Set extended attribute attribute on path to value.
11985
BNMetricsb9427072018-11-02 15:20:19 +000011986path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100011987If follow_symlinks is False, and the last element of the path is a symbolic
11988 link, setxattr will modify the symbolic link itself instead of the file
11989 the link points to.
11990
11991[clinic start generated code]*/
11992
Benjamin Peterson799bd802011-08-31 22:15:17 -040011993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011994os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011995 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000011996/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040011997{
Larry Hastings2f936352014-08-05 14:04:04 +100011998 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011999
Larry Hastings2f936352014-08-05 14:04:04 +100012000 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012001 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012002
Benjamin Peterson799bd802011-08-31 22:15:17 -040012003 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012004 if (path->fd > -1)
12005 result = fsetxattr(path->fd, attribute->narrow,
12006 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012007 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012008 result = setxattr(path->narrow, attribute->narrow,
12009 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012010 else
Larry Hastings2f936352014-08-05 14:04:04 +100012011 result = lsetxattr(path->narrow, attribute->narrow,
12012 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012013 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012014
Larry Hastings9cf065c2012-06-22 16:30:09 -070012015 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012016 path_error(path);
12017 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012018 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012019
Larry Hastings2f936352014-08-05 14:04:04 +100012020 Py_RETURN_NONE;
12021}
12022
12023
12024/*[clinic input]
12025os.removexattr
12026
12027 path: path_t(allow_fd=True)
12028 attribute: path_t
12029 *
12030 follow_symlinks: bool = True
12031
12032Remove extended attribute attribute on path.
12033
BNMetricsb9427072018-11-02 15:20:19 +000012034path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012035If follow_symlinks is False, and the last element of the path is a symbolic
12036 link, removexattr will modify the symbolic link itself instead of the file
12037 the link points to.
12038
12039[clinic start generated code]*/
12040
Larry Hastings2f936352014-08-05 14:04:04 +100012041static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012042os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012043 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012044/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012045{
12046 ssize_t result;
12047
12048 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12049 return NULL;
12050
12051 Py_BEGIN_ALLOW_THREADS;
12052 if (path->fd > -1)
12053 result = fremovexattr(path->fd, attribute->narrow);
12054 else if (follow_symlinks)
12055 result = removexattr(path->narrow, attribute->narrow);
12056 else
12057 result = lremovexattr(path->narrow, attribute->narrow);
12058 Py_END_ALLOW_THREADS;
12059
12060 if (result) {
12061 return path_error(path);
12062 }
12063
12064 Py_RETURN_NONE;
12065}
12066
12067
12068/*[clinic input]
12069os.listxattr
12070
12071 path: path_t(allow_fd=True, nullable=True) = None
12072 *
12073 follow_symlinks: bool = True
12074
12075Return a list of extended attributes on path.
12076
BNMetricsb9427072018-11-02 15:20:19 +000012077path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012078if path is None, listxattr will examine the current directory.
12079If follow_symlinks is False, and the last element of the path is a symbolic
12080 link, listxattr will examine the symbolic link itself instead of the file
12081 the link points to.
12082[clinic start generated code]*/
12083
Larry Hastings2f936352014-08-05 14:04:04 +100012084static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012085os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012086/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012087{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012088 Py_ssize_t i;
12089 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012090 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012091 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012092
Larry Hastings2f936352014-08-05 14:04:04 +100012093 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012094 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012095
Larry Hastings2f936352014-08-05 14:04:04 +100012096 name = path->narrow ? path->narrow : ".";
12097
Larry Hastings9cf065c2012-06-22 16:30:09 -070012098 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012099 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012100 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012101 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012102 Py_ssize_t buffer_size = buffer_sizes[i];
12103 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012104 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012105 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012106 break;
12107 }
12108 buffer = PyMem_MALLOC(buffer_size);
12109 if (!buffer) {
12110 PyErr_NoMemory();
12111 break;
12112 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012113
Larry Hastings9cf065c2012-06-22 16:30:09 -070012114 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012115 if (path->fd > -1)
12116 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012117 else if (follow_symlinks)
12118 length = listxattr(name, buffer, buffer_size);
12119 else
12120 length = llistxattr(name, buffer, buffer_size);
12121 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012122
Larry Hastings9cf065c2012-06-22 16:30:09 -070012123 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012124 if (errno == ERANGE) {
12125 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012126 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012127 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012128 }
Larry Hastings2f936352014-08-05 14:04:04 +100012129 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012130 break;
12131 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012132
Larry Hastings9cf065c2012-06-22 16:30:09 -070012133 result = PyList_New(0);
12134 if (!result) {
12135 goto exit;
12136 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012137
Larry Hastings9cf065c2012-06-22 16:30:09 -070012138 end = buffer + length;
12139 for (trace = start = buffer; trace != end; trace++) {
12140 if (!*trace) {
12141 int error;
12142 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12143 trace - start);
12144 if (!attribute) {
12145 Py_DECREF(result);
12146 result = NULL;
12147 goto exit;
12148 }
12149 error = PyList_Append(result, attribute);
12150 Py_DECREF(attribute);
12151 if (error) {
12152 Py_DECREF(result);
12153 result = NULL;
12154 goto exit;
12155 }
12156 start = trace + 1;
12157 }
12158 }
12159 break;
12160 }
12161exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012162 if (buffer)
12163 PyMem_FREE(buffer);
12164 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012165}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012166#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012167
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012168
Larry Hastings2f936352014-08-05 14:04:04 +100012169/*[clinic input]
12170os.urandom
12171
12172 size: Py_ssize_t
12173 /
12174
12175Return a bytes object containing random bytes suitable for cryptographic use.
12176[clinic start generated code]*/
12177
Larry Hastings2f936352014-08-05 14:04:04 +100012178static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012179os_urandom_impl(PyObject *module, Py_ssize_t size)
12180/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012181{
12182 PyObject *bytes;
12183 int result;
12184
Georg Brandl2fb477c2012-02-21 00:33:36 +010012185 if (size < 0)
12186 return PyErr_Format(PyExc_ValueError,
12187 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012188 bytes = PyBytes_FromStringAndSize(NULL, size);
12189 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012190 return NULL;
12191
Victor Stinnere66987e2016-09-06 16:33:52 -070012192 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012193 if (result == -1) {
12194 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012195 return NULL;
12196 }
Larry Hastings2f936352014-08-05 14:04:04 +100012197 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012198}
12199
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012200#ifdef HAVE_MEMFD_CREATE
12201/*[clinic input]
12202os.memfd_create
12203
12204 name: FSConverter
12205 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12206
12207[clinic start generated code]*/
12208
12209static PyObject *
12210os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12211/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12212{
12213 int fd;
12214 const char *bytes = PyBytes_AS_STRING(name);
12215 Py_BEGIN_ALLOW_THREADS
12216 fd = memfd_create(bytes, flags);
12217 Py_END_ALLOW_THREADS
12218 if (fd == -1) {
12219 return PyErr_SetFromErrno(PyExc_OSError);
12220 }
12221 return PyLong_FromLong(fd);
12222}
12223#endif
12224
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012225/* Terminal size querying */
12226
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012227PyDoc_STRVAR(TerminalSize_docstring,
12228 "A tuple of (columns, lines) for holding terminal window size");
12229
12230static PyStructSequence_Field TerminalSize_fields[] = {
12231 {"columns", "width of the terminal window in characters"},
12232 {"lines", "height of the terminal window in characters"},
12233 {NULL, NULL}
12234};
12235
12236static PyStructSequence_Desc TerminalSize_desc = {
12237 "os.terminal_size",
12238 TerminalSize_docstring,
12239 TerminalSize_fields,
12240 2,
12241};
12242
12243#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100012244/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012245PyDoc_STRVAR(termsize__doc__,
12246 "Return the size of the terminal window as (columns, lines).\n" \
12247 "\n" \
12248 "The optional argument fd (default standard output) specifies\n" \
12249 "which file descriptor should be queried.\n" \
12250 "\n" \
12251 "If the file descriptor is not connected to a terminal, an OSError\n" \
12252 "is thrown.\n" \
12253 "\n" \
12254 "This function will only be defined if an implementation is\n" \
12255 "available for this system.\n" \
12256 "\n" \
oldkaa0735f2018-02-02 16:52:55 +080012257 "shutil.get_terminal_size is the high-level function which should\n" \
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012258 "normally be used, os.get_terminal_size is the low-level implementation.");
12259
12260static PyObject*
12261get_terminal_size(PyObject *self, PyObject *args)
12262{
12263 int columns, lines;
12264 PyObject *termsize;
12265
12266 int fd = fileno(stdout);
12267 /* Under some conditions stdout may not be connected and
12268 * fileno(stdout) may point to an invalid file descriptor. For example
12269 * GUI apps don't have valid standard streams by default.
12270 *
12271 * If this happens, and the optional fd argument is not present,
12272 * the ioctl below will fail returning EBADF. This is what we want.
12273 */
12274
12275 if (!PyArg_ParseTuple(args, "|i", &fd))
12276 return NULL;
12277
12278#ifdef TERMSIZE_USE_IOCTL
12279 {
12280 struct winsize w;
12281 if (ioctl(fd, TIOCGWINSZ, &w))
12282 return PyErr_SetFromErrno(PyExc_OSError);
12283 columns = w.ws_col;
12284 lines = w.ws_row;
12285 }
12286#endif /* TERMSIZE_USE_IOCTL */
12287
12288#ifdef TERMSIZE_USE_CONIO
12289 {
12290 DWORD nhandle;
12291 HANDLE handle;
12292 CONSOLE_SCREEN_BUFFER_INFO csbi;
12293 switch (fd) {
12294 case 0: nhandle = STD_INPUT_HANDLE;
12295 break;
12296 case 1: nhandle = STD_OUTPUT_HANDLE;
12297 break;
12298 case 2: nhandle = STD_ERROR_HANDLE;
12299 break;
12300 default:
12301 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12302 }
12303 handle = GetStdHandle(nhandle);
12304 if (handle == NULL)
12305 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12306 if (handle == INVALID_HANDLE_VALUE)
12307 return PyErr_SetFromWindowsErr(0);
12308
12309 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12310 return PyErr_SetFromWindowsErr(0);
12311
12312 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12313 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12314 }
12315#endif /* TERMSIZE_USE_CONIO */
12316
Eddie Elizondob3966632019-11-05 07:16:14 -080012317 PyObject *TerminalSizeType = _posixstate(self)->TerminalSizeType;
12318 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012319 if (termsize == NULL)
12320 return NULL;
12321 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12322 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12323 if (PyErr_Occurred()) {
12324 Py_DECREF(termsize);
12325 return NULL;
12326 }
12327 return termsize;
12328}
12329#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12330
Larry Hastings2f936352014-08-05 14:04:04 +100012331
12332/*[clinic input]
12333os.cpu_count
12334
Charles-François Natali80d62e62015-08-13 20:37:08 +010012335Return the number of CPUs in the system; return None if indeterminable.
12336
12337This number is not equivalent to the number of CPUs the current process can
12338use. The number of usable CPUs can be obtained with
12339``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012340[clinic start generated code]*/
12341
Larry Hastings2f936352014-08-05 14:04:04 +100012342static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012343os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012344/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012345{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012346 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012347#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012348 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012349#elif defined(__hpux)
12350 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12351#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12352 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012353#elif defined(__DragonFly__) || \
12354 defined(__OpenBSD__) || \
12355 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012356 defined(__NetBSD__) || \
12357 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012358 int mib[2];
12359 size_t len = sizeof(ncpu);
12360 mib[0] = CTL_HW;
12361 mib[1] = HW_NCPU;
12362 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12363 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012364#endif
12365 if (ncpu >= 1)
12366 return PyLong_FromLong(ncpu);
12367 else
12368 Py_RETURN_NONE;
12369}
12370
Victor Stinnerdaf45552013-08-28 00:53:59 +020012371
Larry Hastings2f936352014-08-05 14:04:04 +100012372/*[clinic input]
12373os.get_inheritable -> bool
12374
12375 fd: int
12376 /
12377
12378Get the close-on-exe flag of the specified file descriptor.
12379[clinic start generated code]*/
12380
Larry Hastings2f936352014-08-05 14:04:04 +100012381static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012382os_get_inheritable_impl(PyObject *module, int fd)
12383/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012384{
Steve Dower8fc89802015-04-12 00:26:27 -040012385 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012386 _Py_BEGIN_SUPPRESS_IPH
12387 return_value = _Py_get_inheritable(fd);
12388 _Py_END_SUPPRESS_IPH
12389 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012390}
12391
12392
12393/*[clinic input]
12394os.set_inheritable
12395 fd: int
12396 inheritable: int
12397 /
12398
12399Set the inheritable flag of the specified file descriptor.
12400[clinic start generated code]*/
12401
Larry Hastings2f936352014-08-05 14:04:04 +100012402static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012403os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12404/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012405{
Steve Dower8fc89802015-04-12 00:26:27 -040012406 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012407
Steve Dower8fc89802015-04-12 00:26:27 -040012408 _Py_BEGIN_SUPPRESS_IPH
12409 result = _Py_set_inheritable(fd, inheritable, NULL);
12410 _Py_END_SUPPRESS_IPH
12411 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012412 return NULL;
12413 Py_RETURN_NONE;
12414}
12415
12416
12417#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012418/*[clinic input]
12419os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012420 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012421 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012422
Larry Hastings2f936352014-08-05 14:04:04 +100012423Get the close-on-exe flag of the specified file descriptor.
12424[clinic start generated code]*/
12425
Larry Hastings2f936352014-08-05 14:04:04 +100012426static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012427os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012428/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012429{
12430 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012431
12432 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12433 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012434 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012435 }
12436
Larry Hastings2f936352014-08-05 14:04:04 +100012437 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012438}
12439
Victor Stinnerdaf45552013-08-28 00:53:59 +020012440
Larry Hastings2f936352014-08-05 14:04:04 +100012441/*[clinic input]
12442os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012443 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012444 inheritable: bool
12445 /
12446
12447Set the inheritable flag of the specified handle.
12448[clinic start generated code]*/
12449
Larry Hastings2f936352014-08-05 14:04:04 +100012450static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012451os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012452 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012453/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012454{
12455 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012456 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12457 PyErr_SetFromWindowsErr(0);
12458 return NULL;
12459 }
12460 Py_RETURN_NONE;
12461}
Larry Hastings2f936352014-08-05 14:04:04 +100012462#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012463
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012464#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012465/*[clinic input]
12466os.get_blocking -> bool
12467 fd: int
12468 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012469
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012470Get the blocking mode of the file descriptor.
12471
12472Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12473[clinic start generated code]*/
12474
12475static int
12476os_get_blocking_impl(PyObject *module, int fd)
12477/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012478{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012479 int blocking;
12480
Steve Dower8fc89802015-04-12 00:26:27 -040012481 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012482 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012483 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012484 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012485}
12486
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012487/*[clinic input]
12488os.set_blocking
12489 fd: int
12490 blocking: bool(accept={int})
12491 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012492
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012493Set the blocking mode of the specified file descriptor.
12494
12495Set the O_NONBLOCK flag if blocking is False,
12496clear the O_NONBLOCK flag otherwise.
12497[clinic start generated code]*/
12498
12499static PyObject *
12500os_set_blocking_impl(PyObject *module, int fd, int blocking)
12501/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012502{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012503 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012504
Steve Dower8fc89802015-04-12 00:26:27 -040012505 _Py_BEGIN_SUPPRESS_IPH
12506 result = _Py_set_blocking(fd, blocking);
12507 _Py_END_SUPPRESS_IPH
12508 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012509 return NULL;
12510 Py_RETURN_NONE;
12511}
12512#endif /* !MS_WINDOWS */
12513
12514
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012515/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080012516class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012517[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080012518/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012519
12520typedef struct {
12521 PyObject_HEAD
12522 PyObject *name;
12523 PyObject *path;
12524 PyObject *stat;
12525 PyObject *lstat;
12526#ifdef MS_WINDOWS
12527 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012528 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012529 int got_file_index;
12530#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012531#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012532 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012533#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012534 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012535 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012536#endif
12537} DirEntry;
12538
Eddie Elizondob3966632019-11-05 07:16:14 -080012539static PyObject *
12540_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
12541{
12542 PyErr_Format(PyExc_TypeError,
12543 "cannot create '%.100s' instances", _PyType_Name(type));
12544 return NULL;
12545}
12546
Victor Stinner6036e442015-03-08 01:58:04 +010012547static void
12548DirEntry_dealloc(DirEntry *entry)
12549{
Eddie Elizondob3966632019-11-05 07:16:14 -080012550 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010012551 Py_XDECREF(entry->name);
12552 Py_XDECREF(entry->path);
12553 Py_XDECREF(entry->stat);
12554 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080012555 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
12556 free_func(entry);
12557 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010012558}
12559
12560/* Forward reference */
12561static int
12562DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12563
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012564/*[clinic input]
12565os.DirEntry.is_symlink -> bool
12566
12567Return True if the entry is a symbolic link; cached per entry.
12568[clinic start generated code]*/
12569
Victor Stinner6036e442015-03-08 01:58:04 +010012570static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012571os_DirEntry_is_symlink_impl(DirEntry *self)
12572/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012573{
12574#ifdef MS_WINDOWS
12575 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012576#elif defined(HAVE_DIRENT_D_TYPE)
12577 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012578 if (self->d_type != DT_UNKNOWN)
12579 return self->d_type == DT_LNK;
12580 else
12581 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012582#else
12583 /* POSIX without d_type */
12584 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012585#endif
12586}
12587
12588static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012589DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12590{
12591 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012592 STRUCT_STAT st;
12593 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012594
12595#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012596 if (!PyUnicode_FSDecoder(self->path, &ub))
12597 return NULL;
12598 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012599#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012600 if (!PyUnicode_FSConverter(self->path, &ub))
12601 return NULL;
12602 const char *path = PyBytes_AS_STRING(ub);
12603 if (self->dir_fd != DEFAULT_DIR_FD) {
12604#ifdef HAVE_FSTATAT
12605 result = fstatat(self->dir_fd, path, &st,
12606 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12607#else
12608 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12609 return NULL;
12610#endif /* HAVE_FSTATAT */
12611 }
12612 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012613#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012614 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012615 if (follow_symlinks)
12616 result = STAT(path, &st);
12617 else
12618 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012619 }
12620 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012621
12622 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012623 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012624
12625 return _pystat_fromstructstat(&st);
12626}
12627
12628static PyObject *
12629DirEntry_get_lstat(DirEntry *self)
12630{
12631 if (!self->lstat) {
12632#ifdef MS_WINDOWS
12633 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12634#else /* POSIX */
12635 self->lstat = DirEntry_fetch_stat(self, 0);
12636#endif
12637 }
12638 Py_XINCREF(self->lstat);
12639 return self->lstat;
12640}
12641
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012642/*[clinic input]
12643os.DirEntry.stat
12644 *
12645 follow_symlinks: bool = True
12646
12647Return stat_result object for the entry; cached per entry.
12648[clinic start generated code]*/
12649
Victor Stinner6036e442015-03-08 01:58:04 +010012650static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012651os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12652/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012653{
12654 if (!follow_symlinks)
12655 return DirEntry_get_lstat(self);
12656
12657 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012658 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012659 if (result == -1)
12660 return NULL;
12661 else if (result)
12662 self->stat = DirEntry_fetch_stat(self, 1);
12663 else
12664 self->stat = DirEntry_get_lstat(self);
12665 }
12666
12667 Py_XINCREF(self->stat);
12668 return self->stat;
12669}
12670
Victor Stinner6036e442015-03-08 01:58:04 +010012671/* Set exception and return -1 on error, 0 for False, 1 for True */
12672static int
12673DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12674{
12675 PyObject *stat = NULL;
12676 PyObject *st_mode = NULL;
12677 long mode;
12678 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012679#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012680 int is_symlink;
12681 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012682#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012683#ifdef MS_WINDOWS
12684 unsigned long dir_bits;
12685#endif
12686
12687#ifdef MS_WINDOWS
12688 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12689 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012690#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012691 is_symlink = self->d_type == DT_LNK;
12692 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12693#endif
12694
Victor Stinner35a97c02015-03-08 02:59:09 +010012695#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012696 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012697#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012698 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012699 if (!stat) {
12700 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12701 /* If file doesn't exist (anymore), then return False
12702 (i.e., say it's not a file/directory) */
12703 PyErr_Clear();
12704 return 0;
12705 }
12706 goto error;
12707 }
Eddie Elizondob3966632019-11-05 07:16:14 -080012708 st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012709 if (!st_mode)
12710 goto error;
12711
12712 mode = PyLong_AsLong(st_mode);
12713 if (mode == -1 && PyErr_Occurred())
12714 goto error;
12715 Py_CLEAR(st_mode);
12716 Py_CLEAR(stat);
12717 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012718#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012719 }
12720 else if (is_symlink) {
12721 assert(mode_bits != S_IFLNK);
12722 result = 0;
12723 }
12724 else {
12725 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12726#ifdef MS_WINDOWS
12727 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12728 if (mode_bits == S_IFDIR)
12729 result = dir_bits != 0;
12730 else
12731 result = dir_bits == 0;
12732#else /* POSIX */
12733 if (mode_bits == S_IFDIR)
12734 result = self->d_type == DT_DIR;
12735 else
12736 result = self->d_type == DT_REG;
12737#endif
12738 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012739#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012740
12741 return result;
12742
12743error:
12744 Py_XDECREF(st_mode);
12745 Py_XDECREF(stat);
12746 return -1;
12747}
12748
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012749/*[clinic input]
12750os.DirEntry.is_dir -> bool
12751 *
12752 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012753
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012754Return True if the entry is a directory; cached per entry.
12755[clinic start generated code]*/
12756
12757static int
12758os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12759/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12760{
12761 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012762}
12763
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012764/*[clinic input]
12765os.DirEntry.is_file -> bool
12766 *
12767 follow_symlinks: bool = True
12768
12769Return True if the entry is a file; cached per entry.
12770[clinic start generated code]*/
12771
12772static int
12773os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12774/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012775{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012776 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012777}
12778
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012779/*[clinic input]
12780os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012781
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012782Return inode of the entry; cached per entry.
12783[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012784
12785static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012786os_DirEntry_inode_impl(DirEntry *self)
12787/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012788{
12789#ifdef MS_WINDOWS
12790 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012791 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012792 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012793 STRUCT_STAT stat;
12794 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012795
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012796 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012797 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012798 path = PyUnicode_AsUnicode(unicode);
12799 result = LSTAT(path, &stat);
12800 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012801
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012802 if (result != 0)
12803 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012804
12805 self->win32_file_index = stat.st_ino;
12806 self->got_file_index = 1;
12807 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012808 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12809 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012810#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012811 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12812 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012813#endif
12814}
12815
12816static PyObject *
12817DirEntry_repr(DirEntry *self)
12818{
12819 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
12820}
12821
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012822/*[clinic input]
12823os.DirEntry.__fspath__
12824
12825Returns the path for the entry.
12826[clinic start generated code]*/
12827
Brett Cannon96881cd2016-06-10 14:37:21 -070012828static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012829os_DirEntry___fspath___impl(DirEntry *self)
12830/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070012831{
12832 Py_INCREF(self->path);
12833 return self->path;
12834}
12835
Victor Stinner6036e442015-03-08 01:58:04 +010012836static PyMemberDef DirEntry_members[] = {
12837 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
12838 "the entry's base filename, relative to scandir() \"path\" argument"},
12839 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
12840 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
12841 {NULL}
12842};
12843
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012844#include "clinic/posixmodule.c.h"
12845
Victor Stinner6036e442015-03-08 01:58:04 +010012846static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012847 OS_DIRENTRY_IS_DIR_METHODDEF
12848 OS_DIRENTRY_IS_FILE_METHODDEF
12849 OS_DIRENTRY_IS_SYMLINK_METHODDEF
12850 OS_DIRENTRY_STAT_METHODDEF
12851 OS_DIRENTRY_INODE_METHODDEF
12852 OS_DIRENTRY___FSPATH___METHODDEF
Victor Stinner6036e442015-03-08 01:58:04 +010012853 {NULL}
12854};
12855
Eddie Elizondob3966632019-11-05 07:16:14 -080012856static PyType_Slot DirEntryType_slots[] = {
12857 {Py_tp_new, _disabled_new},
12858 {Py_tp_dealloc, DirEntry_dealloc},
12859 {Py_tp_repr, DirEntry_repr},
12860 {Py_tp_methods, DirEntry_methods},
12861 {Py_tp_members, DirEntry_members},
12862 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010012863};
12864
Eddie Elizondob3966632019-11-05 07:16:14 -080012865static PyType_Spec DirEntryType_spec = {
12866 MODNAME ".DirEntry",
12867 sizeof(DirEntry),
12868 0,
12869 Py_TPFLAGS_DEFAULT,
12870 DirEntryType_slots
12871};
12872
12873
Victor Stinner6036e442015-03-08 01:58:04 +010012874#ifdef MS_WINDOWS
12875
12876static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012877join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010012878{
12879 Py_ssize_t path_len;
12880 Py_ssize_t size;
12881 wchar_t *result;
12882 wchar_t ch;
12883
12884 if (!path_wide) { /* Default arg: "." */
12885 path_wide = L".";
12886 path_len = 1;
12887 }
12888 else {
12889 path_len = wcslen(path_wide);
12890 }
12891
12892 /* The +1's are for the path separator and the NUL */
12893 size = path_len + 1 + wcslen(filename) + 1;
12894 result = PyMem_New(wchar_t, size);
12895 if (!result) {
12896 PyErr_NoMemory();
12897 return NULL;
12898 }
12899 wcscpy(result, path_wide);
12900 if (path_len > 0) {
12901 ch = result[path_len - 1];
12902 if (ch != SEP && ch != ALTSEP && ch != L':')
12903 result[path_len++] = SEP;
12904 wcscpy(result + path_len, filename);
12905 }
12906 return result;
12907}
12908
12909static PyObject *
12910DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
12911{
12912 DirEntry *entry;
12913 BY_HANDLE_FILE_INFORMATION file_info;
12914 ULONG reparse_tag;
12915 wchar_t *joined_path;
12916
Eddie Elizondob3966632019-11-05 07:16:14 -080012917 PyObject *DirEntryType = _posixstate_global->DirEntryType;
12918 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010012919 if (!entry)
12920 return NULL;
12921 entry->name = NULL;
12922 entry->path = NULL;
12923 entry->stat = NULL;
12924 entry->lstat = NULL;
12925 entry->got_file_index = 0;
12926
12927 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
12928 if (!entry->name)
12929 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012930 if (path->narrow) {
12931 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
12932 if (!entry->name)
12933 goto error;
12934 }
Victor Stinner6036e442015-03-08 01:58:04 +010012935
12936 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
12937 if (!joined_path)
12938 goto error;
12939
12940 entry->path = PyUnicode_FromWideChar(joined_path, -1);
12941 PyMem_Free(joined_path);
12942 if (!entry->path)
12943 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070012944 if (path->narrow) {
12945 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
12946 if (!entry->path)
12947 goto error;
12948 }
Victor Stinner6036e442015-03-08 01:58:04 +010012949
Steve Dowercc16be82016-09-08 10:35:16 -070012950 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010012951 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
12952
12953 return (PyObject *)entry;
12954
12955error:
12956 Py_DECREF(entry);
12957 return NULL;
12958}
12959
12960#else /* POSIX */
12961
12962static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012963join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010012964{
12965 Py_ssize_t path_len;
12966 Py_ssize_t size;
12967 char *result;
12968
12969 if (!path_narrow) { /* Default arg: "." */
12970 path_narrow = ".";
12971 path_len = 1;
12972 }
12973 else {
12974 path_len = strlen(path_narrow);
12975 }
12976
12977 if (filename_len == -1)
12978 filename_len = strlen(filename);
12979
12980 /* The +1's are for the path separator and the NUL */
12981 size = path_len + 1 + filename_len + 1;
12982 result = PyMem_New(char, size);
12983 if (!result) {
12984 PyErr_NoMemory();
12985 return NULL;
12986 }
12987 strcpy(result, path_narrow);
12988 if (path_len > 0 && result[path_len - 1] != '/')
12989 result[path_len++] = '/';
12990 strcpy(result + path_len, filename);
12991 return result;
12992}
12993
12994static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020012995DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010012996 ino_t d_ino
12997#ifdef HAVE_DIRENT_D_TYPE
12998 , unsigned char d_type
12999#endif
13000 )
Victor Stinner6036e442015-03-08 01:58:04 +010013001{
13002 DirEntry *entry;
13003 char *joined_path;
13004
Eddie Elizondob3966632019-11-05 07:16:14 -080013005 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13006 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013007 if (!entry)
13008 return NULL;
13009 entry->name = NULL;
13010 entry->path = NULL;
13011 entry->stat = NULL;
13012 entry->lstat = NULL;
13013
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013014 if (path->fd != -1) {
13015 entry->dir_fd = path->fd;
13016 joined_path = NULL;
13017 }
13018 else {
13019 entry->dir_fd = DEFAULT_DIR_FD;
13020 joined_path = join_path_filename(path->narrow, name, name_len);
13021 if (!joined_path)
13022 goto error;
13023 }
Victor Stinner6036e442015-03-08 01:58:04 +010013024
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013025 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013026 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013027 if (joined_path)
13028 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013029 }
13030 else {
13031 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013032 if (joined_path)
13033 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013034 }
13035 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013036 if (!entry->name)
13037 goto error;
13038
13039 if (path->fd != -1) {
13040 entry->path = entry->name;
13041 Py_INCREF(entry->path);
13042 }
13043 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013044 goto error;
13045
Victor Stinner35a97c02015-03-08 02:59:09 +010013046#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013047 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013048#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013049 entry->d_ino = d_ino;
13050
13051 return (PyObject *)entry;
13052
13053error:
13054 Py_XDECREF(entry);
13055 return NULL;
13056}
13057
13058#endif
13059
13060
13061typedef struct {
13062 PyObject_HEAD
13063 path_t path;
13064#ifdef MS_WINDOWS
13065 HANDLE handle;
13066 WIN32_FIND_DATAW file_data;
13067 int first_time;
13068#else /* POSIX */
13069 DIR *dirp;
13070#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013071#ifdef HAVE_FDOPENDIR
13072 int fd;
13073#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013074} ScandirIterator;
13075
13076#ifdef MS_WINDOWS
13077
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013078static int
13079ScandirIterator_is_closed(ScandirIterator *iterator)
13080{
13081 return iterator->handle == INVALID_HANDLE_VALUE;
13082}
13083
Victor Stinner6036e442015-03-08 01:58:04 +010013084static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013085ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013086{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013087 HANDLE handle = iterator->handle;
13088
13089 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013090 return;
13091
Victor Stinner6036e442015-03-08 01:58:04 +010013092 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013093 Py_BEGIN_ALLOW_THREADS
13094 FindClose(handle);
13095 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013096}
13097
13098static PyObject *
13099ScandirIterator_iternext(ScandirIterator *iterator)
13100{
13101 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13102 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013103 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013104
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013105 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013106 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013107 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013108
13109 while (1) {
13110 if (!iterator->first_time) {
13111 Py_BEGIN_ALLOW_THREADS
13112 success = FindNextFileW(iterator->handle, file_data);
13113 Py_END_ALLOW_THREADS
13114 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013115 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013116 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013117 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013118 break;
13119 }
13120 }
13121 iterator->first_time = 0;
13122
13123 /* Skip over . and .. */
13124 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013125 wcscmp(file_data->cFileName, L"..") != 0) {
13126 entry = DirEntry_from_find_data(&iterator->path, file_data);
13127 if (!entry)
13128 break;
13129 return entry;
13130 }
Victor Stinner6036e442015-03-08 01:58:04 +010013131
13132 /* Loop till we get a non-dot directory or finish iterating */
13133 }
13134
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013135 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013136 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013137 return NULL;
13138}
13139
13140#else /* POSIX */
13141
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013142static int
13143ScandirIterator_is_closed(ScandirIterator *iterator)
13144{
13145 return !iterator->dirp;
13146}
13147
Victor Stinner6036e442015-03-08 01:58:04 +010013148static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013149ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013150{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013151 DIR *dirp = iterator->dirp;
13152
13153 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013154 return;
13155
Victor Stinner6036e442015-03-08 01:58:04 +010013156 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013157 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013158#ifdef HAVE_FDOPENDIR
13159 if (iterator->path.fd != -1)
13160 rewinddir(dirp);
13161#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013162 closedir(dirp);
13163 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013164 return;
13165}
13166
13167static PyObject *
13168ScandirIterator_iternext(ScandirIterator *iterator)
13169{
13170 struct dirent *direntp;
13171 Py_ssize_t name_len;
13172 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013173 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013174
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013175 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013176 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013177 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013178
13179 while (1) {
13180 errno = 0;
13181 Py_BEGIN_ALLOW_THREADS
13182 direntp = readdir(iterator->dirp);
13183 Py_END_ALLOW_THREADS
13184
13185 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013186 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013187 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013188 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013189 break;
13190 }
13191
13192 /* Skip over . and .. */
13193 name_len = NAMLEN(direntp);
13194 is_dot = direntp->d_name[0] == '.' &&
13195 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13196 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013197 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013198 name_len, direntp->d_ino
13199#ifdef HAVE_DIRENT_D_TYPE
13200 , direntp->d_type
13201#endif
13202 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013203 if (!entry)
13204 break;
13205 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013206 }
13207
13208 /* Loop till we get a non-dot directory or finish iterating */
13209 }
13210
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013211 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013212 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013213 return NULL;
13214}
13215
13216#endif
13217
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013218static PyObject *
13219ScandirIterator_close(ScandirIterator *self, PyObject *args)
13220{
13221 ScandirIterator_closedir(self);
13222 Py_RETURN_NONE;
13223}
13224
13225static PyObject *
13226ScandirIterator_enter(PyObject *self, PyObject *args)
13227{
13228 Py_INCREF(self);
13229 return self;
13230}
13231
13232static PyObject *
13233ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13234{
13235 ScandirIterator_closedir(self);
13236 Py_RETURN_NONE;
13237}
13238
Victor Stinner6036e442015-03-08 01:58:04 +010013239static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013240ScandirIterator_finalize(ScandirIterator *iterator)
13241{
13242 PyObject *error_type, *error_value, *error_traceback;
13243
13244 /* Save the current exception, if any. */
13245 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13246
13247 if (!ScandirIterator_is_closed(iterator)) {
13248 ScandirIterator_closedir(iterator);
13249
13250 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13251 "unclosed scandir iterator %R", iterator)) {
13252 /* Spurious errors can appear at shutdown */
13253 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13254 PyErr_WriteUnraisable((PyObject *) iterator);
13255 }
13256 }
13257 }
13258
Victor Stinner7bfa4092016-03-23 00:43:54 +010013259 path_cleanup(&iterator->path);
13260
13261 /* Restore the saved exception. */
13262 PyErr_Restore(error_type, error_value, error_traceback);
13263}
13264
13265static void
Victor Stinner6036e442015-03-08 01:58:04 +010013266ScandirIterator_dealloc(ScandirIterator *iterator)
13267{
Eddie Elizondob3966632019-11-05 07:16:14 -080013268 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013269 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13270 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013271
Eddie Elizondob3966632019-11-05 07:16:14 -080013272 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13273 free_func(iterator);
13274 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013275}
13276
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013277static PyMethodDef ScandirIterator_methods[] = {
13278 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13279 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13280 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13281 {NULL}
13282};
13283
Eddie Elizondob3966632019-11-05 07:16:14 -080013284static PyType_Slot ScandirIteratorType_slots[] = {
13285 {Py_tp_new, _disabled_new},
13286 {Py_tp_dealloc, ScandirIterator_dealloc},
13287 {Py_tp_finalize, ScandirIterator_finalize},
13288 {Py_tp_iter, PyObject_SelfIter},
13289 {Py_tp_iternext, ScandirIterator_iternext},
13290 {Py_tp_methods, ScandirIterator_methods},
13291 {0, 0},
13292};
13293
13294static PyType_Spec ScandirIteratorType_spec = {
13295 MODNAME ".ScandirIterator",
13296 sizeof(ScandirIterator),
13297 0,
13298 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13299 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013300};
13301
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013302/*[clinic input]
13303os.scandir
13304
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013305 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013306
13307Return an iterator of DirEntry objects for given path.
13308
BNMetricsb9427072018-11-02 15:20:19 +000013309path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013310is bytes, the names of yielded DirEntry objects will also be bytes; in
13311all other circumstances they will be str.
13312
13313If path is None, uses the path='.'.
13314[clinic start generated code]*/
13315
Victor Stinner6036e442015-03-08 01:58:04 +010013316static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013317os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013318/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013319{
13320 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013321#ifdef MS_WINDOWS
13322 wchar_t *path_strW;
13323#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013324 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013325#ifdef HAVE_FDOPENDIR
13326 int fd = -1;
13327#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013328#endif
13329
Steve Dower60419a72019-06-24 08:42:54 -070013330 if (PySys_Audit("os.scandir", "O",
13331 path->object ? path->object : Py_None) < 0) {
13332 return NULL;
13333 }
13334
Eddie Elizondob3966632019-11-05 07:16:14 -080013335 PyObject *ScandirIteratorType = _posixstate(module)->ScandirIteratorType;
13336 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010013337 if (!iterator)
13338 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013339
13340#ifdef MS_WINDOWS
13341 iterator->handle = INVALID_HANDLE_VALUE;
13342#else
13343 iterator->dirp = NULL;
13344#endif
13345
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013346 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013347 /* Move the ownership to iterator->path */
13348 path->object = NULL;
13349 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013350
13351#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013352 iterator->first_time = 1;
13353
13354 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13355 if (!path_strW)
13356 goto error;
13357
13358 Py_BEGIN_ALLOW_THREADS
13359 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13360 Py_END_ALLOW_THREADS
13361
13362 PyMem_Free(path_strW);
13363
13364 if (iterator->handle == INVALID_HANDLE_VALUE) {
13365 path_error(&iterator->path);
13366 goto error;
13367 }
13368#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013369 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013370#ifdef HAVE_FDOPENDIR
13371 if (path->fd != -1) {
13372 /* closedir() closes the FD, so we duplicate it */
13373 fd = _Py_dup(path->fd);
13374 if (fd == -1)
13375 goto error;
13376
13377 Py_BEGIN_ALLOW_THREADS
13378 iterator->dirp = fdopendir(fd);
13379 Py_END_ALLOW_THREADS
13380 }
13381 else
13382#endif
13383 {
13384 if (iterator->path.narrow)
13385 path_str = iterator->path.narrow;
13386 else
13387 path_str = ".";
13388
13389 Py_BEGIN_ALLOW_THREADS
13390 iterator->dirp = opendir(path_str);
13391 Py_END_ALLOW_THREADS
13392 }
Victor Stinner6036e442015-03-08 01:58:04 +010013393
13394 if (!iterator->dirp) {
13395 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013396#ifdef HAVE_FDOPENDIR
13397 if (fd != -1) {
13398 Py_BEGIN_ALLOW_THREADS
13399 close(fd);
13400 Py_END_ALLOW_THREADS
13401 }
13402#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013403 goto error;
13404 }
13405#endif
13406
13407 return (PyObject *)iterator;
13408
13409error:
13410 Py_DECREF(iterator);
13411 return NULL;
13412}
13413
Ethan Furman410ef8e2016-06-04 12:06:26 -070013414/*
13415 Return the file system path representation of the object.
13416
13417 If the object is str or bytes, then allow it to pass through with
13418 an incremented refcount. If the object defines __fspath__(), then
13419 return the result of that method. All other types raise a TypeError.
13420*/
13421PyObject *
13422PyOS_FSPath(PyObject *path)
13423{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013424 /* For error message reasons, this function is manually inlined in
13425 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013426 PyObject *func = NULL;
13427 PyObject *path_repr = NULL;
13428
13429 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13430 Py_INCREF(path);
13431 return path;
13432 }
13433
13434 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13435 if (NULL == func) {
13436 return PyErr_Format(PyExc_TypeError,
13437 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013438 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080013439 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070013440 }
13441
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013442 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013443 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013444 if (NULL == path_repr) {
13445 return NULL;
13446 }
13447
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013448 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13449 PyErr_Format(PyExc_TypeError,
13450 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080013451 "not %.200s", _PyType_Name(Py_TYPE(path)),
13452 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013453 Py_DECREF(path_repr);
13454 return NULL;
13455 }
13456
Ethan Furman410ef8e2016-06-04 12:06:26 -070013457 return path_repr;
13458}
13459
13460/*[clinic input]
13461os.fspath
13462
13463 path: object
13464
13465Return the file system path representation of the object.
13466
Brett Cannonb4f43e92016-06-09 14:32:08 -070013467If the object is str or bytes, then allow it to pass through as-is. If the
13468object defines __fspath__(), then return the result of that method. All other
13469types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013470[clinic start generated code]*/
13471
13472static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013473os_fspath_impl(PyObject *module, PyObject *path)
13474/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013475{
13476 return PyOS_FSPath(path);
13477}
Victor Stinner6036e442015-03-08 01:58:04 +010013478
Victor Stinner9b1f4742016-09-06 16:18:52 -070013479#ifdef HAVE_GETRANDOM_SYSCALL
13480/*[clinic input]
13481os.getrandom
13482
13483 size: Py_ssize_t
13484 flags: int=0
13485
13486Obtain a series of random bytes.
13487[clinic start generated code]*/
13488
13489static PyObject *
13490os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13491/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13492{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013493 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013494 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013495
13496 if (size < 0) {
13497 errno = EINVAL;
13498 return posix_error();
13499 }
13500
Victor Stinnerec2319c2016-09-20 23:00:59 +020013501 bytes = PyBytes_FromStringAndSize(NULL, size);
13502 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013503 PyErr_NoMemory();
13504 return NULL;
13505 }
13506
13507 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013508 n = syscall(SYS_getrandom,
13509 PyBytes_AS_STRING(bytes),
13510 PyBytes_GET_SIZE(bytes),
13511 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013512 if (n < 0 && errno == EINTR) {
13513 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013514 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013515 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013516
13517 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013518 continue;
13519 }
13520 break;
13521 }
13522
13523 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013524 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013525 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013526 }
13527
Victor Stinnerec2319c2016-09-20 23:00:59 +020013528 if (n != size) {
13529 _PyBytes_Resize(&bytes, n);
13530 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013531
13532 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013533
13534error:
13535 Py_DECREF(bytes);
13536 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013537}
13538#endif /* HAVE_GETRANDOM_SYSCALL */
13539
Steve Dower2438cdf2019-03-29 16:37:16 -070013540#ifdef MS_WINDOWS
13541/* bpo-36085: Helper functions for managing DLL search directories
13542 * on win32
13543 */
13544
13545typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13546typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13547
13548/*[clinic input]
13549os._add_dll_directory
13550
13551 path: path_t
13552
13553Add a path to the DLL search path.
13554
13555This search path is used when resolving dependencies for imported
13556extension modules (the module itself is resolved through sys.path),
13557and also by ctypes.
13558
13559Returns an opaque value that may be passed to os.remove_dll_directory
13560to remove this directory from the search path.
13561[clinic start generated code]*/
13562
13563static PyObject *
13564os__add_dll_directory_impl(PyObject *module, path_t *path)
13565/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13566{
13567 HMODULE hKernel32;
13568 PAddDllDirectory AddDllDirectory;
13569 DLL_DIRECTORY_COOKIE cookie = 0;
13570 DWORD err = 0;
13571
13572 /* For Windows 7, we have to load this. As this will be a fairly
13573 infrequent operation, just do it each time. Kernel32 is always
13574 loaded. */
13575 Py_BEGIN_ALLOW_THREADS
13576 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13577 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13578 hKernel32, "AddDllDirectory")) ||
13579 !(cookie = (*AddDllDirectory)(path->wide))) {
13580 err = GetLastError();
13581 }
13582 Py_END_ALLOW_THREADS
13583
13584 if (err) {
13585 return win32_error_object_err("add_dll_directory",
13586 path->object, err);
13587 }
13588
13589 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13590}
13591
13592/*[clinic input]
13593os._remove_dll_directory
13594
13595 cookie: object
13596
13597Removes a path from the DLL search path.
13598
13599The parameter is an opaque value that was returned from
13600os.add_dll_directory. You can only remove directories that you added
13601yourself.
13602[clinic start generated code]*/
13603
13604static PyObject *
13605os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13606/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13607{
13608 HMODULE hKernel32;
13609 PRemoveDllDirectory RemoveDllDirectory;
13610 DLL_DIRECTORY_COOKIE cookieValue;
13611 DWORD err = 0;
13612
13613 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13614 PyErr_SetString(PyExc_TypeError,
13615 "Provided cookie was not returned from os.add_dll_directory");
13616 return NULL;
13617 }
13618
13619 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13620 cookie, "DLL directory cookie");
13621
13622 /* For Windows 7, we have to load this. As this will be a fairly
13623 infrequent operation, just do it each time. Kernel32 is always
13624 loaded. */
13625 Py_BEGIN_ALLOW_THREADS
13626 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13627 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13628 hKernel32, "RemoveDllDirectory")) ||
13629 !(*RemoveDllDirectory)(cookieValue)) {
13630 err = GetLastError();
13631 }
13632 Py_END_ALLOW_THREADS
13633
13634 if (err) {
13635 return win32_error_object_err("remove_dll_directory",
13636 NULL, err);
13637 }
13638
13639 if (PyCapsule_SetName(cookie, NULL)) {
13640 return NULL;
13641 }
13642
13643 Py_RETURN_NONE;
13644}
13645
13646#endif
Larry Hastings31826802013-10-19 00:09:25 -070013647
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013648static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013649
13650 OS_STAT_METHODDEF
13651 OS_ACCESS_METHODDEF
13652 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013653 OS_CHDIR_METHODDEF
13654 OS_CHFLAGS_METHODDEF
13655 OS_CHMOD_METHODDEF
13656 OS_FCHMOD_METHODDEF
13657 OS_LCHMOD_METHODDEF
13658 OS_CHOWN_METHODDEF
13659 OS_FCHOWN_METHODDEF
13660 OS_LCHOWN_METHODDEF
13661 OS_LCHFLAGS_METHODDEF
13662 OS_CHROOT_METHODDEF
13663 OS_CTERMID_METHODDEF
13664 OS_GETCWD_METHODDEF
13665 OS_GETCWDB_METHODDEF
13666 OS_LINK_METHODDEF
13667 OS_LISTDIR_METHODDEF
13668 OS_LSTAT_METHODDEF
13669 OS_MKDIR_METHODDEF
13670 OS_NICE_METHODDEF
13671 OS_GETPRIORITY_METHODDEF
13672 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013673 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013674 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013675 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013676 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013677 OS_RENAME_METHODDEF
13678 OS_REPLACE_METHODDEF
13679 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013680 OS_SYMLINK_METHODDEF
13681 OS_SYSTEM_METHODDEF
13682 OS_UMASK_METHODDEF
13683 OS_UNAME_METHODDEF
13684 OS_UNLINK_METHODDEF
13685 OS_REMOVE_METHODDEF
13686 OS_UTIME_METHODDEF
13687 OS_TIMES_METHODDEF
13688 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013689 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013690 OS_EXECV_METHODDEF
13691 OS_EXECVE_METHODDEF
13692 OS_SPAWNV_METHODDEF
13693 OS_SPAWNVE_METHODDEF
13694 OS_FORK1_METHODDEF
13695 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013696 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013697 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13698 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13699 OS_SCHED_GETPARAM_METHODDEF
13700 OS_SCHED_GETSCHEDULER_METHODDEF
13701 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13702 OS_SCHED_SETPARAM_METHODDEF
13703 OS_SCHED_SETSCHEDULER_METHODDEF
13704 OS_SCHED_YIELD_METHODDEF
13705 OS_SCHED_SETAFFINITY_METHODDEF
13706 OS_SCHED_GETAFFINITY_METHODDEF
13707 OS_OPENPTY_METHODDEF
13708 OS_FORKPTY_METHODDEF
13709 OS_GETEGID_METHODDEF
13710 OS_GETEUID_METHODDEF
13711 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020013712#ifdef HAVE_GETGROUPLIST
13713 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
13714#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013715 OS_GETGROUPS_METHODDEF
13716 OS_GETPID_METHODDEF
13717 OS_GETPGRP_METHODDEF
13718 OS_GETPPID_METHODDEF
13719 OS_GETUID_METHODDEF
13720 OS_GETLOGIN_METHODDEF
13721 OS_KILL_METHODDEF
13722 OS_KILLPG_METHODDEF
13723 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013724#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070013725 OS_STARTFILE_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000013726#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013727 OS_SETUID_METHODDEF
13728 OS_SETEUID_METHODDEF
13729 OS_SETREUID_METHODDEF
13730 OS_SETGID_METHODDEF
13731 OS_SETEGID_METHODDEF
13732 OS_SETREGID_METHODDEF
13733 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000013734#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000013735 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000013736#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100013737 OS_GETPGID_METHODDEF
13738 OS_SETPGRP_METHODDEF
13739 OS_WAIT_METHODDEF
13740 OS_WAIT3_METHODDEF
13741 OS_WAIT4_METHODDEF
13742 OS_WAITID_METHODDEF
13743 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080013744 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013745 OS_GETSID_METHODDEF
13746 OS_SETSID_METHODDEF
13747 OS_SETPGID_METHODDEF
13748 OS_TCGETPGRP_METHODDEF
13749 OS_TCSETPGRP_METHODDEF
13750 OS_OPEN_METHODDEF
13751 OS_CLOSE_METHODDEF
13752 OS_CLOSERANGE_METHODDEF
13753 OS_DEVICE_ENCODING_METHODDEF
13754 OS_DUP_METHODDEF
13755 OS_DUP2_METHODDEF
13756 OS_LOCKF_METHODDEF
13757 OS_LSEEK_METHODDEF
13758 OS_READ_METHODDEF
13759 OS_READV_METHODDEF
13760 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013761 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013762 OS_WRITE_METHODDEF
13763 OS_WRITEV_METHODDEF
13764 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000013765 OS_PWRITEV_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013766#ifdef HAVE_SENDFILE
Serhiy Storchaka62be7422018-11-27 13:27:31 +020013767 {"sendfile", (PyCFunction)(void(*)(void))posix_sendfile, METH_VARARGS | METH_KEYWORDS,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000013768 posix_sendfile__doc__},
13769#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013770 OS_FSTAT_METHODDEF
13771 OS_ISATTY_METHODDEF
13772 OS_PIPE_METHODDEF
13773 OS_PIPE2_METHODDEF
13774 OS_MKFIFO_METHODDEF
13775 OS_MKNOD_METHODDEF
13776 OS_MAJOR_METHODDEF
13777 OS_MINOR_METHODDEF
13778 OS_MAKEDEV_METHODDEF
13779 OS_FTRUNCATE_METHODDEF
13780 OS_TRUNCATE_METHODDEF
13781 OS_POSIX_FALLOCATE_METHODDEF
13782 OS_POSIX_FADVISE_METHODDEF
13783 OS_PUTENV_METHODDEF
13784 OS_UNSETENV_METHODDEF
13785 OS_STRERROR_METHODDEF
13786 OS_FCHDIR_METHODDEF
13787 OS_FSYNC_METHODDEF
13788 OS_SYNC_METHODDEF
13789 OS_FDATASYNC_METHODDEF
13790 OS_WCOREDUMP_METHODDEF
13791 OS_WIFCONTINUED_METHODDEF
13792 OS_WIFSTOPPED_METHODDEF
13793 OS_WIFSIGNALED_METHODDEF
13794 OS_WIFEXITED_METHODDEF
13795 OS_WEXITSTATUS_METHODDEF
13796 OS_WTERMSIG_METHODDEF
13797 OS_WSTOPSIG_METHODDEF
13798 OS_FSTATVFS_METHODDEF
13799 OS_STATVFS_METHODDEF
13800 OS_CONFSTR_METHODDEF
13801 OS_SYSCONF_METHODDEF
13802 OS_FPATHCONF_METHODDEF
13803 OS_PATHCONF_METHODDEF
13804 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030013805 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013806 OS__GETDISKUSAGE_METHODDEF
13807 OS__GETFINALPATHNAME_METHODDEF
13808 OS__GETVOLUMEPATHNAME_METHODDEF
13809 OS_GETLOADAVG_METHODDEF
13810 OS_URANDOM_METHODDEF
13811 OS_SETRESUID_METHODDEF
13812 OS_SETRESGID_METHODDEF
13813 OS_GETRESUID_METHODDEF
13814 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000013815
Larry Hastings2f936352014-08-05 14:04:04 +100013816 OS_GETXATTR_METHODDEF
13817 OS_SETXATTR_METHODDEF
13818 OS_REMOVEXATTR_METHODDEF
13819 OS_LISTXATTR_METHODDEF
13820
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013821#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
13822 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
13823#endif
Larry Hastings2f936352014-08-05 14:04:04 +100013824 OS_CPU_COUNT_METHODDEF
13825 OS_GET_INHERITABLE_METHODDEF
13826 OS_SET_INHERITABLE_METHODDEF
13827 OS_GET_HANDLE_INHERITABLE_METHODDEF
13828 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013829#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013830 OS_GET_BLOCKING_METHODDEF
13831 OS_SET_BLOCKING_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013832#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013833 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070013834 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070013835 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060013836 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070013837#ifdef MS_WINDOWS
13838 OS__ADD_DLL_DIRECTORY_METHODDEF
13839 OS__REMOVE_DLL_DIRECTORY_METHODDEF
13840#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013841 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013842};
13843
Barry Warsaw4a342091996-12-19 23:50:02 +000013844static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013845all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000013846{
Guido van Rossum94f6f721999-01-06 18:42:14 +000013847#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013848 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013849#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013850#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013851 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013852#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013853#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013854 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013855#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000013856#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013857 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013858#endif
Fred Drakec9680921999-12-13 16:37:25 +000013859#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013860 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000013861#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013862#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013863 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013864#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013865#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013866 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013867#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013868#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013869 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013870#endif
Fred Drake106c1a02002-04-23 15:58:02 +000013871#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013872 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000013873#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000013874#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013875 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013876#endif
13877#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013878 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013879#endif
13880#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013881 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013882#endif
13883#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013884 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013885#endif
13886#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013887 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013888#endif
13889#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013890 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013891#endif
13892#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013893 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013894#endif
13895#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013896 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013897#endif
13898#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013899 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013900#endif
13901#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013902 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013903#endif
13904#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013905 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013906#endif
13907#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013908 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013909#endif
13910#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013911 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000013912#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000013913#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013914 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013915#endif
13916#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013917 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000013918#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013919#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013920 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013921#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013922#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013923 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013924#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013925#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000013926#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013927 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013928#endif
13929#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013930 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000013931#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020013932#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013933#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013934 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013935#endif
13936#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013937 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013938#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013939#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013940 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050013941#endif
Jesus Ceacf381202012-04-24 20:44:40 +020013942#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013943 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020013944#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020013945#ifdef O_TMPFILE
13946 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
13947#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013948#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013949 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013950#endif
13951#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013952 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013953#endif
13954#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013955 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013956#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020013957#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013958 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020013959#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020013960#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013961 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020013962#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000013963
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013964
Jesus Cea94363612012-06-22 18:32:07 +020013965#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013966 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013967#endif
13968#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013969 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020013970#endif
13971
Tim Peters5aa91602002-01-30 05:46:57 +000013972/* MS Windows */
13973#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000013974 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013975 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013976#endif
13977#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000013978 /* Optimize for short life (keep in memory). */
13979 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013980 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013981#endif
13982#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000013983 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013984 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013985#endif
13986#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000013987 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013988 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013989#endif
13990#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000013991 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013992 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000013993#endif
13994
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000013995/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000013996#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000013997 /* Send a SIGIO signal whenever input or output
13998 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013999 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014000#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014001#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000014002 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014003 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014004#endif
14005#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000014006 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014007 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014008#endif
14009#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000014010 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014011 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014012#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014013#ifdef O_NOLINKS
14014 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014015 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014016#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014017#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000014018 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014019 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014020#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000014021
Victor Stinner8c62be82010-05-06 00:08:46 +000014022 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014023#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014024 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014025#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014026#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014027 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014028#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014029#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014030 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014031#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014032#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014033 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014034#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014035#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014036 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014037#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014038#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014039 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014040#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014041#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014042 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014043#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014044#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014045 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014046#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014047#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014048 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014049#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014050#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014051 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014052#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014053#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014054 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014055#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014056#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014057 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014058#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014059#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014060 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014061#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014062#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014063 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014064#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014065#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014066 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014067#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014068#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014069 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014070#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014071#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014072 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014073#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014074
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014075 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014076#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014077 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014078#endif /* ST_RDONLY */
14079#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014080 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014081#endif /* ST_NOSUID */
14082
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014083 /* GNU extensions */
14084#ifdef ST_NODEV
14085 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14086#endif /* ST_NODEV */
14087#ifdef ST_NOEXEC
14088 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14089#endif /* ST_NOEXEC */
14090#ifdef ST_SYNCHRONOUS
14091 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14092#endif /* ST_SYNCHRONOUS */
14093#ifdef ST_MANDLOCK
14094 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14095#endif /* ST_MANDLOCK */
14096#ifdef ST_WRITE
14097 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14098#endif /* ST_WRITE */
14099#ifdef ST_APPEND
14100 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14101#endif /* ST_APPEND */
14102#ifdef ST_NOATIME
14103 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14104#endif /* ST_NOATIME */
14105#ifdef ST_NODIRATIME
14106 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14107#endif /* ST_NODIRATIME */
14108#ifdef ST_RELATIME
14109 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14110#endif /* ST_RELATIME */
14111
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014112 /* FreeBSD sendfile() constants */
14113#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014114 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014115#endif
14116#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014117 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014118#endif
14119#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014120 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014121#endif
14122
Ross Lagerwall7807c352011-03-17 20:20:30 +020014123 /* constants for posix_fadvise */
14124#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014125 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014126#endif
14127#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014128 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014129#endif
14130#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014131 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014132#endif
14133#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014134 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014135#endif
14136#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014137 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014138#endif
14139#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014140 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014141#endif
14142
14143 /* constants for waitid */
14144#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014145 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14146 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14147 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080014148#ifdef P_PIDFD
14149 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
14150#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014151#endif
14152#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014153 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014154#endif
14155#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014156 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014157#endif
14158#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014159 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014160#endif
14161#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014162 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014163#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014164#ifdef CLD_KILLED
14165 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14166#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014167#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014168 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014169#endif
14170#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014171 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014172#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014173#ifdef CLD_STOPPED
14174 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14175#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014176#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014177 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014178#endif
14179
14180 /* constants for lockf */
14181#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014182 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014183#endif
14184#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014185 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014186#endif
14187#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014188 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014189#endif
14190#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014191 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014192#endif
14193
Pablo Galindo4defba32018-01-27 16:16:37 +000014194#ifdef RWF_DSYNC
14195 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14196#endif
14197#ifdef RWF_HIPRI
14198 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14199#endif
14200#ifdef RWF_SYNC
14201 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14202#endif
14203#ifdef RWF_NOWAIT
14204 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14205#endif
14206
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014207/* constants for posix_spawn */
14208#ifdef HAVE_POSIX_SPAWN
14209 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14210 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14211 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14212#endif
14213
pxinwrf2d7ac72019-05-21 18:46:37 +080014214#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014215 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14216 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014217 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014218#endif
14219#ifdef HAVE_SPAWNV
14220 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014221 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014222#endif
14223
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014224#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014225#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014226 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014227#endif
14228#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014229 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014230#endif
14231#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014232 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014233#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014234#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014235 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014236#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014237#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014238 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014239#endif
14240#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014241 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014242#endif
14243#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014244 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014245#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014246#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014247 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014248#endif
14249#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014250 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014251#endif
14252#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014253 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014254#endif
14255#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014256 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014257#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014258#endif
14259
Benjamin Peterson9428d532011-09-14 11:45:52 -040014260#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014261 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14262 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14263 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014264#endif
14265
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014266#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014267 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014268#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014269#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014270 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014271#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014272#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014273 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014274#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014275#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014276 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014277#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014278#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014279 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014280#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014281#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014282 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014283#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014284#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014285 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014286#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014287#if HAVE_DECL_RTLD_MEMBER
14288 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14289#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014290
Victor Stinner9b1f4742016-09-06 16:18:52 -070014291#ifdef HAVE_GETRANDOM_SYSCALL
14292 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14293 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14294#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014295#ifdef HAVE_MEMFD_CREATE
14296 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14297 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14298#ifdef MFD_HUGETLB
14299 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014300#endif
14301#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014302 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014303#endif
14304#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014305 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014306#endif
14307#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014308 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014309#endif
14310#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014311 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014312#endif
14313#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014314 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014315#endif
14316#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014317 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014318#endif
14319#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014320 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014321#endif
14322#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014323 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014324#endif
14325#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014326 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014327#endif
14328#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014329 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014330#endif
14331#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014332 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014333#endif
14334#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014335 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014336#endif
14337#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014338 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014339#endif
14340#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014341 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14342#endif
14343#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014344
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014345#if defined(__APPLE__)
14346 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14347#endif
14348
Steve Dower2438cdf2019-03-29 16:37:16 -070014349#ifdef MS_WINDOWS
14350 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14351 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14352 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14353 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14354 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14355#endif
14356
Victor Stinner8c62be82010-05-06 00:08:46 +000014357 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014358}
14359
14360
Martin v. Löwis1a214512008-06-11 05:26:20 +000014361static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014362 PyModuleDef_HEAD_INIT,
14363 MODNAME,
14364 posix__doc__,
Eddie Elizondob3966632019-11-05 07:16:14 -080014365 sizeof(_posixstate),
Victor Stinner8c62be82010-05-06 00:08:46 +000014366 posix_methods,
14367 NULL,
Eddie Elizondob3966632019-11-05 07:16:14 -080014368 _posix_traverse,
14369 _posix_clear,
14370 _posix_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +000014371};
14372
14373
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014374static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014375
14376#ifdef HAVE_FACCESSAT
14377 "HAVE_FACCESSAT",
14378#endif
14379
14380#ifdef HAVE_FCHDIR
14381 "HAVE_FCHDIR",
14382#endif
14383
14384#ifdef HAVE_FCHMOD
14385 "HAVE_FCHMOD",
14386#endif
14387
14388#ifdef HAVE_FCHMODAT
14389 "HAVE_FCHMODAT",
14390#endif
14391
14392#ifdef HAVE_FCHOWN
14393 "HAVE_FCHOWN",
14394#endif
14395
Larry Hastings00964ed2013-08-12 13:49:30 -040014396#ifdef HAVE_FCHOWNAT
14397 "HAVE_FCHOWNAT",
14398#endif
14399
Larry Hastings9cf065c2012-06-22 16:30:09 -070014400#ifdef HAVE_FEXECVE
14401 "HAVE_FEXECVE",
14402#endif
14403
14404#ifdef HAVE_FDOPENDIR
14405 "HAVE_FDOPENDIR",
14406#endif
14407
Georg Brandl306336b2012-06-24 12:55:33 +020014408#ifdef HAVE_FPATHCONF
14409 "HAVE_FPATHCONF",
14410#endif
14411
Larry Hastings9cf065c2012-06-22 16:30:09 -070014412#ifdef HAVE_FSTATAT
14413 "HAVE_FSTATAT",
14414#endif
14415
14416#ifdef HAVE_FSTATVFS
14417 "HAVE_FSTATVFS",
14418#endif
14419
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014420#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014421 "HAVE_FTRUNCATE",
14422#endif
14423
Larry Hastings9cf065c2012-06-22 16:30:09 -070014424#ifdef HAVE_FUTIMENS
14425 "HAVE_FUTIMENS",
14426#endif
14427
14428#ifdef HAVE_FUTIMES
14429 "HAVE_FUTIMES",
14430#endif
14431
14432#ifdef HAVE_FUTIMESAT
14433 "HAVE_FUTIMESAT",
14434#endif
14435
14436#ifdef HAVE_LINKAT
14437 "HAVE_LINKAT",
14438#endif
14439
14440#ifdef HAVE_LCHFLAGS
14441 "HAVE_LCHFLAGS",
14442#endif
14443
14444#ifdef HAVE_LCHMOD
14445 "HAVE_LCHMOD",
14446#endif
14447
14448#ifdef HAVE_LCHOWN
14449 "HAVE_LCHOWN",
14450#endif
14451
14452#ifdef HAVE_LSTAT
14453 "HAVE_LSTAT",
14454#endif
14455
14456#ifdef HAVE_LUTIMES
14457 "HAVE_LUTIMES",
14458#endif
14459
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014460#ifdef HAVE_MEMFD_CREATE
14461 "HAVE_MEMFD_CREATE",
14462#endif
14463
Larry Hastings9cf065c2012-06-22 16:30:09 -070014464#ifdef HAVE_MKDIRAT
14465 "HAVE_MKDIRAT",
14466#endif
14467
14468#ifdef HAVE_MKFIFOAT
14469 "HAVE_MKFIFOAT",
14470#endif
14471
14472#ifdef HAVE_MKNODAT
14473 "HAVE_MKNODAT",
14474#endif
14475
14476#ifdef HAVE_OPENAT
14477 "HAVE_OPENAT",
14478#endif
14479
14480#ifdef HAVE_READLINKAT
14481 "HAVE_READLINKAT",
14482#endif
14483
14484#ifdef HAVE_RENAMEAT
14485 "HAVE_RENAMEAT",
14486#endif
14487
14488#ifdef HAVE_SYMLINKAT
14489 "HAVE_SYMLINKAT",
14490#endif
14491
14492#ifdef HAVE_UNLINKAT
14493 "HAVE_UNLINKAT",
14494#endif
14495
14496#ifdef HAVE_UTIMENSAT
14497 "HAVE_UTIMENSAT",
14498#endif
14499
14500#ifdef MS_WINDOWS
14501 "MS_WINDOWS",
14502#endif
14503
14504 NULL
14505};
14506
14507
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014508PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014509INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014510{
Victor Stinner8c62be82010-05-06 00:08:46 +000014511 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014512 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014513 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014514
Eddie Elizondob3966632019-11-05 07:16:14 -080014515 m = PyState_FindModule(&posixmodule);
14516 if (m != NULL) {
14517 Py_INCREF(m);
14518 return m;
14519 }
14520
Victor Stinner8c62be82010-05-06 00:08:46 +000014521 m = PyModule_Create(&posixmodule);
14522 if (m == NULL)
14523 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014524
Victor Stinner8c62be82010-05-06 00:08:46 +000014525 /* Initialize environ dictionary */
14526 v = convertenviron();
14527 Py_XINCREF(v);
14528 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14529 return NULL;
14530 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014531
Victor Stinner8c62be82010-05-06 00:08:46 +000014532 if (all_ins(m))
14533 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014534
Victor Stinner8c62be82010-05-06 00:08:46 +000014535 if (setup_confname_tables(m))
14536 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014537
Victor Stinner8c62be82010-05-06 00:08:46 +000014538 Py_INCREF(PyExc_OSError);
14539 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014540
Guido van Rossumb3d39562000-01-31 18:41:26 +000014541#ifdef HAVE_PUTENV
Eddie Elizondob3966632019-11-05 07:16:14 -080014542 /* Save putenv() parameters as values here, so we can collect them when they
14543 * get re-set with another call for the same key. */
14544 _posixstate(m)->posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000014545#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000014546
Ross Lagerwall7807c352011-03-17 20:20:30 +020014547#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080014548 waitid_result_desc.name = MODNAME ".waitid_result";
14549 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
14550 if (WaitidResultType == NULL) {
14551 return NULL;
14552 }
14553 Py_INCREF(WaitidResultType);
14554 PyModule_AddObject(m, "waitid_result", WaitidResultType);
14555 _posixstate(m)->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014556#endif
14557
Eddie Elizondob3966632019-11-05 07:16:14 -080014558 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
14559 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14560 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14561 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
14562 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
14563 if (StatResultType == NULL) {
14564 return NULL;
14565 }
14566 Py_INCREF(StatResultType);
14567 PyModule_AddObject(m, "stat_result", StatResultType);
14568 _posixstate(m)->StatResultType = StatResultType;
14569 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14570 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014571
Eddie Elizondob3966632019-11-05 07:16:14 -080014572 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
14573 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
14574 if (StatVFSResultType == NULL) {
14575 return NULL;
14576 }
14577 Py_INCREF(StatVFSResultType);
14578 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
14579 _posixstate(m)->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014580#ifdef NEED_TICKS_PER_SECOND
14581# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080014582 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014583# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080014584 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014585# else
Eddie Elizondob3966632019-11-05 07:16:14 -080014586 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014587# endif
14588#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014589
William Orr81574b82018-10-01 22:19:56 -070014590#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080014591 sched_param_desc.name = MODNAME ".sched_param";
14592 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
14593 if (SchedParamType == NULL) {
14594 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014595 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014596 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014597 PyModule_AddObject(m, "sched_param", SchedParamType);
14598 _posixstate(m)->SchedParamType = SchedParamType;
14599 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014600#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014601
Eddie Elizondob3966632019-11-05 07:16:14 -080014602 /* initialize TerminalSize_info */
14603 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
14604 if (TerminalSizeType == NULL) {
14605 return NULL;
14606 }
14607 Py_INCREF(TerminalSizeType);
14608 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
14609 _posixstate(m)->TerminalSizeType = TerminalSizeType;
14610
14611 /* initialize scandir types */
14612 PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
14613 if (ScandirIteratorType == NULL) {
14614 return NULL;
14615 }
14616 _posixstate(m)->ScandirIteratorType = ScandirIteratorType;
14617
14618 PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
14619 if (DirEntryType == NULL) {
14620 return NULL;
14621 }
14622 Py_INCREF(DirEntryType);
14623 PyModule_AddObject(m, "DirEntry", DirEntryType);
14624 _posixstate(m)->DirEntryType = DirEntryType;
14625
Larry Hastings605a62d2012-06-24 04:33:36 -070014626 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080014627 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014628 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014629 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014630 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014631 Py_INCREF(TimesResultType);
14632 PyModule_AddObject(m, "times_result", TimesResultType);
14633 _posixstate(m)->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014634
Eddie Elizondob3966632019-11-05 07:16:14 -080014635 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014636 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014637 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014638 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014639 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014640 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014641 _posixstate(m)->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014642
Thomas Wouters477c8d52006-05-27 19:21:47 +000014643#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014644 /*
14645 * Step 2 of weak-linking support on Mac OS X.
14646 *
14647 * The code below removes functions that are not available on the
14648 * currently active platform.
14649 *
14650 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014651 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014652 * OSX 10.4.
14653 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014654#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014655 if (fstatvfs == NULL) {
14656 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14657 return NULL;
14658 }
14659 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014660#endif /* HAVE_FSTATVFS */
14661
14662#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014663 if (statvfs == NULL) {
14664 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14665 return NULL;
14666 }
14667 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014668#endif /* HAVE_STATVFS */
14669
14670# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014671 if (lchown == NULL) {
14672 if (PyObject_DelAttrString(m, "lchown") == -1) {
14673 return NULL;
14674 }
14675 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014676#endif /* HAVE_LCHOWN */
14677
14678
14679#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014680
Eddie Elizondob3966632019-11-05 07:16:14 -080014681 if ((_posixstate(m)->billion = PyLong_FromLong(1000000000)) == NULL)
14682 return NULL;
14683#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
14684 _posixstate(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
14685 if (_posixstate(m)->struct_rusage == NULL)
14686 return NULL;
14687#endif
14688 _posixstate(m)->st_mode = PyUnicode_InternFromString("st_mode");
14689 if (_posixstate(m)->st_mode == NULL)
Larry Hastings6fe20b32012-04-19 15:07:49 -070014690 return NULL;
14691
Larry Hastings9cf065c2012-06-22 16:30:09 -070014692 /* suppress "function not used" warnings */
14693 {
14694 int ignored;
14695 fd_specified("", -1);
14696 follow_symlinks_specified("", 1);
14697 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14698 dir_fd_converter(Py_None, &ignored);
14699 dir_fd_unavailable(Py_None, &ignored);
14700 }
14701
14702 /*
14703 * provide list of locally available functions
14704 * so os.py can populate support_* lists
14705 */
14706 list = PyList_New(0);
14707 if (!list)
14708 return NULL;
14709 for (trace = have_functions; *trace; trace++) {
14710 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14711 if (!unicode)
14712 return NULL;
14713 if (PyList_Append(list, unicode))
14714 return NULL;
14715 Py_DECREF(unicode);
14716 }
14717 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014718
Victor Stinner8c62be82010-05-06 00:08:46 +000014719 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014720}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014721
14722#ifdef __cplusplus
14723}
14724#endif