blob: df295c25b66c7b1c56dbc53f9ea8a7a1250bf796 [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 Stinner6036e442015-03-08 01:58:04 +010028#include "structmember.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020029#ifndef MS_WINDOWS
30#include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010031#else
32#include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020033#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000034
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020035/* On android API level 21, 'AT_EACCESS' is not declared although
36 * HAVE_FACCESSAT is defined. */
37#ifdef __ANDROID__
38#undef HAVE_FACCESSAT
39#endif
40
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000041#include <stdio.h> /* needed for ctermid() */
42
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000043#ifdef __cplusplus
44extern "C" {
45#endif
46
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000047PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000048"This module provides access to operating system functionality that is\n\
49standardized by the C Standard and the POSIX standard (a thinly\n\
50disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000051corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000053
Ross Lagerwall4d076da2011-03-18 06:56:53 +020054#ifdef HAVE_SYS_UIO_H
55#include <sys/uio.h>
56#endif
57
Thomas Wouters0e3f5912006-08-11 14:57:12 +000058#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000059#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000060#endif /* HAVE_SYS_TYPES_H */
61
62#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000063#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000065
Guido van Rossum36bc6801995-06-14 22:54:23 +000066#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000067#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000068#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000069
Thomas Wouters0e3f5912006-08-11 14:57:12 +000070#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000071#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000072#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000073
Guido van Rossumb6775db1994-08-01 11:34:53 +000074#ifdef HAVE_FCNTL_H
75#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000076#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000077
Guido van Rossuma6535fd2001-10-18 19:44:10 +000078#ifdef HAVE_GRP_H
79#include <grp.h>
80#endif
81
Barry Warsaw5676bd12003-01-07 20:57:09 +000082#ifdef HAVE_SYSEXITS_H
83#include <sysexits.h>
84#endif /* HAVE_SYSEXITS_H */
85
Anthony Baxter8a560de2004-10-13 15:30:56 +000086#ifdef HAVE_SYS_LOADAVG_H
87#include <sys/loadavg.h>
88#endif
89
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000090#ifdef HAVE_LANGINFO_H
91#include <langinfo.h>
92#endif
93
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000094#ifdef HAVE_SYS_SENDFILE_H
95#include <sys/sendfile.h>
96#endif
97
Benjamin Peterson94b580d2011-08-02 17:30:04 -050098#ifdef HAVE_SCHED_H
99#include <sched.h>
100#endif
101
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500102#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500103#undef HAVE_SCHED_SETAFFINITY
104#endif
105
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200106#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Benjamin Peterson9428d532011-09-14 11:45:52 -0400107#define USE_XATTRS
108#endif
109
110#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400111#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400112#endif
113
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000114#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
115#ifdef HAVE_SYS_SOCKET_H
116#include <sys/socket.h>
117#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000118#endif
119
Victor Stinner8b905bd2011-10-25 13:34:04 +0200120#ifdef HAVE_DLFCN_H
121#include <dlfcn.h>
122#endif
123
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200124#ifdef __hpux
125#include <sys/mpctl.h>
126#endif
127
128#if defined(__DragonFly__) || \
129 defined(__OpenBSD__) || \
130 defined(__FreeBSD__) || \
131 defined(__NetBSD__) || \
132 defined(__APPLE__)
133#include <sys/sysctl.h>
134#endif
135
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100136#if defined(MS_WINDOWS)
137# define TERMSIZE_USE_CONIO
138#elif defined(HAVE_SYS_IOCTL_H)
139# include <sys/ioctl.h>
140# if defined(HAVE_TERMIOS_H)
141# include <termios.h>
142# endif
143# if defined(TIOCGWINSZ)
144# define TERMSIZE_USE_IOCTL
145# endif
146#endif /* MS_WINDOWS */
147
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000148/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000149/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000150#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000152#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000153#include <process.h>
154#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000155#ifdef _MSC_VER /* Microsoft compiler */
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000156#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000157#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000158#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000159#define HAVE_EXECV 1
160#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000161#define HAVE_SYSTEM 1
162#define HAVE_CWAIT 1
163#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000164#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000165#else
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000166/* Unix functions that the configure script doesn't check for */
167#define HAVE_EXECV 1
168#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000169#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000170#define HAVE_FORK1 1
171#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000172#define HAVE_GETEGID 1
173#define HAVE_GETEUID 1
174#define HAVE_GETGID 1
175#define HAVE_GETPPID 1
176#define HAVE_GETUID 1
177#define HAVE_KILL 1
178#define HAVE_OPENDIR 1
179#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000180#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000181#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000182#define HAVE_TTYNAME 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000183#endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000184#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000185
Victor Stinnera2f7c002012-02-08 03:36:25 +0100186
Larry Hastings61272b72014-01-07 12:41:53 -0800187/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000188# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800189module os
Larry Hastings61272b72014-01-07 12:41:53 -0800190[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000191/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100192
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000193#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000194
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000195#if defined(__sgi)&&_COMPILER_VERSION>=700
196/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
197 (default) */
198extern char *ctermid_r(char *);
199#endif
200
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000201#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000202#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000203extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000204#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000205#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000206extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000207#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000208extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000209#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000210#endif
211#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int chdir(char *);
213extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000215extern int chdir(const char *);
216extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000217#endif
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000218extern int chmod(const char *, mode_t);
Christian Heimes4e30a842007-11-30 22:12:06 +0000219/*#ifdef HAVE_FCHMOD
220extern int fchmod(int, mode_t);
221#endif*/
222/*#ifdef HAVE_LCHMOD
223extern int lchmod(const char *, mode_t);
224#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000225extern int chown(const char *, uid_t, gid_t);
226extern char *getcwd(char *, int);
227extern char *strerror(int);
228extern int link(const char *, const char *);
229extern int rename(const char *, const char *);
230extern int stat(const char *, struct stat *);
231extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000232#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000234#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000235#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000236extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000237#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000238#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000239
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000240#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000241
Guido van Rossumb6775db1994-08-01 11:34:53 +0000242#ifdef HAVE_UTIME_H
243#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000244#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000245
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000246#ifdef HAVE_SYS_UTIME_H
247#include <sys/utime.h>
248#define HAVE_UTIME_H /* pretend we do for the rest of this file */
249#endif /* HAVE_SYS_UTIME_H */
250
Guido van Rossumb6775db1994-08-01 11:34:53 +0000251#ifdef HAVE_SYS_TIMES_H
252#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000253#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254
255#ifdef HAVE_SYS_PARAM_H
256#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000257#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000258
259#ifdef HAVE_SYS_UTSNAME_H
260#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000261#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000263#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000264#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000265#define NAMLEN(dirent) strlen((dirent)->d_name)
266#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000267#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000268#include <direct.h>
269#define NAMLEN(dirent) strlen((dirent)->d_name)
270#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000271#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000272#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000273#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000274#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000275#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000276#endif
277#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000278#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000279#endif
280#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#endif
283#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000284
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000285#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000286#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288#endif
289#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000290#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000291#endif
292#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000295#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000296#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000297#endif
298#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000299#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000300#endif
301#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000302#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000303#endif
Tim Golden0321cf22014-05-05 19:46:17 +0100304#ifndef IO_REPARSE_TAG_MOUNT_POINT
305#define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
306#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000307#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000308#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000309#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000310#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000311#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000312#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
313#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000314static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000315#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000316#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000317
Tim Petersbc2e10e2002-03-03 23:17:02 +0000318#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000319#if defined(PATH_MAX) && PATH_MAX > 1024
320#define MAXPATHLEN PATH_MAX
321#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000322#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000323#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000324#endif /* MAXPATHLEN */
325
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000326#ifdef UNION_WAIT
327/* Emulate some macros on systems that have a union instead of macros */
328
329#ifndef WIFEXITED
330#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
331#endif
332
333#ifndef WEXITSTATUS
334#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
335#endif
336
337#ifndef WTERMSIG
338#define WTERMSIG(u_wait) ((u_wait).w_termsig)
339#endif
340
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341#define WAIT_TYPE union wait
342#define WAIT_STATUS_INT(s) (s.w_status)
343
344#else /* !UNION_WAIT */
345#define WAIT_TYPE int
346#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000347#endif /* UNION_WAIT */
348
Greg Wardb48bc172000-03-01 21:51:56 +0000349/* Don't use the "_r" form if we don't need it (also, won't have a
350 prototype for it, at least on Solaris -- maybe others as well?). */
351#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
352#define USE_CTERMID_R
353#endif
354
Fred Drake699f3522000-06-29 21:12:41 +0000355/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000356#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000357#undef FSTAT
358#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200359#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +0000360# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700361# define LSTAT win32_lstat
Victor Stinnere134a7f2015-03-30 10:09:31 +0200362# define FSTAT _Py_fstat_noraise
Steve Dowerf2f373f2015-02-21 08:44:05 -0800363# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000364#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000365# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700366# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000367# define FSTAT fstat
368# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000369#endif
370
Tim Peters11b23062003-04-23 02:39:17 +0000371#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000372#include <sys/mkdev.h>
373#else
374#if defined(MAJOR_IN_SYSMACROS)
375#include <sys/sysmacros.h>
376#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000377#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
378#include <sys/mkdev.h>
379#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000380#endif
Fred Drake699f3522000-06-29 21:12:41 +0000381
Victor Stinner6edddfa2013-11-24 19:22:57 +0100382#define DWORD_MAX 4294967295U
383
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200384#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +0100385#define INITFUNC PyInit_nt
386#define MODNAME "nt"
387#else
388#define INITFUNC PyInit_posix
389#define MODNAME "posix"
390#endif
391
392#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200393/* defined in fileutils.c */
394PyAPI_FUNC(void) _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
395PyAPI_FUNC(void) _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
396 ULONG, struct _Py_stat_struct *);
397#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700398
399#ifdef MS_WINDOWS
400static int
401win32_warn_bytes_api()
402{
403 return PyErr_WarnEx(PyExc_DeprecationWarning,
404 "The Windows bytes API has been deprecated, "
405 "use Unicode filenames instead",
406 1);
407}
408#endif
409
410
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200411#ifndef MS_WINDOWS
412PyObject *
413_PyLong_FromUid(uid_t uid)
414{
415 if (uid == (uid_t)-1)
416 return PyLong_FromLong(-1);
417 return PyLong_FromUnsignedLong(uid);
418}
419
420PyObject *
421_PyLong_FromGid(gid_t gid)
422{
423 if (gid == (gid_t)-1)
424 return PyLong_FromLong(-1);
425 return PyLong_FromUnsignedLong(gid);
426}
427
428int
429_Py_Uid_Converter(PyObject *obj, void *p)
430{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700431 uid_t uid;
432 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200433 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200434 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700435 unsigned long uresult;
436
437 index = PyNumber_Index(obj);
438 if (index == NULL) {
439 PyErr_Format(PyExc_TypeError,
440 "uid should be integer, not %.200s",
441 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200442 return 0;
443 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700444
445 /*
446 * Handling uid_t is complicated for two reasons:
447 * * Although uid_t is (always?) unsigned, it still
448 * accepts -1.
449 * * We don't know its size in advance--it may be
450 * bigger than an int, or it may be smaller than
451 * a long.
452 *
453 * So a bit of defensive programming is in order.
454 * Start with interpreting the value passed
455 * in as a signed long and see if it works.
456 */
457
458 result = PyLong_AsLongAndOverflow(index, &overflow);
459
460 if (!overflow) {
461 uid = (uid_t)result;
462
463 if (result == -1) {
464 if (PyErr_Occurred())
465 goto fail;
466 /* It's a legitimate -1, we're done. */
467 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200468 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700469
470 /* Any other negative number is disallowed. */
471 if (result < 0)
472 goto underflow;
473
474 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200475 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700476 (long)uid != result)
477 goto underflow;
478 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200479 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700480
481 if (overflow < 0)
482 goto underflow;
483
484 /*
485 * Okay, the value overflowed a signed long. If it
486 * fits in an *unsigned* long, it may still be okay,
487 * as uid_t may be unsigned long on this platform.
488 */
489 uresult = PyLong_AsUnsignedLong(index);
490 if (PyErr_Occurred()) {
491 if (PyErr_ExceptionMatches(PyExc_OverflowError))
492 goto overflow;
493 goto fail;
494 }
495
496 uid = (uid_t)uresult;
497
498 /*
499 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
500 * but this value would get interpreted as (uid_t)-1 by chown
501 * and its siblings. That's not what the user meant! So we
502 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100503 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700504 */
505 if (uid == (uid_t)-1)
506 goto overflow;
507
508 /* Ensure the value wasn't truncated. */
509 if (sizeof(uid_t) < sizeof(long) &&
510 (unsigned long)uid != uresult)
511 goto overflow;
512 /* fallthrough */
513
514success:
515 Py_DECREF(index);
516 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200517 return 1;
518
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700519underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200520 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700521 "uid is less than minimum");
522 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200523
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700524overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200525 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700526 "uid is greater than maximum");
527 /* fallthrough */
528
529fail:
530 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200531 return 0;
532}
533
534int
535_Py_Gid_Converter(PyObject *obj, void *p)
536{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700537 gid_t gid;
538 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200539 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200540 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700541 unsigned long uresult;
542
543 index = PyNumber_Index(obj);
544 if (index == NULL) {
545 PyErr_Format(PyExc_TypeError,
546 "gid should be integer, not %.200s",
547 Py_TYPE(obj)->tp_name);
Serhiy Storchakab4621892013-02-10 23:28:02 +0200548 return 0;
549 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700550
551 /*
552 * Handling gid_t is complicated for two reasons:
553 * * Although gid_t is (always?) unsigned, it still
554 * accepts -1.
555 * * We don't know its size in advance--it may be
556 * bigger than an int, or it may be smaller than
557 * a long.
558 *
559 * So a bit of defensive programming is in order.
560 * Start with interpreting the value passed
561 * in as a signed long and see if it works.
562 */
563
564 result = PyLong_AsLongAndOverflow(index, &overflow);
565
566 if (!overflow) {
567 gid = (gid_t)result;
568
569 if (result == -1) {
570 if (PyErr_Occurred())
571 goto fail;
572 /* It's a legitimate -1, we're done. */
573 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200574 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700575
576 /* Any other negative number is disallowed. */
577 if (result < 0) {
578 goto underflow;
579 }
580
581 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200582 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700583 (long)gid != result)
584 goto underflow;
585 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200586 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700587
588 if (overflow < 0)
589 goto underflow;
590
591 /*
592 * Okay, the value overflowed a signed long. If it
593 * fits in an *unsigned* long, it may still be okay,
594 * as gid_t may be unsigned long on this platform.
595 */
596 uresult = PyLong_AsUnsignedLong(index);
597 if (PyErr_Occurred()) {
598 if (PyErr_ExceptionMatches(PyExc_OverflowError))
599 goto overflow;
600 goto fail;
601 }
602
603 gid = (gid_t)uresult;
604
605 /*
606 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
607 * but this value would get interpreted as (gid_t)-1 by chown
608 * and its siblings. That's not what the user meant! So we
609 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100610 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700611 */
612 if (gid == (gid_t)-1)
613 goto overflow;
614
615 /* Ensure the value wasn't truncated. */
616 if (sizeof(gid_t) < sizeof(long) &&
617 (unsigned long)gid != uresult)
618 goto overflow;
619 /* fallthrough */
620
621success:
622 Py_DECREF(index);
623 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200624 return 1;
625
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700626underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200627 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700628 "gid is less than minimum");
629 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200630
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700631overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200632 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700633 "gid is greater than maximum");
634 /* fallthrough */
635
636fail:
637 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200638 return 0;
639}
640#endif /* MS_WINDOWS */
641
642
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700643#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800644
645
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200646#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
647static int
648_Py_Dev_Converter(PyObject *obj, void *p)
649{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200650 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200651 if (PyErr_Occurred())
652 return 0;
653 return 1;
654}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800655#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200656
657
Larry Hastings9cf065c2012-06-22 16:30:09 -0700658#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400659/*
660 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
661 * without the int cast, the value gets interpreted as uint (4291925331),
662 * which doesn't play nicely with all the initializer lines in this file that
663 * look like this:
664 * int dir_fd = DEFAULT_DIR_FD;
665 */
666#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700667#else
668#define DEFAULT_DIR_FD (-100)
669#endif
670
671static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300672_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200673{
674 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700675 long long_value;
676
677 PyObject *index = PyNumber_Index(o);
678 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700679 return 0;
680 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700681
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300682 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700683 long_value = PyLong_AsLongAndOverflow(index, &overflow);
684 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300685 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200686 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700687 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700688 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700689 return 0;
690 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200691 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700692 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700693 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700694 return 0;
695 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700696
Larry Hastings9cf065c2012-06-22 16:30:09 -0700697 *p = (int)long_value;
698 return 1;
699}
700
701static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200702dir_fd_converter(PyObject *o, void *p)
703{
704 if (o == Py_None) {
705 *(int *)p = DEFAULT_DIR_FD;
706 return 1;
707 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300708 else if (PyIndex_Check(o)) {
709 return _fd_converter(o, (int *)p);
710 }
711 else {
712 PyErr_Format(PyExc_TypeError,
713 "argument should be integer or None, not %.200s",
714 Py_TYPE(o)->tp_name);
715 return 0;
716 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700717}
718
719
Larry Hastings9cf065c2012-06-22 16:30:09 -0700720/*
721 * A PyArg_ParseTuple "converter" function
722 * that handles filesystem paths in the manner
723 * preferred by the os module.
724 *
725 * path_converter accepts (Unicode) strings and their
726 * subclasses, and bytes and their subclasses. What
727 * it does with the argument depends on the platform:
728 *
729 * * On Windows, if we get a (Unicode) string we
730 * extract the wchar_t * and return it; if we get
731 * bytes we extract the char * and return that.
732 *
733 * * On all other platforms, strings are encoded
734 * to bytes using PyUnicode_FSConverter, then we
735 * extract the char * from the bytes object and
736 * return that.
737 *
738 * path_converter also optionally accepts signed
739 * integers (representing open file descriptors) instead
740 * of path strings.
741 *
742 * Input fields:
743 * path.nullable
744 * If nonzero, the path is permitted to be None.
745 * path.allow_fd
746 * If nonzero, the path is permitted to be a file handle
747 * (a signed int) instead of a string.
748 * path.function_name
749 * If non-NULL, path_converter will use that as the name
750 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700751 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700752 * path.argument_name
753 * If non-NULL, path_converter will use that as the name
754 * of the parameter in error messages.
755 * (If path.argument_name is NULL it uses "path".)
756 *
757 * Output fields:
758 * path.wide
759 * Points to the path if it was expressed as Unicode
760 * and was not encoded. (Only used on Windows.)
761 * path.narrow
762 * Points to the path if it was expressed as bytes,
763 * or it was Unicode and was encoded to bytes.
764 * path.fd
765 * Contains a file descriptor if path.accept_fd was true
766 * and the caller provided a signed integer instead of any
767 * sort of string.
768 *
769 * WARNING: if your "path" parameter is optional, and is
770 * unspecified, path_converter will never get called.
771 * So if you set allow_fd, you *MUST* initialize path.fd = -1
772 * yourself!
773 * path.length
774 * The length of the path in characters, if specified as
775 * a string.
776 * path.object
777 * The original object passed in.
778 * path.cleanup
779 * For internal use only. May point to a temporary object.
780 * (Pay no attention to the man behind the curtain.)
781 *
782 * At most one of path.wide or path.narrow will be non-NULL.
783 * If path was None and path.nullable was set,
784 * or if path was an integer and path.allow_fd was set,
785 * both path.wide and path.narrow will be NULL
786 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200787 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700788 * path_converter takes care to not write to the path_t
789 * unless it's successful. However it must reset the
790 * "cleanup" field each time it's called.
791 *
792 * Use as follows:
793 * path_t path;
794 * memset(&path, 0, sizeof(path));
795 * PyArg_ParseTuple(args, "O&", path_converter, &path);
796 * // ... use values from path ...
797 * path_cleanup(&path);
798 *
799 * (Note that if PyArg_Parse fails you don't need to call
800 * path_cleanup(). However it is safe to do so.)
801 */
802typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100803 const char *function_name;
804 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700805 int nullable;
806 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300807 const wchar_t *wide;
808 const char *narrow;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700809 int fd;
810 Py_ssize_t length;
811 PyObject *object;
812 PyObject *cleanup;
813} path_t;
814
Larry Hastings2f936352014-08-05 14:04:04 +1000815#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
816 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Larry Hastings31826802013-10-19 00:09:25 -0700817
Larry Hastings9cf065c2012-06-22 16:30:09 -0700818static void
819path_cleanup(path_t *path) {
820 if (path->cleanup) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +0200821 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700822 }
823}
824
825static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300826path_converter(PyObject *o, void *p)
827{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700828 path_t *path = (path_t *)p;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700829 PyObject *bytes, *to_cleanup = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700830 Py_ssize_t length;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700831 int is_index, is_buffer, is_bytes, is_unicode;
832 /* Default to failure, forcing explicit signaling of succcess. */
833 int ret = 0;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300834 const char *narrow;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700835
836#define FORMAT_EXCEPTION(exc, fmt) \
837 PyErr_Format(exc, "%s%s" fmt, \
838 path->function_name ? path->function_name : "", \
839 path->function_name ? ": " : "", \
840 path->argument_name ? path->argument_name : "path")
841
842 /* Py_CLEANUP_SUPPORTED support */
843 if (o == NULL) {
844 path_cleanup(path);
845 return 1;
846 }
847
Brett Cannon3f9183b2016-08-26 14:44:48 -0700848 /* Ensure it's always safe to call path_cleanup(). */
Larry Hastings9cf065c2012-06-22 16:30:09 -0700849 path->cleanup = NULL;
850
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300851 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700852 path->wide = NULL;
853 path->narrow = NULL;
854 path->length = 0;
855 path->object = o;
856 path->fd = -1;
857 return 1;
858 }
859
Brett Cannon3f9183b2016-08-26 14:44:48 -0700860 /* Only call this here so that we don't treat the return value of
861 os.fspath() as an fd or buffer. */
862 is_index = path->allow_fd && PyIndex_Check(o);
863 is_buffer = PyObject_CheckBuffer(o);
864 is_bytes = PyBytes_Check(o);
865 is_unicode = PyUnicode_Check(o);
866
867 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
868 /* Inline PyOS_FSPath() for better error messages. */
869 _Py_IDENTIFIER(__fspath__);
870 PyObject *func = NULL;
871
872 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
873 if (NULL == func) {
874 goto error_exit;
875 }
876
877 o = to_cleanup = PyObject_CallFunctionObjArgs(func, NULL);
878 Py_DECREF(func);
879 if (NULL == o) {
880 goto error_exit;
881 }
882 else if (PyUnicode_Check(o)) {
883 is_unicode = 1;
884 }
885 else if (PyBytes_Check(o)) {
886 is_bytes = 1;
887 }
888 else {
889 goto error_exit;
890 }
891 }
892
893 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700894#ifdef MS_WINDOWS
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300895 const wchar_t *wide;
Victor Stinner59799a82013-11-13 14:17:30 +0100896
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300897 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +0100898 if (!wide) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700899 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700900 }
Victor Stinner59799a82013-11-13 14:17:30 +0100901 if (length > 32767) {
902 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Brett Cannon3f9183b2016-08-26 14:44:48 -0700903 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700904 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300905 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300906 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Brett Cannon3f9183b2016-08-26 14:44:48 -0700907 goto exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +0300908 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700909
910 path->wide = wide;
911 path->narrow = NULL;
912 path->length = length;
913 path->object = o;
914 path->fd = -1;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700915 ret = 1;
916 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700917#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300918 if (!PyUnicode_FSConverter(o, &bytes)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700919 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300920 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700921#endif
922 }
Brett Cannon3f9183b2016-08-26 14:44:48 -0700923 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300924#ifdef MS_WINDOWS
925 if (win32_warn_bytes_api()) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700926 goto exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300927 }
928#endif
929 bytes = o;
930 Py_INCREF(bytes);
931 }
Brett Cannon3f9183b2016-08-26 14:44:48 -0700932 else if (is_buffer) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300933 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
934 "%s%s%s should be %s, not %.200s",
935 path->function_name ? path->function_name : "",
936 path->function_name ? ": " : "",
937 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -0700938 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
939 "integer or None" :
940 path->allow_fd ? "string, bytes, os.PathLike or integer" :
941 path->nullable ? "string, bytes, os.PathLike or None" :
942 "string, bytes or os.PathLike",
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300943 Py_TYPE(o)->tp_name)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700944 goto exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +0300945 }
Serhiy Storchaka3291d852016-04-06 23:02:46 +0300946#ifdef MS_WINDOWS
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300947 if (win32_warn_bytes_api()) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700948 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300949 }
Serhiy Storchaka3291d852016-04-06 23:02:46 +0300950#endif
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300951 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700952 if (!bytes) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700953 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700954 }
955 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300956 else if (path->allow_fd && PyIndex_Check(o)) {
957 if (!_fd_converter(o, &path->fd)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700958 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300959 }
960 path->wide = NULL;
961 path->narrow = NULL;
962 path->length = 0;
963 path->object = o;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700964 ret = 1;
965 goto exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300966 }
967 else {
Brett Cannon3f9183b2016-08-26 14:44:48 -0700968 error_exit:
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300969 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
970 path->function_name ? path->function_name : "",
971 path->function_name ? ": " : "",
972 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -0700973 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
974 "integer or None" :
975 path->allow_fd ? "string, bytes, os.PathLike or integer" :
976 path->nullable ? "string, bytes, os.PathLike or None" :
977 "string, bytes or os.PathLike",
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300978 Py_TYPE(o)->tp_name);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700979 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700980 }
981
Larry Hastings9cf065c2012-06-22 16:30:09 -0700982 length = PyBytes_GET_SIZE(bytes);
983#ifdef MS_WINDOWS
Victor Stinner75875072013-11-24 19:23:25 +0100984 if (length > MAX_PATH-1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700985 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
986 Py_DECREF(bytes);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700987 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700988 }
989#endif
990
991 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +0200992 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +0300993 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700994 Py_DECREF(bytes);
Brett Cannon3f9183b2016-08-26 14:44:48 -0700995 goto exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700996 }
997
998 path->wide = NULL;
999 path->narrow = narrow;
1000 path->length = length;
1001 path->object = o;
1002 path->fd = -1;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001003 if (bytes == o) {
1004 Py_DECREF(bytes);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001005 ret = 1;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001006 }
1007 else {
1008 path->cleanup = bytes;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001009 ret = Py_CLEANUP_SUPPORTED;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001010 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001011 exit:
1012 Py_XDECREF(to_cleanup);
1013 return ret;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001014}
1015
1016static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001017argument_unavailable_error(const char *function_name, const char *argument_name)
1018{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001019 PyErr_Format(PyExc_NotImplementedError,
1020 "%s%s%s unavailable on this platform",
1021 (function_name != NULL) ? function_name : "",
1022 (function_name != NULL) ? ": ": "",
1023 argument_name);
1024}
1025
1026static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001027dir_fd_unavailable(PyObject *o, void *p)
1028{
1029 int dir_fd;
1030 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001031 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001032 if (dir_fd != DEFAULT_DIR_FD) {
1033 argument_unavailable_error(NULL, "dir_fd");
1034 return 0;
1035 }
1036 *(int *)p = dir_fd;
1037 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001038}
1039
1040static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001041fd_specified(const char *function_name, int fd)
1042{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001043 if (fd == -1)
1044 return 0;
1045
1046 argument_unavailable_error(function_name, "fd");
1047 return 1;
1048}
1049
1050static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001051follow_symlinks_specified(const char *function_name, int follow_symlinks)
1052{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001053 if (follow_symlinks)
1054 return 0;
1055
1056 argument_unavailable_error(function_name, "follow_symlinks");
1057 return 1;
1058}
1059
1060static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001061path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1062{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001063 if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) {
1064 PyErr_Format(PyExc_ValueError,
1065 "%s: can't specify dir_fd without matching path",
1066 function_name);
1067 return 1;
1068 }
1069 return 0;
1070}
1071
1072static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001073dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1074{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001075 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1076 PyErr_Format(PyExc_ValueError,
1077 "%s: can't specify both dir_fd and fd",
1078 function_name);
1079 return 1;
1080 }
1081 return 0;
1082}
1083
1084static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001085fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1086 int follow_symlinks)
1087{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001088 if ((fd > 0) && (!follow_symlinks)) {
1089 PyErr_Format(PyExc_ValueError,
1090 "%s: cannot use fd and follow_symlinks together",
1091 function_name);
1092 return 1;
1093 }
1094 return 0;
1095}
1096
1097static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001098dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1099 int follow_symlinks)
1100{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001101 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1102 PyErr_Format(PyExc_ValueError,
1103 "%s: cannot use dir_fd and follow_symlinks together",
1104 function_name);
1105 return 1;
1106 }
1107 return 0;
1108}
1109
Larry Hastings2f936352014-08-05 14:04:04 +10001110#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001111 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001112#else
Larry Hastings2f936352014-08-05 14:04:04 +10001113 typedef off_t Py_off_t;
1114#endif
1115
1116static int
1117Py_off_t_converter(PyObject *arg, void *addr)
1118{
1119#ifdef HAVE_LARGEFILE_SUPPORT
1120 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1121#else
1122 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001123#endif
1124 if (PyErr_Occurred())
1125 return 0;
1126 return 1;
1127}
Larry Hastings2f936352014-08-05 14:04:04 +10001128
1129static PyObject *
1130PyLong_FromPy_off_t(Py_off_t offset)
1131{
1132#ifdef HAVE_LARGEFILE_SUPPORT
1133 return PyLong_FromLongLong(offset);
1134#else
1135 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001136#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001137}
1138
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001139
Steve Dowerd81431f2015-03-06 14:47:02 -08001140#if defined _MSC_VER && _MSC_VER >= 1400 && _MSC_VER < 1900
1141/* Legacy implementation of _PyVerify_fd_dup2 while transitioning to
1142 * MSVC 14.0. This should eventually be removed. (issue23524)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001143 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001144#define IOINFO_L2E 5
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001145#define IOINFO_ARRAYS 64
Steve Dower65e4cb12014-11-22 12:54:57 -08001146#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001147#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001148#define _NO_CONSOLE_FILENO (intptr_t)-2
1149
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001150/* the special case of checking dup2. The target fd must be in a sensible range */
1151static int
1152_PyVerify_fd_dup2(int fd1, int fd2)
1153{
Victor Stinner8c62be82010-05-06 00:08:46 +00001154 if (!_PyVerify_fd(fd1))
1155 return 0;
1156 if (fd2 == _NO_CONSOLE_FILENO)
1157 return 0;
1158 if ((unsigned)fd2 < _NHANDLE_)
1159 return 1;
1160 else
1161 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001162}
1163#else
Steve Dowerd81431f2015-03-06 14:47:02 -08001164#define _PyVerify_fd_dup2(fd1, fd2) (_PyVerify_fd(fd1) && (fd2) >= 0)
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001165#endif
1166
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001167#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001168
1169static int
Brian Curtind25aef52011-06-13 15:16:04 -05001170win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001171{
Martin Panter70214ad2016-08-04 02:38:59 +00001172 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1173 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001174 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001175
1176 if (0 == DeviceIoControl(
1177 reparse_point_handle,
1178 FSCTL_GET_REPARSE_POINT,
1179 NULL, 0, /* in buffer */
1180 target_buffer, sizeof(target_buffer),
1181 &n_bytes_returned,
1182 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001183 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001184
1185 if (reparse_tag)
1186 *reparse_tag = rdb->ReparseTag;
1187
Brian Curtind25aef52011-06-13 15:16:04 -05001188 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001189}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001190
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001191#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001192
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001193/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001194#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001195/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001196** environ directly, we must obtain it with _NSGetEnviron(). See also
1197** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001198*/
1199#include <crt_externs.h>
1200static char **environ;
1201#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001202extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001203#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001204
Barry Warsaw53699e91996-12-10 23:23:01 +00001205static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001206convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001207{
Victor Stinner8c62be82010-05-06 00:08:46 +00001208 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001209#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001210 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001211#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001212 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001213#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001214
Victor Stinner8c62be82010-05-06 00:08:46 +00001215 d = PyDict_New();
1216 if (d == NULL)
1217 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001218#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001219 if (environ == NULL)
1220 environ = *_NSGetEnviron();
1221#endif
1222#ifdef MS_WINDOWS
1223 /* _wenviron must be initialized in this way if the program is started
1224 through main() instead of wmain(). */
1225 _wgetenv(L"");
1226 if (_wenviron == NULL)
1227 return d;
1228 /* This part ignores errors */
1229 for (e = _wenviron; *e != NULL; e++) {
1230 PyObject *k;
1231 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001232 const wchar_t *p = wcschr(*e, L'=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001233 if (p == NULL)
1234 continue;
1235 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1236 if (k == NULL) {
1237 PyErr_Clear();
1238 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001239 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001240 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1241 if (v == NULL) {
1242 PyErr_Clear();
1243 Py_DECREF(k);
1244 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001245 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001246 if (PyDict_GetItem(d, k) == NULL) {
1247 if (PyDict_SetItem(d, k, v) != 0)
1248 PyErr_Clear();
1249 }
1250 Py_DECREF(k);
1251 Py_DECREF(v);
1252 }
1253#else
1254 if (environ == NULL)
1255 return d;
1256 /* This part ignores errors */
1257 for (e = environ; *e != NULL; e++) {
1258 PyObject *k;
1259 PyObject *v;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001260 const char *p = strchr(*e, '=');
Victor Stinner8c62be82010-05-06 00:08:46 +00001261 if (p == NULL)
1262 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001263 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001264 if (k == NULL) {
1265 PyErr_Clear();
1266 continue;
1267 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001268 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001269 if (v == NULL) {
1270 PyErr_Clear();
1271 Py_DECREF(k);
1272 continue;
1273 }
1274 if (PyDict_GetItem(d, k) == NULL) {
1275 if (PyDict_SetItem(d, k, v) != 0)
1276 PyErr_Clear();
1277 }
1278 Py_DECREF(k);
1279 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001280 }
1281#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001282 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001283}
1284
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001285/* Set a POSIX-specific error from errno, and return NULL */
1286
Barry Warsawd58d7641998-07-23 16:14:40 +00001287static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001288posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001289{
Victor Stinner8c62be82010-05-06 00:08:46 +00001290 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001291}
Mark Hammondef8b6542001-05-13 08:04:26 +00001292
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001293#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001294static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001295win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001296{
Victor Stinner8c62be82010-05-06 00:08:46 +00001297 /* XXX We should pass the function name along in the future.
1298 (winreg.c also wants to pass the function name.)
1299 This would however require an additional param to the
1300 Windows error object, which is non-trivial.
1301 */
1302 errno = GetLastError();
1303 if (filename)
1304 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1305 else
1306 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001307}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001308
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001309static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001310win32_error_object(const char* function, PyObject* filename)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001311{
1312 /* XXX - see win32_error for comments on 'function' */
1313 errno = GetLastError();
1314 if (filename)
1315 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001316 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001317 errno,
1318 filename);
1319 else
1320 return PyErr_SetFromWindowsErr(errno);
1321}
1322
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001323#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001324
Larry Hastings9cf065c2012-06-22 16:30:09 -07001325static PyObject *
Victor Stinner292c8352012-10-30 02:17:38 +01001326path_error(path_t *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001327{
1328#ifdef MS_WINDOWS
Victor Stinner292c8352012-10-30 02:17:38 +01001329 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
1330 0, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001331#else
Victor Stinner292c8352012-10-30 02:17:38 +01001332 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001333#endif
1334}
1335
Larry Hastings31826802013-10-19 00:09:25 -07001336
Larry Hastingsb0827312014-02-09 22:05:19 -08001337static PyObject *
1338path_error2(path_t *path, path_t *path2)
1339{
1340#ifdef MS_WINDOWS
1341 return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError,
1342 0, path->object, path2->object);
1343#else
1344 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError,
1345 path->object, path2->object);
1346#endif
1347}
1348
1349
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001350/* POSIX generic methods */
1351
Larry Hastings2f936352014-08-05 14:04:04 +10001352static int
1353fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001354{
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001356 int *pointer = (int *)p;
1357 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001358 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001359 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001360 *pointer = fd;
1361 return 1;
1362}
1363
1364static PyObject *
1365posix_fildes_fd(int fd, int (*func)(int))
1366{
1367 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001368 int async_err = 0;
1369
Steve Dower8fc89802015-04-12 00:26:27 -04001370 if (!_PyVerify_fd(fd))
1371 return posix_error();
1372
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001373 do {
1374 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001375 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001376 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001377 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001378 Py_END_ALLOW_THREADS
1379 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1380 if (res != 0)
1381 return (!async_err) ? posix_error() : NULL;
1382 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001383}
Guido van Rossum21142a01999-01-08 21:05:37 +00001384
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001385
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001386#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001387/* This is a reimplementation of the C library's chdir function,
1388 but one that produces Win32 errors instead of DOS error codes.
1389 chdir is essentially a wrapper around SetCurrentDirectory; however,
1390 it also needs to set "magic" environment variables indicating
1391 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001392static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001393win32_chdir(LPCSTR path)
1394{
Victor Stinner75875072013-11-24 19:23:25 +01001395 char new_path[MAX_PATH];
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 int result;
1397 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001398
Victor Stinner8c62be82010-05-06 00:08:46 +00001399 if(!SetCurrentDirectoryA(path))
1400 return FALSE;
Victor Stinner75875072013-11-24 19:23:25 +01001401 result = GetCurrentDirectoryA(Py_ARRAY_LENGTH(new_path), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001402 if (!result)
1403 return FALSE;
1404 /* In the ANSI API, there should not be any paths longer
Victor Stinner75875072013-11-24 19:23:25 +01001405 than MAX_PATH-1 (not including the final null character). */
1406 assert(result < Py_ARRAY_LENGTH(new_path));
Victor Stinner8c62be82010-05-06 00:08:46 +00001407 if (strncmp(new_path, "\\\\", 2) == 0 ||
1408 strncmp(new_path, "//", 2) == 0)
1409 /* UNC path, nothing to do. */
1410 return TRUE;
1411 env[1] = new_path[0];
1412 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001413}
1414
1415/* The Unicode version differs from the ANSI version
1416 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001417static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001418win32_wchdir(LPCWSTR path)
1419{
Victor Stinnered537822015-12-13 21:40:26 +01001420 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001421 int result;
1422 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001423
Victor Stinner8c62be82010-05-06 00:08:46 +00001424 if(!SetCurrentDirectoryW(path))
1425 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001426 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001427 if (!result)
1428 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001429 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001430 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001431 if (!new_path) {
1432 SetLastError(ERROR_OUTOFMEMORY);
1433 return FALSE;
1434 }
1435 result = GetCurrentDirectoryW(result, new_path);
1436 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001437 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001438 return FALSE;
1439 }
1440 }
1441 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1442 wcsncmp(new_path, L"//", 2) == 0)
1443 /* UNC path, nothing to do. */
1444 return TRUE;
1445 env[1] = new_path[0];
1446 result = SetEnvironmentVariableW(env, new_path);
Victor Stinnered537822015-12-13 21:40:26 +01001447 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001448 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001449 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001450}
1451#endif
1452
Martin v. Löwis14694662006-02-03 12:54:16 +00001453#ifdef MS_WINDOWS
1454/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1455 - time stamps are restricted to second resolution
1456 - file modification times suffer from forth-and-back conversions between
1457 UTC and local time
1458 Therefore, we implement our own stat, based on the Win32 API directly.
1459*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001460#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001461#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001462
Guido van Rossumd8faa362007-04-27 19:54:29 +00001463static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001464attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001465{
Victor Stinner8c62be82010-05-06 00:08:46 +00001466 HANDLE hFindFile;
1467 WIN32_FIND_DATAA FileData;
1468 hFindFile = FindFirstFileA(pszFile, &FileData);
1469 if (hFindFile == INVALID_HANDLE_VALUE)
1470 return FALSE;
1471 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001472 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001473 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001474 info->dwFileAttributes = FileData.dwFileAttributes;
1475 info->ftCreationTime = FileData.ftCreationTime;
1476 info->ftLastAccessTime = FileData.ftLastAccessTime;
1477 info->ftLastWriteTime = FileData.ftLastWriteTime;
1478 info->nFileSizeHigh = FileData.nFileSizeHigh;
1479 info->nFileSizeLow = FileData.nFileSizeLow;
1480/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001481 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1482 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001483 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001484}
1485
Victor Stinner6036e442015-03-08 01:58:04 +01001486static void
1487find_data_to_file_info_w(WIN32_FIND_DATAW *pFileData,
1488 BY_HANDLE_FILE_INFORMATION *info,
1489 ULONG *reparse_tag)
1490{
1491 memset(info, 0, sizeof(*info));
1492 info->dwFileAttributes = pFileData->dwFileAttributes;
1493 info->ftCreationTime = pFileData->ftCreationTime;
1494 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1495 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1496 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1497 info->nFileSizeLow = pFileData->nFileSizeLow;
1498/* info->nNumberOfLinks = 1; */
1499 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1500 *reparse_tag = pFileData->dwReserved0;
1501 else
1502 *reparse_tag = 0;
1503}
1504
Guido van Rossumd8faa362007-04-27 19:54:29 +00001505static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001506attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001507{
Victor Stinner8c62be82010-05-06 00:08:46 +00001508 HANDLE hFindFile;
1509 WIN32_FIND_DATAW FileData;
1510 hFindFile = FindFirstFileW(pszFile, &FileData);
1511 if (hFindFile == INVALID_HANDLE_VALUE)
1512 return FALSE;
1513 FindClose(hFindFile);
Victor Stinner6036e442015-03-08 01:58:04 +01001514 find_data_to_file_info_w(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001515 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001516}
1517
Brian Curtind25aef52011-06-13 15:16:04 -05001518static BOOL
1519get_target_path(HANDLE hdl, wchar_t **target_path)
1520{
1521 int buf_size, result_length;
1522 wchar_t *buf;
1523
1524 /* We have a good handle to the target, use it to determine
1525 the target path name (then we'll call lstat on it). */
Steve Dower2ea51c92015-03-20 21:49:12 -07001526 buf_size = GetFinalPathNameByHandleW(hdl, 0, 0,
1527 VOLUME_NAME_DOS);
Brian Curtind25aef52011-06-13 15:16:04 -05001528 if(!buf_size)
1529 return FALSE;
1530
Victor Stinnerc36674a2016-03-16 14:30:16 +01001531 buf = (wchar_t *)PyMem_RawMalloc((buf_size + 1) * sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001532 if (!buf) {
1533 SetLastError(ERROR_OUTOFMEMORY);
1534 return FALSE;
1535 }
1536
Steve Dower2ea51c92015-03-20 21:49:12 -07001537 result_length = GetFinalPathNameByHandleW(hdl,
Brian Curtind25aef52011-06-13 15:16:04 -05001538 buf, buf_size, VOLUME_NAME_DOS);
1539
1540 if(!result_length) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001541 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001542 return FALSE;
1543 }
1544
1545 if(!CloseHandle(hdl)) {
Victor Stinnerc36674a2016-03-16 14:30:16 +01001546 PyMem_RawFree(buf);
Brian Curtind25aef52011-06-13 15:16:04 -05001547 return FALSE;
1548 }
1549
1550 buf[result_length] = 0;
1551
1552 *target_path = buf;
1553 return TRUE;
1554}
1555
1556static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001557win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001558 BOOL traverse);
1559static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001560win32_xstat_impl(const char *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001561 BOOL traverse)
1562{
Victor Stinner26de69d2011-06-17 15:15:38 +02001563 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001564 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001565 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001566 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001567 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001568 const char *dot;
1569
1570 hFile = CreateFileA(
1571 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001572 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001573 0, /* share mode */
1574 NULL, /* security attributes */
1575 OPEN_EXISTING,
1576 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001577 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1578 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001579 the symlink path again and not the actual final path. */
Brian Curtind25aef52011-06-13 15:16:04 -05001580 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1581 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001582 NULL);
1583
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001584 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001585 /* Either the target doesn't exist, or we don't have access to
1586 get a handle to it. If the former, we need to return an error.
1587 If the latter, we can use attributes_from_dir. */
1588 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001589 return -1;
1590 /* Could not get attributes on open file. Fall back to
1591 reading the directory. */
1592 if (!attributes_from_dir(path, &info, &reparse_tag))
1593 /* Very strange. This should not fail now */
1594 return -1;
1595 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1596 if (traverse) {
1597 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001598 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001599 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001600 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001601 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001602 } else {
1603 if (!GetFileInformationByHandle(hFile, &info)) {
1604 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001605 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001606 }
1607 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001608 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1609 return -1;
1610
1611 /* Close the outer open file handle now that we're about to
1612 reopen it with different flags. */
1613 if (!CloseHandle(hFile))
1614 return -1;
1615
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001616 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001617 /* In order to call GetFinalPathNameByHandle we need to open
1618 the file without the reparse handling flag set. */
1619 hFile2 = CreateFileA(
1620 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1621 NULL, OPEN_EXISTING,
1622 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1623 NULL);
1624 if (hFile2 == INVALID_HANDLE_VALUE)
1625 return -1;
1626
1627 if (!get_target_path(hFile2, &target_path))
1628 return -1;
1629
1630 code = win32_xstat_impl_w(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001631 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001632 return code;
1633 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001634 } else
1635 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001636 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001637 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001638
1639 /* Set S_IEXEC if it is an .exe, .bat, ... */
1640 dot = strrchr(path, '.');
1641 if (dot) {
1642 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1643 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1644 result->st_mode |= 0111;
1645 }
1646 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001647}
1648
1649static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001650win32_xstat_impl_w(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001651 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001652{
1653 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001654 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001655 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001656 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001657 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001658 const wchar_t *dot;
1659
1660 hFile = CreateFileW(
1661 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001662 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001663 0, /* share mode */
1664 NULL, /* security attributes */
1665 OPEN_EXISTING,
1666 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001667 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1668 Because of this, calls like GetFinalPathNameByHandle will return
R David Murrayfc069992013-12-13 20:52:19 -05001669 the symlink path again and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001670 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001671 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001672 NULL);
1673
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001674 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001675 /* Either the target doesn't exist, or we don't have access to
1676 get a handle to it. If the former, we need to return an error.
1677 If the latter, we can use attributes_from_dir. */
1678 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001679 return -1;
1680 /* Could not get attributes on open file. Fall back to
1681 reading the directory. */
1682 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1683 /* Very strange. This should not fail now */
1684 return -1;
1685 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1686 if (traverse) {
1687 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001688 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001689 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001690 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001691 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001692 } else {
1693 if (!GetFileInformationByHandle(hFile, &info)) {
1694 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001695 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001696 }
1697 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001698 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1699 return -1;
1700
1701 /* Close the outer open file handle now that we're about to
1702 reopen it with different flags. */
1703 if (!CloseHandle(hFile))
1704 return -1;
1705
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001706 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001707 /* In order to call GetFinalPathNameByHandle we need to open
1708 the file without the reparse handling flag set. */
1709 hFile2 = CreateFileW(
1710 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1711 NULL, OPEN_EXISTING,
1712 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1713 NULL);
1714 if (hFile2 == INVALID_HANDLE_VALUE)
1715 return -1;
1716
1717 if (!get_target_path(hFile2, &target_path))
1718 return -1;
1719
1720 code = win32_xstat_impl_w(target_path, result, FALSE);
Victor Stinnerc36674a2016-03-16 14:30:16 +01001721 PyMem_RawFree(target_path);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001722 return code;
1723 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001724 } else
1725 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001726 }
Steve Dowera2af1a52015-02-21 10:04:10 -08001727 _Py_attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001728
1729 /* Set S_IEXEC if it is an .exe, .bat, ... */
1730 dot = wcsrchr(path, '.');
1731 if (dot) {
1732 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1733 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1734 result->st_mode |= 0111;
1735 }
1736 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001737}
1738
1739static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001740win32_xstat(const char *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001741{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001742 /* Protocol violation: we explicitly clear errno, instead of
1743 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001744 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001745 errno = 0;
1746 return code;
1747}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001748
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001749static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001750win32_xstat_w(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001751{
1752 /* Protocol violation: we explicitly clear errno, instead of
1753 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001754 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001755 errno = 0;
1756 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001757}
Brian Curtind25aef52011-06-13 15:16:04 -05001758/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001759
1760 In Posix, stat automatically traverses symlinks and returns the stat
1761 structure for the target. In Windows, the equivalent GetFileAttributes by
1762 default does not traverse symlinks and instead returns attributes for
1763 the symlink.
1764
1765 Therefore, win32_lstat will get the attributes traditionally, and
1766 win32_stat will first explicitly resolve the symlink target and then will
1767 call win32_lstat on that result.
1768
Ezio Melotti4969f702011-03-15 05:59:46 +02001769 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001770
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001771static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001772win32_lstat(const char* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001773{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001774 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001775}
1776
Victor Stinner8c62be82010-05-06 00:08:46 +00001777static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001778win32_lstat_w(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001779{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001780 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001781}
1782
1783static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001784win32_stat(const char* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001785{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001786 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001787}
1788
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001789static int
Steve Dowerf2f373f2015-02-21 08:44:05 -08001790win32_stat_w(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001791{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001792 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001793}
1794
Martin v. Löwis14694662006-02-03 12:54:16 +00001795#endif /* MS_WINDOWS */
1796
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001797PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001798"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001799This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001800 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001801or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1802\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001803Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1804or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001805\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001806See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001807
1808static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 {"st_mode", "protection bits"},
1810 {"st_ino", "inode"},
1811 {"st_dev", "device"},
1812 {"st_nlink", "number of hard links"},
1813 {"st_uid", "user ID of owner"},
1814 {"st_gid", "group ID of owner"},
1815 {"st_size", "total size, in bytes"},
1816 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1817 {NULL, "integer time of last access"},
1818 {NULL, "integer time of last modification"},
1819 {NULL, "integer time of last change"},
1820 {"st_atime", "time of last access"},
1821 {"st_mtime", "time of last modification"},
1822 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001823 {"st_atime_ns", "time of last access in nanoseconds"},
1824 {"st_mtime_ns", "time of last modification in nanoseconds"},
1825 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001826#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001827 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001828#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001829#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001830 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001831#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001832#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001833 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001834#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001835#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001836 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001837#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001838#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001840#endif
1841#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001842 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001843#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001844#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1845 {"st_file_attributes", "Windows file attribute bits"},
1846#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001847 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001848};
1849
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001850#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001851#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001852#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001853#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001854#endif
1855
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001856#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001857#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1858#else
1859#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1860#endif
1861
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001862#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001863#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1864#else
1865#define ST_RDEV_IDX ST_BLOCKS_IDX
1866#endif
1867
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001868#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1869#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1870#else
1871#define ST_FLAGS_IDX ST_RDEV_IDX
1872#endif
1873
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001874#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001875#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001876#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001877#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001878#endif
1879
1880#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1881#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1882#else
1883#define ST_BIRTHTIME_IDX ST_GEN_IDX
1884#endif
1885
Zachary Ware63f277b2014-06-19 09:46:37 -05001886#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1887#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
1888#else
1889#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
1890#endif
1891
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001892static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001893 "stat_result", /* name */
1894 stat_result__doc__, /* doc */
1895 stat_result_fields,
1896 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001897};
1898
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001899PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001900"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1901This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001902 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001903or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001904\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001906
1907static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001908 {"f_bsize", },
1909 {"f_frsize", },
1910 {"f_blocks", },
1911 {"f_bfree", },
1912 {"f_bavail", },
1913 {"f_files", },
1914 {"f_ffree", },
1915 {"f_favail", },
1916 {"f_flag", },
1917 {"f_namemax",},
1918 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001919};
1920
1921static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001922 "statvfs_result", /* name */
1923 statvfs_result__doc__, /* doc */
1924 statvfs_result_fields,
1925 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001926};
1927
Ross Lagerwall7807c352011-03-17 20:20:30 +02001928#if defined(HAVE_WAITID) && !defined(__APPLE__)
1929PyDoc_STRVAR(waitid_result__doc__,
1930"waitid_result: Result from waitid.\n\n\
1931This object may be accessed either as a tuple of\n\
1932 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1933or via the attributes si_pid, si_uid, and so on.\n\
1934\n\
1935See os.waitid for more information.");
1936
1937static PyStructSequence_Field waitid_result_fields[] = {
1938 {"si_pid", },
1939 {"si_uid", },
1940 {"si_signo", },
1941 {"si_status", },
1942 {"si_code", },
1943 {0}
1944};
1945
1946static PyStructSequence_Desc waitid_result_desc = {
1947 "waitid_result", /* name */
1948 waitid_result__doc__, /* doc */
1949 waitid_result_fields,
1950 5
1951};
1952static PyTypeObject WaitidResultType;
1953#endif
1954
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001955static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001956static PyTypeObject StatResultType;
1957static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001958#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001959static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001960#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001961static newfunc structseq_new;
1962
1963static PyObject *
1964statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1965{
Victor Stinner8c62be82010-05-06 00:08:46 +00001966 PyStructSequence *result;
1967 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001968
Victor Stinner8c62be82010-05-06 00:08:46 +00001969 result = (PyStructSequence*)structseq_new(type, args, kwds);
1970 if (!result)
1971 return NULL;
1972 /* If we have been initialized from a tuple,
1973 st_?time might be set to None. Initialize it
1974 from the int slots. */
1975 for (i = 7; i <= 9; i++) {
1976 if (result->ob_item[i+3] == Py_None) {
1977 Py_DECREF(Py_None);
1978 Py_INCREF(result->ob_item[i]);
1979 result->ob_item[i+3] = result->ob_item[i];
1980 }
1981 }
1982 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001983}
1984
1985
1986
1987/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00001988static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001989
1990PyDoc_STRVAR(stat_float_times__doc__,
1991"stat_float_times([newval]) -> oldval\n\n\
1992Determine whether os.[lf]stat represents time stamps as float objects.\n\
Larry Hastings2f936352014-08-05 14:04:04 +10001993\n\
1994If value is True, future calls to stat() return floats; if it is False,\n\
1995future calls return ints.\n\
1996If value is omitted, return the current setting.\n");
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001997
Larry Hastings2f936352014-08-05 14:04:04 +10001998/* AC 3.5: the public default value should be None, not ready for that yet */
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001999static PyObject*
2000stat_float_times(PyObject* self, PyObject *args)
2001{
Victor Stinner8c62be82010-05-06 00:08:46 +00002002 int newval = -1;
2003 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
2004 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02002005 if (PyErr_WarnEx(PyExc_DeprecationWarning,
2006 "stat_float_times() is deprecated",
2007 1))
2008 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002009 if (newval == -1)
2010 /* Return old value */
2011 return PyBool_FromLong(_stat_float_times);
2012 _stat_float_times = newval;
2013 Py_INCREF(Py_None);
2014 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002015}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002016
Larry Hastings6fe20b32012-04-19 15:07:49 -07002017static PyObject *billion = NULL;
2018
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002019static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002020fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002021{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002022 PyObject *s = _PyLong_FromTime_t(sec);
2023 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2024 PyObject *s_in_ns = NULL;
2025 PyObject *ns_total = NULL;
2026 PyObject *float_s = NULL;
2027
2028 if (!(s && ns_fractional))
2029 goto exit;
2030
2031 s_in_ns = PyNumber_Multiply(s, billion);
2032 if (!s_in_ns)
2033 goto exit;
2034
2035 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2036 if (!ns_total)
2037 goto exit;
2038
Victor Stinner4195b5c2012-02-08 23:03:19 +01002039 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07002040 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2041 if (!float_s)
2042 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00002043 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07002044 else {
2045 float_s = s;
2046 Py_INCREF(float_s);
2047 }
2048
2049 PyStructSequence_SET_ITEM(v, index, s);
2050 PyStructSequence_SET_ITEM(v, index+3, float_s);
2051 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2052 s = NULL;
2053 float_s = NULL;
2054 ns_total = NULL;
2055exit:
2056 Py_XDECREF(s);
2057 Py_XDECREF(ns_fractional);
2058 Py_XDECREF(s_in_ns);
2059 Py_XDECREF(ns_total);
2060 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002061}
2062
Tim Peters5aa91602002-01-30 05:46:57 +00002063/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002064 (used by posix_stat() and posix_fstat()) */
2065static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002066_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002067{
Victor Stinner8c62be82010-05-06 00:08:46 +00002068 unsigned long ansec, mnsec, cnsec;
2069 PyObject *v = PyStructSequence_New(&StatResultType);
2070 if (v == NULL)
2071 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002072
Victor Stinner8c62be82010-05-06 00:08:46 +00002073 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00002074#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002075 PyStructSequence_SET_ITEM(v, 1,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002076 PyLong_FromLongLong((long long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002077#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002078 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002079#endif
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002080#ifdef MS_WINDOWS
2081 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002082#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002083 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002084#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002086#if defined(MS_WINDOWS)
2087 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2088 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2089#else
2090 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2091 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2092#endif
Fred Drake699f3522000-06-29 21:12:41 +00002093#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07002095 PyLong_FromLongLong((long long)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002096#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002097 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002098#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002099
Martin v. Löwis14694662006-02-03 12:54:16 +00002100#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002101 ansec = st->st_atim.tv_nsec;
2102 mnsec = st->st_mtim.tv_nsec;
2103 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002104#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002105 ansec = st->st_atimespec.tv_nsec;
2106 mnsec = st->st_mtimespec.tv_nsec;
2107 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002108#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002109 ansec = st->st_atime_nsec;
2110 mnsec = st->st_mtime_nsec;
2111 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002112#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002113 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002114#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002115 fill_time(v, 7, st->st_atime, ansec);
2116 fill_time(v, 8, st->st_mtime, mnsec);
2117 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002118
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002119#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002120 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2121 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002122#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002123#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002124 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2125 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002126#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002127#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002128 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2129 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002130#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002131#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002132 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2133 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002134#endif
2135#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002136 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002137 PyObject *val;
2138 unsigned long bsec,bnsec;
2139 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002140#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002141 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002142#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002143 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002144#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002145 if (_stat_float_times) {
2146 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2147 } else {
2148 val = PyLong_FromLong((long)bsec);
2149 }
2150 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2151 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002152 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002153#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002154#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002155 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2156 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002157#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002158#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2159 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2160 PyLong_FromUnsignedLong(st->st_file_attributes));
2161#endif
Fred Drake699f3522000-06-29 21:12:41 +00002162
Victor Stinner8c62be82010-05-06 00:08:46 +00002163 if (PyErr_Occurred()) {
2164 Py_DECREF(v);
2165 return NULL;
2166 }
Fred Drake699f3522000-06-29 21:12:41 +00002167
Victor Stinner8c62be82010-05-06 00:08:46 +00002168 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002169}
2170
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002171/* POSIX methods */
2172
Guido van Rossum94f6f721999-01-06 18:42:14 +00002173
2174static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002175posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002176 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002177{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002178 STRUCT_STAT st;
2179 int result;
2180
2181#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2182 if (follow_symlinks_specified(function_name, follow_symlinks))
2183 return NULL;
2184#endif
2185
2186 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2187 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2188 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2189 return NULL;
2190
2191 Py_BEGIN_ALLOW_THREADS
2192 if (path->fd != -1)
2193 result = FSTAT(path->fd, &st);
2194 else
2195#ifdef MS_WINDOWS
2196 if (path->wide) {
2197 if (follow_symlinks)
2198 result = win32_stat_w(path->wide, &st);
2199 else
2200 result = win32_lstat_w(path->wide, &st);
2201 }
2202 else
2203#endif
2204#if defined(HAVE_LSTAT) || defined(MS_WINDOWS)
2205 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2206 result = LSTAT(path->narrow, &st);
2207 else
2208#endif
2209#ifdef HAVE_FSTATAT
2210 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2211 result = fstatat(dir_fd, path->narrow, &st,
2212 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2213 else
2214#endif
2215 result = STAT(path->narrow, &st);
2216 Py_END_ALLOW_THREADS
2217
Victor Stinner292c8352012-10-30 02:17:38 +01002218 if (result != 0) {
2219 return path_error(path);
2220 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002221
2222 return _pystat_fromstructstat(&st);
2223}
2224
Larry Hastings2f936352014-08-05 14:04:04 +10002225/*[python input]
2226
2227for s in """
2228
2229FACCESSAT
2230FCHMODAT
2231FCHOWNAT
2232FSTATAT
2233LINKAT
2234MKDIRAT
2235MKFIFOAT
2236MKNODAT
2237OPENAT
2238READLINKAT
2239SYMLINKAT
2240UNLINKAT
2241
2242""".strip().split():
2243 s = s.strip()
2244 print("""
2245#ifdef HAVE_{s}
2246 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002247#else
Larry Hastings2f936352014-08-05 14:04:04 +10002248 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002249#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002250""".rstrip().format(s=s))
2251
2252for s in """
2253
2254FCHDIR
2255FCHMOD
2256FCHOWN
2257FDOPENDIR
2258FEXECVE
2259FPATHCONF
2260FSTATVFS
2261FTRUNCATE
2262
2263""".strip().split():
2264 s = s.strip()
2265 print("""
2266#ifdef HAVE_{s}
2267 #define PATH_HAVE_{s} 1
2268#else
2269 #define PATH_HAVE_{s} 0
2270#endif
2271
2272""".rstrip().format(s=s))
2273[python start generated code]*/
2274
2275#ifdef HAVE_FACCESSAT
2276 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2277#else
2278 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2279#endif
2280
2281#ifdef HAVE_FCHMODAT
2282 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2283#else
2284 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2285#endif
2286
2287#ifdef HAVE_FCHOWNAT
2288 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2289#else
2290 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2291#endif
2292
2293#ifdef HAVE_FSTATAT
2294 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2295#else
2296 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2297#endif
2298
2299#ifdef HAVE_LINKAT
2300 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2301#else
2302 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2303#endif
2304
2305#ifdef HAVE_MKDIRAT
2306 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2307#else
2308 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2309#endif
2310
2311#ifdef HAVE_MKFIFOAT
2312 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2313#else
2314 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2315#endif
2316
2317#ifdef HAVE_MKNODAT
2318 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2319#else
2320 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2321#endif
2322
2323#ifdef HAVE_OPENAT
2324 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2325#else
2326 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2327#endif
2328
2329#ifdef HAVE_READLINKAT
2330 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2331#else
2332 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2333#endif
2334
2335#ifdef HAVE_SYMLINKAT
2336 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2337#else
2338 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2339#endif
2340
2341#ifdef HAVE_UNLINKAT
2342 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2343#else
2344 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2345#endif
2346
2347#ifdef HAVE_FCHDIR
2348 #define PATH_HAVE_FCHDIR 1
2349#else
2350 #define PATH_HAVE_FCHDIR 0
2351#endif
2352
2353#ifdef HAVE_FCHMOD
2354 #define PATH_HAVE_FCHMOD 1
2355#else
2356 #define PATH_HAVE_FCHMOD 0
2357#endif
2358
2359#ifdef HAVE_FCHOWN
2360 #define PATH_HAVE_FCHOWN 1
2361#else
2362 #define PATH_HAVE_FCHOWN 0
2363#endif
2364
2365#ifdef HAVE_FDOPENDIR
2366 #define PATH_HAVE_FDOPENDIR 1
2367#else
2368 #define PATH_HAVE_FDOPENDIR 0
2369#endif
2370
2371#ifdef HAVE_FEXECVE
2372 #define PATH_HAVE_FEXECVE 1
2373#else
2374 #define PATH_HAVE_FEXECVE 0
2375#endif
2376
2377#ifdef HAVE_FPATHCONF
2378 #define PATH_HAVE_FPATHCONF 1
2379#else
2380 #define PATH_HAVE_FPATHCONF 0
2381#endif
2382
2383#ifdef HAVE_FSTATVFS
2384 #define PATH_HAVE_FSTATVFS 1
2385#else
2386 #define PATH_HAVE_FSTATVFS 0
2387#endif
2388
2389#ifdef HAVE_FTRUNCATE
2390 #define PATH_HAVE_FTRUNCATE 1
2391#else
2392 #define PATH_HAVE_FTRUNCATE 0
2393#endif
2394/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002395
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002396#ifdef MS_WINDOWS
2397 #undef PATH_HAVE_FTRUNCATE
2398 #define PATH_HAVE_FTRUNCATE 1
2399#endif
Larry Hastings31826802013-10-19 00:09:25 -07002400
Larry Hastings61272b72014-01-07 12:41:53 -08002401/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002402
2403class path_t_converter(CConverter):
2404
2405 type = "path_t"
2406 impl_by_reference = True
2407 parse_by_reference = True
2408
2409 converter = 'path_converter'
2410
2411 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002412 # right now path_t doesn't support default values.
2413 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002414 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002415 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002416
Larry Hastings2f936352014-08-05 14:04:04 +10002417 if self.c_default not in (None, 'Py_None'):
2418 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002419
2420 self.nullable = nullable
2421 self.allow_fd = allow_fd
2422
Larry Hastings7726ac92014-01-31 22:03:12 -08002423 def pre_render(self):
2424 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002425 if isinstance(value, str):
2426 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002427 return str(int(bool(value)))
2428
2429 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002430 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002431 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002432 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002433 strify(self.nullable),
2434 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002435 )
2436
2437 def cleanup(self):
2438 return "path_cleanup(&" + self.name + ");\n"
2439
2440
2441class dir_fd_converter(CConverter):
2442 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002443
Larry Hastings2f936352014-08-05 14:04:04 +10002444 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002445 if self.default in (unspecified, None):
2446 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002447 if isinstance(requires, str):
2448 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2449 else:
2450 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002451
Larry Hastings2f936352014-08-05 14:04:04 +10002452class fildes_converter(CConverter):
2453 type = 'int'
2454 converter = 'fildes_converter'
2455
2456class uid_t_converter(CConverter):
2457 type = "uid_t"
2458 converter = '_Py_Uid_Converter'
2459
2460class gid_t_converter(CConverter):
2461 type = "gid_t"
2462 converter = '_Py_Gid_Converter'
2463
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002464class dev_t_converter(CConverter):
2465 type = 'dev_t'
2466 converter = '_Py_Dev_Converter'
2467
2468class dev_t_return_converter(unsigned_long_return_converter):
2469 type = 'dev_t'
2470 conversion_fn = '_PyLong_FromDev'
2471 unsigned_cast = '(dev_t)'
2472
Larry Hastings2f936352014-08-05 14:04:04 +10002473class FSConverter_converter(CConverter):
2474 type = 'PyObject *'
2475 converter = 'PyUnicode_FSConverter'
2476 def converter_init(self):
2477 if self.default is not unspecified:
2478 fail("FSConverter_converter does not support default values")
2479 self.c_default = 'NULL'
2480
2481 def cleanup(self):
2482 return "Py_XDECREF(" + self.name + ");\n"
2483
2484class pid_t_converter(CConverter):
2485 type = 'pid_t'
2486 format_unit = '" _Py_PARSE_PID "'
2487
2488class idtype_t_converter(int_converter):
2489 type = 'idtype_t'
2490
2491class id_t_converter(CConverter):
2492 type = 'id_t'
2493 format_unit = '" _Py_PARSE_PID "'
2494
2495class Py_intptr_t_converter(CConverter):
2496 type = 'Py_intptr_t'
2497 format_unit = '" _Py_PARSE_INTPTR "'
2498
2499class Py_off_t_converter(CConverter):
2500 type = 'Py_off_t'
2501 converter = 'Py_off_t_converter'
2502
2503class Py_off_t_return_converter(long_return_converter):
2504 type = 'Py_off_t'
2505 conversion_fn = 'PyLong_FromPy_off_t'
2506
2507class path_confname_converter(CConverter):
2508 type="int"
2509 converter="conv_path_confname"
2510
2511class confstr_confname_converter(path_confname_converter):
2512 converter='conv_confstr_confname'
2513
2514class sysconf_confname_converter(path_confname_converter):
2515 converter="conv_sysconf_confname"
2516
2517class sched_param_converter(CConverter):
2518 type = 'struct sched_param'
2519 converter = 'convert_sched_param'
2520 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002521
Larry Hastings61272b72014-01-07 12:41:53 -08002522[python start generated code]*/
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002523/*[python end generated code: output=da39a3ee5e6b4b0d input=affe68316f160401]*/
Larry Hastings31826802013-10-19 00:09:25 -07002524
Larry Hastings61272b72014-01-07 12:41:53 -08002525/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002526
Larry Hastings2a727912014-01-16 11:32:01 -08002527os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002528
2529 path : path_t(allow_fd=True)
2530 Path to be examined; can be string, bytes, or open-file-descriptor int.
2531
2532 *
2533
Larry Hastings2f936352014-08-05 14:04:04 +10002534 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002535 If not None, it should be a file descriptor open to a directory,
2536 and path should be a relative string; path will then be relative to
2537 that directory.
2538
2539 follow_symlinks: bool = True
2540 If False, and the last element of the path is a symbolic link,
2541 stat will examine the symbolic link itself instead of the file
2542 the link points to.
2543
2544Perform a stat system call on the given path.
2545
2546dir_fd and follow_symlinks may not be implemented
2547 on your platform. If they are unavailable, using them will raise a
2548 NotImplementedError.
2549
2550It's an error to use dir_fd or follow_symlinks when specifying path as
2551 an open file descriptor.
2552
Larry Hastings61272b72014-01-07 12:41:53 -08002553[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002554
Larry Hastings31826802013-10-19 00:09:25 -07002555static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002556os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
2557/*[clinic end generated code: output=7d4976e6f18a59c5 input=099d356c306fa24a]*/
Larry Hastings31826802013-10-19 00:09:25 -07002558{
2559 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2560}
2561
Larry Hastings2f936352014-08-05 14:04:04 +10002562
2563/*[clinic input]
2564os.lstat
2565
2566 path : path_t
2567
2568 *
2569
2570 dir_fd : dir_fd(requires='fstatat') = None
2571
2572Perform a stat system call on the given path, without following symbolic links.
2573
2574Like stat(), but do not follow symbolic links.
2575Equivalent to stat(path, follow_symlinks=False).
2576[clinic start generated code]*/
2577
Larry Hastings2f936352014-08-05 14:04:04 +10002578static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002579os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2580/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002581{
2582 int follow_symlinks = 0;
2583 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2584}
Larry Hastings31826802013-10-19 00:09:25 -07002585
Larry Hastings2f936352014-08-05 14:04:04 +10002586
Larry Hastings61272b72014-01-07 12:41:53 -08002587/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002588os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002589
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002590 path: path_t
2591 Path to be tested; can be string or bytes
Larry Hastings31826802013-10-19 00:09:25 -07002592
2593 mode: int
2594 Operating-system mode bitfield. Can be F_OK to test existence,
2595 or the inclusive-OR of R_OK, W_OK, and X_OK.
2596
2597 *
2598
Larry Hastings2f936352014-08-05 14:04:04 +10002599 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002600 If not None, it should be a file descriptor open to a directory,
2601 and path should be relative; path will then be relative to that
2602 directory.
2603
2604 effective_ids: bool = False
2605 If True, access will use the effective uid/gid instead of
2606 the real uid/gid.
2607
2608 follow_symlinks: bool = True
2609 If False, and the last element of the path is a symbolic link,
2610 access will examine the symbolic link itself instead of the file
2611 the link points to.
2612
2613Use the real uid/gid to test for access to a path.
2614
2615{parameters}
2616dir_fd, effective_ids, and follow_symlinks may not be implemented
2617 on your platform. If they are unavailable, using them will raise a
2618 NotImplementedError.
2619
2620Note that most operations will use the effective uid/gid, therefore this
2621 routine can be used in a suid/sgid environment to test if the invoking user
2622 has the specified access to the path.
2623
Larry Hastings61272b72014-01-07 12:41:53 -08002624[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002625
Larry Hastings2f936352014-08-05 14:04:04 +10002626static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002627os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002628 int effective_ids, int follow_symlinks)
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002629/*[clinic end generated code: output=cf84158bc90b1a77 input=8e8c3a6ba791fee3]*/
Larry Hastings31826802013-10-19 00:09:25 -07002630{
Larry Hastings2f936352014-08-05 14:04:04 +10002631 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002632
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002633#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002634 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002635#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002636 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002637#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002638
Larry Hastings9cf065c2012-06-22 16:30:09 -07002639#ifndef HAVE_FACCESSAT
2640 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002641 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002642
2643 if (effective_ids) {
2644 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002645 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002646 }
2647#endif
2648
2649#ifdef MS_WINDOWS
2650 Py_BEGIN_ALLOW_THREADS
Christian Heimesebe83f92013-10-19 22:36:17 +02002651 if (path->wide != NULL)
2652 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002653 else
Christian Heimesebe83f92013-10-19 22:36:17 +02002654 attr = GetFileAttributesA(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002655 Py_END_ALLOW_THREADS
2656
2657 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002658 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002659 * * we didn't get a -1, and
2660 * * write access wasn't requested,
2661 * * or the file isn't read-only,
2662 * * or it's a directory.
2663 * (Directories cannot be read-only on Windows.)
2664 */
Larry Hastings2f936352014-08-05 14:04:04 +10002665 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002666 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002667 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002668 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002669#else
2670
2671 Py_BEGIN_ALLOW_THREADS
2672#ifdef HAVE_FACCESSAT
2673 if ((dir_fd != DEFAULT_DIR_FD) ||
2674 effective_ids ||
2675 !follow_symlinks) {
2676 int flags = 0;
2677 if (!follow_symlinks)
2678 flags |= AT_SYMLINK_NOFOLLOW;
2679 if (effective_ids)
2680 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002681 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002682 }
2683 else
2684#endif
Larry Hastings31826802013-10-19 00:09:25 -07002685 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002686 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002687 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002688#endif
2689
Larry Hastings9cf065c2012-06-22 16:30:09 -07002690 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002691}
2692
Guido van Rossumd371ff11999-01-25 16:12:23 +00002693#ifndef F_OK
2694#define F_OK 0
2695#endif
2696#ifndef R_OK
2697#define R_OK 4
2698#endif
2699#ifndef W_OK
2700#define W_OK 2
2701#endif
2702#ifndef X_OK
2703#define X_OK 1
2704#endif
2705
Larry Hastings31826802013-10-19 00:09:25 -07002706
Guido van Rossumd371ff11999-01-25 16:12:23 +00002707#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002708/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002709os.ttyname -> DecodeFSDefault
2710
2711 fd: int
2712 Integer file descriptor handle.
2713
2714 /
2715
2716Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002717[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002718
Larry Hastings31826802013-10-19 00:09:25 -07002719static char *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002720os_ttyname_impl(PyObject *module, int fd)
2721/*[clinic end generated code: output=ed16ad216d813591 input=5f72ca83e76b3b45]*/
Larry Hastings31826802013-10-19 00:09:25 -07002722{
2723 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002724
Larry Hastings31826802013-10-19 00:09:25 -07002725 ret = ttyname(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00002726 if (ret == NULL)
Larry Hastings31826802013-10-19 00:09:25 -07002727 posix_error();
2728 return ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002729}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002730#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002731
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002732#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002733/*[clinic input]
2734os.ctermid
2735
2736Return the name of the controlling terminal for this process.
2737[clinic start generated code]*/
2738
Larry Hastings2f936352014-08-05 14:04:04 +10002739static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002740os_ctermid_impl(PyObject *module)
2741/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002742{
Victor Stinner8c62be82010-05-06 00:08:46 +00002743 char *ret;
2744 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002745
Greg Wardb48bc172000-03-01 21:51:56 +00002746#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002747 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002748#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002750#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 if (ret == NULL)
2752 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002753 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002754}
Larry Hastings2f936352014-08-05 14:04:04 +10002755#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002756
Larry Hastings2f936352014-08-05 14:04:04 +10002757
2758/*[clinic input]
2759os.chdir
2760
2761 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2762
2763Change the current working directory to the specified path.
2764
2765path may always be specified as a string.
2766On some platforms, path may also be specified as an open file descriptor.
2767 If this functionality is unavailable, using it raises an exception.
2768[clinic start generated code]*/
2769
Larry Hastings2f936352014-08-05 14:04:04 +10002770static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002771os_chdir_impl(PyObject *module, path_t *path)
2772/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002773{
2774 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002775
2776 Py_BEGIN_ALLOW_THREADS
2777#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10002778 if (path->wide)
2779 result = win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002780 else
Larry Hastings2f936352014-08-05 14:04:04 +10002781 result = win32_chdir(path->narrow);
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03002782 result = !result; /* on unix, success = 0, on windows, success = !0 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002783#else
2784#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002785 if (path->fd != -1)
2786 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002787 else
2788#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002789 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002790#endif
2791 Py_END_ALLOW_THREADS
2792
2793 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002794 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002795 }
2796
Larry Hastings2f936352014-08-05 14:04:04 +10002797 Py_RETURN_NONE;
2798}
2799
2800
2801#ifdef HAVE_FCHDIR
2802/*[clinic input]
2803os.fchdir
2804
2805 fd: fildes
2806
2807Change to the directory of the given file descriptor.
2808
2809fd must be opened on a directory, not a file.
2810Equivalent to os.chdir(fd).
2811
2812[clinic start generated code]*/
2813
Fred Drake4d1e64b2002-04-15 19:40:07 +00002814static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002815os_fchdir_impl(PyObject *module, int fd)
2816/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002817{
Larry Hastings2f936352014-08-05 14:04:04 +10002818 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002819}
2820#endif /* HAVE_FCHDIR */
2821
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002822
Larry Hastings2f936352014-08-05 14:04:04 +10002823/*[clinic input]
2824os.chmod
2825
2826 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
2827 Path to be modified. May always be specified as a str or bytes.
2828 On some platforms, path may also be specified as an open file descriptor.
2829 If this functionality is unavailable, using it raises an exception.
2830
2831 mode: int
2832 Operating-system mode bitfield.
2833
2834 *
2835
2836 dir_fd : dir_fd(requires='fchmodat') = None
2837 If not None, it should be a file descriptor open to a directory,
2838 and path should be relative; path will then be relative to that
2839 directory.
2840
2841 follow_symlinks: bool = True
2842 If False, and the last element of the path is a symbolic link,
2843 chmod will modify the symbolic link itself instead of the file
2844 the link points to.
2845
2846Change the access permissions of a file.
2847
2848It is an error to use dir_fd or follow_symlinks when specifying path as
2849 an open file descriptor.
2850dir_fd and follow_symlinks may not be implemented on your platform.
2851 If they are unavailable, using them will raise a NotImplementedError.
2852
2853[clinic start generated code]*/
2854
Larry Hastings2f936352014-08-05 14:04:04 +10002855static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002856os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002857 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002858/*[clinic end generated code: output=5cf6a94915cc7bff input=7f1618e5e15cc196]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002859{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002860 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002861
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002862#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002863 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002864#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002865
Larry Hastings9cf065c2012-06-22 16:30:09 -07002866#ifdef HAVE_FCHMODAT
2867 int fchmodat_nofollow_unsupported = 0;
2868#endif
2869
Larry Hastings9cf065c2012-06-22 16:30:09 -07002870#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2871 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002872 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002873#endif
2874
2875#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002876 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002877 if (path->wide)
2878 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002879 else
Larry Hastings2f936352014-08-05 14:04:04 +10002880 attr = GetFileAttributesA(path->narrow);
Tim Golden23005082013-10-25 11:22:37 +01002881 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002882 result = 0;
2883 else {
2884 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002885 attr &= ~FILE_ATTRIBUTE_READONLY;
2886 else
2887 attr |= FILE_ATTRIBUTE_READONLY;
Larry Hastings2f936352014-08-05 14:04:04 +10002888 if (path->wide)
2889 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002890 else
Larry Hastings2f936352014-08-05 14:04:04 +10002891 result = SetFileAttributesA(path->narrow, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002892 }
2893 Py_END_ALLOW_THREADS
2894
2895 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002896 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002897 }
2898#else /* MS_WINDOWS */
2899 Py_BEGIN_ALLOW_THREADS
2900#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002901 if (path->fd != -1)
2902 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002903 else
2904#endif
2905#ifdef HAVE_LCHMOD
2906 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10002907 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002908 else
2909#endif
2910#ifdef HAVE_FCHMODAT
2911 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2912 /*
2913 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2914 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002915 * and then says it isn't implemented yet.
2916 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002917 *
2918 * Once it is supported, os.chmod will automatically
2919 * support dir_fd and follow_symlinks=False. (Hopefully.)
2920 * Until then, we need to be careful what exception we raise.
2921 */
Larry Hastings2f936352014-08-05 14:04:04 +10002922 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002923 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2924 /*
2925 * But wait! We can't throw the exception without allowing threads,
2926 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2927 */
2928 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002929 result &&
2930 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2931 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002932 }
2933 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002934#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002935 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002936 Py_END_ALLOW_THREADS
2937
2938 if (result) {
2939#ifdef HAVE_FCHMODAT
2940 if (fchmodat_nofollow_unsupported) {
2941 if (dir_fd != DEFAULT_DIR_FD)
2942 dir_fd_and_follow_symlinks_invalid("chmod",
2943 dir_fd, follow_symlinks);
2944 else
2945 follow_symlinks_specified("chmod", follow_symlinks);
2946 }
2947 else
2948#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002949 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002950 }
2951#endif
2952
Larry Hastings2f936352014-08-05 14:04:04 +10002953 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002954}
2955
Larry Hastings9cf065c2012-06-22 16:30:09 -07002956
Christian Heimes4e30a842007-11-30 22:12:06 +00002957#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002958/*[clinic input]
2959os.fchmod
2960
2961 fd: int
2962 mode: int
2963
2964Change the access permissions of the file given by file descriptor fd.
2965
2966Equivalent to os.chmod(fd, mode).
2967[clinic start generated code]*/
2968
Larry Hastings2f936352014-08-05 14:04:04 +10002969static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002970os_fchmod_impl(PyObject *module, int fd, int mode)
2971/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002972{
2973 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00002974 int async_err = 0;
2975
2976 do {
2977 Py_BEGIN_ALLOW_THREADS
2978 res = fchmod(fd, mode);
2979 Py_END_ALLOW_THREADS
2980 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
2981 if (res != 0)
2982 return (!async_err) ? posix_error() : NULL;
2983
Victor Stinner8c62be82010-05-06 00:08:46 +00002984 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002985}
2986#endif /* HAVE_FCHMOD */
2987
Larry Hastings2f936352014-08-05 14:04:04 +10002988
Christian Heimes4e30a842007-11-30 22:12:06 +00002989#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10002990/*[clinic input]
2991os.lchmod
2992
2993 path: path_t
2994 mode: int
2995
2996Change the access permissions of a file, without following symbolic links.
2997
2998If path is a symlink, this affects the link itself rather than the target.
2999Equivalent to chmod(path, mode, follow_symlinks=False)."
3000[clinic start generated code]*/
3001
Larry Hastings2f936352014-08-05 14:04:04 +10003002static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003003os_lchmod_impl(PyObject *module, path_t *path, int mode)
3004/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003005{
Victor Stinner8c62be82010-05-06 00:08:46 +00003006 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003007 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003008 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003009 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003010 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003011 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003012 return NULL;
3013 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003014 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003015}
3016#endif /* HAVE_LCHMOD */
3017
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003018
Thomas Wouterscf297e42007-02-23 15:07:44 +00003019#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003020/*[clinic input]
3021os.chflags
3022
3023 path: path_t
3024 flags: unsigned_long(bitwise=True)
3025 follow_symlinks: bool=True
3026
3027Set file flags.
3028
3029If follow_symlinks is False, and the last element of the path is a symbolic
3030 link, chflags will change flags on the symbolic link itself instead of the
3031 file the link points to.
3032follow_symlinks may not be implemented on your platform. If it is
3033unavailable, using it will raise a NotImplementedError.
3034
3035[clinic start generated code]*/
3036
Larry Hastings2f936352014-08-05 14:04:04 +10003037static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003038os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003039 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003040/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003041{
3042 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003043
3044#ifndef HAVE_LCHFLAGS
3045 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003046 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003047#endif
3048
Victor Stinner8c62be82010-05-06 00:08:46 +00003049 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003050#ifdef HAVE_LCHFLAGS
3051 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003052 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003053 else
3054#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003055 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003056 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003057
Larry Hastings2f936352014-08-05 14:04:04 +10003058 if (result)
3059 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003060
Larry Hastings2f936352014-08-05 14:04:04 +10003061 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003062}
3063#endif /* HAVE_CHFLAGS */
3064
Larry Hastings2f936352014-08-05 14:04:04 +10003065
Thomas Wouterscf297e42007-02-23 15:07:44 +00003066#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003067/*[clinic input]
3068os.lchflags
3069
3070 path: path_t
3071 flags: unsigned_long(bitwise=True)
3072
3073Set file flags.
3074
3075This function will not follow symbolic links.
3076Equivalent to chflags(path, flags, follow_symlinks=False).
3077[clinic start generated code]*/
3078
Larry Hastings2f936352014-08-05 14:04:04 +10003079static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003080os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3081/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003082{
Victor Stinner8c62be82010-05-06 00:08:46 +00003083 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003084 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003085 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003086 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003087 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003088 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003089 }
Victor Stinner292c8352012-10-30 02:17:38 +01003090 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003091}
3092#endif /* HAVE_LCHFLAGS */
3093
Larry Hastings2f936352014-08-05 14:04:04 +10003094
Martin v. Löwis244edc82001-10-04 22:44:26 +00003095#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003096/*[clinic input]
3097os.chroot
3098 path: path_t
3099
3100Change root directory to path.
3101
3102[clinic start generated code]*/
3103
Larry Hastings2f936352014-08-05 14:04:04 +10003104static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003105os_chroot_impl(PyObject *module, path_t *path)
3106/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003107{
3108 int res;
3109 Py_BEGIN_ALLOW_THREADS
3110 res = chroot(path->narrow);
3111 Py_END_ALLOW_THREADS
3112 if (res < 0)
3113 return path_error(path);
3114 Py_RETURN_NONE;
3115}
3116#endif /* HAVE_CHROOT */
3117
Martin v. Löwis244edc82001-10-04 22:44:26 +00003118
Guido van Rossum21142a01999-01-08 21:05:37 +00003119#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003120/*[clinic input]
3121os.fsync
3122
3123 fd: fildes
3124
3125Force write of fd to disk.
3126[clinic start generated code]*/
3127
Larry Hastings2f936352014-08-05 14:04:04 +10003128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003129os_fsync_impl(PyObject *module, int fd)
3130/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003131{
3132 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003133}
3134#endif /* HAVE_FSYNC */
3135
Larry Hastings2f936352014-08-05 14:04:04 +10003136
Ross Lagerwall7807c352011-03-17 20:20:30 +02003137#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003138/*[clinic input]
3139os.sync
3140
3141Force write of everything to disk.
3142[clinic start generated code]*/
3143
Larry Hastings2f936352014-08-05 14:04:04 +10003144static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003145os_sync_impl(PyObject *module)
3146/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003147{
3148 Py_BEGIN_ALLOW_THREADS
3149 sync();
3150 Py_END_ALLOW_THREADS
3151 Py_RETURN_NONE;
3152}
Larry Hastings2f936352014-08-05 14:04:04 +10003153#endif /* HAVE_SYNC */
3154
Ross Lagerwall7807c352011-03-17 20:20:30 +02003155
Guido van Rossum21142a01999-01-08 21:05:37 +00003156#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003157#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003158extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3159#endif
3160
Larry Hastings2f936352014-08-05 14:04:04 +10003161/*[clinic input]
3162os.fdatasync
3163
3164 fd: fildes
3165
3166Force write of fd to disk without forcing update of metadata.
3167[clinic start generated code]*/
3168
Larry Hastings2f936352014-08-05 14:04:04 +10003169static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003170os_fdatasync_impl(PyObject *module, int fd)
3171/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003172{
3173 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003174}
3175#endif /* HAVE_FDATASYNC */
3176
3177
Fredrik Lundh10723342000-07-10 16:38:09 +00003178#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003179/*[clinic input]
3180os.chown
3181
3182 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
3183 Path to be examined; can be string, bytes, or open-file-descriptor int.
3184
3185 uid: uid_t
3186
3187 gid: gid_t
3188
3189 *
3190
3191 dir_fd : dir_fd(requires='fchownat') = None
3192 If not None, it should be a file descriptor open to a directory,
3193 and path should be relative; path will then be relative to that
3194 directory.
3195
3196 follow_symlinks: bool = True
3197 If False, and the last element of the path is a symbolic link,
3198 stat will examine the symbolic link itself instead of the file
3199 the link points to.
3200
3201Change the owner and group id of path to the numeric uid and gid.\
3202
3203path may always be specified as a string.
3204On some platforms, path may also be specified as an open file descriptor.
3205 If this functionality is unavailable, using it raises an exception.
3206If dir_fd is not None, it should be a file descriptor open to a directory,
3207 and path should be relative; path will then be relative to that directory.
3208If follow_symlinks is False, and the last element of the path is a symbolic
3209 link, chown will modify the symbolic link itself instead of the file the
3210 link points to.
3211It is an error to use dir_fd or follow_symlinks when specifying path as
3212 an open file descriptor.
3213dir_fd and follow_symlinks may not be implemented on your platform.
3214 If they are unavailable, using them will raise a NotImplementedError.
3215
3216[clinic start generated code]*/
3217
Larry Hastings2f936352014-08-05 14:04:04 +10003218static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003219os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003220 int dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003221/*[clinic end generated code: output=4beadab0db5f70cd input=a61cc35574814d5d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003222{
3223 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003224
3225#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3226 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003227 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003228#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003229 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3230 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3231 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003232
3233#ifdef __APPLE__
3234 /*
3235 * This is for Mac OS X 10.3, which doesn't have lchown.
3236 * (But we still have an lchown symbol because of weak-linking.)
3237 * It doesn't have fchownat either. So there's no possibility
3238 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003239 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003240 if ((!follow_symlinks) && (lchown == NULL)) {
3241 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003242 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003243 }
3244#endif
3245
Victor Stinner8c62be82010-05-06 00:08:46 +00003246 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003247#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003248 if (path->fd != -1)
3249 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003250 else
3251#endif
3252#ifdef HAVE_LCHOWN
3253 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003254 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003255 else
3256#endif
3257#ifdef HAVE_FCHOWNAT
3258 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003259 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003260 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3261 else
3262#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003263 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003264 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003265
Larry Hastings2f936352014-08-05 14:04:04 +10003266 if (result)
3267 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003268
Larry Hastings2f936352014-08-05 14:04:04 +10003269 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003270}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003271#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003272
Larry Hastings2f936352014-08-05 14:04:04 +10003273
Christian Heimes4e30a842007-11-30 22:12:06 +00003274#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003275/*[clinic input]
3276os.fchown
3277
3278 fd: int
3279 uid: uid_t
3280 gid: gid_t
3281
3282Change the owner and group id of the file specified by file descriptor.
3283
3284Equivalent to os.chown(fd, uid, gid).
3285
3286[clinic start generated code]*/
3287
Larry Hastings2f936352014-08-05 14:04:04 +10003288static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003289os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3290/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003291{
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003293 int async_err = 0;
3294
3295 do {
3296 Py_BEGIN_ALLOW_THREADS
3297 res = fchown(fd, uid, gid);
3298 Py_END_ALLOW_THREADS
3299 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3300 if (res != 0)
3301 return (!async_err) ? posix_error() : NULL;
3302
Victor Stinner8c62be82010-05-06 00:08:46 +00003303 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003304}
3305#endif /* HAVE_FCHOWN */
3306
Larry Hastings2f936352014-08-05 14:04:04 +10003307
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003308#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003309/*[clinic input]
3310os.lchown
3311
3312 path : path_t
3313 uid: uid_t
3314 gid: gid_t
3315
3316Change the owner and group id of path to the numeric uid and gid.
3317
3318This function will not follow symbolic links.
3319Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3320[clinic start generated code]*/
3321
Larry Hastings2f936352014-08-05 14:04:04 +10003322static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003323os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3324/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003325{
Victor Stinner8c62be82010-05-06 00:08:46 +00003326 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00003327 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003328 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003329 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003330 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003331 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003332 }
Larry Hastings2f936352014-08-05 14:04:04 +10003333 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003334}
3335#endif /* HAVE_LCHOWN */
3336
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003337
Barry Warsaw53699e91996-12-10 23:23:01 +00003338static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003339posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003340{
Victor Stinner4403d7d2015-04-25 00:16:10 +02003341 char *buf, *tmpbuf;
3342 char *cwd;
3343 const size_t chunk = 1024;
3344 size_t buflen = 0;
3345 PyObject *obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003346
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003347#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003348 if (!use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003349 wchar_t wbuf[MAXPATHLEN];
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 wchar_t *wbuf2 = wbuf;
3351 PyObject *resobj;
3352 DWORD len;
3353 Py_BEGIN_ALLOW_THREADS
Victor Stinner75875072013-11-24 19:23:25 +01003354 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003355 /* If the buffer is large enough, len does not include the
3356 terminating \0. If the buffer is too small, len includes
3357 the space needed for the terminator. */
Victor Stinner75875072013-11-24 19:23:25 +01003358 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003359 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003360 if (wbuf2)
3361 len = GetCurrentDirectoryW(len, wbuf2);
3362 }
3363 Py_END_ALLOW_THREADS
3364 if (!wbuf2) {
3365 PyErr_NoMemory();
3366 return NULL;
3367 }
3368 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003369 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003370 PyMem_RawFree(wbuf2);
Victor Stinnerb024e842012-10-31 22:24:06 +01003371 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003372 }
3373 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003374 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003375 PyMem_RawFree(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003376 return resobj;
3377 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003378
3379 if (win32_warn_bytes_api())
3380 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003381#endif
3382
Victor Stinner4403d7d2015-04-25 00:16:10 +02003383 buf = cwd = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003384 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003385 do {
3386 buflen += chunk;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003387#ifdef MS_WINDOWS
3388 if (buflen > INT_MAX) {
3389 PyErr_NoMemory();
3390 break;
3391 }
3392#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003393 tmpbuf = PyMem_RawRealloc(buf, buflen);
3394 if (tmpbuf == NULL)
3395 break;
3396
3397 buf = tmpbuf;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003398#ifdef MS_WINDOWS
3399 cwd = getcwd(buf, (int)buflen);
3400#else
Victor Stinner4403d7d2015-04-25 00:16:10 +02003401 cwd = getcwd(buf, buflen);
Victor Stinnerc44f7072016-03-14 18:07:53 +01003402#endif
Victor Stinner4403d7d2015-04-25 00:16:10 +02003403 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003404 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003405
3406 if (cwd == NULL) {
3407 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003409 }
3410
Victor Stinner8c62be82010-05-06 00:08:46 +00003411 if (use_bytes)
Victor Stinner4403d7d2015-04-25 00:16:10 +02003412 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
3413 else
3414 obj = PyUnicode_DecodeFSDefault(buf);
3415 PyMem_RawFree(buf);
3416
3417 return obj;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003418}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003419
Larry Hastings2f936352014-08-05 14:04:04 +10003420
3421/*[clinic input]
3422os.getcwd
3423
3424Return a unicode string representing the current working directory.
3425[clinic start generated code]*/
3426
Larry Hastings2f936352014-08-05 14:04:04 +10003427static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003428os_getcwd_impl(PyObject *module)
3429/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003430{
3431 return posix_getcwd(0);
3432}
3433
Larry Hastings2f936352014-08-05 14:04:04 +10003434
3435/*[clinic input]
3436os.getcwdb
3437
3438Return a bytes string representing the current working directory.
3439[clinic start generated code]*/
3440
Larry Hastings2f936352014-08-05 14:04:04 +10003441static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003442os_getcwdb_impl(PyObject *module)
3443/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003444{
3445 return posix_getcwd(1);
3446}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003447
Larry Hastings2f936352014-08-05 14:04:04 +10003448
Larry Hastings9cf065c2012-06-22 16:30:09 -07003449#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3450#define HAVE_LINK 1
3451#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003452
Guido van Rossumb6775db1994-08-01 11:34:53 +00003453#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003454/*[clinic input]
3455
3456os.link
3457
3458 src : path_t
3459 dst : path_t
3460 *
3461 src_dir_fd : dir_fd = None
3462 dst_dir_fd : dir_fd = None
3463 follow_symlinks: bool = True
3464
3465Create a hard link to a file.
3466
3467If either src_dir_fd or dst_dir_fd is not None, it should be a file
3468 descriptor open to a directory, and the respective path string (src or dst)
3469 should be relative; the path will then be relative to that directory.
3470If follow_symlinks is False, and the last element of src is a symbolic
3471 link, link will create a link to the symbolic link itself instead of the
3472 file the link points to.
3473src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3474 platform. If they are unavailable, using them will raise a
3475 NotImplementedError.
3476[clinic start generated code]*/
3477
Larry Hastings2f936352014-08-05 14:04:04 +10003478static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003479os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003480 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003481/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003482{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003483#ifdef MS_WINDOWS
3484 BOOL result;
3485#else
3486 int result;
3487#endif
3488
Larry Hastings9cf065c2012-06-22 16:30:09 -07003489#ifndef HAVE_LINKAT
3490 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3491 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003492 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003493 }
3494#endif
3495
Larry Hastings2f936352014-08-05 14:04:04 +10003496 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003497 PyErr_SetString(PyExc_NotImplementedError,
3498 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003499 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003500 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003501
Brian Curtin1b9df392010-11-24 20:24:31 +00003502#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003503 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003504 if (src->wide)
3505 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003506 else
Larry Hastings2f936352014-08-05 14:04:04 +10003507 result = CreateHardLinkA(dst->narrow, src->narrow, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003508 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003509
Larry Hastings2f936352014-08-05 14:04:04 +10003510 if (!result)
3511 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003512#else
3513 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003514#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003515 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3516 (dst_dir_fd != DEFAULT_DIR_FD) ||
3517 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003518 result = linkat(src_dir_fd, src->narrow,
3519 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003520 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3521 else
3522#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003523 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003524 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003525
Larry Hastings2f936352014-08-05 14:04:04 +10003526 if (result)
3527 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003528#endif
3529
Larry Hastings2f936352014-08-05 14:04:04 +10003530 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003531}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003532#endif
3533
Brian Curtin1b9df392010-11-24 20:24:31 +00003534
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003535#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003536static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003537_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003538{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003539 PyObject *v;
3540 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3541 BOOL result;
3542 WIN32_FIND_DATA FileData;
Victor Stinner75875072013-11-24 19:23:25 +01003543 char namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003544 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003545 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003546 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003547
Gregory P. Smith40a21602013-03-20 20:52:50 -07003548 if (!path->narrow) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003549 WIN32_FIND_DATAW wFileData;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003550 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003551
Gregory P. Smith40a21602013-03-20 20:52:50 -07003552 if (!path->wide) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003553 po_wchars = L".";
3554 len = 1;
3555 } else {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003556 po_wchars = path->wide;
3557 len = wcslen(path->wide);
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003558 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003559 /* The +5 is so we can append "\\*.*\0" */
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003560 wnamebuf = PyMem_New(wchar_t, len + 5);
Victor Stinner8c62be82010-05-06 00:08:46 +00003561 if (!wnamebuf) {
3562 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003563 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003564 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003565 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003566 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003567 wchar_t wch = wnamebuf[len-1];
Tim Golden781bbeb2013-10-25 20:24:06 +01003568 if (wch != SEP && wch != ALTSEP && wch != L':')
3569 wnamebuf[len++] = SEP;
Victor Stinner8c62be82010-05-06 00:08:46 +00003570 wcscpy(wnamebuf + len, L"*.*");
3571 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003572 if ((list = PyList_New(0)) == NULL) {
3573 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003574 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003575 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003576 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003577 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003578 if (hFindFile == INVALID_HANDLE_VALUE) {
3579 int error = GetLastError();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003580 if (error == ERROR_FILE_NOT_FOUND)
3581 goto exit;
3582 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003583 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003584 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003585 }
3586 do {
3587 /* Skip over . and .. */
3588 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3589 wcscmp(wFileData.cFileName, L"..") != 0) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003590 v = PyUnicode_FromWideChar(wFileData.cFileName,
3591 wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00003592 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003593 Py_DECREF(list);
3594 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003595 break;
3596 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003597 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003598 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003599 Py_DECREF(list);
3600 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 break;
3602 }
3603 Py_DECREF(v);
3604 }
3605 Py_BEGIN_ALLOW_THREADS
3606 result = FindNextFileW(hFindFile, &wFileData);
3607 Py_END_ALLOW_THREADS
3608 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3609 it got to the end of the directory. */
3610 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003611 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003612 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003613 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003614 }
3615 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003616
Larry Hastings9cf065c2012-06-22 16:30:09 -07003617 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003618 }
Gregory P. Smith40a21602013-03-20 20:52:50 -07003619 strcpy(namebuf, path->narrow);
3620 len = path->length;
Victor Stinner8c62be82010-05-06 00:08:46 +00003621 if (len > 0) {
3622 char ch = namebuf[len-1];
Tim Golden781bbeb2013-10-25 20:24:06 +01003623 if (ch != '\\' && ch != '/' && ch != ':')
3624 namebuf[len++] = '\\';
Victor Stinner8c62be82010-05-06 00:08:46 +00003625 strcpy(namebuf + len, "*.*");
3626 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003627
Larry Hastings9cf065c2012-06-22 16:30:09 -07003628 if ((list = PyList_New(0)) == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00003629 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003630
Antoine Pitroub73caab2010-08-09 23:39:31 +00003631 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003632 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003633 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003634 if (hFindFile == INVALID_HANDLE_VALUE) {
3635 int error = GetLastError();
3636 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003637 goto exit;
3638 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003639 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003640 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003641 }
3642 do {
3643 /* Skip over . and .. */
3644 if (strcmp(FileData.cFileName, ".") != 0 &&
3645 strcmp(FileData.cFileName, "..") != 0) {
3646 v = PyBytes_FromString(FileData.cFileName);
3647 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003648 Py_DECREF(list);
3649 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003650 break;
3651 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003652 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003653 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003654 Py_DECREF(list);
3655 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003656 break;
3657 }
3658 Py_DECREF(v);
3659 }
3660 Py_BEGIN_ALLOW_THREADS
3661 result = FindNextFile(hFindFile, &FileData);
3662 Py_END_ALLOW_THREADS
3663 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3664 it got to the end of the directory. */
3665 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003666 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003667 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003668 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003669 }
3670 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003671
Larry Hastings9cf065c2012-06-22 16:30:09 -07003672exit:
3673 if (hFindFile != INVALID_HANDLE_VALUE) {
3674 if (FindClose(hFindFile) == FALSE) {
3675 if (list != NULL) {
3676 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003677 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003678 }
3679 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003680 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003681 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003682
Larry Hastings9cf065c2012-06-22 16:30:09 -07003683 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003684} /* end of _listdir_windows_no_opendir */
3685
3686#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3687
3688static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003689_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003690{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003691 PyObject *v;
3692 DIR *dirp = NULL;
3693 struct dirent *ep;
3694 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003695#ifdef HAVE_FDOPENDIR
3696 int fd = -1;
3697#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003698
Victor Stinner8c62be82010-05-06 00:08:46 +00003699 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003700#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003701 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003702 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003703 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003704 if (fd == -1)
3705 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003706
Larry Hastingsfdaea062012-06-25 04:42:23 -07003707 return_str = 1;
3708
Larry Hastings9cf065c2012-06-22 16:30:09 -07003709 Py_BEGIN_ALLOW_THREADS
3710 dirp = fdopendir(fd);
3711 Py_END_ALLOW_THREADS
3712 }
3713 else
3714#endif
3715 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003716 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003717 if (path->narrow) {
3718 name = path->narrow;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003719 /* only return bytes if they specified a bytes object */
Gregory P. Smith40a21602013-03-20 20:52:50 -07003720 return_str = !(PyBytes_Check(path->object));
Larry Hastingsfdaea062012-06-25 04:42:23 -07003721 }
3722 else {
3723 name = ".";
3724 return_str = 1;
3725 }
3726
Larry Hastings9cf065c2012-06-22 16:30:09 -07003727 Py_BEGIN_ALLOW_THREADS
3728 dirp = opendir(name);
3729 Py_END_ALLOW_THREADS
3730 }
3731
3732 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003733 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003734#ifdef HAVE_FDOPENDIR
3735 if (fd != -1) {
3736 Py_BEGIN_ALLOW_THREADS
3737 close(fd);
3738 Py_END_ALLOW_THREADS
3739 }
3740#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003741 goto exit;
3742 }
3743 if ((list = PyList_New(0)) == NULL) {
3744 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003745 }
3746 for (;;) {
3747 errno = 0;
3748 Py_BEGIN_ALLOW_THREADS
3749 ep = readdir(dirp);
3750 Py_END_ALLOW_THREADS
3751 if (ep == NULL) {
3752 if (errno == 0) {
3753 break;
3754 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003755 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003756 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003757 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003758 }
3759 }
3760 if (ep->d_name[0] == '.' &&
3761 (NAMLEN(ep) == 1 ||
3762 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3763 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003764 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003765 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3766 else
3767 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003768 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003769 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003770 break;
3771 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003772 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003774 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003775 break;
3776 }
3777 Py_DECREF(v);
3778 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003779
Larry Hastings9cf065c2012-06-22 16:30:09 -07003780exit:
3781 if (dirp != NULL) {
3782 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003783#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003784 if (fd > -1)
3785 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003786#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003787 closedir(dirp);
3788 Py_END_ALLOW_THREADS
3789 }
3790
Larry Hastings9cf065c2012-06-22 16:30:09 -07003791 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003792} /* end of _posix_listdir */
3793#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003794
Larry Hastings2f936352014-08-05 14:04:04 +10003795
3796/*[clinic input]
3797os.listdir
3798
3799 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3800
3801Return a list containing the names of the files in the directory.
3802
3803path can be specified as either str or bytes. If path is bytes,
3804 the filenames returned will also be bytes; in all other circumstances
3805 the filenames returned will be str.
3806If path is None, uses the path='.'.
3807On some platforms, path may also be specified as an open file descriptor;\
3808 the file descriptor must refer to a directory.
3809 If this functionality is unavailable, using it raises NotImplementedError.
3810
3811The list is in arbitrary order. It does not include the special
3812entries '.' and '..' even if they are present in the directory.
3813
3814
3815[clinic start generated code]*/
3816
Larry Hastings2f936352014-08-05 14:04:04 +10003817static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003818os_listdir_impl(PyObject *module, path_t *path)
3819/*[clinic end generated code: output=293045673fcd1a75 input=09e300416e3cd729]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003820{
3821#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3822 return _listdir_windows_no_opendir(path, NULL);
3823#else
3824 return _posix_listdir(path, NULL);
3825#endif
3826}
3827
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003828#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003829/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003830/*[clinic input]
3831os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003832
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003833 path: path_t
3834 /
3835
3836[clinic start generated code]*/
3837
3838static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003839os__getfullpathname_impl(PyObject *module, path_t *path)
3840/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003841{
3842 if (!path->narrow)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003843 {
Victor Stinner75875072013-11-24 19:23:25 +01003844 wchar_t woutbuf[MAX_PATH], *woutbufp = woutbuf;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003845 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00003846 DWORD result;
3847 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003848
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003849 result = GetFullPathNameW(path->wide,
Victor Stinner63941882011-09-29 00:42:28 +02003850 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003851 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02003852 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003853 woutbufp = PyMem_New(wchar_t, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00003854 if (!woutbufp)
3855 return PyErr_NoMemory();
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003856 result = GetFullPathNameW(path->wide, result, woutbufp, &wtemp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003857 }
3858 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003859 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00003860 else
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003861 v = win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003862 if (woutbufp != woutbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003863 PyMem_Free(woutbufp);
Victor Stinner8c62be82010-05-06 00:08:46 +00003864 return v;
3865 }
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003866 else {
3867 char outbuf[MAX_PATH];
3868 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003869
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003870 if (!GetFullPathName(path->narrow, Py_ARRAY_LENGTH(outbuf),
3871 outbuf, &temp)) {
3872 win32_error_object("GetFullPathName", path->object);
3873 return NULL;
3874 }
3875 return PyBytes_FromString(outbuf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003876 }
Larry Hastings2f936352014-08-05 14:04:04 +10003877}
Brian Curtind40e6f72010-07-08 21:39:08 +00003878
Brian Curtind25aef52011-06-13 15:16:04 -05003879
Larry Hastings2f936352014-08-05 14:04:04 +10003880/*[clinic input]
3881os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00003882
Larry Hastings2f936352014-08-05 14:04:04 +10003883 path: unicode
3884 /
3885
3886A helper function for samepath on windows.
3887[clinic start generated code]*/
3888
Larry Hastings2f936352014-08-05 14:04:04 +10003889static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003890os__getfinalpathname_impl(PyObject *module, PyObject *path)
3891/*[clinic end generated code: output=9bd78d0e52782e75 input=71d5e89334891bf4]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00003892{
3893 HANDLE hFile;
3894 int buf_size;
3895 wchar_t *target_path;
3896 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10003897 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003898 const wchar_t *path_wchar;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003899
Larry Hastings2f936352014-08-05 14:04:04 +10003900 path_wchar = PyUnicode_AsUnicode(path);
3901 if (path_wchar == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003902 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003903
Brian Curtind40e6f72010-07-08 21:39:08 +00003904 hFile = CreateFileW(
Larry Hastings2f936352014-08-05 14:04:04 +10003905 path_wchar,
Brian Curtind40e6f72010-07-08 21:39:08 +00003906 0, /* desired access */
3907 0, /* share mode */
3908 NULL, /* security attributes */
3909 OPEN_EXISTING,
3910 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3911 FILE_FLAG_BACKUP_SEMANTICS,
3912 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003913
Victor Stinnereb5657a2011-09-30 01:44:27 +02003914 if(hFile == INVALID_HANDLE_VALUE)
Larry Hastings2f936352014-08-05 14:04:04 +10003915 return win32_error_object("CreateFileW", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003916
3917 /* We have a good handle to the target, use it to determine the
3918 target path name. */
Steve Dower2ea51c92015-03-20 21:49:12 -07003919 buf_size = GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
Brian Curtind40e6f72010-07-08 21:39:08 +00003920
3921 if(!buf_size)
Larry Hastings2f936352014-08-05 14:04:04 +10003922 return win32_error_object("GetFinalPathNameByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003923
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02003924 target_path = PyMem_New(wchar_t, buf_size+1);
Brian Curtind40e6f72010-07-08 21:39:08 +00003925 if(!target_path)
3926 return PyErr_NoMemory();
3927
Steve Dower2ea51c92015-03-20 21:49:12 -07003928 result_length = GetFinalPathNameByHandleW(hFile, target_path,
3929 buf_size, VOLUME_NAME_DOS);
Brian Curtind40e6f72010-07-08 21:39:08 +00003930 if(!result_length)
Larry Hastings2f936352014-08-05 14:04:04 +10003931 return win32_error_object("GetFinalPathNamyByHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003932
3933 if(!CloseHandle(hFile))
Larry Hastings2f936352014-08-05 14:04:04 +10003934 return win32_error_object("CloseHandle", path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003935
3936 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003937 result = PyUnicode_FromWideChar(target_path, result_length);
Victor Stinnerb6404912013-07-07 16:21:41 +02003938 PyMem_Free(target_path);
Brian Curtind40e6f72010-07-08 21:39:08 +00003939 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10003940}
Brian Curtin62857742010-09-06 17:07:27 +00003941
Brian Curtin95d028f2011-06-09 09:10:38 -05003942PyDoc_STRVAR(posix__isdir__doc__,
3943"Return true if the pathname refers to an existing directory.");
3944
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003945/*[clinic input]
3946os._isdir
3947
3948 path: path_t
3949 /
3950
3951[clinic start generated code]*/
3952
Brian Curtin9c669cc2011-06-08 18:17:18 -05003953static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003954os__isdir_impl(PyObject *module, path_t *path)
3955/*[clinic end generated code: output=75f56f32720836cb input=e794f12faab62a2a]*/
Brian Curtin9c669cc2011-06-08 18:17:18 -05003956{
Brian Curtin9c669cc2011-06-08 18:17:18 -05003957 DWORD attributes;
3958
Steve Dowerb22a6772016-07-17 20:49:38 -07003959 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003960 if (!path->narrow)
3961 attributes = GetFileAttributesW(path->wide);
3962 else
3963 attributes = GetFileAttributesA(path->narrow);
Steve Dowerb22a6772016-07-17 20:49:38 -07003964 Py_END_ALLOW_THREADS
Brian Curtin9c669cc2011-06-08 18:17:18 -05003965
Brian Curtin9c669cc2011-06-08 18:17:18 -05003966 if (attributes == INVALID_FILE_ATTRIBUTES)
3967 Py_RETURN_FALSE;
3968
Brian Curtin9c669cc2011-06-08 18:17:18 -05003969 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3970 Py_RETURN_TRUE;
3971 else
3972 Py_RETURN_FALSE;
3973}
Tim Golden6b528062013-08-01 12:44:00 +01003974
Tim Golden6b528062013-08-01 12:44:00 +01003975
Larry Hastings2f936352014-08-05 14:04:04 +10003976/*[clinic input]
3977os._getvolumepathname
3978
3979 path: unicode
3980
3981A helper function for ismount on Win32.
3982[clinic start generated code]*/
3983
Larry Hastings2f936352014-08-05 14:04:04 +10003984static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003985os__getvolumepathname_impl(PyObject *module, PyObject *path)
3986/*[clinic end generated code: output=cbdcbd1059ceef4c input=7eacadc40acbda6b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003987{
3988 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003989 const wchar_t *path_wchar;
3990 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003991 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01003992 BOOL ret;
3993
Larry Hastings2f936352014-08-05 14:04:04 +10003994 path_wchar = PyUnicode_AsUnicodeAndSize(path, &buflen);
3995 if (path_wchar == NULL)
Tim Golden6b528062013-08-01 12:44:00 +01003996 return NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01003997 buflen += 1;
Tim Golden6b528062013-08-01 12:44:00 +01003998
3999 /* Volume path should be shorter than entire path */
Victor Stinner6edddfa2013-11-24 19:22:57 +01004000 buflen = Py_MAX(buflen, MAX_PATH);
4001
4002 if (buflen > DWORD_MAX) {
4003 PyErr_SetString(PyExc_OverflowError, "path too long");
4004 return NULL;
4005 }
4006
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004007 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004008 if (mountpath == NULL)
4009 return PyErr_NoMemory();
4010
4011 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004012 ret = GetVolumePathNameW(path_wchar, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004013 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004014 Py_END_ALLOW_THREADS
4015
4016 if (!ret) {
Larry Hastings2f936352014-08-05 14:04:04 +10004017 result = win32_error_object("_getvolumepathname", path);
Tim Golden6b528062013-08-01 12:44:00 +01004018 goto exit;
4019 }
4020 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
4021
4022exit:
4023 PyMem_Free(mountpath);
4024 return result;
4025}
Tim Golden6b528062013-08-01 12:44:00 +01004026
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004027#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004028
Larry Hastings2f936352014-08-05 14:04:04 +10004029
4030/*[clinic input]
4031os.mkdir
4032
4033 path : path_t
4034
4035 mode: int = 0o777
4036
4037 *
4038
4039 dir_fd : dir_fd(requires='mkdirat') = None
4040
4041# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4042
4043Create a directory.
4044
4045If dir_fd is not None, it should be a file descriptor open to a directory,
4046 and path should be relative; path will then be relative to that directory.
4047dir_fd may not be implemented on your platform.
4048 If it is unavailable, using it will raise a NotImplementedError.
4049
4050The mode argument is ignored on Windows.
4051[clinic start generated code]*/
4052
Larry Hastings2f936352014-08-05 14:04:04 +10004053static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004054os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4055/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004056{
4057 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004058
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004059#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004060 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004061 if (path->wide)
4062 result = CreateDirectoryW(path->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004063 else
Larry Hastings2f936352014-08-05 14:04:04 +10004064 result = CreateDirectoryA(path->narrow, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004065 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004066
Larry Hastings2f936352014-08-05 14:04:04 +10004067 if (!result)
4068 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004069#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004070 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004071#if HAVE_MKDIRAT
4072 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004073 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004074 else
4075#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00004076#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004077 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004078#else
Larry Hastings2f936352014-08-05 14:04:04 +10004079 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004080#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004081 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004082 if (result < 0)
4083 return path_error(path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00004084#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004085 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004086}
4087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004088
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004089/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4090#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004091#include <sys/resource.h>
4092#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004093
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004094
4095#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004096/*[clinic input]
4097os.nice
4098
4099 increment: int
4100 /
4101
4102Add increment to the priority of process and return the new priority.
4103[clinic start generated code]*/
4104
Larry Hastings2f936352014-08-05 14:04:04 +10004105static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004106os_nice_impl(PyObject *module, int increment)
4107/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004108{
4109 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004110
Victor Stinner8c62be82010-05-06 00:08:46 +00004111 /* There are two flavours of 'nice': one that returns the new
4112 priority (as required by almost all standards out there) and the
4113 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
4114 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004115
Victor Stinner8c62be82010-05-06 00:08:46 +00004116 If we are of the nice family that returns the new priority, we
4117 need to clear errno before the call, and check if errno is filled
4118 before calling posix_error() on a returnvalue of -1, because the
4119 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004120
Victor Stinner8c62be82010-05-06 00:08:46 +00004121 errno = 0;
4122 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004123#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004124 if (value == 0)
4125 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004126#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004127 if (value == -1 && errno != 0)
4128 /* either nice() or getpriority() returned an error */
4129 return posix_error();
4130 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004131}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004132#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004133
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004134
4135#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004136/*[clinic input]
4137os.getpriority
4138
4139 which: int
4140 who: int
4141
4142Return program scheduling priority.
4143[clinic start generated code]*/
4144
Larry Hastings2f936352014-08-05 14:04:04 +10004145static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004146os_getpriority_impl(PyObject *module, int which, int who)
4147/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004148{
4149 int retval;
4150
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004151 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004152 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004153 if (errno != 0)
4154 return posix_error();
4155 return PyLong_FromLong((long)retval);
4156}
4157#endif /* HAVE_GETPRIORITY */
4158
4159
4160#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004161/*[clinic input]
4162os.setpriority
4163
4164 which: int
4165 who: int
4166 priority: int
4167
4168Set program scheduling priority.
4169[clinic start generated code]*/
4170
Larry Hastings2f936352014-08-05 14:04:04 +10004171static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004172os_setpriority_impl(PyObject *module, int which, int who, int priority)
4173/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004174{
4175 int retval;
4176
4177 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004178 if (retval == -1)
4179 return posix_error();
4180 Py_RETURN_NONE;
4181}
4182#endif /* HAVE_SETPRIORITY */
4183
4184
Barry Warsaw53699e91996-12-10 23:23:01 +00004185static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004186internal_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 +00004187{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004188 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004189 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004190
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004191#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004192 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004193 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004194#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004195 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004196#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004197
Larry Hastings9cf065c2012-06-22 16:30:09 -07004198 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4199 (dst_dir_fd != DEFAULT_DIR_FD);
4200#ifndef HAVE_RENAMEAT
4201 if (dir_fd_specified) {
4202 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004203 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004204 }
4205#endif
4206
Larry Hastings2f936352014-08-05 14:04:04 +10004207 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004208 PyErr_Format(PyExc_ValueError,
4209 "%s: src and dst must be the same type", function_name);
Larry Hastings2f936352014-08-05 14:04:04 +10004210 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004211 }
4212
4213#ifdef MS_WINDOWS
4214 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004215 if (src->wide)
4216 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004217 else
Larry Hastings2f936352014-08-05 14:04:04 +10004218 result = MoveFileExA(src->narrow, dst->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004219 Py_END_ALLOW_THREADS
4220
Larry Hastings2f936352014-08-05 14:04:04 +10004221 if (!result)
4222 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004223
4224#else
4225 Py_BEGIN_ALLOW_THREADS
4226#ifdef HAVE_RENAMEAT
4227 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004228 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004229 else
4230#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004231 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004232 Py_END_ALLOW_THREADS
4233
Larry Hastings2f936352014-08-05 14:04:04 +10004234 if (result)
4235 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004236#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004237 Py_RETURN_NONE;
4238}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004239
Larry Hastings2f936352014-08-05 14:04:04 +10004240
4241/*[clinic input]
4242os.rename
4243
4244 src : path_t
4245 dst : path_t
4246 *
4247 src_dir_fd : dir_fd = None
4248 dst_dir_fd : dir_fd = None
4249
4250Rename a file or directory.
4251
4252If either src_dir_fd or dst_dir_fd is not None, it should be a file
4253 descriptor open to a directory, and the respective path string (src or dst)
4254 should be relative; the path will then be relative to that directory.
4255src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4256 If they are unavailable, using them will raise a NotImplementedError.
4257[clinic start generated code]*/
4258
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004260os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004261 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004262/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004263{
Larry Hastings2f936352014-08-05 14:04:04 +10004264 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004265}
4266
Larry Hastings2f936352014-08-05 14:04:04 +10004267
4268/*[clinic input]
4269os.replace = os.rename
4270
4271Rename a file or directory, overwriting the destination.
4272
4273If either src_dir_fd or dst_dir_fd is not None, it should be a file
4274 descriptor open to a directory, and the respective path string (src or dst)
4275 should be relative; the path will then be relative to that directory.
4276src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4277 If they are unavailable, using them will raise a NotImplementedError."
4278[clinic start generated code]*/
4279
Larry Hastings2f936352014-08-05 14:04:04 +10004280static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004281os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4282 int dst_dir_fd)
4283/*[clinic end generated code: output=1968c02e7857422b input=25515dfb107c8421]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004284{
4285 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4286}
4287
4288
4289/*[clinic input]
4290os.rmdir
4291
4292 path: path_t
4293 *
4294 dir_fd: dir_fd(requires='unlinkat') = None
4295
4296Remove a directory.
4297
4298If dir_fd is not None, it should be a file descriptor open to a directory,
4299 and path should be relative; path will then be relative to that directory.
4300dir_fd may not be implemented on your platform.
4301 If it is unavailable, using it will raise a NotImplementedError.
4302[clinic start generated code]*/
4303
Larry Hastings2f936352014-08-05 14:04:04 +10004304static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004305os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4306/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004307{
4308 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004309
4310 Py_BEGIN_ALLOW_THREADS
4311#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10004312 if (path->wide)
4313 result = RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004314 else
Larry Hastings2f936352014-08-05 14:04:04 +10004315 result = RemoveDirectoryA(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004316 result = !result; /* Windows, success=1, UNIX, success=0 */
4317#else
4318#ifdef HAVE_UNLINKAT
4319 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004320 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004321 else
4322#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004323 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004324#endif
4325 Py_END_ALLOW_THREADS
4326
Larry Hastings2f936352014-08-05 14:04:04 +10004327 if (result)
4328 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004329
Larry Hastings2f936352014-08-05 14:04:04 +10004330 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004331}
4332
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004333
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004334#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004335#ifdef MS_WINDOWS
4336/*[clinic input]
4337os.system -> long
4338
4339 command: Py_UNICODE
4340
4341Execute the command in a subshell.
4342[clinic start generated code]*/
4343
Larry Hastings2f936352014-08-05 14:04:04 +10004344static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004345os_system_impl(PyObject *module, Py_UNICODE *command)
4346/*[clinic end generated code: output=96c4dffee36dfb48 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004347{
4348 long result;
Victor Stinner8c62be82010-05-06 00:08:46 +00004349 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004350 result = _wsystem(command);
Victor Stinner8c62be82010-05-06 00:08:46 +00004351 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004352 return result;
4353}
4354#else /* MS_WINDOWS */
4355/*[clinic input]
4356os.system -> long
4357
4358 command: FSConverter
4359
4360Execute the command in a subshell.
4361[clinic start generated code]*/
4362
Larry Hastings2f936352014-08-05 14:04:04 +10004363static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004364os_system_impl(PyObject *module, PyObject *command)
4365/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004366{
4367 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004368 const char *bytes = PyBytes_AsString(command);
Larry Hastings2f936352014-08-05 14:04:04 +10004369 Py_BEGIN_ALLOW_THREADS
4370 result = system(bytes);
4371 Py_END_ALLOW_THREADS
4372 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004373}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004374#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004375#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004377
Larry Hastings2f936352014-08-05 14:04:04 +10004378/*[clinic input]
4379os.umask
4380
4381 mask: int
4382 /
4383
4384Set the current numeric umask and return the previous umask.
4385[clinic start generated code]*/
4386
Larry Hastings2f936352014-08-05 14:04:04 +10004387static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004388os_umask_impl(PyObject *module, int mask)
4389/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004390{
4391 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004392 if (i < 0)
4393 return posix_error();
4394 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004395}
4396
Brian Curtind40e6f72010-07-08 21:39:08 +00004397#ifdef MS_WINDOWS
4398
4399/* override the default DeleteFileW behavior so that directory
4400symlinks can be removed with this function, the same as with
4401Unix symlinks */
4402BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4403{
4404 WIN32_FILE_ATTRIBUTE_DATA info;
4405 WIN32_FIND_DATAW find_data;
4406 HANDLE find_data_handle;
4407 int is_directory = 0;
4408 int is_link = 0;
4409
4410 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4411 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004412
Brian Curtind40e6f72010-07-08 21:39:08 +00004413 /* Get WIN32_FIND_DATA structure for the path to determine if
4414 it is a symlink */
4415 if(is_directory &&
4416 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4417 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4418
4419 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004420 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4421 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4422 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4423 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004424 FindClose(find_data_handle);
4425 }
4426 }
4427 }
4428
4429 if (is_directory && is_link)
4430 return RemoveDirectoryW(lpFileName);
4431
4432 return DeleteFileW(lpFileName);
4433}
4434#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004435
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004436
Larry Hastings2f936352014-08-05 14:04:04 +10004437/*[clinic input]
4438os.unlink
4439
4440 path: path_t
4441 *
4442 dir_fd: dir_fd(requires='unlinkat')=None
4443
4444Remove a file (same as remove()).
4445
4446If dir_fd is not None, it should be a file descriptor open to a directory,
4447 and path should be relative; path will then be relative to that directory.
4448dir_fd may not be implemented on your platform.
4449 If it is unavailable, using it will raise a NotImplementedError.
4450
4451[clinic start generated code]*/
4452
Larry Hastings2f936352014-08-05 14:04:04 +10004453static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004454os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4455/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004456{
4457 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004458
4459 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004460 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004461#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10004462 if (path->wide)
4463 result = Py_DeleteFileW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004464 else
Larry Hastings2f936352014-08-05 14:04:04 +10004465 result = DeleteFileA(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004466 result = !result; /* Windows, success=1, UNIX, success=0 */
4467#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004468#ifdef HAVE_UNLINKAT
4469 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004470 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004471 else
4472#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004473 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004474#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004475 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004476 Py_END_ALLOW_THREADS
4477
Larry Hastings2f936352014-08-05 14:04:04 +10004478 if (result)
4479 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004480
Larry Hastings2f936352014-08-05 14:04:04 +10004481 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004482}
4483
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004484
Larry Hastings2f936352014-08-05 14:04:04 +10004485/*[clinic input]
4486os.remove = os.unlink
4487
4488Remove a file (same as unlink()).
4489
4490If dir_fd is not None, it should be a file descriptor open to a directory,
4491 and path should be relative; path will then be relative to that directory.
4492dir_fd may not be implemented on your platform.
4493 If it is unavailable, using it will raise a NotImplementedError.
4494[clinic start generated code]*/
4495
Larry Hastings2f936352014-08-05 14:04:04 +10004496static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004497os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4498/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004499{
4500 return os_unlink_impl(module, path, dir_fd);
4501}
4502
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004503
Larry Hastings605a62d2012-06-24 04:33:36 -07004504static PyStructSequence_Field uname_result_fields[] = {
4505 {"sysname", "operating system name"},
4506 {"nodename", "name of machine on network (implementation-defined)"},
4507 {"release", "operating system release"},
4508 {"version", "operating system version"},
4509 {"machine", "hardware identifier"},
4510 {NULL}
4511};
4512
4513PyDoc_STRVAR(uname_result__doc__,
4514"uname_result: Result from os.uname().\n\n\
4515This object may be accessed either as a tuple of\n\
4516 (sysname, nodename, release, version, machine),\n\
4517or via the attributes sysname, nodename, release, version, and machine.\n\
4518\n\
4519See os.uname for more information.");
4520
4521static PyStructSequence_Desc uname_result_desc = {
4522 "uname_result", /* name */
4523 uname_result__doc__, /* doc */
4524 uname_result_fields,
4525 5
4526};
4527
4528static PyTypeObject UnameResultType;
4529
4530
4531#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004532/*[clinic input]
4533os.uname
4534
4535Return an object identifying the current operating system.
4536
4537The object behaves like a named tuple with the following fields:
4538 (sysname, nodename, release, version, machine)
4539
4540[clinic start generated code]*/
4541
Larry Hastings2f936352014-08-05 14:04:04 +10004542static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004543os_uname_impl(PyObject *module)
4544/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004545{
Victor Stinner8c62be82010-05-06 00:08:46 +00004546 struct utsname u;
4547 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004548 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004549
Victor Stinner8c62be82010-05-06 00:08:46 +00004550 Py_BEGIN_ALLOW_THREADS
4551 res = uname(&u);
4552 Py_END_ALLOW_THREADS
4553 if (res < 0)
4554 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004555
4556 value = PyStructSequence_New(&UnameResultType);
4557 if (value == NULL)
4558 return NULL;
4559
4560#define SET(i, field) \
4561 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004562 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004563 if (!o) { \
4564 Py_DECREF(value); \
4565 return NULL; \
4566 } \
4567 PyStructSequence_SET_ITEM(value, i, o); \
4568 } \
4569
4570 SET(0, u.sysname);
4571 SET(1, u.nodename);
4572 SET(2, u.release);
4573 SET(3, u.version);
4574 SET(4, u.machine);
4575
4576#undef SET
4577
4578 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004579}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004580#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004581
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004582
Larry Hastings9cf065c2012-06-22 16:30:09 -07004583
4584typedef struct {
4585 int now;
4586 time_t atime_s;
4587 long atime_ns;
4588 time_t mtime_s;
4589 long mtime_ns;
4590} utime_t;
4591
4592/*
Victor Stinner484df002014-10-09 13:52:31 +02004593 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004594 * they also intentionally leak the declaration of a pointer named "time"
4595 */
4596#define UTIME_TO_TIMESPEC \
4597 struct timespec ts[2]; \
4598 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004599 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004600 time = NULL; \
4601 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004602 ts[0].tv_sec = ut->atime_s; \
4603 ts[0].tv_nsec = ut->atime_ns; \
4604 ts[1].tv_sec = ut->mtime_s; \
4605 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004606 time = ts; \
4607 } \
4608
4609#define UTIME_TO_TIMEVAL \
4610 struct timeval tv[2]; \
4611 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004612 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004613 time = NULL; \
4614 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004615 tv[0].tv_sec = ut->atime_s; \
4616 tv[0].tv_usec = ut->atime_ns / 1000; \
4617 tv[1].tv_sec = ut->mtime_s; \
4618 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004619 time = tv; \
4620 } \
4621
4622#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004623 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004624 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004625 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004626 time = NULL; \
4627 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004628 u.actime = ut->atime_s; \
4629 u.modtime = ut->mtime_s; \
4630 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004631 }
4632
4633#define UTIME_TO_TIME_T \
4634 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004635 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004636 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004637 time = NULL; \
4638 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004639 timet[0] = ut->atime_s; \
4640 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004641 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004642 } \
4643
4644
Victor Stinner528a9ab2015-09-03 21:30:26 +02004645#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004646
4647static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004648utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004649{
4650#ifdef HAVE_UTIMENSAT
4651 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4652 UTIME_TO_TIMESPEC;
4653 return utimensat(dir_fd, path, time, flags);
4654#elif defined(HAVE_FUTIMESAT)
4655 UTIME_TO_TIMEVAL;
4656 /*
4657 * follow_symlinks will never be false here;
4658 * we only allow !follow_symlinks and dir_fd together
4659 * if we have utimensat()
4660 */
4661 assert(follow_symlinks);
4662 return futimesat(dir_fd, path, time);
4663#endif
4664}
4665
Larry Hastings2f936352014-08-05 14:04:04 +10004666 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4667#else
4668 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004669#endif
4670
Victor Stinner528a9ab2015-09-03 21:30:26 +02004671#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004672
4673static int
Victor Stinner484df002014-10-09 13:52:31 +02004674utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004675{
4676#ifdef HAVE_FUTIMENS
4677 UTIME_TO_TIMESPEC;
4678 return futimens(fd, time);
4679#else
4680 UTIME_TO_TIMEVAL;
4681 return futimes(fd, time);
4682#endif
4683}
4684
Larry Hastings2f936352014-08-05 14:04:04 +10004685 #define PATH_UTIME_HAVE_FD 1
4686#else
4687 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004688#endif
4689
Victor Stinner5ebae872015-09-22 01:29:33 +02004690#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4691# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4692#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004693
Victor Stinner4552ced2015-09-21 22:37:15 +02004694#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004695
4696static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004697utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004698{
4699#ifdef HAVE_UTIMENSAT
4700 UTIME_TO_TIMESPEC;
4701 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4702#else
4703 UTIME_TO_TIMEVAL;
4704 return lutimes(path, time);
4705#endif
4706}
4707
4708#endif
4709
4710#ifndef MS_WINDOWS
4711
4712static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004713utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004714{
4715#ifdef HAVE_UTIMENSAT
4716 UTIME_TO_TIMESPEC;
4717 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4718#elif defined(HAVE_UTIMES)
4719 UTIME_TO_TIMEVAL;
4720 return utimes(path, time);
4721#elif defined(HAVE_UTIME_H)
4722 UTIME_TO_UTIMBUF;
4723 return utime(path, time);
4724#else
4725 UTIME_TO_TIME_T;
4726 return utime(path, time);
4727#endif
4728}
4729
4730#endif
4731
Larry Hastings76ad59b2012-05-03 00:30:07 -07004732static int
4733split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4734{
4735 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004736 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004737 divmod = PyNumber_Divmod(py_long, billion);
4738 if (!divmod)
4739 goto exit;
4740 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4741 if ((*s == -1) && PyErr_Occurred())
4742 goto exit;
4743 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004744 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004745 goto exit;
4746
4747 result = 1;
4748exit:
4749 Py_XDECREF(divmod);
4750 return result;
4751}
4752
Larry Hastings2f936352014-08-05 14:04:04 +10004753
4754/*[clinic input]
4755os.utime
4756
4757 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
4758 times: object = NULL
4759 *
4760 ns: object = NULL
4761 dir_fd: dir_fd(requires='futimensat') = None
4762 follow_symlinks: bool=True
4763
Martin Panter0ff89092015-09-09 01:56:53 +00004764# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004765
4766Set the access and modified time of path.
4767
4768path may always be specified as a string.
4769On some platforms, path may also be specified as an open file descriptor.
4770 If this functionality is unavailable, using it raises an exception.
4771
4772If times is not None, it must be a tuple (atime, mtime);
4773 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004774If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004775 atime_ns and mtime_ns should be expressed as integer nanoseconds
4776 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004777If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004778Specifying tuples for both times and ns is an error.
4779
4780If dir_fd is not None, it should be a file descriptor open to a directory,
4781 and path should be relative; path will then be relative to that directory.
4782If follow_symlinks is False, and the last element of the path is a symbolic
4783 link, utime will modify the symbolic link itself instead of the file the
4784 link points to.
4785It is an error to use dir_fd or follow_symlinks when specifying path
4786 as an open file descriptor.
4787dir_fd and follow_symlinks may not be available on your platform.
4788 If they are unavailable, using them will raise a NotImplementedError.
4789
4790[clinic start generated code]*/
4791
Larry Hastings2f936352014-08-05 14:04:04 +10004792static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004793os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4794 int dir_fd, int follow_symlinks)
4795/*[clinic end generated code: output=cfcac69d027b82cf input=081cdc54ca685385]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004796{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797#ifdef MS_WINDOWS
4798 HANDLE hFile;
4799 FILETIME atime, mtime;
4800#else
4801 int result;
4802#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004803
Larry Hastings9cf065c2012-06-22 16:30:09 -07004804 PyObject *return_value = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10004805 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004806
Christian Heimesb3c87242013-08-01 00:08:16 +02004807 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004808
Larry Hastings9cf065c2012-06-22 16:30:09 -07004809 if (times && (times != Py_None) && ns) {
4810 PyErr_SetString(PyExc_ValueError,
4811 "utime: you may specify either 'times'"
4812 " or 'ns' but not both");
4813 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004814 }
4815
4816 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004817 time_t a_sec, m_sec;
4818 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004819 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004820 PyErr_SetString(PyExc_TypeError,
4821 "utime: 'times' must be either"
4822 " a tuple of two ints or None");
4823 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004824 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004825 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004826 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004827 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004828 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004829 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004830 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004831 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004832 utime.atime_s = a_sec;
4833 utime.atime_ns = a_nsec;
4834 utime.mtime_s = m_sec;
4835 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004836 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004837 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004838 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004839 PyErr_SetString(PyExc_TypeError,
4840 "utime: 'ns' must be a tuple of two ints");
4841 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004842 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004843 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004844 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004845 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004846 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004847 &utime.mtime_s, &utime.mtime_ns)) {
4848 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004849 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004850 }
4851 else {
4852 /* times and ns are both None/unspecified. use "now". */
4853 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004854 }
4855
Victor Stinner4552ced2015-09-21 22:37:15 +02004856#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004857 if (follow_symlinks_specified("utime", follow_symlinks))
4858 goto exit;
4859#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004860
Larry Hastings2f936352014-08-05 14:04:04 +10004861 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4862 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4863 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004864 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004865
Larry Hastings9cf065c2012-06-22 16:30:09 -07004866#if !defined(HAVE_UTIMENSAT)
4867 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004868 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004869 "utime: cannot use dir_fd and follow_symlinks "
4870 "together on this platform");
4871 goto exit;
4872 }
4873#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004874
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004875#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004876 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004877 if (path->wide)
4878 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 NULL, OPEN_EXISTING,
4880 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004881 else
Larry Hastings2f936352014-08-05 14:04:04 +10004882 hFile = CreateFileA(path->narrow, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004883 NULL, OPEN_EXISTING,
4884 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004885 Py_END_ALLOW_THREADS
4886 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10004887 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004888 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004889 }
4890
Larry Hastings9cf065c2012-06-22 16:30:09 -07004891 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01004892 GetSystemTimeAsFileTime(&mtime);
4893 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00004894 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004895 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08004896 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4897 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004898 }
4899 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4900 /* Avoid putting the file name into the error here,
4901 as that may confuse the user into believing that
4902 something is wrong with the file, when it also
4903 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004904 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004905 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004906 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004907#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004908 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004909
Victor Stinner4552ced2015-09-21 22:37:15 +02004910#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004911 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10004912 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004913 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004914#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004915
Victor Stinner528a9ab2015-09-03 21:30:26 +02004916#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004917 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10004918 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004919 else
4920#endif
4921
Victor Stinner528a9ab2015-09-03 21:30:26 +02004922#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10004923 if (path->fd != -1)
4924 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004925 else
4926#endif
4927
Larry Hastings2f936352014-08-05 14:04:04 +10004928 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004929
4930 Py_END_ALLOW_THREADS
4931
4932 if (result < 0) {
4933 /* see previous comment about not putting filename in error here */
4934 return_value = posix_error();
4935 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004936 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004937
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004938#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004939
4940 Py_INCREF(Py_None);
4941 return_value = Py_None;
4942
4943exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004944#ifdef MS_WINDOWS
4945 if (hFile != INVALID_HANDLE_VALUE)
4946 CloseHandle(hFile);
4947#endif
4948 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004949}
4950
Guido van Rossum3b066191991-06-04 19:40:25 +00004951/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004952
Larry Hastings2f936352014-08-05 14:04:04 +10004953
4954/*[clinic input]
4955os._exit
4956
4957 status: int
4958
4959Exit to the system with specified status, without normal exit processing.
4960[clinic start generated code]*/
4961
Larry Hastings2f936352014-08-05 14:04:04 +10004962static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004963os__exit_impl(PyObject *module, int status)
4964/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004965{
4966 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00004967 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004968}
4969
Martin v. Löwis114619e2002-10-07 06:44:21 +00004970#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4971static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00004972free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004973{
Victor Stinner8c62be82010-05-06 00:08:46 +00004974 Py_ssize_t i;
4975 for (i = 0; i < count; i++)
4976 PyMem_Free(array[i]);
4977 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004978}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004979
Antoine Pitrou69f71142009-05-24 21:25:49 +00004980static
Martin v. Löwis011e8422009-05-05 04:43:17 +00004981int fsconvert_strdup(PyObject *o, char**out)
4982{
Victor Stinner8c62be82010-05-06 00:08:46 +00004983 PyObject *bytes;
4984 Py_ssize_t size;
4985 if (!PyUnicode_FSConverter(o, &bytes))
4986 return 0;
4987 size = PyBytes_GET_SIZE(bytes);
4988 *out = PyMem_Malloc(size+1);
Victor Stinner50abf222013-11-07 23:56:10 +01004989 if (!*out) {
4990 PyErr_NoMemory();
Victor Stinner8c62be82010-05-06 00:08:46 +00004991 return 0;
Victor Stinner50abf222013-11-07 23:56:10 +01004992 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004993 memcpy(*out, PyBytes_AsString(bytes), size+1);
4994 Py_DECREF(bytes);
4995 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004996}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004997#endif
4998
Ross Lagerwall7807c352011-03-17 20:20:30 +02004999#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00005000static char**
5001parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5002{
Victor Stinner8c62be82010-05-06 00:08:46 +00005003 char **envlist;
5004 Py_ssize_t i, pos, envc;
5005 PyObject *keys=NULL, *vals=NULL;
5006 PyObject *key, *val, *key2, *val2;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03005007 char *p;
5008 const char *k, *v;
Victor Stinner8c62be82010-05-06 00:08:46 +00005009 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005010
Victor Stinner8c62be82010-05-06 00:08:46 +00005011 i = PyMapping_Size(env);
5012 if (i < 0)
5013 return NULL;
5014 envlist = PyMem_NEW(char *, i + 1);
5015 if (envlist == NULL) {
5016 PyErr_NoMemory();
5017 return NULL;
5018 }
5019 envc = 0;
5020 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005021 if (!keys)
5022 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005023 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005024 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005025 goto error;
5026 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5027 PyErr_Format(PyExc_TypeError,
5028 "env.keys() or env.values() is not a list");
5029 goto error;
5030 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005031
Victor Stinner8c62be82010-05-06 00:08:46 +00005032 for (pos = 0; pos < i; pos++) {
5033 key = PyList_GetItem(keys, pos);
5034 val = PyList_GetItem(vals, pos);
5035 if (!key || !val)
5036 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005037
Victor Stinner8c62be82010-05-06 00:08:46 +00005038 if (PyUnicode_FSConverter(key, &key2) == 0)
5039 goto error;
5040 if (PyUnicode_FSConverter(val, &val2) == 0) {
5041 Py_DECREF(key2);
5042 goto error;
5043 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005044
Victor Stinner8c62be82010-05-06 00:08:46 +00005045 k = PyBytes_AsString(key2);
5046 v = PyBytes_AsString(val2);
5047 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005048
Victor Stinner8c62be82010-05-06 00:08:46 +00005049 p = PyMem_NEW(char, len);
5050 if (p == NULL) {
5051 PyErr_NoMemory();
5052 Py_DECREF(key2);
5053 Py_DECREF(val2);
5054 goto error;
5055 }
5056 PyOS_snprintf(p, len, "%s=%s", k, v);
5057 envlist[envc++] = p;
5058 Py_DECREF(key2);
5059 Py_DECREF(val2);
Victor Stinner8c62be82010-05-06 00:08:46 +00005060 }
5061 Py_DECREF(vals);
5062 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005063
Victor Stinner8c62be82010-05-06 00:08:46 +00005064 envlist[envc] = 0;
5065 *envc_ptr = envc;
5066 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005067
5068error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005069 Py_XDECREF(keys);
5070 Py_XDECREF(vals);
5071 while (--envc >= 0)
5072 PyMem_DEL(envlist[envc]);
5073 PyMem_DEL(envlist);
5074 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005075}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005076
Ross Lagerwall7807c352011-03-17 20:20:30 +02005077static char**
5078parse_arglist(PyObject* argv, Py_ssize_t *argc)
5079{
5080 int i;
5081 char **argvlist = PyMem_NEW(char *, *argc+1);
5082 if (argvlist == NULL) {
5083 PyErr_NoMemory();
5084 return NULL;
5085 }
5086 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005087 PyObject* item = PySequence_ITEM(argv, i);
5088 if (item == NULL)
5089 goto fail;
5090 if (!fsconvert_strdup(item, &argvlist[i])) {
5091 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005092 goto fail;
5093 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005094 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005095 }
5096 argvlist[*argc] = NULL;
5097 return argvlist;
5098fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005099 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005100 free_string_array(argvlist, *argc);
5101 return NULL;
5102}
5103#endif
5104
Larry Hastings2f936352014-08-05 14:04:04 +10005105
Ross Lagerwall7807c352011-03-17 20:20:30 +02005106#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005107/*[clinic input]
5108os.execv
5109
5110 path: FSConverter
5111 Path of executable file.
5112 argv: object
5113 Tuple or list of strings.
5114 /
5115
5116Execute an executable path with arguments, replacing current process.
5117[clinic start generated code]*/
5118
Larry Hastings2f936352014-08-05 14:04:04 +10005119static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005120os_execv_impl(PyObject *module, PyObject *path, PyObject *argv)
5121/*[clinic end generated code: output=b21dc34deeb5b004 input=96041559925e5229]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005122{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03005123 const char *path_char;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005124 char **argvlist;
5125 Py_ssize_t argc;
5126
5127 /* execv has two arguments: (path, argv), where
5128 argv is a list or tuple of strings. */
5129
Larry Hastings2f936352014-08-05 14:04:04 +10005130 path_char = PyBytes_AsString(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005131 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5132 PyErr_SetString(PyExc_TypeError,
5133 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005134 return NULL;
5135 }
5136 argc = PySequence_Size(argv);
5137 if (argc < 1) {
5138 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005139 return NULL;
5140 }
5141
5142 argvlist = parse_arglist(argv, &argc);
5143 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005144 return NULL;
5145 }
5146
Larry Hastings2f936352014-08-05 14:04:04 +10005147 execv(path_char, argvlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005148
5149 /* If we get here it's definitely an error */
5150
5151 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005152 return posix_error();
5153}
5154
Larry Hastings2f936352014-08-05 14:04:04 +10005155
5156/*[clinic input]
5157os.execve
5158
5159 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5160 Path of executable file.
5161 argv: object
5162 Tuple or list of strings.
5163 env: object
5164 Dictionary of strings mapping to strings.
5165
5166Execute an executable path with arguments, replacing current process.
5167[clinic start generated code]*/
5168
Larry Hastings2f936352014-08-05 14:04:04 +10005169static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005170os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5171/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005172{
Larry Hastings9cf065c2012-06-22 16:30:09 -07005173 char **argvlist = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005174 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005175 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005176
Victor Stinner8c62be82010-05-06 00:08:46 +00005177 /* execve has three arguments: (path, argv, env), where
5178 argv is a list or tuple of strings and env is a dictionary
5179 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005180
Ross Lagerwall7807c352011-03-17 20:20:30 +02005181 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005182 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005183 "execve: argv must be a tuple or list");
5184 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005185 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005186 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00005187 if (!PyMapping_Check(env)) {
5188 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005189 "execve: environment must be a mapping object");
5190 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005191 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005192
Ross Lagerwall7807c352011-03-17 20:20:30 +02005193 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005194 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005195 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00005196 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005197
Victor Stinner8c62be82010-05-06 00:08:46 +00005198 envlist = parse_envlist(env, &envc);
5199 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02005200 goto fail;
5201
Larry Hastings9cf065c2012-06-22 16:30:09 -07005202#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005203 if (path->fd > -1)
5204 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005205 else
5206#endif
Larry Hastings2f936352014-08-05 14:04:04 +10005207 execve(path->narrow, argvlist, envlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005208
5209 /* If we get here it's definitely an error */
5210
Larry Hastings2f936352014-08-05 14:04:04 +10005211 path_error(path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005212
5213 while (--envc >= 0)
5214 PyMem_DEL(envlist[envc]);
5215 PyMem_DEL(envlist);
5216 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005217 if (argvlist)
5218 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005219 return NULL;
5220}
Larry Hastings9cf065c2012-06-22 16:30:09 -07005221#endif /* HAVE_EXECV */
5222
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005223
Guido van Rossuma1065681999-01-25 23:20:23 +00005224#ifdef HAVE_SPAWNV
Larry Hastings2f936352014-08-05 14:04:04 +10005225/*[clinic input]
5226os.spawnv
5227
5228 mode: int
5229 Mode of process creation.
5230 path: FSConverter
5231 Path of executable file.
5232 argv: object
5233 Tuple or list of strings.
5234 /
5235
5236Execute the program specified by path in a new process.
5237[clinic start generated code]*/
5238
Larry Hastings2f936352014-08-05 14:04:04 +10005239static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005240os_spawnv_impl(PyObject *module, int mode, PyObject *path, PyObject *argv)
5241/*[clinic end generated code: output=c427c0ce40f10638 input=042c91dfc1e6debc]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005242{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03005243 const char *path_char;
Victor Stinner8c62be82010-05-06 00:08:46 +00005244 char **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005245 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005246 Py_ssize_t argc;
5247 Py_intptr_t spawnval;
5248 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005249
Victor Stinner8c62be82010-05-06 00:08:46 +00005250 /* spawnv has three arguments: (mode, path, argv), where
5251 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005252
Larry Hastings2f936352014-08-05 14:04:04 +10005253 path_char = PyBytes_AsString(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00005254 if (PyList_Check(argv)) {
5255 argc = PyList_Size(argv);
5256 getitem = PyList_GetItem;
5257 }
5258 else if (PyTuple_Check(argv)) {
5259 argc = PyTuple_Size(argv);
5260 getitem = PyTuple_GetItem;
5261 }
5262 else {
5263 PyErr_SetString(PyExc_TypeError,
5264 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005265 return NULL;
5266 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005267
Victor Stinner8c62be82010-05-06 00:08:46 +00005268 argvlist = PyMem_NEW(char *, argc+1);
5269 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005270 return PyErr_NoMemory();
5271 }
5272 for (i = 0; i < argc; i++) {
5273 if (!fsconvert_strdup((*getitem)(argv, i),
5274 &argvlist[i])) {
5275 free_string_array(argvlist, i);
5276 PyErr_SetString(
5277 PyExc_TypeError,
5278 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005279 return NULL;
5280 }
5281 }
5282 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005283
Victor Stinner8c62be82010-05-06 00:08:46 +00005284 if (mode == _OLD_P_OVERLAY)
5285 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005286
Victor Stinner8c62be82010-05-06 00:08:46 +00005287 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10005288 spawnval = _spawnv(mode, path_char, argvlist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005289 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005290
Victor Stinner8c62be82010-05-06 00:08:46 +00005291 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00005292
Victor Stinner8c62be82010-05-06 00:08:46 +00005293 if (spawnval == -1)
5294 return posix_error();
5295 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005296 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005297}
5298
5299
Larry Hastings2f936352014-08-05 14:04:04 +10005300/*[clinic input]
5301os.spawnve
5302
5303 mode: int
5304 Mode of process creation.
5305 path: FSConverter
5306 Path of executable file.
5307 argv: object
5308 Tuple or list of strings.
5309 env: object
5310 Dictionary of strings mapping to strings.
5311 /
5312
5313Execute the program specified by path in a new process.
5314[clinic start generated code]*/
5315
Larry Hastings2f936352014-08-05 14:04:04 +10005316static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005317os_spawnve_impl(PyObject *module, int mode, PyObject *path, PyObject *argv,
5318 PyObject *env)
5319/*[clinic end generated code: output=ebcfa5f7ba2f4219 input=02362fd937963f8f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005320{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03005321 const char *path_char;
Victor Stinner8c62be82010-05-06 00:08:46 +00005322 char **argvlist;
5323 char **envlist;
5324 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005325 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00005326 Py_intptr_t spawnval;
5327 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5328 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005329
Victor Stinner8c62be82010-05-06 00:08:46 +00005330 /* spawnve has four arguments: (mode, path, argv, env), where
5331 argv is a list or tuple of strings and env is a dictionary
5332 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005333
Larry Hastings2f936352014-08-05 14:04:04 +10005334 path_char = PyBytes_AsString(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00005335 if (PyList_Check(argv)) {
5336 argc = PyList_Size(argv);
5337 getitem = PyList_GetItem;
5338 }
5339 else if (PyTuple_Check(argv)) {
5340 argc = PyTuple_Size(argv);
5341 getitem = PyTuple_GetItem;
5342 }
5343 else {
5344 PyErr_SetString(PyExc_TypeError,
5345 "spawnve() arg 2 must be a tuple or list");
5346 goto fail_0;
5347 }
5348 if (!PyMapping_Check(env)) {
5349 PyErr_SetString(PyExc_TypeError,
5350 "spawnve() arg 3 must be a mapping object");
5351 goto fail_0;
5352 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005353
Victor Stinner8c62be82010-05-06 00:08:46 +00005354 argvlist = PyMem_NEW(char *, argc+1);
5355 if (argvlist == NULL) {
5356 PyErr_NoMemory();
5357 goto fail_0;
5358 }
5359 for (i = 0; i < argc; i++) {
5360 if (!fsconvert_strdup((*getitem)(argv, i),
5361 &argvlist[i]))
5362 {
5363 lastarg = i;
5364 goto fail_1;
5365 }
5366 }
5367 lastarg = argc;
5368 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005369
Victor Stinner8c62be82010-05-06 00:08:46 +00005370 envlist = parse_envlist(env, &envc);
5371 if (envlist == NULL)
5372 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005373
Victor Stinner8c62be82010-05-06 00:08:46 +00005374 if (mode == _OLD_P_OVERLAY)
5375 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005376
Victor Stinner8c62be82010-05-06 00:08:46 +00005377 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10005378 spawnval = _spawnve(mode, path_char, argvlist, envlist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005379 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005380
Victor Stinner8c62be82010-05-06 00:08:46 +00005381 if (spawnval == -1)
5382 (void) posix_error();
5383 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01005384 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00005385
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 while (--envc >= 0)
5387 PyMem_DEL(envlist[envc]);
5388 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005389 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005390 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005391 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005392 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005393}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005394
Guido van Rossuma1065681999-01-25 23:20:23 +00005395#endif /* HAVE_SPAWNV */
5396
5397
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005398#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10005399/*[clinic input]
5400os.fork1
5401
5402Fork a child process with a single multiplexed (i.e., not bound) thread.
5403
5404Return 0 to child process and PID of child to parent process.
5405[clinic start generated code]*/
5406
Larry Hastings2f936352014-08-05 14:04:04 +10005407static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005408os_fork1_impl(PyObject *module)
5409/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005410{
Victor Stinner8c62be82010-05-06 00:08:46 +00005411 pid_t pid;
5412 int result = 0;
5413 _PyImport_AcquireLock();
5414 pid = fork1();
5415 if (pid == 0) {
5416 /* child: this clobbers and resets the import lock. */
5417 PyOS_AfterFork();
5418 } else {
5419 /* parent: release the import lock. */
5420 result = _PyImport_ReleaseLock();
5421 }
5422 if (pid == -1)
5423 return posix_error();
5424 if (result < 0) {
5425 /* Don't clobber the OSError if the fork failed. */
5426 PyErr_SetString(PyExc_RuntimeError,
5427 "not holding the import lock");
5428 return NULL;
5429 }
5430 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005431}
Larry Hastings2f936352014-08-05 14:04:04 +10005432#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005433
5434
Guido van Rossumad0ee831995-03-01 10:34:45 +00005435#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10005436/*[clinic input]
5437os.fork
5438
5439Fork a child process.
5440
5441Return 0 to child process and PID of child to parent process.
5442[clinic start generated code]*/
5443
Larry Hastings2f936352014-08-05 14:04:04 +10005444static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005445os_fork_impl(PyObject *module)
5446/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00005447{
Victor Stinner8c62be82010-05-06 00:08:46 +00005448 pid_t pid;
5449 int result = 0;
5450 _PyImport_AcquireLock();
5451 pid = fork();
5452 if (pid == 0) {
5453 /* child: this clobbers and resets the import lock. */
5454 PyOS_AfterFork();
5455 } else {
5456 /* parent: release the import lock. */
5457 result = _PyImport_ReleaseLock();
5458 }
5459 if (pid == -1)
5460 return posix_error();
5461 if (result < 0) {
5462 /* Don't clobber the OSError if the fork failed. */
5463 PyErr_SetString(PyExc_RuntimeError,
5464 "not holding the import lock");
5465 return NULL;
5466 }
5467 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005468}
Larry Hastings2f936352014-08-05 14:04:04 +10005469#endif /* HAVE_FORK */
5470
Guido van Rossum85e3b011991-06-03 12:42:10 +00005471
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005472#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005473#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10005474/*[clinic input]
5475os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005476
Larry Hastings2f936352014-08-05 14:04:04 +10005477 policy: int
5478
5479Get the maximum scheduling priority for policy.
5480[clinic start generated code]*/
5481
Larry Hastings2f936352014-08-05 14:04:04 +10005482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005483os_sched_get_priority_max_impl(PyObject *module, int policy)
5484/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005485{
5486 int max;
5487
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005488 max = sched_get_priority_max(policy);
5489 if (max < 0)
5490 return posix_error();
5491 return PyLong_FromLong(max);
5492}
5493
Larry Hastings2f936352014-08-05 14:04:04 +10005494
5495/*[clinic input]
5496os.sched_get_priority_min
5497
5498 policy: int
5499
5500Get the minimum scheduling priority for policy.
5501[clinic start generated code]*/
5502
Larry Hastings2f936352014-08-05 14:04:04 +10005503static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005504os_sched_get_priority_min_impl(PyObject *module, int policy)
5505/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005506{
5507 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005508 if (min < 0)
5509 return posix_error();
5510 return PyLong_FromLong(min);
5511}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005512#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5513
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005514
Larry Hastings2f936352014-08-05 14:04:04 +10005515#ifdef HAVE_SCHED_SETSCHEDULER
5516/*[clinic input]
5517os.sched_getscheduler
5518 pid: pid_t
5519 /
5520
5521Get the scheduling policy for the process identifiedy by pid.
5522
5523Passing 0 for pid returns the scheduling policy for the calling process.
5524[clinic start generated code]*/
5525
Larry Hastings2f936352014-08-05 14:04:04 +10005526static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005527os_sched_getscheduler_impl(PyObject *module, pid_t pid)
5528/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=5f14cfd1f189e1a0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005529{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005530 int policy;
5531
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005532 policy = sched_getscheduler(pid);
5533 if (policy < 0)
5534 return posix_error();
5535 return PyLong_FromLong(policy);
5536}
Larry Hastings2f936352014-08-05 14:04:04 +10005537#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005538
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005539
5540#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10005541/*[clinic input]
5542class os.sched_param "PyObject *" "&SchedParamType"
5543
5544@classmethod
5545os.sched_param.__new__
5546
5547 sched_priority: object
5548 A scheduling parameter.
5549
5550Current has only one field: sched_priority");
5551[clinic start generated code]*/
5552
Larry Hastings2f936352014-08-05 14:04:04 +10005553static PyObject *
5554os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005555/*[clinic end generated code: output=48f4067d60f48c13 input=73a4c22f7071fc62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005556{
5557 PyObject *res;
5558
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005559 res = PyStructSequence_New(type);
5560 if (!res)
5561 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10005562 Py_INCREF(sched_priority);
5563 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005564 return res;
5565}
5566
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005567
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03005568PyDoc_VAR(os_sched_param__doc__);
5569
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005570static PyStructSequence_Field sched_param_fields[] = {
5571 {"sched_priority", "the scheduling priority"},
5572 {0}
5573};
5574
5575static PyStructSequence_Desc sched_param_desc = {
5576 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10005577 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005578 sched_param_fields,
5579 1
5580};
5581
5582static int
5583convert_sched_param(PyObject *param, struct sched_param *res)
5584{
5585 long priority;
5586
5587 if (Py_TYPE(param) != &SchedParamType) {
5588 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5589 return 0;
5590 }
5591 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5592 if (priority == -1 && PyErr_Occurred())
5593 return 0;
5594 if (priority > INT_MAX || priority < INT_MIN) {
5595 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5596 return 0;
5597 }
5598 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5599 return 1;
5600}
Larry Hastings2f936352014-08-05 14:04:04 +10005601#endif /* defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005602
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005603
5604#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10005605/*[clinic input]
5606os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005607
Larry Hastings2f936352014-08-05 14:04:04 +10005608 pid: pid_t
5609 policy: int
5610 param: sched_param
5611 /
5612
5613Set the scheduling policy for the process identified by pid.
5614
5615If pid is 0, the calling process is changed.
5616param is an instance of sched_param.
5617[clinic start generated code]*/
5618
Larry Hastings2f936352014-08-05 14:04:04 +10005619static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005620os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04005621 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005622/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005623{
Jesus Cea9c822272011-09-10 01:40:52 +02005624 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005625 ** sched_setscheduler() returns 0 in Linux, but the previous
5626 ** scheduling policy under Solaris/Illumos, and others.
5627 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005628 */
Larry Hastings2f936352014-08-05 14:04:04 +10005629 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005630 return posix_error();
5631 Py_RETURN_NONE;
5632}
Larry Hastings2f936352014-08-05 14:04:04 +10005633#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005634
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005635
5636#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10005637/*[clinic input]
5638os.sched_getparam
5639 pid: pid_t
5640 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005641
Larry Hastings2f936352014-08-05 14:04:04 +10005642Returns scheduling parameters for the process identified by pid.
5643
5644If pid is 0, returns parameters for the calling process.
5645Return value is an instance of sched_param.
5646[clinic start generated code]*/
5647
Larry Hastings2f936352014-08-05 14:04:04 +10005648static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005649os_sched_getparam_impl(PyObject *module, pid_t pid)
5650/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005651{
5652 struct sched_param param;
5653 PyObject *result;
5654 PyObject *priority;
5655
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005656 if (sched_getparam(pid, &param))
5657 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10005658 result = PyStructSequence_New(&SchedParamType);
5659 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005660 return NULL;
5661 priority = PyLong_FromLong(param.sched_priority);
5662 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10005663 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005664 return NULL;
5665 }
Larry Hastings2f936352014-08-05 14:04:04 +10005666 PyStructSequence_SET_ITEM(result, 0, priority);
5667 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005668}
5669
Larry Hastings2f936352014-08-05 14:04:04 +10005670
5671/*[clinic input]
5672os.sched_setparam
5673 pid: pid_t
5674 param: sched_param
5675 /
5676
5677Set scheduling parameters for the process identified by pid.
5678
5679If pid is 0, sets parameters for the calling process.
5680param should be an instance of sched_param.
5681[clinic start generated code]*/
5682
Larry Hastings2f936352014-08-05 14:04:04 +10005683static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005684os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04005685 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005686/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005687{
5688 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005689 return posix_error();
5690 Py_RETURN_NONE;
5691}
Larry Hastings2f936352014-08-05 14:04:04 +10005692#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005693
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005694
5695#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10005696/*[clinic input]
5697os.sched_rr_get_interval -> double
5698 pid: pid_t
5699 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005700
Larry Hastings2f936352014-08-05 14:04:04 +10005701Return the round-robin quantum for the process identified by pid, in seconds.
5702
5703Value returned is a float.
5704[clinic start generated code]*/
5705
Larry Hastings2f936352014-08-05 14:04:04 +10005706static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005707os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
5708/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005709{
5710 struct timespec interval;
5711 if (sched_rr_get_interval(pid, &interval)) {
5712 posix_error();
5713 return -1.0;
5714 }
5715 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
5716}
5717#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005718
Larry Hastings2f936352014-08-05 14:04:04 +10005719
5720/*[clinic input]
5721os.sched_yield
5722
5723Voluntarily relinquish the CPU.
5724[clinic start generated code]*/
5725
Larry Hastings2f936352014-08-05 14:04:04 +10005726static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005727os_sched_yield_impl(PyObject *module)
5728/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005729{
5730 if (sched_yield())
5731 return posix_error();
5732 Py_RETURN_NONE;
5733}
5734
Benjamin Peterson2740af82011-08-02 17:41:34 -05005735#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02005736/* The minimum number of CPUs allocated in a cpu_set_t */
5737static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005738
Larry Hastings2f936352014-08-05 14:04:04 +10005739/*[clinic input]
5740os.sched_setaffinity
5741 pid: pid_t
5742 mask : object
5743 /
5744
5745Set the CPU affinity of the process identified by pid to mask.
5746
5747mask should be an iterable of integers identifying CPUs.
5748[clinic start generated code]*/
5749
Larry Hastings2f936352014-08-05 14:04:04 +10005750static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005751os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
5752/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005753{
Antoine Pitrou84869872012-08-04 16:16:35 +02005754 int ncpus;
5755 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005756 cpu_set_t *cpu_set = NULL;
5757 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005758
Larry Hastings2f936352014-08-05 14:04:04 +10005759 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02005760 if (iterator == NULL)
5761 return NULL;
5762
5763 ncpus = NCPUS_START;
5764 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10005765 cpu_set = CPU_ALLOC(ncpus);
5766 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005767 PyErr_NoMemory();
5768 goto error;
5769 }
Larry Hastings2f936352014-08-05 14:04:04 +10005770 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005771
5772 while ((item = PyIter_Next(iterator))) {
5773 long cpu;
5774 if (!PyLong_Check(item)) {
5775 PyErr_Format(PyExc_TypeError,
5776 "expected an iterator of ints, "
5777 "but iterator yielded %R",
5778 Py_TYPE(item));
5779 Py_DECREF(item);
5780 goto error;
5781 }
5782 cpu = PyLong_AsLong(item);
5783 Py_DECREF(item);
5784 if (cpu < 0) {
5785 if (!PyErr_Occurred())
5786 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5787 goto error;
5788 }
5789 if (cpu > INT_MAX - 1) {
5790 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5791 goto error;
5792 }
5793 if (cpu >= ncpus) {
5794 /* Grow CPU mask to fit the CPU number */
5795 int newncpus = ncpus;
5796 cpu_set_t *newmask;
5797 size_t newsetsize;
5798 while (newncpus <= cpu) {
5799 if (newncpus > INT_MAX / 2)
5800 newncpus = cpu + 1;
5801 else
5802 newncpus = newncpus * 2;
5803 }
5804 newmask = CPU_ALLOC(newncpus);
5805 if (newmask == NULL) {
5806 PyErr_NoMemory();
5807 goto error;
5808 }
5809 newsetsize = CPU_ALLOC_SIZE(newncpus);
5810 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10005811 memcpy(newmask, cpu_set, setsize);
5812 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005813 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10005814 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02005815 ncpus = newncpus;
5816 }
Larry Hastings2f936352014-08-05 14:04:04 +10005817 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005818 }
5819 Py_CLEAR(iterator);
5820
Larry Hastings2f936352014-08-05 14:04:04 +10005821 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02005822 posix_error();
5823 goto error;
5824 }
Larry Hastings2f936352014-08-05 14:04:04 +10005825 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005826 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005827
5828error:
Larry Hastings2f936352014-08-05 14:04:04 +10005829 if (cpu_set)
5830 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02005831 Py_XDECREF(iterator);
5832 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005833}
5834
Larry Hastings2f936352014-08-05 14:04:04 +10005835
5836/*[clinic input]
5837os.sched_getaffinity
5838 pid: pid_t
5839 /
5840
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01005841Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10005842
5843The affinity is returned as a set of CPU identifiers.
5844[clinic start generated code]*/
5845
Larry Hastings2f936352014-08-05 14:04:04 +10005846static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005847os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03005848/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005849{
Antoine Pitrou84869872012-08-04 16:16:35 +02005850 int cpu, ncpus, count;
5851 size_t setsize;
5852 cpu_set_t *mask = NULL;
5853 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005854
Antoine Pitrou84869872012-08-04 16:16:35 +02005855 ncpus = NCPUS_START;
5856 while (1) {
5857 setsize = CPU_ALLOC_SIZE(ncpus);
5858 mask = CPU_ALLOC(ncpus);
5859 if (mask == NULL)
5860 return PyErr_NoMemory();
5861 if (sched_getaffinity(pid, setsize, mask) == 0)
5862 break;
5863 CPU_FREE(mask);
5864 if (errno != EINVAL)
5865 return posix_error();
5866 if (ncpus > INT_MAX / 2) {
5867 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5868 "a large enough CPU set");
5869 return NULL;
5870 }
5871 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005872 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005873
5874 res = PySet_New(NULL);
5875 if (res == NULL)
5876 goto error;
5877 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5878 if (CPU_ISSET_S(cpu, setsize, mask)) {
5879 PyObject *cpu_num = PyLong_FromLong(cpu);
5880 --count;
5881 if (cpu_num == NULL)
5882 goto error;
5883 if (PySet_Add(res, cpu_num)) {
5884 Py_DECREF(cpu_num);
5885 goto error;
5886 }
5887 Py_DECREF(cpu_num);
5888 }
5889 }
5890 CPU_FREE(mask);
5891 return res;
5892
5893error:
5894 if (mask)
5895 CPU_FREE(mask);
5896 Py_XDECREF(res);
5897 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005898}
5899
Benjamin Peterson2740af82011-08-02 17:41:34 -05005900#endif /* HAVE_SCHED_SETAFFINITY */
5901
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005902#endif /* HAVE_SCHED_H */
5903
Larry Hastings2f936352014-08-05 14:04:04 +10005904
Neal Norwitzb59798b2003-03-21 01:43:31 +00005905/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005906/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5907#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005908#define DEV_PTY_FILE "/dev/ptc"
5909#define HAVE_DEV_PTMX
5910#else
5911#define DEV_PTY_FILE "/dev/ptmx"
5912#endif
5913
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005914#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005915#ifdef HAVE_PTY_H
5916#include <pty.h>
5917#else
5918#ifdef HAVE_LIBUTIL_H
5919#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005920#else
5921#ifdef HAVE_UTIL_H
5922#include <util.h>
5923#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005924#endif /* HAVE_LIBUTIL_H */
5925#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005926#ifdef HAVE_STROPTS_H
5927#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005928#endif
5929#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005930
Larry Hastings2f936352014-08-05 14:04:04 +10005931
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005932#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10005933/*[clinic input]
5934os.openpty
5935
5936Open a pseudo-terminal.
5937
5938Return a tuple of (master_fd, slave_fd) containing open file descriptors
5939for both the master and slave ends.
5940[clinic start generated code]*/
5941
Larry Hastings2f936352014-08-05 14:04:04 +10005942static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005943os_openpty_impl(PyObject *module)
5944/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00005945{
Victor Stinnerdaf45552013-08-28 00:53:59 +02005946 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005947#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005948 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005949#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005950#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005951 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005952#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005954#endif
5955#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005956
Thomas Wouters70c21a12000-07-14 14:28:33 +00005957#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005958 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005959 goto posix_error;
5960
5961 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5962 goto error;
5963 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
5964 goto error;
5965
Neal Norwitzb59798b2003-03-21 01:43:31 +00005966#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005967 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5968 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005969 goto posix_error;
5970 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
5971 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005972
Victor Stinnerdaf45552013-08-28 00:53:59 +02005973 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00005974 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01005975 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02005976
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005977#else
Victor Stinner000de532013-11-25 23:19:58 +01005978 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00005979 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02005980 goto posix_error;
5981
Victor Stinner8c62be82010-05-06 00:08:46 +00005982 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005983
Victor Stinner8c62be82010-05-06 00:08:46 +00005984 /* change permission of slave */
5985 if (grantpt(master_fd) < 0) {
5986 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005987 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005989
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 /* unlock slave */
5991 if (unlockpt(master_fd) < 0) {
5992 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005993 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005994 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02005995
Victor Stinner8c62be82010-05-06 00:08:46 +00005996 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02005997
Victor Stinner8c62be82010-05-06 00:08:46 +00005998 slave_name = ptsname(master_fd); /* get name of slave */
5999 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006000 goto posix_error;
6001
6002 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006003 if (slave_fd == -1)
6004 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006005
6006 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6007 goto posix_error;
6008
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006009#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006010 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6011 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006012#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006013 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006014#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006015#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006016#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006017
Victor Stinner8c62be82010-05-06 00:08:46 +00006018 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006019
Victor Stinnerdaf45552013-08-28 00:53:59 +02006020posix_error:
6021 posix_error();
6022error:
6023 if (master_fd != -1)
6024 close(master_fd);
6025 if (slave_fd != -1)
6026 close(slave_fd);
6027 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006028}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006029#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006030
Larry Hastings2f936352014-08-05 14:04:04 +10006031
Fred Drake8cef4cf2000-06-28 16:40:38 +00006032#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006033/*[clinic input]
6034os.forkpty
6035
6036Fork a new process with a new pseudo-terminal as controlling tty.
6037
6038Returns a tuple of (pid, master_fd).
6039Like fork(), return pid of 0 to the child process,
6040and pid of child to the parent process.
6041To both, return fd of newly opened pseudo-terminal.
6042[clinic start generated code]*/
6043
Larry Hastings2f936352014-08-05 14:04:04 +10006044static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006045os_forkpty_impl(PyObject *module)
6046/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006047{
Victor Stinner8c62be82010-05-06 00:08:46 +00006048 int master_fd = -1, result = 0;
6049 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006050
Victor Stinner8c62be82010-05-06 00:08:46 +00006051 _PyImport_AcquireLock();
6052 pid = forkpty(&master_fd, NULL, NULL, NULL);
6053 if (pid == 0) {
6054 /* child: this clobbers and resets the import lock. */
6055 PyOS_AfterFork();
6056 } else {
6057 /* parent: release the import lock. */
6058 result = _PyImport_ReleaseLock();
6059 }
6060 if (pid == -1)
6061 return posix_error();
6062 if (result < 0) {
6063 /* Don't clobber the OSError if the fork failed. */
6064 PyErr_SetString(PyExc_RuntimeError,
6065 "not holding the import lock");
6066 return NULL;
6067 }
6068 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006069}
Larry Hastings2f936352014-08-05 14:04:04 +10006070#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006071
Ross Lagerwall7807c352011-03-17 20:20:30 +02006072
Guido van Rossumad0ee831995-03-01 10:34:45 +00006073#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006074/*[clinic input]
6075os.getegid
6076
6077Return the current process's effective group id.
6078[clinic start generated code]*/
6079
Larry Hastings2f936352014-08-05 14:04:04 +10006080static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006081os_getegid_impl(PyObject *module)
6082/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006083{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006084 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006085}
Larry Hastings2f936352014-08-05 14:04:04 +10006086#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006087
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006088
Guido van Rossumad0ee831995-03-01 10:34:45 +00006089#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006090/*[clinic input]
6091os.geteuid
6092
6093Return the current process's effective user id.
6094[clinic start generated code]*/
6095
Larry Hastings2f936352014-08-05 14:04:04 +10006096static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006097os_geteuid_impl(PyObject *module)
6098/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006099{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006100 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006101}
Larry Hastings2f936352014-08-05 14:04:04 +10006102#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006103
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006104
Guido van Rossumad0ee831995-03-01 10:34:45 +00006105#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006106/*[clinic input]
6107os.getgid
6108
6109Return the current process's group id.
6110[clinic start generated code]*/
6111
Larry Hastings2f936352014-08-05 14:04:04 +10006112static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006113os_getgid_impl(PyObject *module)
6114/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006115{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006116 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006117}
Larry Hastings2f936352014-08-05 14:04:04 +10006118#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006119
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006120
Larry Hastings2f936352014-08-05 14:04:04 +10006121/*[clinic input]
6122os.getpid
6123
6124Return the current process id.
6125[clinic start generated code]*/
6126
Larry Hastings2f936352014-08-05 14:04:04 +10006127static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006128os_getpid_impl(PyObject *module)
6129/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006130{
Victor Stinner8c62be82010-05-06 00:08:46 +00006131 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006132}
6133
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006134#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006135
6136/* AC 3.5: funny apple logic below */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006137PyDoc_STRVAR(posix_getgrouplist__doc__,
6138"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
6139Returns a list of groups to which a user belongs.\n\n\
6140 user: username to lookup\n\
6141 group: base group id of the user");
6142
6143static PyObject *
6144posix_getgrouplist(PyObject *self, PyObject *args)
6145{
6146#ifdef NGROUPS_MAX
6147#define MAX_GROUPS NGROUPS_MAX
6148#else
6149 /* defined to be 16 on Solaris7, so this should be a small number */
6150#define MAX_GROUPS 64
6151#endif
6152
6153 const char *user;
6154 int i, ngroups;
6155 PyObject *list;
6156#ifdef __APPLE__
6157 int *groups, basegid;
6158#else
6159 gid_t *groups, basegid;
6160#endif
6161 ngroups = MAX_GROUPS;
6162
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006163#ifdef __APPLE__
6164 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006165 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006166#else
6167 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
6168 _Py_Gid_Converter, &basegid))
6169 return NULL;
6170#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006171
6172#ifdef __APPLE__
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006173 groups = PyMem_New(int, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006174#else
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006175 groups = PyMem_New(gid_t, ngroups);
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006176#endif
6177 if (groups == NULL)
6178 return PyErr_NoMemory();
6179
6180 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
6181 PyMem_Del(groups);
6182 return posix_error();
6183 }
6184
6185 list = PyList_New(ngroups);
6186 if (list == NULL) {
6187 PyMem_Del(groups);
6188 return NULL;
6189 }
6190
6191 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006192#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006193 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006194#else
6195 PyObject *o = _PyLong_FromGid(groups[i]);
6196#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006197 if (o == NULL) {
6198 Py_DECREF(list);
6199 PyMem_Del(groups);
6200 return NULL;
6201 }
6202 PyList_SET_ITEM(list, i, o);
6203 }
6204
6205 PyMem_Del(groups);
6206
6207 return list;
6208}
Larry Hastings2f936352014-08-05 14:04:04 +10006209#endif /* HAVE_GETGROUPLIST */
6210
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006211
Fred Drakec9680921999-12-13 16:37:25 +00006212#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006213/*[clinic input]
6214os.getgroups
6215
6216Return list of supplemental group IDs for the process.
6217[clinic start generated code]*/
6218
Larry Hastings2f936352014-08-05 14:04:04 +10006219static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006220os_getgroups_impl(PyObject *module)
6221/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00006222{
6223 PyObject *result = NULL;
6224
Fred Drakec9680921999-12-13 16:37:25 +00006225#ifdef NGROUPS_MAX
6226#define MAX_GROUPS NGROUPS_MAX
6227#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006228 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00006229#define MAX_GROUPS 64
6230#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006231 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006232
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006233 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006234 * This is a helper variable to store the intermediate result when
6235 * that happens.
6236 *
6237 * To keep the code readable the OSX behaviour is unconditional,
6238 * according to the POSIX spec this should be safe on all unix-y
6239 * systems.
6240 */
6241 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006242 int n;
Fred Drakec9680921999-12-13 16:37:25 +00006243
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006244#ifdef __APPLE__
6245 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
6246 * there are more groups than can fit in grouplist. Therefore, on OS X
6247 * always first call getgroups with length 0 to get the actual number
6248 * of groups.
6249 */
6250 n = getgroups(0, NULL);
6251 if (n < 0) {
6252 return posix_error();
6253 } else if (n <= MAX_GROUPS) {
6254 /* groups will fit in existing array */
6255 alt_grouplist = grouplist;
6256 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006257 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006258 if (alt_grouplist == NULL) {
6259 errno = EINVAL;
6260 return posix_error();
6261 }
6262 }
6263
6264 n = getgroups(n, alt_grouplist);
6265 if (n == -1) {
6266 if (alt_grouplist != grouplist) {
6267 PyMem_Free(alt_grouplist);
6268 }
6269 return posix_error();
6270 }
6271#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006272 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006273 if (n < 0) {
6274 if (errno == EINVAL) {
6275 n = getgroups(0, NULL);
6276 if (n == -1) {
6277 return posix_error();
6278 }
6279 if (n == 0) {
6280 /* Avoid malloc(0) */
6281 alt_grouplist = grouplist;
6282 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02006283 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006284 if (alt_grouplist == NULL) {
6285 errno = EINVAL;
6286 return posix_error();
6287 }
6288 n = getgroups(n, alt_grouplist);
6289 if (n == -1) {
6290 PyMem_Free(alt_grouplist);
6291 return posix_error();
6292 }
6293 }
6294 } else {
6295 return posix_error();
6296 }
6297 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07006298#endif
6299
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006300 result = PyList_New(n);
6301 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006302 int i;
6303 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006304 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00006305 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00006306 Py_DECREF(result);
6307 result = NULL;
6308 break;
Fred Drakec9680921999-12-13 16:37:25 +00006309 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006310 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00006311 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00006312 }
6313
6314 if (alt_grouplist != grouplist) {
6315 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 }
Neal Norwitze241ce82003-02-17 18:17:05 +00006317
Fred Drakec9680921999-12-13 16:37:25 +00006318 return result;
6319}
Larry Hastings2f936352014-08-05 14:04:04 +10006320#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00006321
Antoine Pitroub7572f02009-12-02 20:46:48 +00006322#ifdef HAVE_INITGROUPS
6323PyDoc_STRVAR(posix_initgroups__doc__,
6324"initgroups(username, gid) -> None\n\n\
6325Call the system initgroups() to initialize the group access list with all of\n\
6326the groups of which the specified username is a member, plus the specified\n\
6327group id.");
6328
Larry Hastings2f936352014-08-05 14:04:04 +10006329/* AC 3.5: funny apple logic */
Antoine Pitroub7572f02009-12-02 20:46:48 +00006330static PyObject *
6331posix_initgroups(PyObject *self, PyObject *args)
6332{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006333 PyObject *oname;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03006334 const char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006335 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006336#ifdef __APPLE__
6337 int gid;
6338#else
6339 gid_t gid;
6340#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00006341
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006342#ifdef __APPLE__
6343 if (!PyArg_ParseTuple(args, "O&i:initgroups",
6344 PyUnicode_FSConverter, &oname,
6345 &gid))
6346#else
6347 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
6348 PyUnicode_FSConverter, &oname,
6349 _Py_Gid_Converter, &gid))
6350#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00006351 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006352 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006353
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006354 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00006355 Py_DECREF(oname);
6356 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00006357 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00006358
Victor Stinner8c62be82010-05-06 00:08:46 +00006359 Py_INCREF(Py_None);
6360 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00006361}
Larry Hastings2f936352014-08-05 14:04:04 +10006362#endif /* HAVE_INITGROUPS */
6363
Antoine Pitroub7572f02009-12-02 20:46:48 +00006364
Martin v. Löwis606edc12002-06-13 21:09:11 +00006365#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10006366/*[clinic input]
6367os.getpgid
6368
6369 pid: pid_t
6370
6371Call the system call getpgid(), and return the result.
6372[clinic start generated code]*/
6373
Larry Hastings2f936352014-08-05 14:04:04 +10006374static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006375os_getpgid_impl(PyObject *module, pid_t pid)
6376/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006377{
6378 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006379 if (pgid < 0)
6380 return posix_error();
6381 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00006382}
6383#endif /* HAVE_GETPGID */
6384
6385
Guido van Rossumb6775db1994-08-01 11:34:53 +00006386#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006387/*[clinic input]
6388os.getpgrp
6389
6390Return the current process group id.
6391[clinic start generated code]*/
6392
Larry Hastings2f936352014-08-05 14:04:04 +10006393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006394os_getpgrp_impl(PyObject *module)
6395/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00006396{
Guido van Rossumb6775db1994-08-01 11:34:53 +00006397#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006398 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006399#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006400 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006401#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00006402}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006403#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00006404
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006405
Guido van Rossumb6775db1994-08-01 11:34:53 +00006406#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10006407/*[clinic input]
6408os.setpgrp
6409
6410Make the current process the leader of its process group.
6411[clinic start generated code]*/
6412
Larry Hastings2f936352014-08-05 14:04:04 +10006413static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006414os_setpgrp_impl(PyObject *module)
6415/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00006416{
Guido van Rossum64933891994-10-20 21:56:42 +00006417#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006418 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006419#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006420 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006421#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006422 return posix_error();
6423 Py_INCREF(Py_None);
6424 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006425}
Guido van Rossumb6775db1994-08-01 11:34:53 +00006426#endif /* HAVE_SETPGRP */
6427
Guido van Rossumad0ee831995-03-01 10:34:45 +00006428#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006429
6430#ifdef MS_WINDOWS
6431#include <tlhelp32.h>
6432
6433static PyObject*
6434win32_getppid()
6435{
6436 HANDLE snapshot;
6437 pid_t mypid;
6438 PyObject* result = NULL;
6439 BOOL have_record;
6440 PROCESSENTRY32 pe;
6441
6442 mypid = getpid(); /* This function never fails */
6443
6444 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6445 if (snapshot == INVALID_HANDLE_VALUE)
6446 return PyErr_SetFromWindowsErr(GetLastError());
6447
6448 pe.dwSize = sizeof(pe);
6449 have_record = Process32First(snapshot, &pe);
6450 while (have_record) {
6451 if (mypid == (pid_t)pe.th32ProcessID) {
6452 /* We could cache the ulong value in a static variable. */
6453 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6454 break;
6455 }
6456
6457 have_record = Process32Next(snapshot, &pe);
6458 }
6459
6460 /* If our loop exits and our pid was not found (result will be NULL)
6461 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6462 * error anyway, so let's raise it. */
6463 if (!result)
6464 result = PyErr_SetFromWindowsErr(GetLastError());
6465
6466 CloseHandle(snapshot);
6467
6468 return result;
6469}
6470#endif /*MS_WINDOWS*/
6471
Larry Hastings2f936352014-08-05 14:04:04 +10006472
6473/*[clinic input]
6474os.getppid
6475
6476Return the parent's process id.
6477
6478If the parent process has already exited, Windows machines will still
6479return its id; others systems will return the id of the 'init' process (1).
6480[clinic start generated code]*/
6481
Larry Hastings2f936352014-08-05 14:04:04 +10006482static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006483os_getppid_impl(PyObject *module)
6484/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006485{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006486#ifdef MS_WINDOWS
6487 return win32_getppid();
6488#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006490#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006491}
6492#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006493
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006494
Fred Drake12c6e2d1999-12-14 21:25:03 +00006495#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10006496/*[clinic input]
6497os.getlogin
6498
6499Return the actual login name.
6500[clinic start generated code]*/
6501
Larry Hastings2f936352014-08-05 14:04:04 +10006502static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006503os_getlogin_impl(PyObject *module)
6504/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00006505{
Victor Stinner8c62be82010-05-06 00:08:46 +00006506 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006507#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006508 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006509 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006510
6511 if (GetUserNameW(user_name, &num_chars)) {
6512 /* num_chars is the number of unicode chars plus null terminator */
6513 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006514 }
6515 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006516 result = PyErr_SetFromWindowsErr(GetLastError());
6517#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006518 char *name;
6519 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006520
Victor Stinner8c62be82010-05-06 00:08:46 +00006521 errno = 0;
6522 name = getlogin();
6523 if (name == NULL) {
6524 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006525 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006526 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006527 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006528 }
6529 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006530 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006531 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006532#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006533 return result;
6534}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006535#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006536
Larry Hastings2f936352014-08-05 14:04:04 +10006537
Guido van Rossumad0ee831995-03-01 10:34:45 +00006538#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006539/*[clinic input]
6540os.getuid
6541
6542Return the current process's user id.
6543[clinic start generated code]*/
6544
Larry Hastings2f936352014-08-05 14:04:04 +10006545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006546os_getuid_impl(PyObject *module)
6547/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006548{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006549 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006550}
Larry Hastings2f936352014-08-05 14:04:04 +10006551#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006552
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006553
Brian Curtineb24d742010-04-12 17:16:38 +00006554#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10006555#define HAVE_KILL
6556#endif /* MS_WINDOWS */
6557
6558#ifdef HAVE_KILL
6559/*[clinic input]
6560os.kill
6561
6562 pid: pid_t
6563 signal: Py_ssize_t
6564 /
6565
6566Kill a process with a signal.
6567[clinic start generated code]*/
6568
Larry Hastings2f936352014-08-05 14:04:04 +10006569static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006570os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
6571/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006572#ifndef MS_WINDOWS
6573{
6574 if (kill(pid, (int)signal) == -1)
6575 return posix_error();
6576 Py_RETURN_NONE;
6577}
6578#else /* !MS_WINDOWS */
Brian Curtineb24d742010-04-12 17:16:38 +00006579{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006580 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10006581 DWORD sig = (DWORD)signal;
6582 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00006583 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006584
Victor Stinner8c62be82010-05-06 00:08:46 +00006585 /* Console processes which share a common console can be sent CTRL+C or
6586 CTRL+BREAK events, provided they handle said events. */
6587 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006588 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006589 err = GetLastError();
6590 PyErr_SetFromWindowsErr(err);
6591 }
6592 else
6593 Py_RETURN_NONE;
6594 }
Brian Curtineb24d742010-04-12 17:16:38 +00006595
Victor Stinner8c62be82010-05-06 00:08:46 +00006596 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6597 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006598 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 if (handle == NULL) {
6600 err = GetLastError();
6601 return PyErr_SetFromWindowsErr(err);
6602 }
Brian Curtineb24d742010-04-12 17:16:38 +00006603
Victor Stinner8c62be82010-05-06 00:08:46 +00006604 if (TerminateProcess(handle, sig) == 0) {
6605 err = GetLastError();
6606 result = PyErr_SetFromWindowsErr(err);
6607 } else {
6608 Py_INCREF(Py_None);
6609 result = Py_None;
6610 }
Brian Curtineb24d742010-04-12 17:16:38 +00006611
Victor Stinner8c62be82010-05-06 00:08:46 +00006612 CloseHandle(handle);
6613 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006614}
Larry Hastings2f936352014-08-05 14:04:04 +10006615#endif /* !MS_WINDOWS */
6616#endif /* HAVE_KILL */
6617
6618
6619#ifdef HAVE_KILLPG
6620/*[clinic input]
6621os.killpg
6622
6623 pgid: pid_t
6624 signal: int
6625 /
6626
6627Kill a process group with a signal.
6628[clinic start generated code]*/
6629
Larry Hastings2f936352014-08-05 14:04:04 +10006630static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006631os_killpg_impl(PyObject *module, pid_t pgid, int signal)
6632/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006633{
6634 /* XXX some man pages make the `pgid` parameter an int, others
6635 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6636 take the same type. Moreover, pid_t is always at least as wide as
6637 int (else compilation of this module fails), which is safe. */
6638 if (killpg(pgid, signal) == -1)
6639 return posix_error();
6640 Py_RETURN_NONE;
6641}
6642#endif /* HAVE_KILLPG */
6643
Brian Curtineb24d742010-04-12 17:16:38 +00006644
Guido van Rossumc0125471996-06-28 18:55:32 +00006645#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00006646#ifdef HAVE_SYS_LOCK_H
6647#include <sys/lock.h>
6648#endif
6649
Larry Hastings2f936352014-08-05 14:04:04 +10006650/*[clinic input]
6651os.plock
6652 op: int
6653 /
6654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006655Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10006656[clinic start generated code]*/
6657
Larry Hastings2f936352014-08-05 14:04:04 +10006658static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006659os_plock_impl(PyObject *module, int op)
6660/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006661{
Victor Stinner8c62be82010-05-06 00:08:46 +00006662 if (plock(op) == -1)
6663 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006664 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00006665}
Larry Hastings2f936352014-08-05 14:04:04 +10006666#endif /* HAVE_PLOCK */
6667
Guido van Rossumc0125471996-06-28 18:55:32 +00006668
Guido van Rossumb6775db1994-08-01 11:34:53 +00006669#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10006670/*[clinic input]
6671os.setuid
6672
6673 uid: uid_t
6674 /
6675
6676Set the current process's user id.
6677[clinic start generated code]*/
6678
Larry Hastings2f936352014-08-05 14:04:04 +10006679static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006680os_setuid_impl(PyObject *module, uid_t uid)
6681/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006682{
Victor Stinner8c62be82010-05-06 00:08:46 +00006683 if (setuid(uid) < 0)
6684 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006685 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006686}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006687#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006688
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006689
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006690#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006691/*[clinic input]
6692os.seteuid
6693
6694 euid: uid_t
6695 /
6696
6697Set the current process's effective user id.
6698[clinic start generated code]*/
6699
Larry Hastings2f936352014-08-05 14:04:04 +10006700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006701os_seteuid_impl(PyObject *module, uid_t euid)
6702/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006703{
6704 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006705 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006706 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006707}
6708#endif /* HAVE_SETEUID */
6709
Larry Hastings2f936352014-08-05 14:04:04 +10006710
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006711#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006712/*[clinic input]
6713os.setegid
6714
6715 egid: gid_t
6716 /
6717
6718Set the current process's effective group id.
6719[clinic start generated code]*/
6720
Larry Hastings2f936352014-08-05 14:04:04 +10006721static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006722os_setegid_impl(PyObject *module, gid_t egid)
6723/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006724{
6725 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006726 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006727 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006728}
6729#endif /* HAVE_SETEGID */
6730
Larry Hastings2f936352014-08-05 14:04:04 +10006731
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006732#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10006733/*[clinic input]
6734os.setreuid
6735
6736 ruid: uid_t
6737 euid: uid_t
6738 /
6739
6740Set the current process's real and effective user ids.
6741[clinic start generated code]*/
6742
Larry Hastings2f936352014-08-05 14:04:04 +10006743static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006744os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
6745/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006746{
Victor Stinner8c62be82010-05-06 00:08:46 +00006747 if (setreuid(ruid, euid) < 0) {
6748 return posix_error();
6749 } else {
6750 Py_INCREF(Py_None);
6751 return Py_None;
6752 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006753}
6754#endif /* HAVE_SETREUID */
6755
Larry Hastings2f936352014-08-05 14:04:04 +10006756
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006757#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10006758/*[clinic input]
6759os.setregid
6760
6761 rgid: gid_t
6762 egid: gid_t
6763 /
6764
6765Set the current process's real and effective group ids.
6766[clinic start generated code]*/
6767
Larry Hastings2f936352014-08-05 14:04:04 +10006768static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006769os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
6770/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006771{
6772 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00006773 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006774 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006775}
6776#endif /* HAVE_SETREGID */
6777
Larry Hastings2f936352014-08-05 14:04:04 +10006778
Guido van Rossumb6775db1994-08-01 11:34:53 +00006779#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006780/*[clinic input]
6781os.setgid
6782 gid: gid_t
6783 /
6784
6785Set the current process's 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_setgid_impl(PyObject *module, gid_t gid)
6790/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006791{
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 if (setgid(gid) < 0)
6793 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10006794 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006795}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006796#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006797
Larry Hastings2f936352014-08-05 14:04:04 +10006798
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006799#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10006800/*[clinic input]
6801os.setgroups
6802
6803 groups: object
6804 /
6805
6806Set the groups of the current process to list.
6807[clinic start generated code]*/
6808
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006810os_setgroups(PyObject *module, PyObject *groups)
6811/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006812{
Victor Stinner8c62be82010-05-06 00:08:46 +00006813 int i, len;
6814 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006815
Victor Stinner8c62be82010-05-06 00:08:46 +00006816 if (!PySequence_Check(groups)) {
6817 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6818 return NULL;
6819 }
6820 len = PySequence_Size(groups);
6821 if (len > MAX_GROUPS) {
6822 PyErr_SetString(PyExc_ValueError, "too many groups");
6823 return NULL;
6824 }
6825 for(i = 0; i < len; i++) {
6826 PyObject *elem;
6827 elem = PySequence_GetItem(groups, i);
6828 if (!elem)
6829 return NULL;
6830 if (!PyLong_Check(elem)) {
6831 PyErr_SetString(PyExc_TypeError,
6832 "groups must be integers");
6833 Py_DECREF(elem);
6834 return NULL;
6835 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006836 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006837 Py_DECREF(elem);
6838 return NULL;
6839 }
6840 }
6841 Py_DECREF(elem);
6842 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006843
Victor Stinner8c62be82010-05-06 00:08:46 +00006844 if (setgroups(len, grouplist) < 0)
6845 return posix_error();
6846 Py_INCREF(Py_None);
6847 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006848}
6849#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006850
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006851#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6852static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006853wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006854{
Victor Stinner8c62be82010-05-06 00:08:46 +00006855 PyObject *result;
6856 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006857 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006858
Victor Stinner8c62be82010-05-06 00:08:46 +00006859 if (pid == -1)
6860 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006861
Victor Stinner8c62be82010-05-06 00:08:46 +00006862 if (struct_rusage == NULL) {
6863 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6864 if (m == NULL)
6865 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006866 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 Py_DECREF(m);
6868 if (struct_rusage == NULL)
6869 return NULL;
6870 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006871
Victor Stinner8c62be82010-05-06 00:08:46 +00006872 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6873 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6874 if (!result)
6875 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006876
6877#ifndef doubletime
6878#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6879#endif
6880
Victor Stinner8c62be82010-05-06 00:08:46 +00006881 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006882 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006883 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006884 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006885#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006886 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6887 SET_INT(result, 2, ru->ru_maxrss);
6888 SET_INT(result, 3, ru->ru_ixrss);
6889 SET_INT(result, 4, ru->ru_idrss);
6890 SET_INT(result, 5, ru->ru_isrss);
6891 SET_INT(result, 6, ru->ru_minflt);
6892 SET_INT(result, 7, ru->ru_majflt);
6893 SET_INT(result, 8, ru->ru_nswap);
6894 SET_INT(result, 9, ru->ru_inblock);
6895 SET_INT(result, 10, ru->ru_oublock);
6896 SET_INT(result, 11, ru->ru_msgsnd);
6897 SET_INT(result, 12, ru->ru_msgrcv);
6898 SET_INT(result, 13, ru->ru_nsignals);
6899 SET_INT(result, 14, ru->ru_nvcsw);
6900 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006901#undef SET_INT
6902
Victor Stinner8c62be82010-05-06 00:08:46 +00006903 if (PyErr_Occurred()) {
6904 Py_DECREF(result);
6905 return NULL;
6906 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006907
Victor Stinner8c62be82010-05-06 00:08:46 +00006908 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006909}
6910#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6911
Larry Hastings2f936352014-08-05 14:04:04 +10006912
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006913#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10006914/*[clinic input]
6915os.wait3
6916
6917 options: int
6918Wait for completion of a child process.
6919
6920Returns a tuple of information about the child process:
6921 (pid, status, rusage)
6922[clinic start generated code]*/
6923
Larry Hastings2f936352014-08-05 14:04:04 +10006924static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006925os_wait3_impl(PyObject *module, int options)
6926/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006927{
Victor Stinner8c62be82010-05-06 00:08:46 +00006928 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00006929 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006930 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006931 WAIT_TYPE status;
6932 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006933
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006934 do {
6935 Py_BEGIN_ALLOW_THREADS
6936 pid = wait3(&status, options, &ru);
6937 Py_END_ALLOW_THREADS
6938 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6939 if (pid < 0)
6940 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006941
Victor Stinner4195b5c2012-02-08 23:03:19 +01006942 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006943}
6944#endif /* HAVE_WAIT3 */
6945
Larry Hastings2f936352014-08-05 14:04:04 +10006946
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006947#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10006948/*[clinic input]
6949
6950os.wait4
6951
6952 pid: pid_t
6953 options: int
6954
6955Wait for completion of a specific child process.
6956
6957Returns a tuple of information about the child process:
6958 (pid, status, rusage)
6959[clinic start generated code]*/
6960
Larry Hastings2f936352014-08-05 14:04:04 +10006961static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006962os_wait4_impl(PyObject *module, pid_t pid, int options)
6963/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006964{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006965 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00006966 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006967 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00006968 WAIT_TYPE status;
6969 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006970
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006971 do {
6972 Py_BEGIN_ALLOW_THREADS
6973 res = wait4(pid, &status, options, &ru);
6974 Py_END_ALLOW_THREADS
6975 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6976 if (res < 0)
6977 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006978
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00006979 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006980}
6981#endif /* HAVE_WAIT4 */
6982
Larry Hastings2f936352014-08-05 14:04:04 +10006983
Ross Lagerwall7807c352011-03-17 20:20:30 +02006984#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10006985/*[clinic input]
6986os.waitid
6987
6988 idtype: idtype_t
6989 Must be one of be P_PID, P_PGID or P_ALL.
6990 id: id_t
6991 The id to wait on.
6992 options: int
6993 Constructed from the ORing of one or more of WEXITED, WSTOPPED
6994 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
6995 /
6996
6997Returns the result of waiting for a process or processes.
6998
6999Returns either waitid_result or None if WNOHANG is specified and there are
7000no children in a waitable state.
7001[clinic start generated code]*/
7002
Larry Hastings2f936352014-08-05 14:04:04 +10007003static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007004os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7005/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007006{
7007 PyObject *result;
7008 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007009 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007010 siginfo_t si;
7011 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007012
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007013 do {
7014 Py_BEGIN_ALLOW_THREADS
7015 res = waitid(idtype, id, &si, options);
7016 Py_END_ALLOW_THREADS
7017 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7018 if (res < 0)
7019 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007020
7021 if (si.si_pid == 0)
7022 Py_RETURN_NONE;
7023
7024 result = PyStructSequence_New(&WaitidResultType);
7025 if (!result)
7026 return NULL;
7027
7028 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007029 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007030 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7031 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7032 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7033 if (PyErr_Occurred()) {
7034 Py_DECREF(result);
7035 return NULL;
7036 }
7037
7038 return result;
7039}
Larry Hastings2f936352014-08-05 14:04:04 +10007040#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007041
Larry Hastings2f936352014-08-05 14:04:04 +10007042
7043#if defined(HAVE_WAITPID)
7044/*[clinic input]
7045os.waitpid
7046 pid: pid_t
7047 options: int
7048 /
7049
7050Wait for completion of a given child process.
7051
7052Returns a tuple of information regarding the child process:
7053 (pid, status)
7054
7055The options argument is ignored on Windows.
7056[clinic start generated code]*/
7057
Larry Hastings2f936352014-08-05 14:04:04 +10007058static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007059os_waitpid_impl(PyObject *module, pid_t pid, int options)
7060/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007061{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007062 pid_t res;
7063 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007064 WAIT_TYPE status;
7065 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007066
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007067 do {
7068 Py_BEGIN_ALLOW_THREADS
7069 res = waitpid(pid, &status, options);
7070 Py_END_ALLOW_THREADS
7071 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7072 if (res < 0)
7073 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007074
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007075 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007076}
Tim Petersab034fa2002-02-01 11:27:43 +00007077#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007078/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007079/*[clinic input]
7080os.waitpid
7081 pid: Py_intptr_t
7082 options: int
7083 /
7084
7085Wait for completion of a given process.
7086
7087Returns a tuple of information regarding the process:
7088 (pid, status << 8)
7089
7090The options argument is ignored on Windows.
7091[clinic start generated code]*/
7092
Larry Hastings2f936352014-08-05 14:04:04 +10007093static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007094os_waitpid_impl(PyObject *module, Py_intptr_t pid, int options)
7095/*[clinic end generated code: output=15f1ce005a346b09 input=444c8f51cca5b862]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007096{
7097 int status;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007098 Py_intptr_t res;
7099 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007100
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007101 do {
7102 Py_BEGIN_ALLOW_THREADS
7103 res = _cwait(&status, pid, options);
7104 Py_END_ALLOW_THREADS
7105 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007106 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007107 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007108
Victor Stinner8c62be82010-05-06 00:08:46 +00007109 /* shift the status left a byte so this is more like the POSIX waitpid */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007110 return Py_BuildValue(_Py_PARSE_INTPTR "i", res, status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007111}
Larry Hastings2f936352014-08-05 14:04:04 +10007112#endif
7113
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007114
Guido van Rossumad0ee831995-03-01 10:34:45 +00007115#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007116/*[clinic input]
7117os.wait
7118
7119Wait for completion of a child process.
7120
7121Returns a tuple of information about the child process:
7122 (pid, status)
7123[clinic start generated code]*/
7124
Larry Hastings2f936352014-08-05 14:04:04 +10007125static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007126os_wait_impl(PyObject *module)
7127/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007128{
Victor Stinner8c62be82010-05-06 00:08:46 +00007129 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007130 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007131 WAIT_TYPE status;
7132 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00007133
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007134 do {
7135 Py_BEGIN_ALLOW_THREADS
7136 pid = wait(&status);
7137 Py_END_ALLOW_THREADS
7138 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7139 if (pid < 0)
7140 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007141
Victor Stinner8c62be82010-05-06 00:08:46 +00007142 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00007143}
Larry Hastings2f936352014-08-05 14:04:04 +10007144#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007145
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007146
Larry Hastings9cf065c2012-06-22 16:30:09 -07007147#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
7148PyDoc_STRVAR(readlink__doc__,
7149"readlink(path, *, dir_fd=None) -> path\n\n\
7150Return a string representing the path to which the symbolic link points.\n\
7151\n\
7152If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7153 and path should be relative; path will then be relative to that directory.\n\
7154dir_fd may not be implemented on your platform.\n\
7155 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007156#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007157
Guido van Rossumb6775db1994-08-01 11:34:53 +00007158#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007159
Larry Hastings2f936352014-08-05 14:04:04 +10007160/* AC 3.5: merge win32 and not together */
Barry Warsaw53699e91996-12-10 23:23:01 +00007161static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007162posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007163{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007164 path_t path;
7165 int dir_fd = DEFAULT_DIR_FD;
7166 char buffer[MAXPATHLEN];
7167 ssize_t length;
7168 PyObject *return_value = NULL;
7169 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00007170
Larry Hastings9cf065c2012-06-22 16:30:09 -07007171 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007172 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007173 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
7174 path_converter, &path,
Larry Hastings2f936352014-08-05 14:04:04 +10007175 READLINKAT_DIR_FD_CONVERTER, &dir_fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00007177
Victor Stinner8c62be82010-05-06 00:08:46 +00007178 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007179#ifdef HAVE_READLINKAT
7180 if (dir_fd != DEFAULT_DIR_FD)
7181 length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer));
Victor Stinnera45598a2010-05-14 16:35:39 +00007182 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007183#endif
7184 length = readlink(path.narrow, buffer, sizeof(buffer));
7185 Py_END_ALLOW_THREADS
7186
7187 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01007188 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007189 goto exit;
7190 }
7191
7192 if (PyUnicode_Check(path.object))
7193 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
7194 else
7195 return_value = PyBytes_FromStringAndSize(buffer, length);
7196exit:
7197 path_cleanup(&path);
7198 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007199}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007200
Guido van Rossumb6775db1994-08-01 11:34:53 +00007201#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007202
Larry Hastings2f936352014-08-05 14:04:04 +10007203#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
7204
7205static PyObject *
7206win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
7207{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007208 const wchar_t *path;
Larry Hastings2f936352014-08-05 14:04:04 +10007209 DWORD n_bytes_returned;
7210 DWORD io_result;
7211 PyObject *po, *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007212 int dir_fd;
Larry Hastings2f936352014-08-05 14:04:04 +10007213 HANDLE reparse_point_handle;
7214
Martin Panter70214ad2016-08-04 02:38:59 +00007215 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
7216 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007217 const wchar_t *print_name;
Larry Hastings2f936352014-08-05 14:04:04 +10007218
7219 static char *keywords[] = {"path", "dir_fd", NULL};
7220
7221 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
7222 &po,
7223 dir_fd_unavailable, &dir_fd
7224 ))
7225 return NULL;
7226
7227 path = PyUnicode_AsUnicode(po);
7228 if (path == NULL)
7229 return NULL;
7230
7231 /* First get a handle to the reparse point */
7232 Py_BEGIN_ALLOW_THREADS
7233 reparse_point_handle = CreateFileW(
7234 path,
7235 0,
7236 0,
7237 0,
7238 OPEN_EXISTING,
7239 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7240 0);
7241 Py_END_ALLOW_THREADS
7242
7243 if (reparse_point_handle==INVALID_HANDLE_VALUE)
7244 return win32_error_object("readlink", po);
7245
7246 Py_BEGIN_ALLOW_THREADS
7247 /* New call DeviceIoControl to read the reparse point */
7248 io_result = DeviceIoControl(
7249 reparse_point_handle,
7250 FSCTL_GET_REPARSE_POINT,
7251 0, 0, /* in buffer */
7252 target_buffer, sizeof(target_buffer),
7253 &n_bytes_returned,
7254 0 /* we're not using OVERLAPPED_IO */
7255 );
7256 CloseHandle(reparse_point_handle);
7257 Py_END_ALLOW_THREADS
7258
7259 if (io_result==0)
7260 return win32_error_object("readlink", po);
7261
7262 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7263 {
7264 PyErr_SetString(PyExc_ValueError,
7265 "not a symbolic link");
7266 return NULL;
7267 }
7268 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7269 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7270
7271 result = PyUnicode_FromWideChar(print_name,
7272 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
7273 return result;
7274}
7275
7276#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7277
7278
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007279
Larry Hastings9cf065c2012-06-22 16:30:09 -07007280#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07007281
7282#if defined(MS_WINDOWS)
7283
7284/* Grab CreateSymbolicLinkW dynamically from kernel32 */
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007285static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPCWSTR, LPCWSTR, DWORD) = NULL;
7286static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPCSTR, LPCSTR, DWORD) = NULL;
Victor Stinner31b3b922013-06-05 01:49:17 +02007287
Larry Hastings9cf065c2012-06-22 16:30:09 -07007288static int
Victor Stinner31b3b922013-06-05 01:49:17 +02007289check_CreateSymbolicLink(void)
Larry Hastings9cf065c2012-06-22 16:30:09 -07007290{
7291 HINSTANCE hKernel32;
7292 /* only recheck */
7293 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA)
7294 return 1;
7295 hKernel32 = GetModuleHandleW(L"KERNEL32");
7296 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
7297 "CreateSymbolicLinkW");
7298 *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32,
7299 "CreateSymbolicLinkA");
7300 return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA);
7301}
7302
Victor Stinner31b3b922013-06-05 01:49:17 +02007303/* Remove the last portion of the path */
7304static void
7305_dirnameW(WCHAR *path)
7306{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007307 WCHAR *ptr;
7308
7309 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02007310 for(ptr = path + wcslen(path); ptr != path; ptr--) {
Victor Stinner072318b2013-06-05 02:07:46 +02007311 if (*ptr == L'\\' || *ptr == L'/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04007312 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007313 }
7314 *ptr = 0;
7315}
7316
Victor Stinner31b3b922013-06-05 01:49:17 +02007317/* Remove the last portion of the path */
7318static void
7319_dirnameA(char *path)
7320{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007321 char *ptr;
7322
7323 /* walk the path from the end until a backslash is encountered */
Victor Stinner31b3b922013-06-05 01:49:17 +02007324 for(ptr = path + strlen(path); ptr != path; ptr--) {
7325 if (*ptr == '\\' || *ptr == '/')
Jason R. Coombs3a092862013-05-27 23:21:28 -04007326 break;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007327 }
7328 *ptr = 0;
7329}
7330
Victor Stinner31b3b922013-06-05 01:49:17 +02007331/* Is this path absolute? */
7332static int
7333_is_absW(const WCHAR *path)
7334{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007335 return path[0] == L'\\' || path[0] == L'/' || path[1] == L':';
7336
7337}
7338
Victor Stinner31b3b922013-06-05 01:49:17 +02007339/* Is this path absolute? */
7340static int
7341_is_absA(const char *path)
7342{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007343 return path[0] == '\\' || path[0] == '/' || path[1] == ':';
7344
7345}
7346
Victor Stinner31b3b922013-06-05 01:49:17 +02007347/* join root and rest with a backslash */
7348static void
7349_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
7350{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02007351 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007352
Victor Stinner31b3b922013-06-05 01:49:17 +02007353 if (_is_absW(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007354 wcscpy(dest_path, rest);
7355 return;
7356 }
7357
7358 root_len = wcslen(root);
7359
7360 wcscpy(dest_path, root);
7361 if(root_len) {
Victor Stinner31b3b922013-06-05 01:49:17 +02007362 dest_path[root_len] = L'\\';
7363 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007364 }
7365 wcscpy(dest_path+root_len, rest);
7366}
7367
Victor Stinner31b3b922013-06-05 01:49:17 +02007368/* join root and rest with a backslash */
7369static void
7370_joinA(char *dest_path, const char *root, const char *rest)
7371{
Victor Stinnere7e7eba2013-06-05 00:35:54 +02007372 size_t root_len;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007373
Victor Stinner31b3b922013-06-05 01:49:17 +02007374 if (_is_absA(rest)) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007375 strcpy(dest_path, rest);
7376 return;
7377 }
7378
7379 root_len = strlen(root);
7380
7381 strcpy(dest_path, root);
7382 if(root_len) {
7383 dest_path[root_len] = '\\';
Victor Stinner31b3b922013-06-05 01:49:17 +02007384 root_len++;
Jason R. Coombs3a092862013-05-27 23:21:28 -04007385 }
7386 strcpy(dest_path+root_len, rest);
7387}
7388
Victor Stinner31b3b922013-06-05 01:49:17 +02007389/* Return True if the path at src relative to dest is a directory */
7390static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007391_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007392{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007393 WIN32_FILE_ATTRIBUTE_DATA src_info;
7394 WCHAR dest_parent[MAX_PATH];
7395 WCHAR src_resolved[MAX_PATH] = L"";
7396
7397 /* dest_parent = os.path.dirname(dest) */
7398 wcscpy(dest_parent, dest);
7399 _dirnameW(dest_parent);
7400 /* src_resolved = os.path.join(dest_parent, src) */
7401 _joinW(src_resolved, dest_parent, src);
7402 return (
7403 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
7404 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7405 );
7406}
7407
Victor Stinner31b3b922013-06-05 01:49:17 +02007408/* Return True if the path at src relative to dest is a directory */
7409static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03007410_check_dirA(LPCSTR src, LPCSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04007411{
Jason R. Coombs3a092862013-05-27 23:21:28 -04007412 WIN32_FILE_ATTRIBUTE_DATA src_info;
7413 char dest_parent[MAX_PATH];
7414 char src_resolved[MAX_PATH] = "";
7415
7416 /* dest_parent = os.path.dirname(dest) */
7417 strcpy(dest_parent, dest);
Victor Stinner5a436762013-06-05 00:37:12 +02007418 _dirnameA(dest_parent);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007419 /* src_resolved = os.path.join(dest_parent, src) */
Victor Stinner5a436762013-06-05 00:37:12 +02007420 _joinA(src_resolved, dest_parent, src);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007421 return (
7422 GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info)
7423 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
7424 );
7425}
Larry Hastings9cf065c2012-06-22 16:30:09 -07007426#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007427
Larry Hastings2f936352014-08-05 14:04:04 +10007428
7429/*[clinic input]
7430os.symlink
7431 src: path_t
7432 dst: path_t
7433 target_is_directory: bool = False
7434 *
7435 dir_fd: dir_fd(requires='symlinkat')=None
7436
7437# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
7438
7439Create a symbolic link pointing to src named dst.
7440
7441target_is_directory is required on Windows if the target is to be
7442 interpreted as a directory. (On Windows, symlink requires
7443 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
7444 target_is_directory is ignored on non-Windows platforms.
7445
7446If dir_fd is not None, it should be a file descriptor open to a directory,
7447 and path should be relative; path will then be relative to that directory.
7448dir_fd may not be implemented on your platform.
7449 If it is unavailable, using it will raise a NotImplementedError.
7450
7451[clinic start generated code]*/
7452
Larry Hastings2f936352014-08-05 14:04:04 +10007453static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007454os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04007455 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007456/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007457{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007458#ifdef MS_WINDOWS
7459 DWORD result;
7460#else
7461 int result;
7462#endif
7463
Larry Hastings9cf065c2012-06-22 16:30:09 -07007464#ifdef MS_WINDOWS
7465 if (!check_CreateSymbolicLink()) {
7466 PyErr_SetString(PyExc_NotImplementedError,
7467 "CreateSymbolicLink functions not found");
Larry Hastings2f936352014-08-05 14:04:04 +10007468 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007469 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007470 if (!win32_can_symlink) {
7471 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Larry Hastings2f936352014-08-05 14:04:04 +10007472 return NULL;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03007473 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007474#endif
7475
Larry Hastings2f936352014-08-05 14:04:04 +10007476 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07007477 PyErr_SetString(PyExc_ValueError,
7478 "symlink: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10007479 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007480 }
7481
7482#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04007483
Larry Hastings9cf065c2012-06-22 16:30:09 -07007484 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10007485 if (dst->wide) {
Jason R. Coombs3a092862013-05-27 23:21:28 -04007486 /* if src is a directory, ensure target_is_directory==1 */
Larry Hastings2f936352014-08-05 14:04:04 +10007487 target_is_directory |= _check_dirW(src->wide, dst->wide);
7488 result = Py_CreateSymbolicLinkW(dst->wide, src->wide,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007489 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007490 }
7491 else {
7492 /* if src is a directory, ensure target_is_directory==1 */
Larry Hastings2f936352014-08-05 14:04:04 +10007493 target_is_directory |= _check_dirA(src->narrow, dst->narrow);
7494 result = Py_CreateSymbolicLinkA(dst->narrow, src->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007495 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04007496 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07007497 Py_END_ALLOW_THREADS
7498
Larry Hastings2f936352014-08-05 14:04:04 +10007499 if (!result)
7500 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007501
7502#else
7503
7504 Py_BEGIN_ALLOW_THREADS
7505#if HAVE_SYMLINKAT
7506 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10007507 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007508 else
7509#endif
Larry Hastings2f936352014-08-05 14:04:04 +10007510 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007511 Py_END_ALLOW_THREADS
7512
Larry Hastings2f936352014-08-05 14:04:04 +10007513 if (result)
7514 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007515#endif
7516
Larry Hastings2f936352014-08-05 14:04:04 +10007517 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007518}
7519#endif /* HAVE_SYMLINK */
7520
Larry Hastings9cf065c2012-06-22 16:30:09 -07007521
Brian Curtind40e6f72010-07-08 21:39:08 +00007522
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007523
Larry Hastings605a62d2012-06-24 04:33:36 -07007524static PyStructSequence_Field times_result_fields[] = {
7525 {"user", "user time"},
7526 {"system", "system time"},
7527 {"children_user", "user time of children"},
7528 {"children_system", "system time of children"},
7529 {"elapsed", "elapsed time since an arbitrary point in the past"},
7530 {NULL}
7531};
7532
7533PyDoc_STRVAR(times_result__doc__,
7534"times_result: Result from os.times().\n\n\
7535This object may be accessed either as a tuple of\n\
7536 (user, system, children_user, children_system, elapsed),\n\
7537or via the attributes user, system, children_user, children_system,\n\
7538and elapsed.\n\
7539\n\
7540See os.times for more information.");
7541
7542static PyStructSequence_Desc times_result_desc = {
7543 "times_result", /* name */
7544 times_result__doc__, /* doc */
7545 times_result_fields,
7546 5
7547};
7548
7549static PyTypeObject TimesResultType;
7550
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007551#ifdef MS_WINDOWS
7552#define HAVE_TIMES /* mandatory, for the method table */
7553#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007554
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007555#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007556
7557static PyObject *
7558build_times_result(double user, double system,
7559 double children_user, double children_system,
7560 double elapsed)
7561{
7562 PyObject *value = PyStructSequence_New(&TimesResultType);
7563 if (value == NULL)
7564 return NULL;
7565
7566#define SET(i, field) \
7567 { \
7568 PyObject *o = PyFloat_FromDouble(field); \
7569 if (!o) { \
7570 Py_DECREF(value); \
7571 return NULL; \
7572 } \
7573 PyStructSequence_SET_ITEM(value, i, o); \
7574 } \
7575
7576 SET(0, user);
7577 SET(1, system);
7578 SET(2, children_user);
7579 SET(3, children_system);
7580 SET(4, elapsed);
7581
7582#undef SET
7583
7584 return value;
7585}
7586
Larry Hastings605a62d2012-06-24 04:33:36 -07007587
Larry Hastings2f936352014-08-05 14:04:04 +10007588#ifndef MS_WINDOWS
7589#define NEED_TICKS_PER_SECOND
7590static long ticks_per_second = -1;
7591#endif /* MS_WINDOWS */
7592
7593/*[clinic input]
7594os.times
7595
7596Return a collection containing process timing information.
7597
7598The object returned behaves like a named tuple with these fields:
7599 (utime, stime, cutime, cstime, elapsed_time)
7600All fields are floating point numbers.
7601[clinic start generated code]*/
7602
Larry Hastings2f936352014-08-05 14:04:04 +10007603static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007604os_times_impl(PyObject *module)
7605/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007606#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007607{
Victor Stinner8c62be82010-05-06 00:08:46 +00007608 FILETIME create, exit, kernel, user;
7609 HANDLE hProc;
7610 hProc = GetCurrentProcess();
7611 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7612 /* The fields of a FILETIME structure are the hi and lo part
7613 of a 64-bit value expressed in 100 nanosecond units.
7614 1e7 is one second in such units; 1e-7 the inverse.
7615 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7616 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007617 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007618 (double)(user.dwHighDateTime*429.4967296 +
7619 user.dwLowDateTime*1e-7),
7620 (double)(kernel.dwHighDateTime*429.4967296 +
7621 kernel.dwLowDateTime*1e-7),
7622 (double)0,
7623 (double)0,
7624 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007625}
Larry Hastings2f936352014-08-05 14:04:04 +10007626#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007627{
Larry Hastings2f936352014-08-05 14:04:04 +10007628
7629
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007630 struct tms t;
7631 clock_t c;
7632 errno = 0;
7633 c = times(&t);
7634 if (c == (clock_t) -1)
7635 return posix_error();
7636 return build_times_result(
7637 (double)t.tms_utime / ticks_per_second,
7638 (double)t.tms_stime / ticks_per_second,
7639 (double)t.tms_cutime / ticks_per_second,
7640 (double)t.tms_cstime / ticks_per_second,
7641 (double)c / ticks_per_second);
7642}
Larry Hastings2f936352014-08-05 14:04:04 +10007643#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007644#endif /* HAVE_TIMES */
7645
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007646
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007647#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007648/*[clinic input]
7649os.getsid
7650
7651 pid: pid_t
7652 /
7653
7654Call the system call getsid(pid) and return the result.
7655[clinic start generated code]*/
7656
Larry Hastings2f936352014-08-05 14:04:04 +10007657static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007658os_getsid_impl(PyObject *module, pid_t pid)
7659/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007660{
Victor Stinner8c62be82010-05-06 00:08:46 +00007661 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007662 sid = getsid(pid);
7663 if (sid < 0)
7664 return posix_error();
7665 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007666}
7667#endif /* HAVE_GETSID */
7668
7669
Guido van Rossumb6775db1994-08-01 11:34:53 +00007670#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10007671/*[clinic input]
7672os.setsid
7673
7674Call the system call setsid().
7675[clinic start generated code]*/
7676
Larry Hastings2f936352014-08-05 14:04:04 +10007677static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007678os_setsid_impl(PyObject *module)
7679/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007680{
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 if (setsid() < 0)
7682 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007683 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007684}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007685#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007686
Larry Hastings2f936352014-08-05 14:04:04 +10007687
Guido van Rossumb6775db1994-08-01 11:34:53 +00007688#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007689/*[clinic input]
7690os.setpgid
7691
7692 pid: pid_t
7693 pgrp: pid_t
7694 /
7695
7696Call the system call setpgid(pid, pgrp).
7697[clinic start generated code]*/
7698
Larry Hastings2f936352014-08-05 14:04:04 +10007699static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007700os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
7701/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007702{
Victor Stinner8c62be82010-05-06 00:08:46 +00007703 if (setpgid(pid, pgrp) < 0)
7704 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007705 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007706}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007707#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007708
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007709
Guido van Rossumb6775db1994-08-01 11:34:53 +00007710#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007711/*[clinic input]
7712os.tcgetpgrp
7713
7714 fd: int
7715 /
7716
7717Return the process group associated with the terminal specified by fd.
7718[clinic start generated code]*/
7719
Larry Hastings2f936352014-08-05 14:04:04 +10007720static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007721os_tcgetpgrp_impl(PyObject *module, int fd)
7722/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007723{
7724 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007725 if (pgid < 0)
7726 return posix_error();
7727 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007728}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007729#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007730
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007731
Guido van Rossumb6775db1994-08-01 11:34:53 +00007732#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007733/*[clinic input]
7734os.tcsetpgrp
7735
7736 fd: int
7737 pgid: pid_t
7738 /
7739
7740Set the process group associated with the terminal specified by fd.
7741[clinic start generated code]*/
7742
Larry Hastings2f936352014-08-05 14:04:04 +10007743static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007744os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
7745/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007746{
Victor Stinner8c62be82010-05-06 00:08:46 +00007747 if (tcsetpgrp(fd, pgid) < 0)
7748 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007749 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007750}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007751#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007752
Guido van Rossum687dd131993-05-17 08:34:16 +00007753/* Functions acting on file descriptors */
7754
Victor Stinnerdaf45552013-08-28 00:53:59 +02007755#ifdef O_CLOEXEC
7756extern int _Py_open_cloexec_works;
7757#endif
7758
Larry Hastings2f936352014-08-05 14:04:04 +10007759
7760/*[clinic input]
7761os.open -> int
7762 path: path_t
7763 flags: int
7764 mode: int = 0o777
7765 *
7766 dir_fd: dir_fd(requires='openat') = None
7767
7768# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7769
7770Open a file for low level IO. Returns a file descriptor (integer).
7771
7772If dir_fd is not None, it should be a file descriptor open to a directory,
7773 and path should be relative; path will then be relative to that directory.
7774dir_fd may not be implemented on your platform.
7775 If it is unavailable, using it will raise a NotImplementedError.
7776[clinic start generated code]*/
7777
Larry Hastings2f936352014-08-05 14:04:04 +10007778static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007779os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
7780/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007781{
7782 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007783 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007784
Victor Stinnerdaf45552013-08-28 00:53:59 +02007785#ifdef O_CLOEXEC
7786 int *atomic_flag_works = &_Py_open_cloexec_works;
7787#elif !defined(MS_WINDOWS)
7788 int *atomic_flag_works = NULL;
7789#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007790
Victor Stinnerdaf45552013-08-28 00:53:59 +02007791#ifdef MS_WINDOWS
7792 flags |= O_NOINHERIT;
7793#elif defined(O_CLOEXEC)
7794 flags |= O_CLOEXEC;
7795#endif
7796
Steve Dower8fc89802015-04-12 00:26:27 -04007797 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007798 do {
7799 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007800#ifdef MS_WINDOWS
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007801 if (path->wide)
7802 fd = _wopen(path->wide, flags, mode);
7803 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007804#endif
7805#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007806 if (dir_fd != DEFAULT_DIR_FD)
7807 fd = openat(dir_fd, path->narrow, flags, mode);
7808 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07007809#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007810 fd = open(path->narrow, flags, mode);
7811 Py_END_ALLOW_THREADS
7812 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04007813 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00007814
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007815 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007816 if (!async_err)
7817 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10007818 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007819 }
7820
Victor Stinnerdaf45552013-08-28 00:53:59 +02007821#ifndef MS_WINDOWS
7822 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
7823 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10007824 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007825 }
7826#endif
7827
Larry Hastings2f936352014-08-05 14:04:04 +10007828 return fd;
7829}
7830
7831
7832/*[clinic input]
7833os.close
7834
7835 fd: int
7836
7837Close a file descriptor.
7838[clinic start generated code]*/
7839
Barry Warsaw53699e91996-12-10 23:23:01 +00007840static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007841os_close_impl(PyObject *module, int fd)
7842/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00007843{
Larry Hastings2f936352014-08-05 14:04:04 +10007844 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007845 if (!_PyVerify_fd(fd))
7846 return posix_error();
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007847 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
7848 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
7849 * for more details.
7850 */
Victor Stinner8c62be82010-05-06 00:08:46 +00007851 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007852 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007853 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04007854 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007855 Py_END_ALLOW_THREADS
7856 if (res < 0)
7857 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007858 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007859}
7860
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007861
Larry Hastings2f936352014-08-05 14:04:04 +10007862/*[clinic input]
7863os.closerange
7864
7865 fd_low: int
7866 fd_high: int
7867 /
7868
7869Closes all file descriptors in [fd_low, fd_high), ignoring errors.
7870[clinic start generated code]*/
7871
Larry Hastings2f936352014-08-05 14:04:04 +10007872static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007873os_closerange_impl(PyObject *module, int fd_low, int fd_high)
7874/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007875{
7876 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00007877 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007878 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10007879 for (i = fd_low; i < fd_high; i++)
Victor Stinner8c62be82010-05-06 00:08:46 +00007880 if (_PyVerify_fd(i))
7881 close(i);
Steve Dower8fc89802015-04-12 00:26:27 -04007882 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007883 Py_END_ALLOW_THREADS
7884 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007885}
7886
7887
Larry Hastings2f936352014-08-05 14:04:04 +10007888/*[clinic input]
7889os.dup -> int
7890
7891 fd: int
7892 /
7893
7894Return a duplicate of a file descriptor.
7895[clinic start generated code]*/
7896
Larry Hastings2f936352014-08-05 14:04:04 +10007897static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007898os_dup_impl(PyObject *module, int fd)
7899/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007900{
7901 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007902}
7903
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007904
Larry Hastings2f936352014-08-05 14:04:04 +10007905/*[clinic input]
7906os.dup2
7907 fd: int
7908 fd2: int
7909 inheritable: bool=True
7910
7911Duplicate file descriptor.
7912[clinic start generated code]*/
7913
Larry Hastings2f936352014-08-05 14:04:04 +10007914static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007915os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
7916/*[clinic end generated code: output=db832a2d872ccc5f input=76e96f511be0352f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007917{
Victor Stinnerdaf45552013-08-28 00:53:59 +02007918 int res;
7919#if defined(HAVE_DUP3) && \
7920 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
7921 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
7922 int dup3_works = -1;
7923#endif
7924
Victor Stinner8c62be82010-05-06 00:08:46 +00007925 if (!_PyVerify_fd_dup2(fd, fd2))
7926 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007927
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007928 /* dup2() can fail with EINTR if the target FD is already open, because it
7929 * then has to be closed. See os_close_impl() for why we don't handle EINTR
7930 * upon close(), and therefore below.
7931 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02007932#ifdef MS_WINDOWS
7933 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04007934 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00007935 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04007936 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02007937 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00007938 if (res < 0)
7939 return posix_error();
Victor Stinnerdaf45552013-08-28 00:53:59 +02007940
7941 /* Character files like console cannot be make non-inheritable */
7942 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7943 close(fd2);
7944 return NULL;
7945 }
7946
7947#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
7948 Py_BEGIN_ALLOW_THREADS
7949 if (!inheritable)
7950 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
7951 else
7952 res = dup2(fd, fd2);
7953 Py_END_ALLOW_THREADS
7954 if (res < 0)
7955 return posix_error();
7956
7957#else
7958
7959#ifdef HAVE_DUP3
7960 if (!inheritable && dup3_works != 0) {
7961 Py_BEGIN_ALLOW_THREADS
7962 res = dup3(fd, fd2, O_CLOEXEC);
7963 Py_END_ALLOW_THREADS
7964 if (res < 0) {
7965 if (dup3_works == -1)
7966 dup3_works = (errno != ENOSYS);
7967 if (dup3_works)
7968 return posix_error();
7969 }
7970 }
7971
7972 if (inheritable || dup3_works == 0)
7973 {
7974#endif
7975 Py_BEGIN_ALLOW_THREADS
7976 res = dup2(fd, fd2);
7977 Py_END_ALLOW_THREADS
7978 if (res < 0)
7979 return posix_error();
7980
7981 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
7982 close(fd2);
7983 return NULL;
7984 }
7985#ifdef HAVE_DUP3
7986 }
7987#endif
7988
7989#endif
7990
Larry Hastings2f936352014-08-05 14:04:04 +10007991 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00007992}
7993
Larry Hastings2f936352014-08-05 14:04:04 +10007994
Ross Lagerwall7807c352011-03-17 20:20:30 +02007995#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10007996/*[clinic input]
7997os.lockf
7998
7999 fd: int
8000 An open file descriptor.
8001 command: int
8002 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8003 length: Py_off_t
8004 The number of bytes to lock, starting at the current position.
8005 /
8006
8007Apply, test or remove a POSIX lock on an open file descriptor.
8008
8009[clinic start generated code]*/
8010
Larry Hastings2f936352014-08-05 14:04:04 +10008011static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008012os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8013/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008014{
8015 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008016
8017 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008018 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008019 Py_END_ALLOW_THREADS
8020
8021 if (res < 0)
8022 return posix_error();
8023
8024 Py_RETURN_NONE;
8025}
Larry Hastings2f936352014-08-05 14:04:04 +10008026#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008027
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008028
Larry Hastings2f936352014-08-05 14:04:04 +10008029/*[clinic input]
8030os.lseek -> Py_off_t
8031
8032 fd: int
8033 position: Py_off_t
8034 how: int
8035 /
8036
8037Set the position of a file descriptor. Return the new position.
8038
8039Return the new cursor position in number of bytes
8040relative to the beginning of the file.
8041[clinic start generated code]*/
8042
Larry Hastings2f936352014-08-05 14:04:04 +10008043static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008044os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8045/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008046{
8047 Py_off_t result;
8048
8049 if (!_PyVerify_fd(fd)) {
8050 posix_error();
8051 return -1;
8052 }
Guido van Rossum687dd131993-05-17 08:34:16 +00008053#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008054 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8055 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008056 case 0: how = SEEK_SET; break;
8057 case 1: how = SEEK_CUR; break;
8058 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008059 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008060#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008061
Victor Stinner8c62be82010-05-06 00:08:46 +00008062 if (PyErr_Occurred())
Larry Hastings2f936352014-08-05 14:04:04 +10008063 return -1;
Guido van Rossum94f6f721999-01-06 18:42:14 +00008064
Larry Hastings2f936352014-08-05 14:04:04 +10008065 if (!_PyVerify_fd(fd)) {
8066 posix_error();
8067 return -1;
8068 }
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008070 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008071#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008072 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008073#else
Larry Hastings2f936352014-08-05 14:04:04 +10008074 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008075#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008076 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008078 if (result < 0)
8079 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008080
Larry Hastings2f936352014-08-05 14:04:04 +10008081 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008082}
8083
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008084
Larry Hastings2f936352014-08-05 14:04:04 +10008085/*[clinic input]
8086os.read
8087 fd: int
8088 length: Py_ssize_t
8089 /
8090
8091Read from a file descriptor. Returns a bytes object.
8092[clinic start generated code]*/
8093
Larry Hastings2f936352014-08-05 14:04:04 +10008094static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008095os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8096/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008097{
Victor Stinner8c62be82010-05-06 00:08:46 +00008098 Py_ssize_t n;
8099 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008100
8101 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 errno = EINVAL;
8103 return posix_error();
8104 }
Larry Hastings2f936352014-08-05 14:04:04 +10008105
8106#ifdef MS_WINDOWS
Victor Stinner66aab0c2015-03-19 22:53:20 +01008107 /* On Windows, the count parameter of read() is an int */
Larry Hastings2f936352014-08-05 14:04:04 +10008108 if (length > INT_MAX)
8109 length = INT_MAX;
Larry Hastings2f936352014-08-05 14:04:04 +10008110#endif
8111
8112 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008113 if (buffer == NULL)
8114 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008115
Victor Stinner66aab0c2015-03-19 22:53:20 +01008116 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8117 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008118 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008119 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008120 }
Larry Hastings2f936352014-08-05 14:04:04 +10008121
8122 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008124
Victor Stinner8c62be82010-05-06 00:08:46 +00008125 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008126}
8127
Ross Lagerwall7807c352011-03-17 20:20:30 +02008128#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
8129 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008130static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008131iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
8132{
8133 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008134 Py_ssize_t blen, total = 0;
8135
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008136 *iov = PyMem_New(struct iovec, cnt);
8137 if (*iov == NULL) {
8138 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008139 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008140 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008141
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008142 *buf = PyMem_New(Py_buffer, cnt);
8143 if (*buf == NULL) {
8144 PyMem_Del(*iov);
8145 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008146 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008147 }
8148
8149 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008150 PyObject *item = PySequence_GetItem(seq, i);
8151 if (item == NULL)
8152 goto fail;
8153 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
8154 Py_DECREF(item);
8155 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008156 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008157 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008158 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008159 blen = (*buf)[i].len;
8160 (*iov)[i].iov_len = blen;
8161 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008162 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008163 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02008164
8165fail:
8166 PyMem_Del(*iov);
8167 for (j = 0; j < i; j++) {
8168 PyBuffer_Release(&(*buf)[j]);
8169 }
8170 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01008171 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008172}
8173
8174static void
8175iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
8176{
8177 int i;
8178 PyMem_Del(iov);
8179 for (i = 0; i < cnt; i++) {
8180 PyBuffer_Release(&buf[i]);
8181 }
8182 PyMem_Del(buf);
8183}
8184#endif
8185
Larry Hastings2f936352014-08-05 14:04:04 +10008186
Ross Lagerwall7807c352011-03-17 20:20:30 +02008187#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10008188/*[clinic input]
8189os.readv -> Py_ssize_t
8190
8191 fd: int
8192 buffers: object
8193 /
8194
8195Read from a file descriptor fd into an iterable of buffers.
8196
8197The buffers should be mutable buffers accepting bytes.
8198readv will transfer data into each buffer until it is full
8199and then move on to the next buffer in the sequence to hold
8200the rest of the data.
8201
8202readv returns the total number of bytes read,
8203which may be less than the total capacity of all the buffers.
8204[clinic start generated code]*/
8205
Larry Hastings2f936352014-08-05 14:04:04 +10008206static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008207os_readv_impl(PyObject *module, int fd, PyObject *buffers)
8208/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008209{
8210 int cnt;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008211 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008212 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008213 struct iovec *iov;
8214 Py_buffer *buf;
8215
Larry Hastings2f936352014-08-05 14:04:04 +10008216 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008217 PyErr_SetString(PyExc_TypeError,
8218 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008219 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008220 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02008221
Larry Hastings2f936352014-08-05 14:04:04 +10008222 cnt = PySequence_Size(buffers);
8223
8224 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
8225 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008226
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008227 do {
8228 Py_BEGIN_ALLOW_THREADS
8229 n = readv(fd, iov, cnt);
8230 Py_END_ALLOW_THREADS
8231 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008232
8233 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10008234 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008235 if (!async_err)
8236 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008237 return -1;
8238 }
Victor Stinner57ddf782014-01-08 15:21:28 +01008239
Larry Hastings2f936352014-08-05 14:04:04 +10008240 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008241}
Larry Hastings2f936352014-08-05 14:04:04 +10008242#endif /* HAVE_READV */
8243
Ross Lagerwall7807c352011-03-17 20:20:30 +02008244
8245#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10008246/*[clinic input]
8247# TODO length should be size_t! but Python doesn't support parsing size_t yet.
8248os.pread
8249
8250 fd: int
8251 length: int
8252 offset: Py_off_t
8253 /
8254
8255Read a number of bytes from a file descriptor starting at a particular offset.
8256
8257Read length bytes from file descriptor fd, starting at offset bytes from
8258the beginning of the file. The file offset remains unchanged.
8259[clinic start generated code]*/
8260
Larry Hastings2f936352014-08-05 14:04:04 +10008261static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008262os_pread_impl(PyObject *module, int fd, int length, Py_off_t offset)
8263/*[clinic end generated code: output=435b29ee32b54a78 input=084948dcbaa35d4c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008264{
Ross Lagerwall7807c352011-03-17 20:20:30 +02008265 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008266 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008267 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008268
Larry Hastings2f936352014-08-05 14:04:04 +10008269 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008270 errno = EINVAL;
8271 return posix_error();
8272 }
Larry Hastings2f936352014-08-05 14:04:04 +10008273 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008274 if (buffer == NULL)
8275 return NULL;
8276 if (!_PyVerify_fd(fd)) {
8277 Py_DECREF(buffer);
8278 return posix_error();
8279 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008280
8281 do {
8282 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008283 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008284 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008285 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008286 Py_END_ALLOW_THREADS
8287 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8288
Ross Lagerwall7807c352011-03-17 20:20:30 +02008289 if (n < 0) {
8290 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008291 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008292 }
Larry Hastings2f936352014-08-05 14:04:04 +10008293 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008294 _PyBytes_Resize(&buffer, n);
8295 return buffer;
8296}
Larry Hastings2f936352014-08-05 14:04:04 +10008297#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008298
Larry Hastings2f936352014-08-05 14:04:04 +10008299
8300/*[clinic input]
8301os.write -> Py_ssize_t
8302
8303 fd: int
8304 data: Py_buffer
8305 /
8306
8307Write a bytes object to a file descriptor.
8308[clinic start generated code]*/
8309
Larry Hastings2f936352014-08-05 14:04:04 +10008310static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008311os_write_impl(PyObject *module, int fd, Py_buffer *data)
8312/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008313{
Victor Stinner66aab0c2015-03-19 22:53:20 +01008314 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008315}
8316
8317#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008318PyDoc_STRVAR(posix_sendfile__doc__,
Martin Panterbf19d162015-09-09 01:01:13 +00008319"sendfile(out, in, offset, count) -> byteswritten\n\
Martin Panter94994132015-09-09 05:29:24 +00008320sendfile(out, in, offset, count[, headers][, trailers], flags=0)\n\
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008321 -> byteswritten\n\
Martin Panterbf19d162015-09-09 01:01:13 +00008322Copy count bytes from file descriptor in to file descriptor out.");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008323
Larry Hastings2f936352014-08-05 14:04:04 +10008324/* AC 3.5: don't bother converting, has optional group*/
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008325static PyObject *
8326posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
8327{
8328 int in, out;
8329 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008330 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008331 off_t offset;
8332
8333#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
8334#ifndef __APPLE__
8335 Py_ssize_t len;
8336#endif
8337 PyObject *headers = NULL, *trailers = NULL;
8338 Py_buffer *hbuf, *tbuf;
8339 off_t sbytes;
8340 struct sf_hdtr sf;
8341 int flags = 0;
Martin Panterbf19d162015-09-09 01:01:13 +00008342 /* Beware that "in" clashes with Python's own "in" operator keyword */
Benjamin Petersond8a43b42011-02-26 21:35:16 +00008343 static char *keywords[] = {"out", "in",
8344 "offset", "count",
8345 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008346
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02008347 sf.headers = NULL;
8348 sf.trailers = NULL;
8349
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008350#ifdef __APPLE__
8351 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008352 keywords, &out, &in, Py_off_t_converter, &offset, Py_off_t_converter, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008353#else
8354 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Larry Hastings2f936352014-08-05 14:04:04 +10008355 keywords, &out, &in, Py_off_t_converter, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008356#endif
8357 &headers, &trailers, &flags))
8358 return NULL;
8359 if (headers != NULL) {
8360 if (!PySequence_Check(headers)) {
8361 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008362 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008363 return NULL;
8364 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008365 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008366 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008367 if (sf.hdr_cnt > 0 &&
Victor Stinner57ddf782014-01-08 15:21:28 +01008368 (i = iov_setup(&(sf.headers), &hbuf,
8369 headers, sf.hdr_cnt, PyBUF_SIMPLE)) < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008370 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008371#ifdef __APPLE__
8372 sbytes += i;
8373#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008374 }
8375 }
8376 if (trailers != NULL) {
8377 if (!PySequence_Check(trailers)) {
8378 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00008379 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008380 return NULL;
8381 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008382 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008383 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008384 if (sf.trl_cnt > 0 &&
Victor Stinner57ddf782014-01-08 15:21:28 +01008385 (i = iov_setup(&(sf.trailers), &tbuf,
8386 trailers, sf.trl_cnt, PyBUF_SIMPLE)) < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008387 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008388#ifdef __APPLE__
8389 sbytes += i;
8390#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008391 }
8392 }
8393
Steve Dower8fc89802015-04-12 00:26:27 -04008394 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008395 do {
8396 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008397#ifdef __APPLE__
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008398 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008399#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008400 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008401#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008402 Py_END_ALLOW_THREADS
8403 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008404 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008405
8406 if (sf.headers != NULL)
8407 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
8408 if (sf.trailers != NULL)
8409 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
8410
8411 if (ret < 0) {
8412 if ((errno == EAGAIN) || (errno == EBUSY)) {
8413 if (sbytes != 0) {
8414 // some data has been sent
8415 goto done;
8416 }
8417 else {
8418 // no data has been sent; upper application is supposed
8419 // to retry on EAGAIN or EBUSY
8420 return posix_error();
8421 }
8422 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008423 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008424 }
8425 goto done;
8426
8427done:
8428 #if !defined(HAVE_LARGEFILE_SUPPORT)
8429 return Py_BuildValue("l", sbytes);
8430 #else
8431 return Py_BuildValue("L", sbytes);
8432 #endif
8433
8434#else
8435 Py_ssize_t count;
8436 PyObject *offobj;
8437 static char *keywords[] = {"out", "in",
8438 "offset", "count", NULL};
8439 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
8440 keywords, &out, &in, &offobj, &count))
8441 return NULL;
8442#ifdef linux
8443 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008444 do {
8445 Py_BEGIN_ALLOW_THREADS
8446 ret = sendfile(out, in, NULL, count);
8447 Py_END_ALLOW_THREADS
8448 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008449 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008450 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02008451 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008452 }
8453#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008454 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00008455 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008456
8457 do {
8458 Py_BEGIN_ALLOW_THREADS
8459 ret = sendfile(out, in, &offset, count);
8460 Py_END_ALLOW_THREADS
8461 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008462 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008463 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008464 return Py_BuildValue("n", ret);
8465#endif
8466}
Larry Hastings2f936352014-08-05 14:04:04 +10008467#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008468
Larry Hastings2f936352014-08-05 14:04:04 +10008469
8470/*[clinic input]
8471os.fstat
8472
8473 fd : int
8474
8475Perform a stat system call on the given file descriptor.
8476
8477Like stat(), but for an open file descriptor.
8478Equivalent to os.stat(fd).
8479[clinic start generated code]*/
8480
Larry Hastings2f936352014-08-05 14:04:04 +10008481static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008482os_fstat_impl(PyObject *module, int fd)
8483/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008484{
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 STRUCT_STAT st;
8486 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008487 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008488
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008489 do {
8490 Py_BEGIN_ALLOW_THREADS
8491 res = FSTAT(fd, &st);
8492 Py_END_ALLOW_THREADS
8493 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00008494 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00008495#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01008496 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00008497#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008498 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00008499#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008500 }
Tim Peters5aa91602002-01-30 05:46:57 +00008501
Victor Stinner4195b5c2012-02-08 23:03:19 +01008502 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00008503}
8504
Larry Hastings2f936352014-08-05 14:04:04 +10008505
8506/*[clinic input]
8507os.isatty -> bool
8508 fd: int
8509 /
8510
8511Return True if the fd is connected to a terminal.
8512
8513Return True if the file descriptor is an open file descriptor
8514connected to the slave end of a terminal.
8515[clinic start generated code]*/
8516
Larry Hastings2f936352014-08-05 14:04:04 +10008517static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008518os_isatty_impl(PyObject *module, int fd)
8519/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008520{
Steve Dower8fc89802015-04-12 00:26:27 -04008521 int return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008522 if (!_PyVerify_fd(fd))
8523 return 0;
Steve Dower8fc89802015-04-12 00:26:27 -04008524 _Py_BEGIN_SUPPRESS_IPH
8525 return_value = isatty(fd);
8526 _Py_END_SUPPRESS_IPH
8527 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10008528}
8529
8530
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008531#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10008532/*[clinic input]
8533os.pipe
8534
8535Create a pipe.
8536
8537Returns a tuple of two file descriptors:
8538 (read_fd, write_fd)
8539[clinic start generated code]*/
8540
Larry Hastings2f936352014-08-05 14:04:04 +10008541static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008542os_pipe_impl(PyObject *module)
8543/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008544{
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02008546#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008547 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008548 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00008549 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008550#else
8551 int res;
8552#endif
8553
8554#ifdef MS_WINDOWS
8555 attr.nLength = sizeof(attr);
8556 attr.lpSecurityDescriptor = NULL;
8557 attr.bInheritHandle = FALSE;
8558
8559 Py_BEGIN_ALLOW_THREADS
8560 ok = CreatePipe(&read, &write, &attr, 0);
8561 if (ok) {
8562 fds[0] = _open_osfhandle((Py_intptr_t)read, _O_RDONLY);
8563 fds[1] = _open_osfhandle((Py_intptr_t)write, _O_WRONLY);
8564 if (fds[0] == -1 || fds[1] == -1) {
8565 CloseHandle(read);
8566 CloseHandle(write);
8567 ok = 0;
8568 }
8569 }
8570 Py_END_ALLOW_THREADS
8571
Victor Stinner8c62be82010-05-06 00:08:46 +00008572 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01008573 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02008574#else
8575
8576#ifdef HAVE_PIPE2
8577 Py_BEGIN_ALLOW_THREADS
8578 res = pipe2(fds, O_CLOEXEC);
8579 Py_END_ALLOW_THREADS
8580
8581 if (res != 0 && errno == ENOSYS)
8582 {
8583#endif
8584 Py_BEGIN_ALLOW_THREADS
8585 res = pipe(fds);
8586 Py_END_ALLOW_THREADS
8587
8588 if (res == 0) {
8589 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
8590 close(fds[0]);
8591 close(fds[1]);
8592 return NULL;
8593 }
8594 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
8595 close(fds[0]);
8596 close(fds[1]);
8597 return NULL;
8598 }
8599 }
8600#ifdef HAVE_PIPE2
8601 }
8602#endif
8603
8604 if (res != 0)
8605 return PyErr_SetFromErrno(PyExc_OSError);
8606#endif /* !MS_WINDOWS */
8607 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00008608}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008609#endif /* HAVE_PIPE */
8610
Larry Hastings2f936352014-08-05 14:04:04 +10008611
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008612#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10008613/*[clinic input]
8614os.pipe2
8615
8616 flags: int
8617 /
8618
8619Create a pipe with flags set atomically.
8620
8621Returns a tuple of two file descriptors:
8622 (read_fd, write_fd)
8623
8624flags can be constructed by ORing together one or more of these values:
8625O_NONBLOCK, O_CLOEXEC.
8626[clinic start generated code]*/
8627
Larry Hastings2f936352014-08-05 14:04:04 +10008628static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008629os_pipe2_impl(PyObject *module, int flags)
8630/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008631{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008632 int fds[2];
8633 int res;
8634
Charles-François Natalidaafdd52011-05-29 20:07:40 +02008635 res = pipe2(fds, flags);
8636 if (res != 0)
8637 return posix_error();
8638 return Py_BuildValue("(ii)", fds[0], fds[1]);
8639}
8640#endif /* HAVE_PIPE2 */
8641
Larry Hastings2f936352014-08-05 14:04:04 +10008642
Ross Lagerwall7807c352011-03-17 20:20:30 +02008643#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10008644/*[clinic input]
8645os.writev -> Py_ssize_t
8646 fd: int
8647 buffers: object
8648 /
8649
8650Iterate over buffers, and write the contents of each to a file descriptor.
8651
8652Returns the total number of bytes written.
8653buffers must be a sequence of bytes-like objects.
8654[clinic start generated code]*/
8655
Larry Hastings2f936352014-08-05 14:04:04 +10008656static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008657os_writev_impl(PyObject *module, int fd, PyObject *buffers)
8658/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008659{
8660 int cnt;
8661 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008662 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008663 struct iovec *iov;
8664 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10008665
8666 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02008667 PyErr_SetString(PyExc_TypeError,
8668 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10008669 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008670 }
Larry Hastings2f936352014-08-05 14:04:04 +10008671 cnt = PySequence_Size(buffers);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008672
Larry Hastings2f936352014-08-05 14:04:04 +10008673 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
8674 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008675 }
8676
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008677 do {
8678 Py_BEGIN_ALLOW_THREADS
8679 result = writev(fd, iov, cnt);
8680 Py_END_ALLOW_THREADS
8681 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008682
8683 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008684 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008685 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01008686
Georg Brandl306336b2012-06-24 12:55:33 +02008687 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008688}
Larry Hastings2f936352014-08-05 14:04:04 +10008689#endif /* HAVE_WRITEV */
8690
8691
8692#ifdef HAVE_PWRITE
8693/*[clinic input]
8694os.pwrite -> Py_ssize_t
8695
8696 fd: int
8697 buffer: Py_buffer
8698 offset: Py_off_t
8699 /
8700
8701Write bytes to a file descriptor starting at a particular offset.
8702
8703Write buffer to fd, starting at offset bytes from the beginning of
8704the file. Returns the number of bytes writte. Does not change the
8705current file offset.
8706[clinic start generated code]*/
8707
Larry Hastings2f936352014-08-05 14:04:04 +10008708static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008709os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
8710/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008711{
8712 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008713 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008714
8715 if (!_PyVerify_fd(fd)) {
8716 posix_error();
8717 return -1;
8718 }
8719
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008720 do {
8721 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008722 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008723 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04008724 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008725 Py_END_ALLOW_THREADS
8726 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10008727
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008728 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10008729 posix_error();
8730 return size;
8731}
8732#endif /* HAVE_PWRITE */
8733
8734
8735#ifdef HAVE_MKFIFO
8736/*[clinic input]
8737os.mkfifo
8738
8739 path: path_t
8740 mode: int=0o666
8741 *
8742 dir_fd: dir_fd(requires='mkfifoat')=None
8743
8744Create a "fifo" (a POSIX named pipe).
8745
8746If dir_fd is not None, it should be a file descriptor open to a directory,
8747 and path should be relative; path will then be relative to that directory.
8748dir_fd may not be implemented on your platform.
8749 If it is unavailable, using it will raise a NotImplementedError.
8750[clinic start generated code]*/
8751
Larry Hastings2f936352014-08-05 14:04:04 +10008752static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008753os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
8754/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008755{
8756 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008757 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008758
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008759 do {
8760 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008761#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008762 if (dir_fd != DEFAULT_DIR_FD)
8763 result = mkfifoat(dir_fd, path->narrow, mode);
8764 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02008765#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008766 result = mkfifo(path->narrow, mode);
8767 Py_END_ALLOW_THREADS
8768 } while (result != 0 && errno == EINTR &&
8769 !(async_err = PyErr_CheckSignals()));
8770 if (result != 0)
8771 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008772
8773 Py_RETURN_NONE;
8774}
8775#endif /* HAVE_MKFIFO */
8776
8777
8778#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
8779/*[clinic input]
8780os.mknod
8781
8782 path: path_t
8783 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008784 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10008785 *
8786 dir_fd: dir_fd(requires='mknodat')=None
8787
8788Create a node in the file system.
8789
8790Create a node in the file system (file, device special file or named pipe)
8791at path. mode specifies both the permissions to use and the
8792type of node to be created, being combined (bitwise OR) with one of
8793S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
8794device defines the newly created device special file (probably using
8795os.makedev()). Otherwise device is ignored.
8796
8797If dir_fd is not None, it should be a file descriptor open to a directory,
8798 and path should be relative; path will then be relative to that directory.
8799dir_fd may not be implemented on your platform.
8800 If it is unavailable, using it will raise a NotImplementedError.
8801[clinic start generated code]*/
8802
Larry Hastings2f936352014-08-05 14:04:04 +10008803static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008804os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04008805 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008806/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008807{
8808 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008809 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008810
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008811 do {
8812 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008813#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008814 if (dir_fd != DEFAULT_DIR_FD)
8815 result = mknodat(dir_fd, path->narrow, mode, device);
8816 else
Larry Hastings2f936352014-08-05 14:04:04 +10008817#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008818 result = mknod(path->narrow, mode, device);
8819 Py_END_ALLOW_THREADS
8820 } while (result != 0 && errno == EINTR &&
8821 !(async_err = PyErr_CheckSignals()));
8822 if (result != 0)
8823 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008824
8825 Py_RETURN_NONE;
8826}
8827#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
8828
8829
8830#ifdef HAVE_DEVICE_MACROS
8831/*[clinic input]
8832os.major -> unsigned_int
8833
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008834 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008835 /
8836
8837Extracts a device major number from a raw device number.
8838[clinic start generated code]*/
8839
Larry Hastings2f936352014-08-05 14:04:04 +10008840static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008841os_major_impl(PyObject *module, dev_t device)
8842/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008843{
8844 return major(device);
8845}
8846
8847
8848/*[clinic input]
8849os.minor -> unsigned_int
8850
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008851 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008852 /
8853
8854Extracts a device minor number from a raw device number.
8855[clinic start generated code]*/
8856
Larry Hastings2f936352014-08-05 14:04:04 +10008857static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008858os_minor_impl(PyObject *module, dev_t device)
8859/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008860{
8861 return minor(device);
8862}
8863
8864
8865/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008866os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +10008867
8868 major: int
8869 minor: int
8870 /
8871
8872Composes a raw device number from the major and minor device numbers.
8873[clinic start generated code]*/
8874
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02008875static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008876os_makedev_impl(PyObject *module, int major, int minor)
8877/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008878{
8879 return makedev(major, minor);
8880}
8881#endif /* HAVE_DEVICE_MACROS */
8882
8883
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008884#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008885/*[clinic input]
8886os.ftruncate
8887
8888 fd: int
8889 length: Py_off_t
8890 /
8891
8892Truncate a file, specified by file descriptor, to a specific length.
8893[clinic start generated code]*/
8894
Larry Hastings2f936352014-08-05 14:04:04 +10008895static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008896os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
8897/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008898{
8899 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008900 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008901
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008902 if (!_PyVerify_fd(fd))
8903 return posix_error();
8904
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008905 do {
8906 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008907 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008908#ifdef MS_WINDOWS
8909 result = _chsize_s(fd, length);
8910#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008911 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008912#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008913 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008914 Py_END_ALLOW_THREADS
8915 } while (result != 0 && errno == EINTR &&
8916 !(async_err = PyErr_CheckSignals()));
8917 if (result != 0)
8918 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10008919 Py_RETURN_NONE;
8920}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008921#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008922
8923
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008924#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008925/*[clinic input]
8926os.truncate
8927 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
8928 length: Py_off_t
8929
8930Truncate a file, specified by path, to a specific length.
8931
8932On some platforms, path may also be specified as an open file descriptor.
8933 If this functionality is unavailable, using it raises an exception.
8934[clinic start generated code]*/
8935
Larry Hastings2f936352014-08-05 14:04:04 +10008936static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008937os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
8938/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008939{
8940 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008941#ifdef MS_WINDOWS
8942 int fd;
8943#endif
8944
8945 if (path->fd != -1)
8946 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008947
8948 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -04008949 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008950#ifdef MS_WINDOWS
8951 if (path->wide)
8952 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Larry Hastings2f936352014-08-05 14:04:04 +10008953 else
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008954 fd = _open(path->narrow, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +02008955 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008956 result = -1;
8957 else {
8958 result = _chsize_s(fd, length);
8959 close(fd);
8960 if (result < 0)
8961 errno = result;
8962 }
8963#else
8964 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +10008965#endif
Steve Dowera1c7e722015-04-12 00:26:43 -04008966 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10008967 Py_END_ALLOW_THREADS
8968 if (result < 0)
8969 return path_error(path);
8970
8971 Py_RETURN_NONE;
8972}
Steve Dowerfe0a41a2015-03-20 19:50:46 -07008973#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10008974
Ross Lagerwall7807c352011-03-17 20:20:30 +02008975
Victor Stinnerd6b17692014-09-30 12:20:05 +02008976/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
8977 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
8978 defined, which is the case in Python on AIX. AIX bug report:
8979 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
8980#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
8981# define POSIX_FADVISE_AIX_BUG
8982#endif
8983
Victor Stinnerec39e262014-09-30 12:35:58 +02008984
Victor Stinnerd6b17692014-09-30 12:20:05 +02008985#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10008986/*[clinic input]
8987os.posix_fallocate
8988
8989 fd: int
8990 offset: Py_off_t
8991 length: Py_off_t
8992 /
8993
8994Ensure a file has allocated at least a particular number of bytes on disk.
8995
8996Ensure that the file specified by fd encompasses a range of bytes
8997starting at offset bytes from the beginning and continuing for length bytes.
8998[clinic start generated code]*/
8999
Larry Hastings2f936352014-08-05 14:04:04 +10009000static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009001os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009002 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009003/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009004{
9005 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009006 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009007
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009008 do {
9009 Py_BEGIN_ALLOW_THREADS
9010 result = posix_fallocate(fd, offset, length);
9011 Py_END_ALLOW_THREADS
9012 } while (result != 0 && errno == EINTR &&
9013 !(async_err = PyErr_CheckSignals()));
9014 if (result != 0)
9015 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009016 Py_RETURN_NONE;
9017}
Victor Stinnerec39e262014-09-30 12:35:58 +02009018#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +10009019
Ross Lagerwall7807c352011-03-17 20:20:30 +02009020
Victor Stinnerd6b17692014-09-30 12:20:05 +02009021#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +10009022/*[clinic input]
9023os.posix_fadvise
9024
9025 fd: int
9026 offset: Py_off_t
9027 length: Py_off_t
9028 advice: int
9029 /
9030
9031Announce an intention to access data in a specific pattern.
9032
9033Announce an intention to access data in a specific pattern, thus allowing
9034the kernel to make optimizations.
9035The advice applies to the region of the file specified by fd starting at
9036offset and continuing for length bytes.
9037advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
9038POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
9039POSIX_FADV_DONTNEED.
9040[clinic start generated code]*/
9041
Larry Hastings2f936352014-08-05 14:04:04 +10009042static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009043os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -04009044 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009045/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009046{
9047 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009048 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009049
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009050 do {
9051 Py_BEGIN_ALLOW_THREADS
9052 result = posix_fadvise(fd, offset, length, advice);
9053 Py_END_ALLOW_THREADS
9054 } while (result != 0 && errno == EINTR &&
9055 !(async_err = PyErr_CheckSignals()));
9056 if (result != 0)
9057 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009058 Py_RETURN_NONE;
9059}
Victor Stinnerec39e262014-09-30 12:35:58 +02009060#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009061
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009062#ifdef HAVE_PUTENV
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009063
Fred Drake762e2061999-08-26 17:23:54 +00009064/* Save putenv() parameters as values here, so we can collect them when they
9065 * get re-set with another call for the same key. */
9066static PyObject *posix_putenv_garbage;
9067
Larry Hastings2f936352014-08-05 14:04:04 +10009068static void
9069posix_putenv_garbage_setitem(PyObject *name, PyObject *value)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009070{
Larry Hastings2f936352014-08-05 14:04:04 +10009071 /* Install the first arg and newstr in posix_putenv_garbage;
9072 * this will cause previous value to be collected. This has to
9073 * happen after the real putenv() call because the old value
9074 * was still accessible until then. */
9075 if (PyDict_SetItem(posix_putenv_garbage, name, value))
9076 /* really not much we can do; just leak */
9077 PyErr_Clear();
9078 else
9079 Py_DECREF(value);
9080}
9081
9082
Thomas Hellerf78f12a2007-11-08 19:33:05 +00009083#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009084/*[clinic input]
9085os.putenv
9086
9087 name: unicode
9088 value: unicode
9089 /
9090
9091Change or add an environment variable.
9092[clinic start generated code]*/
9093
Larry Hastings2f936352014-08-05 14:04:04 +10009094static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009095os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9096/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009097{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009098 const wchar_t *env;
Larry Hastings2f936352014-08-05 14:04:04 +10009099
9100 PyObject *unicode = PyUnicode_FromFormat("%U=%U", name, value);
9101 if (unicode == NULL) {
Victor Stinner84ae1182010-05-06 22:05:07 +00009102 PyErr_NoMemory();
Larry Hastings2f936352014-08-05 14:04:04 +10009103 return NULL;
Victor Stinner84ae1182010-05-06 22:05:07 +00009104 }
Larry Hastings2f936352014-08-05 14:04:04 +10009105 if (_MAX_ENV < PyUnicode_GET_LENGTH(unicode)) {
Victor Stinner65170952011-11-22 22:16:17 +01009106 PyErr_Format(PyExc_ValueError,
9107 "the environment variable is longer than %u characters",
9108 _MAX_ENV);
9109 goto error;
9110 }
9111
Larry Hastings2f936352014-08-05 14:04:04 +10009112 env = PyUnicode_AsUnicode(unicode);
9113 if (env == NULL)
Victor Stinnereb5657a2011-09-30 01:44:27 +02009114 goto error;
Larry Hastings2f936352014-08-05 14:04:04 +10009115 if (_wputenv(env)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009116 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00009117 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 }
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009119
Larry Hastings2f936352014-08-05 14:04:04 +10009120 posix_putenv_garbage_setitem(name, unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009121 Py_RETURN_NONE;
9122
9123error:
Larry Hastings2f936352014-08-05 14:04:04 +10009124 Py_DECREF(unicode);
Victor Stinner84ae1182010-05-06 22:05:07 +00009125 return NULL;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009126}
Larry Hastings2f936352014-08-05 14:04:04 +10009127#else /* MS_WINDOWS */
9128/*[clinic input]
9129os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +00009130
Larry Hastings2f936352014-08-05 14:04:04 +10009131 name: FSConverter
9132 value: FSConverter
9133 /
9134
9135Change or add an environment variable.
9136[clinic start generated code]*/
9137
Larry Hastings2f936352014-08-05 14:04:04 +10009138static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009139os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
9140/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009141{
9142 PyObject *bytes = NULL;
9143 char *env;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03009144 const char *name_string = PyBytes_AsString(name);
9145 const char *value_string = PyBytes_AsString(value);
Larry Hastings2f936352014-08-05 14:04:04 +10009146
9147 bytes = PyBytes_FromFormat("%s=%s", name_string, value_string);
9148 if (bytes == NULL) {
9149 PyErr_NoMemory();
9150 return NULL;
9151 }
9152
9153 env = PyBytes_AS_STRING(bytes);
9154 if (putenv(env)) {
9155 Py_DECREF(bytes);
9156 return posix_error();
9157 }
9158
9159 posix_putenv_garbage_setitem(name, bytes);
9160 Py_RETURN_NONE;
9161}
9162#endif /* MS_WINDOWS */
9163#endif /* HAVE_PUTENV */
9164
9165
9166#ifdef HAVE_UNSETENV
9167/*[clinic input]
9168os.unsetenv
9169 name: FSConverter
9170 /
9171
9172Delete an environment variable.
9173[clinic start generated code]*/
9174
Larry Hastings2f936352014-08-05 14:04:04 +10009175static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009176os_unsetenv_impl(PyObject *module, PyObject *name)
9177/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009178{
Victor Stinner984890f2011-11-24 13:53:38 +01009179#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01009180 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01009181#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00009182
Victor Stinner984890f2011-11-24 13:53:38 +01009183#ifdef HAVE_BROKEN_UNSETENV
9184 unsetenv(PyBytes_AS_STRING(name));
9185#else
Victor Stinner65170952011-11-22 22:16:17 +01009186 err = unsetenv(PyBytes_AS_STRING(name));
Larry Hastings2f936352014-08-05 14:04:04 +10009187 if (err)
Victor Stinner60b385e2011-11-22 22:01:28 +01009188 return posix_error();
Victor Stinner984890f2011-11-24 13:53:38 +01009189#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00009190
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 /* Remove the key from posix_putenv_garbage;
9192 * this will cause it to be collected. This has to
9193 * happen after the real unsetenv() call because the
9194 * old value was still accessible until then.
9195 */
Victor Stinner65170952011-11-22 22:16:17 +01009196 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009197 /* really not much we can do; just leak */
9198 PyErr_Clear();
9199 }
Victor Stinner84ae1182010-05-06 22:05:07 +00009200 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00009201}
Larry Hastings2f936352014-08-05 14:04:04 +10009202#endif /* HAVE_UNSETENV */
Guido van Rossumc524d952001-10-19 01:31:59 +00009203
Larry Hastings2f936352014-08-05 14:04:04 +10009204
9205/*[clinic input]
9206os.strerror
9207
9208 code: int
9209 /
9210
9211Translate an error code to a message string.
9212[clinic start generated code]*/
9213
Larry Hastings2f936352014-08-05 14:04:04 +10009214static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009215os_strerror_impl(PyObject *module, int code)
9216/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009217{
9218 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +00009219 if (message == NULL) {
9220 PyErr_SetString(PyExc_ValueError,
9221 "strerror() argument out of range");
9222 return NULL;
9223 }
Victor Stinner1b579672011-12-17 05:47:23 +01009224 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00009225}
Guido van Rossumb6a47161997-09-15 22:54:34 +00009226
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00009227
Guido van Rossumc9641791998-08-04 15:26:23 +00009228#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +00009229#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +10009230/*[clinic input]
9231os.WCOREDUMP -> bool
9232
9233 status: int
9234 /
9235
9236Return True if the process returning status was dumped to a core file.
9237[clinic start generated code]*/
9238
Larry Hastings2f936352014-08-05 14:04:04 +10009239static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009240os_WCOREDUMP_impl(PyObject *module, int status)
9241/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009242{
9243 WAIT_TYPE wait_status;
9244 WAIT_STATUS_INT(wait_status) = status;
9245 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009246}
9247#endif /* WCOREDUMP */
9248
Larry Hastings2f936352014-08-05 14:04:04 +10009249
Fred Drake106c1a02002-04-23 15:58:02 +00009250#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +10009251/*[clinic input]
9252os.WIFCONTINUED -> bool
9253
9254 status: int
9255
9256Return True if a particular process was continued from a job control stop.
9257
9258Return True if the process returning status was continued from a
9259job control stop.
9260[clinic start generated code]*/
9261
Larry Hastings2f936352014-08-05 14:04:04 +10009262static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009263os_WIFCONTINUED_impl(PyObject *module, int status)
9264/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009265{
9266 WAIT_TYPE wait_status;
9267 WAIT_STATUS_INT(wait_status) = status;
9268 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +00009269}
9270#endif /* WIFCONTINUED */
9271
Larry Hastings2f936352014-08-05 14:04:04 +10009272
Guido van Rossumc9641791998-08-04 15:26:23 +00009273#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +10009274/*[clinic input]
9275os.WIFSTOPPED -> bool
9276
9277 status: int
9278
9279Return True if the process returning status was stopped.
9280[clinic start generated code]*/
9281
Larry Hastings2f936352014-08-05 14:04:04 +10009282static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009283os_WIFSTOPPED_impl(PyObject *module, int status)
9284/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009285{
9286 WAIT_TYPE wait_status;
9287 WAIT_STATUS_INT(wait_status) = status;
9288 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009289}
9290#endif /* WIFSTOPPED */
9291
Larry Hastings2f936352014-08-05 14:04:04 +10009292
Guido van Rossumc9641791998-08-04 15:26:23 +00009293#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +10009294/*[clinic input]
9295os.WIFSIGNALED -> bool
9296
9297 status: int
9298
9299Return True if the process returning status was terminated by a signal.
9300[clinic start generated code]*/
9301
Larry Hastings2f936352014-08-05 14:04:04 +10009302static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009303os_WIFSIGNALED_impl(PyObject *module, int status)
9304/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009305{
9306 WAIT_TYPE wait_status;
9307 WAIT_STATUS_INT(wait_status) = status;
9308 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009309}
9310#endif /* WIFSIGNALED */
9311
Larry Hastings2f936352014-08-05 14:04:04 +10009312
Guido van Rossumc9641791998-08-04 15:26:23 +00009313#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +10009314/*[clinic input]
9315os.WIFEXITED -> bool
9316
9317 status: int
9318
9319Return True if the process returning status exited via the exit() system call.
9320[clinic start generated code]*/
9321
Larry Hastings2f936352014-08-05 14:04:04 +10009322static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009323os_WIFEXITED_impl(PyObject *module, int status)
9324/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009325{
9326 WAIT_TYPE wait_status;
9327 WAIT_STATUS_INT(wait_status) = status;
9328 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009329}
9330#endif /* WIFEXITED */
9331
Larry Hastings2f936352014-08-05 14:04:04 +10009332
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00009333#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +10009334/*[clinic input]
9335os.WEXITSTATUS -> int
9336
9337 status: int
9338
9339Return the process return code from status.
9340[clinic start generated code]*/
9341
Larry Hastings2f936352014-08-05 14:04:04 +10009342static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009343os_WEXITSTATUS_impl(PyObject *module, int status)
9344/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009345{
9346 WAIT_TYPE wait_status;
9347 WAIT_STATUS_INT(wait_status) = status;
9348 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009349}
9350#endif /* WEXITSTATUS */
9351
Larry Hastings2f936352014-08-05 14:04:04 +10009352
Guido van Rossumc9641791998-08-04 15:26:23 +00009353#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009354/*[clinic input]
9355os.WTERMSIG -> int
9356
9357 status: int
9358
9359Return the signal that terminated the process that provided the status value.
9360[clinic start generated code]*/
9361
Larry Hastings2f936352014-08-05 14:04:04 +10009362static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009363os_WTERMSIG_impl(PyObject *module, int status)
9364/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009365{
9366 WAIT_TYPE wait_status;
9367 WAIT_STATUS_INT(wait_status) = status;
9368 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009369}
9370#endif /* WTERMSIG */
9371
Larry Hastings2f936352014-08-05 14:04:04 +10009372
Guido van Rossumc9641791998-08-04 15:26:23 +00009373#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +10009374/*[clinic input]
9375os.WSTOPSIG -> int
9376
9377 status: int
9378
9379Return the signal that stopped the process that provided the status value.
9380[clinic start generated code]*/
9381
Larry Hastings2f936352014-08-05 14:04:04 +10009382static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009383os_WSTOPSIG_impl(PyObject *module, int status)
9384/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009385{
9386 WAIT_TYPE wait_status;
9387 WAIT_STATUS_INT(wait_status) = status;
9388 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +00009389}
9390#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +00009391#endif /* HAVE_SYS_WAIT_H */
9392
9393
Thomas Wouters477c8d52006-05-27 19:21:47 +00009394#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00009395#ifdef _SCO_DS
9396/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
9397 needed definitions in sys/statvfs.h */
9398#define _SVID3
9399#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00009400#include <sys/statvfs.h>
9401
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009402static PyObject*
9403_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 PyObject *v = PyStructSequence_New(&StatVFSResultType);
9405 if (v == NULL)
9406 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009407
9408#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00009409 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9410 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9411 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
9412 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
9413 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
9414 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
9415 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
9416 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
9417 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9418 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009419#else
Victor Stinner8c62be82010-05-06 00:08:46 +00009420 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
9421 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
9422 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009423 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +00009424 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009425 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009426 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009427 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009428 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009429 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +00009430 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009431 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +00009432 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -07009433 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +00009434 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
9435 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009436#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +01009437 if (PyErr_Occurred()) {
9438 Py_DECREF(v);
9439 return NULL;
9440 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009441
Victor Stinner8c62be82010-05-06 00:08:46 +00009442 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009443}
9444
Larry Hastings2f936352014-08-05 14:04:04 +10009445
9446/*[clinic input]
9447os.fstatvfs
9448 fd: int
9449 /
9450
9451Perform an fstatvfs system call on the given fd.
9452
9453Equivalent to statvfs(fd).
9454[clinic start generated code]*/
9455
Larry Hastings2f936352014-08-05 14:04:04 +10009456static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009457os_fstatvfs_impl(PyObject *module, int fd)
9458/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009459{
9460 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009461 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009462 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009463
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009464 do {
9465 Py_BEGIN_ALLOW_THREADS
9466 result = fstatvfs(fd, &st);
9467 Py_END_ALLOW_THREADS
9468 } while (result != 0 && errno == EINTR &&
9469 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009470 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009471 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00009472
Victor Stinner8c62be82010-05-06 00:08:46 +00009473 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009474}
Larry Hastings2f936352014-08-05 14:04:04 +10009475#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009476
9477
Thomas Wouters477c8d52006-05-27 19:21:47 +00009478#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00009479#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +10009480/*[clinic input]
9481os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +00009482
Larry Hastings2f936352014-08-05 14:04:04 +10009483 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
9484
9485Perform a statvfs system call on the given path.
9486
9487path may always be specified as a string.
9488On some platforms, path may also be specified as an open file descriptor.
9489 If this functionality is unavailable, using it raises an exception.
9490[clinic start generated code]*/
9491
Larry Hastings2f936352014-08-05 14:04:04 +10009492static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009493os_statvfs_impl(PyObject *module, path_t *path)
9494/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009495{
9496 int result;
9497 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009498
9499 Py_BEGIN_ALLOW_THREADS
9500#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +10009501 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07009502#ifdef __APPLE__
9503 /* handle weak-linking on Mac OS X 10.3 */
9504 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +10009505 fd_specified("statvfs", path->fd);
9506 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009507 }
9508#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009509 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009510 }
9511 else
9512#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009513 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009514 Py_END_ALLOW_THREADS
9515
9516 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10009517 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009518 }
9519
Larry Hastings2f936352014-08-05 14:04:04 +10009520 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00009521}
Larry Hastings2f936352014-08-05 14:04:04 +10009522#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
9523
Guido van Rossum94f6f721999-01-06 18:42:14 +00009524
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009525#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009526/*[clinic input]
9527os._getdiskusage
9528
9529 path: Py_UNICODE
9530
9531Return disk usage statistics about the given path as a (total, free) tuple.
9532[clinic start generated code]*/
9533
Larry Hastings2f936352014-08-05 14:04:04 +10009534static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009535os__getdiskusage_impl(PyObject *module, Py_UNICODE *path)
9536/*[clinic end generated code: output=76d6adcd86b1db0b input=6458133aed893c78]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009537{
9538 BOOL retval;
9539 ULARGE_INTEGER _, total, free;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009540
9541 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01009542 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009543 Py_END_ALLOW_THREADS
9544 if (retval == 0)
9545 return PyErr_SetFromWindowsErr(0);
9546
9547 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
9548}
Larry Hastings2f936352014-08-05 14:04:04 +10009549#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02009550
9551
Fred Drakec9680921999-12-13 16:37:25 +00009552/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
9553 * It maps strings representing configuration variable names to
9554 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00009555 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00009556 * rarely-used constants. There are three separate tables that use
9557 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00009558 *
9559 * This code is always included, even if none of the interfaces that
9560 * need it are included. The #if hackery needed to avoid it would be
9561 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00009562 */
9563struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02009564 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009565 int value;
Fred Drakec9680921999-12-13 16:37:25 +00009566};
9567
Fred Drake12c6e2d1999-12-14 21:25:03 +00009568static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009569conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00009570 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00009571{
Christian Heimes217cfd12007-12-02 14:31:20 +00009572 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +03009573 int value = _PyLong_AsInt(arg);
9574 if (value == -1 && PyErr_Occurred())
9575 return 0;
9576 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +00009577 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00009578 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00009579 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00009580 /* look up the value in the table using a binary search */
9581 size_t lo = 0;
9582 size_t mid;
9583 size_t hi = tablesize;
9584 int cmp;
9585 const char *confname;
9586 if (!PyUnicode_Check(arg)) {
9587 PyErr_SetString(PyExc_TypeError,
9588 "configuration names must be strings or integers");
9589 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009590 }
Stefan Krah0e803b32010-11-26 16:16:47 +00009591 confname = _PyUnicode_AsString(arg);
9592 if (confname == NULL)
9593 return 0;
9594 while (lo < hi) {
9595 mid = (lo + hi) / 2;
9596 cmp = strcmp(confname, table[mid].name);
9597 if (cmp < 0)
9598 hi = mid;
9599 else if (cmp > 0)
9600 lo = mid + 1;
9601 else {
9602 *valuep = table[mid].value;
9603 return 1;
9604 }
9605 }
9606 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
9607 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00009608 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00009609}
9610
9611
9612#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
9613static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009614#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009615 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009616#endif
9617#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009618 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009619#endif
Fred Drakec9680921999-12-13 16:37:25 +00009620#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009621 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009622#endif
9623#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00009624 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00009625#endif
9626#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00009627 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00009628#endif
9629#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00009630 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00009631#endif
9632#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009633 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009634#endif
9635#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00009636 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00009637#endif
9638#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00009639 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00009640#endif
9641#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009642 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009643#endif
9644#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009645 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00009646#endif
9647#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009648 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009649#endif
9650#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009651 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00009652#endif
9653#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009654 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009655#endif
9656#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00009657 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00009658#endif
9659#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009660 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009661#endif
9662#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00009663 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00009664#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00009665#ifdef _PC_ACL_ENABLED
9666 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
9667#endif
9668#ifdef _PC_MIN_HOLE_SIZE
9669 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
9670#endif
9671#ifdef _PC_ALLOC_SIZE_MIN
9672 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
9673#endif
9674#ifdef _PC_REC_INCR_XFER_SIZE
9675 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
9676#endif
9677#ifdef _PC_REC_MAX_XFER_SIZE
9678 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
9679#endif
9680#ifdef _PC_REC_MIN_XFER_SIZE
9681 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
9682#endif
9683#ifdef _PC_REC_XFER_ALIGN
9684 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
9685#endif
9686#ifdef _PC_SYMLINK_MAX
9687 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
9688#endif
9689#ifdef _PC_XATTR_ENABLED
9690 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
9691#endif
9692#ifdef _PC_XATTR_EXISTS
9693 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
9694#endif
9695#ifdef _PC_TIMESTAMP_RESOLUTION
9696 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
9697#endif
Fred Drakec9680921999-12-13 16:37:25 +00009698};
9699
Fred Drakec9680921999-12-13 16:37:25 +00009700static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009701conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009702{
9703 return conv_confname(arg, valuep, posix_constants_pathconf,
9704 sizeof(posix_constants_pathconf)
9705 / sizeof(struct constdef));
9706}
9707#endif
9708
Larry Hastings2f936352014-08-05 14:04:04 +10009709
Fred Drakec9680921999-12-13 16:37:25 +00009710#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009711/*[clinic input]
9712os.fpathconf -> long
9713
9714 fd: int
9715 name: path_confname
9716 /
9717
9718Return the configuration limit name for the file descriptor fd.
9719
9720If there is no limit, return -1.
9721[clinic start generated code]*/
9722
Larry Hastings2f936352014-08-05 14:04:04 +10009723static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009724os_fpathconf_impl(PyObject *module, int fd, int name)
9725/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009726{
9727 long limit;
9728
9729 errno = 0;
9730 limit = fpathconf(fd, name);
9731 if (limit == -1 && errno != 0)
9732 posix_error();
9733
9734 return limit;
9735}
9736#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009737
9738
9739#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009740/*[clinic input]
9741os.pathconf -> long
9742 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
9743 name: path_confname
9744
9745Return the configuration limit name for the file or directory path.
9746
9747If there is no limit, return -1.
9748On some platforms, path may also be specified as an open file descriptor.
9749 If this functionality is unavailable, using it raises an exception.
9750[clinic start generated code]*/
9751
Larry Hastings2f936352014-08-05 14:04:04 +10009752static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009753os_pathconf_impl(PyObject *module, path_t *path, int name)
9754/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009755{
Victor Stinner8c62be82010-05-06 00:08:46 +00009756 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00009757
Victor Stinner8c62be82010-05-06 00:08:46 +00009758 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02009759#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +10009760 if (path->fd != -1)
9761 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +02009762 else
9763#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009764 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00009765 if (limit == -1 && errno != 0) {
9766 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00009767 /* could be a path or name problem */
9768 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00009769 else
Larry Hastings2f936352014-08-05 14:04:04 +10009770 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +00009771 }
Larry Hastings2f936352014-08-05 14:04:04 +10009772
9773 return limit;
Fred Drakec9680921999-12-13 16:37:25 +00009774}
Larry Hastings2f936352014-08-05 14:04:04 +10009775#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +00009776
9777#ifdef HAVE_CONFSTR
9778static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00009779#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00009780 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00009781#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00009782#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009783 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009784#endif
9785#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009786 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00009787#endif
Fred Draked86ed291999-12-15 15:34:33 +00009788#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009789 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009790#endif
9791#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009792 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009793#endif
9794#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009795 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009796#endif
9797#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009798 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009799#endif
Fred Drakec9680921999-12-13 16:37:25 +00009800#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009801 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009802#endif
9803#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009804 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009805#endif
9806#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009807 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009808#endif
9809#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009810 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009811#endif
9812#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009813 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009814#endif
9815#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009816 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009817#endif
9818#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009819 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009820#endif
9821#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009822 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009823#endif
Fred Draked86ed291999-12-15 15:34:33 +00009824#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009825 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009826#endif
Fred Drakec9680921999-12-13 16:37:25 +00009827#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009828 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009829#endif
Fred Draked86ed291999-12-15 15:34:33 +00009830#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009831 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009832#endif
9833#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009834 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009835#endif
9836#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009837 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009838#endif
9839#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009840 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009841#endif
Fred Drakec9680921999-12-13 16:37:25 +00009842#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009843 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009844#endif
9845#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009846 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009847#endif
9848#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009849 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009850#endif
9851#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009852 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009853#endif
9854#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009855 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009856#endif
9857#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009858 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009859#endif
9860#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009861 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009862#endif
9863#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009864 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009865#endif
9866#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009867 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009868#endif
9869#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009870 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009871#endif
9872#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009873 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009874#endif
9875#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009876 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009877#endif
9878#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009879 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009880#endif
9881#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009882 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009883#endif
9884#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009885 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009886#endif
9887#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009888 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009889#endif
Fred Draked86ed291999-12-15 15:34:33 +00009890#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009891 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009892#endif
9893#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009894 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009895#endif
9896#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009897 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009898#endif
9899#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009900 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009901#endif
9902#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009903 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009904#endif
9905#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009906 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009907#endif
9908#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009909 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009910#endif
9911#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009912 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009913#endif
9914#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009915 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009916#endif
9917#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009918 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009919#endif
9920#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009921 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009922#endif
9923#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009924 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009925#endif
9926#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009927 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009928#endif
Fred Drakec9680921999-12-13 16:37:25 +00009929};
9930
9931static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009932conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009933{
9934 return conv_confname(arg, valuep, posix_constants_confstr,
9935 sizeof(posix_constants_confstr)
9936 / sizeof(struct constdef));
9937}
9938
Larry Hastings2f936352014-08-05 14:04:04 +10009939
9940/*[clinic input]
9941os.confstr
9942
9943 name: confstr_confname
9944 /
9945
9946Return a string-valued system configuration variable.
9947[clinic start generated code]*/
9948
Larry Hastings2f936352014-08-05 14:04:04 +10009949static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009950os_confstr_impl(PyObject *module, int name)
9951/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +00009952{
9953 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +00009954 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009955 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +00009956
Victor Stinnercb043522010-09-10 23:49:04 +00009957 errno = 0;
9958 len = confstr(name, buffer, sizeof(buffer));
9959 if (len == 0) {
9960 if (errno) {
9961 posix_error();
9962 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009963 }
9964 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009965 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009966 }
9967 }
Victor Stinnercb043522010-09-10 23:49:04 +00009968
Victor Stinnerdd3a6a52013-06-25 23:13:47 +02009969 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +01009970 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +00009971 char *buf = PyMem_Malloc(len);
9972 if (buf == NULL)
9973 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +01009974 len2 = confstr(name, buf, len);
9975 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +02009976 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +00009977 PyMem_Free(buf);
9978 }
9979 else
9980 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009981 return result;
9982}
Larry Hastings2f936352014-08-05 14:04:04 +10009983#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +00009984
9985
9986#ifdef HAVE_SYSCONF
9987static struct constdef posix_constants_sysconf[] = {
9988#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009989 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009990#endif
9991#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009992 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009993#endif
9994#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009995 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009996#endif
9997#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009998 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009999#endif
10000#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010001 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010002#endif
10003#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000010004 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000010005#endif
10006#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000010007 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000010008#endif
10009#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000010010 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000010011#endif
10012#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010013 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000010014#endif
10015#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010016 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010017#endif
Fred Draked86ed291999-12-15 15:34:33 +000010018#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010019 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010020#endif
10021#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000010022 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000010023#endif
Fred Drakec9680921999-12-13 16:37:25 +000010024#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010025 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010026#endif
Fred Drakec9680921999-12-13 16:37:25 +000010027#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010028 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010029#endif
10030#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010031 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010032#endif
10033#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010034 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010035#endif
10036#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010037 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010038#endif
10039#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010040 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010041#endif
Fred Draked86ed291999-12-15 15:34:33 +000010042#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010043 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000010044#endif
Fred Drakec9680921999-12-13 16:37:25 +000010045#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010046 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010047#endif
10048#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010049 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010050#endif
10051#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010052 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010053#endif
10054#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010055 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010056#endif
10057#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010058 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010059#endif
Fred Draked86ed291999-12-15 15:34:33 +000010060#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000010061 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000010062#endif
Fred Drakec9680921999-12-13 16:37:25 +000010063#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010064 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010065#endif
10066#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010067 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010068#endif
10069#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010070 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010071#endif
10072#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010073 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010074#endif
10075#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010076 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010077#endif
10078#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010079 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000010080#endif
10081#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010082 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010083#endif
10084#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010085 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010086#endif
10087#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010088 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010089#endif
10090#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010091 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010092#endif
10093#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010094 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010095#endif
10096#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010097 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010098#endif
10099#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010100 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010101#endif
10102#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010103 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010104#endif
10105#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010106 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010107#endif
10108#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010109 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010110#endif
10111#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010112 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000010113#endif
10114#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010115 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010116#endif
10117#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010118 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010119#endif
10120#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000010121 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000010122#endif
10123#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010124 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000010125#endif
10126#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010127 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000010128#endif
10129#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000010130 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000010131#endif
Fred Draked86ed291999-12-15 15:34:33 +000010132#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000010133 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000010134#endif
Fred Drakec9680921999-12-13 16:37:25 +000010135#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010136 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010137#endif
10138#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010139 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010140#endif
10141#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010142 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010143#endif
Fred Draked86ed291999-12-15 15:34:33 +000010144#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010145 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000010146#endif
Fred Drakec9680921999-12-13 16:37:25 +000010147#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000010148 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000010149#endif
Fred Draked86ed291999-12-15 15:34:33 +000010150#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010151 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000010152#endif
10153#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000010154 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000010155#endif
Fred Drakec9680921999-12-13 16:37:25 +000010156#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010157 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010158#endif
10159#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010160 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010161#endif
10162#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010163 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010164#endif
10165#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010166 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010167#endif
Fred Draked86ed291999-12-15 15:34:33 +000010168#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000010169 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000010170#endif
Fred Drakec9680921999-12-13 16:37:25 +000010171#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000010172 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000010173#endif
10174#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010175 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000010176#endif
10177#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010178 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010179#endif
10180#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010181 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000010182#endif
10183#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000010184 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000010185#endif
10186#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000010187 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000010188#endif
10189#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000010190 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000010191#endif
Fred Draked86ed291999-12-15 15:34:33 +000010192#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000010193 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000010194#endif
Fred Drakec9680921999-12-13 16:37:25 +000010195#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010196 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010197#endif
10198#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010199 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010200#endif
Fred Draked86ed291999-12-15 15:34:33 +000010201#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010202 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010203#endif
Fred Drakec9680921999-12-13 16:37:25 +000010204#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010205 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010206#endif
10207#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010208 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010209#endif
10210#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010211 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010212#endif
10213#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010214 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010215#endif
10216#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010217 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010218#endif
10219#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010220 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010221#endif
10222#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010223 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000010224#endif
10225#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010226 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000010227#endif
10228#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010229 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000010230#endif
Fred Draked86ed291999-12-15 15:34:33 +000010231#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010232 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000010233#endif
10234#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000010235 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000010236#endif
Fred Drakec9680921999-12-13 16:37:25 +000010237#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000010238 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000010239#endif
10240#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010241 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010242#endif
10243#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010244 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010245#endif
10246#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010247 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010248#endif
10249#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010250 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010251#endif
10252#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000010253 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000010254#endif
10255#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000010256 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000010257#endif
10258#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000010259 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000010260#endif
10261#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010262 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000010263#endif
10264#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000010265 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000010266#endif
10267#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000010268 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000010269#endif
10270#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010271 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000010272#endif
10273#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010274 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000010275#endif
10276#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000010277 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000010278#endif
10279#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000010280 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000010281#endif
10282#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000010283 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000010284#endif
10285#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000010286 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000010287#endif
10288#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010289 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010290#endif
10291#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010292 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010293#endif
10294#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000010295 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000010296#endif
10297#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010298 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010299#endif
10300#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010301 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010302#endif
10303#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000010304 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000010305#endif
10306#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010307 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010308#endif
10309#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010310 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010311#endif
10312#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010313 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000010314#endif
10315#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000010316 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000010317#endif
10318#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010319 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010320#endif
10321#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010322 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010323#endif
10324#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000010325 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000010326#endif
10327#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010328 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010329#endif
10330#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010331 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010332#endif
10333#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010334 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010335#endif
10336#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010337 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010338#endif
10339#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010340 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010341#endif
Fred Draked86ed291999-12-15 15:34:33 +000010342#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000010343 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000010344#endif
Fred Drakec9680921999-12-13 16:37:25 +000010345#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000010346 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000010347#endif
10348#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010349 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010350#endif
10351#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000010352 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000010353#endif
10354#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010355 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010356#endif
10357#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010358 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010359#endif
10360#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010361 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010362#endif
10363#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000010364 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000010365#endif
10366#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000010367 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000010368#endif
10369#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010370 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010371#endif
10372#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010373 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010374#endif
10375#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000010376 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000010377#endif
10378#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010379 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000010380#endif
10381#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010382 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000010383#endif
10384#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000010385 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000010386#endif
10387#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000010388 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000010389#endif
10390#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010391 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000010392#endif
10393#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010394 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010395#endif
10396#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000010397 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000010398#endif
10399#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010400 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010401#endif
10402#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010403 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010404#endif
10405#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010406 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010407#endif
10408#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010409 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010410#endif
10411#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010412 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010413#endif
10414#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010415 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010416#endif
10417#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000010418 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000010419#endif
10420#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010421 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010422#endif
10423#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010424 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010425#endif
10426#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010427 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010428#endif
10429#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010430 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000010431#endif
10432#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000010433 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000010434#endif
10435#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010436 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010437#endif
10438#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000010439 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000010440#endif
10441#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010442 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000010443#endif
10444#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000010445 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000010446#endif
10447#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000010448 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000010449#endif
10450#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000010451 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000010452#endif
10453#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000010454 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000010455#endif
10456#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000010457 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000010458#endif
10459#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000010460 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000010461#endif
10462#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000010463 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000010464#endif
10465#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010466 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010467#endif
10468#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000010469 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000010470#endif
10471#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000010472 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000010473#endif
10474#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000010475 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000010476#endif
10477#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000010478 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000010479#endif
10480};
10481
10482static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010483conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010484{
10485 return conv_confname(arg, valuep, posix_constants_sysconf,
10486 sizeof(posix_constants_sysconf)
10487 / sizeof(struct constdef));
10488}
10489
Larry Hastings2f936352014-08-05 14:04:04 +100010490
10491/*[clinic input]
10492os.sysconf -> long
10493 name: sysconf_confname
10494 /
10495
10496Return an integer-valued system configuration variable.
10497[clinic start generated code]*/
10498
Larry Hastings2f936352014-08-05 14:04:04 +100010499static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010500os_sysconf_impl(PyObject *module, int name)
10501/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010502{
10503 long value;
10504
10505 errno = 0;
10506 value = sysconf(name);
10507 if (value == -1 && errno != 0)
10508 posix_error();
10509 return value;
10510}
10511#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010512
10513
Fred Drakebec628d1999-12-15 18:31:10 +000010514/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020010515 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000010516 * the exported dictionaries that are used to publish information about the
10517 * names available on the host platform.
10518 *
10519 * Sorting the table at runtime ensures that the table is properly ordered
10520 * when used, even for platforms we're not able to test on. It also makes
10521 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000010522 */
Fred Drakebec628d1999-12-15 18:31:10 +000010523
10524static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010525cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000010526{
10527 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000010529 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000010530 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000010531
10532 return strcmp(c1->name, c2->name);
10533}
10534
10535static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010536setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010537 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010538{
Fred Drakebec628d1999-12-15 18:31:10 +000010539 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000010540 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000010541
10542 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
10543 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000010544 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000010545 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010546
Barry Warsaw3155db32000-04-13 15:20:40 +000010547 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010548 PyObject *o = PyLong_FromLong(table[i].value);
10549 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
10550 Py_XDECREF(o);
10551 Py_DECREF(d);
10552 return -1;
10553 }
10554 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000010555 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000010556 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000010557}
10558
Fred Drakebec628d1999-12-15 18:31:10 +000010559/* Return -1 on failure, 0 on success. */
10560static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000010561setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000010562{
10563#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000010564 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000010565 sizeof(posix_constants_pathconf)
10566 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010567 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010568 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010569#endif
10570#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000010571 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000010572 sizeof(posix_constants_confstr)
10573 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010574 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010575 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010576#endif
10577#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000010578 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000010579 sizeof(posix_constants_sysconf)
10580 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000010581 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000010582 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000010583#endif
Fred Drakebec628d1999-12-15 18:31:10 +000010584 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000010585}
Fred Draked86ed291999-12-15 15:34:33 +000010586
10587
Larry Hastings2f936352014-08-05 14:04:04 +100010588/*[clinic input]
10589os.abort
10590
10591Abort the interpreter immediately.
10592
10593This function 'dumps core' or otherwise fails in the hardest way possible
10594on the hosting operating system. This function never returns.
10595[clinic start generated code]*/
10596
Larry Hastings2f936352014-08-05 14:04:04 +100010597static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010598os_abort_impl(PyObject *module)
10599/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010600{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010601 abort();
10602 /*NOTREACHED*/
10603 Py_FatalError("abort() called from Python code didn't abort!");
10604 return NULL;
10605}
Fred Drakebec628d1999-12-15 18:31:10 +000010606
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010607#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010608/* AC 3.5: change to path_t? but that might change exceptions */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010609PyDoc_STRVAR(win32_startfile__doc__,
Larry Hastings2f936352014-08-05 14:04:04 +100010610"startfile(filepath [, operation])\n\
10611\n\
10612Start a file with its associated application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +000010613\n\
Georg Brandlf4f44152006-02-18 22:29:33 +000010614When \"operation\" is not specified or \"open\", this acts like\n\
10615double-clicking the file in Explorer, or giving the file name as an\n\
10616argument to the DOS \"start\" command: the file is opened with whatever\n\
10617application (if any) its extension is associated.\n\
10618When another \"operation\" is given, it specifies what should be done with\n\
10619the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +000010620\n\
10621startfile returns as soon as the associated application is launched.\n\
10622There is no option to wait for the application to close, and no way\n\
10623to retrieve the application's exit status.\n\
10624\n\
10625The filepath is relative to the current directory. If you want to use\n\
10626an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000010627the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +000010628
Steve Dower7d0e0c92015-01-24 08:18:24 -080010629/* Grab ShellExecute dynamically from shell32 */
10630static int has_ShellExecute = -1;
10631static HINSTANCE (CALLBACK *Py_ShellExecuteA)(HWND, LPCSTR, LPCSTR, LPCSTR,
10632 LPCSTR, INT);
10633static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
10634 LPCWSTR, INT);
10635static int
10636check_ShellExecute()
10637{
10638 HINSTANCE hShell32;
10639
10640 /* only recheck */
10641 if (-1 == has_ShellExecute) {
10642 Py_BEGIN_ALLOW_THREADS
10643 hShell32 = LoadLibraryW(L"SHELL32");
10644 Py_END_ALLOW_THREADS
10645 if (hShell32) {
10646 *(FARPROC*)&Py_ShellExecuteA = GetProcAddress(hShell32,
10647 "ShellExecuteA");
10648 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
10649 "ShellExecuteW");
10650 has_ShellExecute = Py_ShellExecuteA &&
10651 Py_ShellExecuteW;
10652 } else {
10653 has_ShellExecute = 0;
10654 }
10655 }
10656 return has_ShellExecute;
10657}
10658
10659
Tim Petersf58a7aa2000-09-22 10:05:54 +000010660static PyObject *
10661win32_startfile(PyObject *self, PyObject *args)
10662{
Victor Stinner8c62be82010-05-06 00:08:46 +000010663 PyObject *ofilepath;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030010664 const char *filepath;
10665 const char *operation = NULL;
10666 const wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +000010667 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000010668
Victor Stinnereb5657a2011-09-30 01:44:27 +020010669 PyObject *unipath, *uoperation = NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080010670
10671 if(!check_ShellExecute()) {
10672 /* If the OS doesn't have ShellExecute, return a
10673 NotImplementedError. */
10674 return PyErr_Format(PyExc_NotImplementedError,
10675 "startfile not available on this platform");
10676 }
10677
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 if (!PyArg_ParseTuple(args, "U|s:startfile",
10679 &unipath, &operation)) {
10680 PyErr_Clear();
10681 goto normal;
10682 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +000010683
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +020010685 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +000010686 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +020010687 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +000010688 PyErr_Clear();
10689 operation = NULL;
10690 goto normal;
10691 }
10692 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +000010693
Victor Stinnereb5657a2011-09-30 01:44:27 +020010694 wpath = PyUnicode_AsUnicode(unipath);
10695 if (wpath == NULL)
10696 goto normal;
10697 if (uoperation) {
10698 woperation = PyUnicode_AsUnicode(uoperation);
10699 if (woperation == NULL)
10700 goto normal;
10701 }
10702 else
10703 woperation = NULL;
10704
Victor Stinner8c62be82010-05-06 00:08:46 +000010705 Py_BEGIN_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010706 rc = Py_ShellExecuteW((HWND)0, woperation, wpath,
10707 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010708 Py_END_ALLOW_THREADS
10709
Victor Stinnereb5657a2011-09-30 01:44:27 +020010710 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +000010711 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +020010712 win32_error_object("startfile", unipath);
10713 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000010714 }
10715 Py_INCREF(Py_None);
10716 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010717
10718normal:
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 if (!PyArg_ParseTuple(args, "O&|s:startfile",
10720 PyUnicode_FSConverter, &ofilepath,
10721 &operation))
10722 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +010010723 if (win32_warn_bytes_api()) {
10724 Py_DECREF(ofilepath);
10725 return NULL;
10726 }
Victor Stinner8c62be82010-05-06 00:08:46 +000010727 filepath = PyBytes_AsString(ofilepath);
10728 Py_BEGIN_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080010729 rc = Py_ShellExecuteA((HWND)0, operation, filepath,
10730 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 Py_END_ALLOW_THREADS
10732 if (rc <= (HINSTANCE)32) {
10733 PyObject *errval = win32_error("startfile", filepath);
10734 Py_DECREF(ofilepath);
10735 return errval;
10736 }
10737 Py_DECREF(ofilepath);
10738 Py_INCREF(Py_None);
10739 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +000010740}
Larry Hastings2f936352014-08-05 14:04:04 +100010741#endif /* MS_WINDOWS */
10742
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010743
Martin v. Löwis438b5342002-12-27 10:16:42 +000010744#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100010745/*[clinic input]
10746os.getloadavg
10747
10748Return average recent system load information.
10749
10750Return the number of processes in the system run queue averaged over
10751the last 1, 5, and 15 minutes as a tuple of three floats.
10752Raises OSError if the load average was unobtainable.
10753[clinic start generated code]*/
10754
Larry Hastings2f936352014-08-05 14:04:04 +100010755static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010756os_getloadavg_impl(PyObject *module)
10757/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000010758{
10759 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000010760 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000010761 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
10762 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000010763 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000010764 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000010765}
Larry Hastings2f936352014-08-05 14:04:04 +100010766#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000010767
Larry Hastings2f936352014-08-05 14:04:04 +100010768
10769/*[clinic input]
10770os.device_encoding
10771 fd: int
10772
10773Return a string describing the encoding of a terminal's file descriptor.
10774
10775The file descriptor must be attached to a terminal.
10776If the device is not a terminal, return None.
10777[clinic start generated code]*/
10778
Larry Hastings2f936352014-08-05 14:04:04 +100010779static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010780os_device_encoding_impl(PyObject *module, int fd)
10781/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010782{
Brett Cannonefb00c02012-02-29 18:31:31 -050010783 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000010784}
10785
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010786
Larry Hastings2f936352014-08-05 14:04:04 +100010787#ifdef HAVE_SETRESUID
10788/*[clinic input]
10789os.setresuid
10790
10791 ruid: uid_t
10792 euid: uid_t
10793 suid: uid_t
10794 /
10795
10796Set the current process's real, effective, and saved user ids.
10797[clinic start generated code]*/
10798
Larry Hastings2f936352014-08-05 14:04:04 +100010799static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010800os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
10801/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010802{
Victor Stinner8c62be82010-05-06 00:08:46 +000010803 if (setresuid(ruid, euid, suid) < 0)
10804 return posix_error();
10805 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010806}
Larry Hastings2f936352014-08-05 14:04:04 +100010807#endif /* HAVE_SETRESUID */
10808
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010809
10810#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010811/*[clinic input]
10812os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010813
Larry Hastings2f936352014-08-05 14:04:04 +100010814 rgid: gid_t
10815 egid: gid_t
10816 sgid: gid_t
10817 /
10818
10819Set the current process's real, effective, and saved group ids.
10820[clinic start generated code]*/
10821
Larry Hastings2f936352014-08-05 14:04:04 +100010822static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010823os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
10824/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010825{
Victor Stinner8c62be82010-05-06 00:08:46 +000010826 if (setresgid(rgid, egid, sgid) < 0)
10827 return posix_error();
10828 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010829}
Larry Hastings2f936352014-08-05 14:04:04 +100010830#endif /* HAVE_SETRESGID */
10831
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010832
10833#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100010834/*[clinic input]
10835os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010836
Larry Hastings2f936352014-08-05 14:04:04 +100010837Return a tuple of the current process's real, effective, and saved user ids.
10838[clinic start generated code]*/
10839
Larry Hastings2f936352014-08-05 14:04:04 +100010840static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010841os_getresuid_impl(PyObject *module)
10842/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010843{
Victor Stinner8c62be82010-05-06 00:08:46 +000010844 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010845 if (getresuid(&ruid, &euid, &suid) < 0)
10846 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010847 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
10848 _PyLong_FromUid(euid),
10849 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010850}
Larry Hastings2f936352014-08-05 14:04:04 +100010851#endif /* HAVE_GETRESUID */
10852
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010853
10854#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100010855/*[clinic input]
10856os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010857
Larry Hastings2f936352014-08-05 14:04:04 +100010858Return a tuple of the current process's real, effective, and saved group ids.
10859[clinic start generated code]*/
10860
Larry Hastings2f936352014-08-05 14:04:04 +100010861static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010862os_getresgid_impl(PyObject *module)
10863/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010864{
10865 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 if (getresgid(&rgid, &egid, &sgid) < 0)
10867 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020010868 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
10869 _PyLong_FromGid(egid),
10870 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010871}
Larry Hastings2f936352014-08-05 14:04:04 +100010872#endif /* HAVE_GETRESGID */
10873
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010874
Benjamin Peterson9428d532011-09-14 11:45:52 -040010875#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100010876/*[clinic input]
10877os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040010878
Larry Hastings2f936352014-08-05 14:04:04 +100010879 path: path_t(allow_fd=True)
10880 attribute: path_t
10881 *
10882 follow_symlinks: bool = True
10883
10884Return the value of extended attribute attribute on path.
10885
10886path may be either a string or an open file descriptor.
10887If follow_symlinks is False, and the last element of the path is a symbolic
10888 link, getxattr will examine the symbolic link itself instead of the file
10889 the link points to.
10890
10891[clinic start generated code]*/
10892
Larry Hastings2f936352014-08-05 14:04:04 +100010893static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010894os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010895 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010896/*[clinic end generated code: output=5f2f44200a43cff2 input=8c8ea3bab78d89c2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010897{
10898 Py_ssize_t i;
10899 PyObject *buffer = NULL;
10900
10901 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
10902 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010903
Larry Hastings9cf065c2012-06-22 16:30:09 -070010904 for (i = 0; ; i++) {
10905 void *ptr;
10906 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010907 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070010908 Py_ssize_t buffer_size = buffer_sizes[i];
10909 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100010910 path_error(path);
10911 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010912 }
10913 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10914 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100010915 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010916 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010917
Larry Hastings9cf065c2012-06-22 16:30:09 -070010918 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010919 if (path->fd >= 0)
10920 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010921 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010922 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010923 else
Larry Hastings2f936352014-08-05 14:04:04 +100010924 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010925 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010926
Larry Hastings9cf065c2012-06-22 16:30:09 -070010927 if (result < 0) {
10928 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010929 if (errno == ERANGE)
10930 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100010931 path_error(path);
10932 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010933 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010934
Larry Hastings9cf065c2012-06-22 16:30:09 -070010935 if (result != buffer_size) {
10936 /* Can only shrink. */
10937 _PyBytes_Resize(&buffer, result);
10938 }
10939 break;
10940 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010941
Larry Hastings9cf065c2012-06-22 16:30:09 -070010942 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010943}
10944
Larry Hastings2f936352014-08-05 14:04:04 +100010945
10946/*[clinic input]
10947os.setxattr
10948
10949 path: path_t(allow_fd=True)
10950 attribute: path_t
10951 value: Py_buffer
10952 flags: int = 0
10953 *
10954 follow_symlinks: bool = True
10955
10956Set extended attribute attribute on path to value.
10957
10958path may be either a string or an open file descriptor.
10959If follow_symlinks is False, and the last element of the path is a symbolic
10960 link, setxattr will modify the symbolic link itself instead of the file
10961 the link points to.
10962
10963[clinic start generated code]*/
10964
Benjamin Peterson799bd802011-08-31 22:15:17 -040010965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010966os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040010967 Py_buffer *value, int flags, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010968/*[clinic end generated code: output=98b83f63fdde26bb input=f0d26833992015c2]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040010969{
Larry Hastings2f936352014-08-05 14:04:04 +100010970 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010971
Larry Hastings2f936352014-08-05 14:04:04 +100010972 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010973 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010974
Benjamin Peterson799bd802011-08-31 22:15:17 -040010975 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100010976 if (path->fd > -1)
10977 result = fsetxattr(path->fd, attribute->narrow,
10978 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010979 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100010980 result = setxattr(path->narrow, attribute->narrow,
10981 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010982 else
Larry Hastings2f936352014-08-05 14:04:04 +100010983 result = lsetxattr(path->narrow, attribute->narrow,
10984 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010985 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010986
Larry Hastings9cf065c2012-06-22 16:30:09 -070010987 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010988 path_error(path);
10989 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010990 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010991
Larry Hastings2f936352014-08-05 14:04:04 +100010992 Py_RETURN_NONE;
10993}
10994
10995
10996/*[clinic input]
10997os.removexattr
10998
10999 path: path_t(allow_fd=True)
11000 attribute: path_t
11001 *
11002 follow_symlinks: bool = True
11003
11004Remove extended attribute attribute on path.
11005
11006path may be either a string or an open file descriptor.
11007If follow_symlinks is False, and the last element of the path is a symbolic
11008 link, removexattr will modify the symbolic link itself instead of the file
11009 the link points to.
11010
11011[clinic start generated code]*/
11012
Larry Hastings2f936352014-08-05 14:04:04 +100011013static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011014os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040011015 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011016/*[clinic end generated code: output=521a51817980cda6 input=cdb54834161e3329]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011017{
11018 ssize_t result;
11019
11020 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
11021 return NULL;
11022
11023 Py_BEGIN_ALLOW_THREADS;
11024 if (path->fd > -1)
11025 result = fremovexattr(path->fd, attribute->narrow);
11026 else if (follow_symlinks)
11027 result = removexattr(path->narrow, attribute->narrow);
11028 else
11029 result = lremovexattr(path->narrow, attribute->narrow);
11030 Py_END_ALLOW_THREADS;
11031
11032 if (result) {
11033 return path_error(path);
11034 }
11035
11036 Py_RETURN_NONE;
11037}
11038
11039
11040/*[clinic input]
11041os.listxattr
11042
11043 path: path_t(allow_fd=True, nullable=True) = None
11044 *
11045 follow_symlinks: bool = True
11046
11047Return a list of extended attributes on path.
11048
11049path may be either None, a string, or an open file descriptor.
11050if path is None, listxattr will examine the current directory.
11051If follow_symlinks is False, and the last element of the path is a symbolic
11052 link, listxattr will examine the symbolic link itself instead of the file
11053 the link points to.
11054[clinic start generated code]*/
11055
Larry Hastings2f936352014-08-05 14:04:04 +100011056static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011057os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
11058/*[clinic end generated code: output=bebdb4e2ad0ce435 input=08cca53ac0b07c13]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011059{
Larry Hastings9cf065c2012-06-22 16:30:09 -070011060 Py_ssize_t i;
11061 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100011062 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011063 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011064
Larry Hastings2f936352014-08-05 14:04:04 +100011065 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070011066 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011067
Larry Hastings2f936352014-08-05 14:04:04 +100011068 name = path->narrow ? path->narrow : ".";
11069
Larry Hastings9cf065c2012-06-22 16:30:09 -070011070 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011071 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011072 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011073 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070011074 Py_ssize_t buffer_size = buffer_sizes[i];
11075 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020011076 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100011077 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011078 break;
11079 }
11080 buffer = PyMem_MALLOC(buffer_size);
11081 if (!buffer) {
11082 PyErr_NoMemory();
11083 break;
11084 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011085
Larry Hastings9cf065c2012-06-22 16:30:09 -070011086 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100011087 if (path->fd > -1)
11088 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011089 else if (follow_symlinks)
11090 length = listxattr(name, buffer, buffer_size);
11091 else
11092 length = llistxattr(name, buffer, buffer_size);
11093 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011094
Larry Hastings9cf065c2012-06-22 16:30:09 -070011095 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020011096 if (errno == ERANGE) {
11097 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050011098 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011099 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020011100 }
Larry Hastings2f936352014-08-05 14:04:04 +100011101 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011102 break;
11103 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011104
Larry Hastings9cf065c2012-06-22 16:30:09 -070011105 result = PyList_New(0);
11106 if (!result) {
11107 goto exit;
11108 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040011109
Larry Hastings9cf065c2012-06-22 16:30:09 -070011110 end = buffer + length;
11111 for (trace = start = buffer; trace != end; trace++) {
11112 if (!*trace) {
11113 int error;
11114 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
11115 trace - start);
11116 if (!attribute) {
11117 Py_DECREF(result);
11118 result = NULL;
11119 goto exit;
11120 }
11121 error = PyList_Append(result, attribute);
11122 Py_DECREF(attribute);
11123 if (error) {
11124 Py_DECREF(result);
11125 result = NULL;
11126 goto exit;
11127 }
11128 start = trace + 1;
11129 }
11130 }
11131 break;
11132 }
11133exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070011134 if (buffer)
11135 PyMem_FREE(buffer);
11136 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011137}
Benjamin Peterson9428d532011-09-14 11:45:52 -040011138#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040011139
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011140
Larry Hastings2f936352014-08-05 14:04:04 +100011141/*[clinic input]
11142os.urandom
11143
11144 size: Py_ssize_t
11145 /
11146
11147Return a bytes object containing random bytes suitable for cryptographic use.
11148[clinic start generated code]*/
11149
Larry Hastings2f936352014-08-05 14:04:04 +100011150static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011151os_urandom_impl(PyObject *module, Py_ssize_t size)
11152/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011153{
11154 PyObject *bytes;
11155 int result;
11156
Georg Brandl2fb477c2012-02-21 00:33:36 +010011157 if (size < 0)
11158 return PyErr_Format(PyExc_ValueError,
11159 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100011160 bytes = PyBytes_FromStringAndSize(NULL, size);
11161 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010011162 return NULL;
11163
Larry Hastings2f936352014-08-05 14:04:04 +100011164 result = _PyOS_URandom(PyBytes_AS_STRING(bytes),
11165 PyBytes_GET_SIZE(bytes));
11166 if (result == -1) {
11167 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010011168 return NULL;
11169 }
Larry Hastings2f936352014-08-05 14:04:04 +100011170 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010011171}
11172
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011173/* Terminal size querying */
11174
11175static PyTypeObject TerminalSizeType;
11176
11177PyDoc_STRVAR(TerminalSize_docstring,
11178 "A tuple of (columns, lines) for holding terminal window size");
11179
11180static PyStructSequence_Field TerminalSize_fields[] = {
11181 {"columns", "width of the terminal window in characters"},
11182 {"lines", "height of the terminal window in characters"},
11183 {NULL, NULL}
11184};
11185
11186static PyStructSequence_Desc TerminalSize_desc = {
11187 "os.terminal_size",
11188 TerminalSize_docstring,
11189 TerminalSize_fields,
11190 2,
11191};
11192
11193#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Larry Hastings2f936352014-08-05 14:04:04 +100011194/* AC 3.5: fd should accept None */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011195PyDoc_STRVAR(termsize__doc__,
11196 "Return the size of the terminal window as (columns, lines).\n" \
11197 "\n" \
11198 "The optional argument fd (default standard output) specifies\n" \
11199 "which file descriptor should be queried.\n" \
11200 "\n" \
11201 "If the file descriptor is not connected to a terminal, an OSError\n" \
11202 "is thrown.\n" \
11203 "\n" \
11204 "This function will only be defined if an implementation is\n" \
11205 "available for this system.\n" \
11206 "\n" \
11207 "shutil.get_terminal_size is the high-level function which should \n" \
11208 "normally be used, os.get_terminal_size is the low-level implementation.");
11209
11210static PyObject*
11211get_terminal_size(PyObject *self, PyObject *args)
11212{
11213 int columns, lines;
11214 PyObject *termsize;
11215
11216 int fd = fileno(stdout);
11217 /* Under some conditions stdout may not be connected and
11218 * fileno(stdout) may point to an invalid file descriptor. For example
11219 * GUI apps don't have valid standard streams by default.
11220 *
11221 * If this happens, and the optional fd argument is not present,
11222 * the ioctl below will fail returning EBADF. This is what we want.
11223 */
11224
11225 if (!PyArg_ParseTuple(args, "|i", &fd))
11226 return NULL;
11227
11228#ifdef TERMSIZE_USE_IOCTL
11229 {
11230 struct winsize w;
11231 if (ioctl(fd, TIOCGWINSZ, &w))
11232 return PyErr_SetFromErrno(PyExc_OSError);
11233 columns = w.ws_col;
11234 lines = w.ws_row;
11235 }
11236#endif /* TERMSIZE_USE_IOCTL */
11237
11238#ifdef TERMSIZE_USE_CONIO
11239 {
11240 DWORD nhandle;
11241 HANDLE handle;
11242 CONSOLE_SCREEN_BUFFER_INFO csbi;
11243 switch (fd) {
11244 case 0: nhandle = STD_INPUT_HANDLE;
11245 break;
11246 case 1: nhandle = STD_OUTPUT_HANDLE;
11247 break;
11248 case 2: nhandle = STD_ERROR_HANDLE;
11249 break;
11250 default:
11251 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
11252 }
11253 handle = GetStdHandle(nhandle);
11254 if (handle == NULL)
11255 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
11256 if (handle == INVALID_HANDLE_VALUE)
11257 return PyErr_SetFromWindowsErr(0);
11258
11259 if (!GetConsoleScreenBufferInfo(handle, &csbi))
11260 return PyErr_SetFromWindowsErr(0);
11261
11262 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
11263 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
11264 }
11265#endif /* TERMSIZE_USE_CONIO */
11266
11267 termsize = PyStructSequence_New(&TerminalSizeType);
11268 if (termsize == NULL)
11269 return NULL;
11270 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
11271 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
11272 if (PyErr_Occurred()) {
11273 Py_DECREF(termsize);
11274 return NULL;
11275 }
11276 return termsize;
11277}
11278#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
11279
Larry Hastings2f936352014-08-05 14:04:04 +100011280
11281/*[clinic input]
11282os.cpu_count
11283
Charles-François Natali80d62e62015-08-13 20:37:08 +010011284Return the number of CPUs in the system; return None if indeterminable.
11285
11286This number is not equivalent to the number of CPUs the current process can
11287use. The number of usable CPUs can be obtained with
11288``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100011289[clinic start generated code]*/
11290
Larry Hastings2f936352014-08-05 14:04:04 +100011291static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011292os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030011293/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011294{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011295 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011296#ifdef MS_WINDOWS
11297 SYSTEM_INFO sysinfo;
11298 GetSystemInfo(&sysinfo);
11299 ncpu = sysinfo.dwNumberOfProcessors;
11300#elif defined(__hpux)
11301 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
11302#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
11303 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011304#elif defined(__DragonFly__) || \
11305 defined(__OpenBSD__) || \
11306 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020011307 defined(__NetBSD__) || \
11308 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020011309 int mib[2];
11310 size_t len = sizeof(ncpu);
11311 mib[0] = CTL_HW;
11312 mib[1] = HW_NCPU;
11313 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
11314 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020011315#endif
11316 if (ncpu >= 1)
11317 return PyLong_FromLong(ncpu);
11318 else
11319 Py_RETURN_NONE;
11320}
11321
Victor Stinnerdaf45552013-08-28 00:53:59 +020011322
Larry Hastings2f936352014-08-05 14:04:04 +100011323/*[clinic input]
11324os.get_inheritable -> bool
11325
11326 fd: int
11327 /
11328
11329Get the close-on-exe flag of the specified file descriptor.
11330[clinic start generated code]*/
11331
Larry Hastings2f936352014-08-05 14:04:04 +100011332static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011333os_get_inheritable_impl(PyObject *module, int fd)
11334/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011335{
Steve Dower8fc89802015-04-12 00:26:27 -040011336 int return_value;
11337 if (!_PyVerify_fd(fd)) {
Larry Hastings2f936352014-08-05 14:04:04 +100011338 posix_error();
11339 return -1;
11340 }
11341
Steve Dower8fc89802015-04-12 00:26:27 -040011342 _Py_BEGIN_SUPPRESS_IPH
11343 return_value = _Py_get_inheritable(fd);
11344 _Py_END_SUPPRESS_IPH
11345 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100011346}
11347
11348
11349/*[clinic input]
11350os.set_inheritable
11351 fd: int
11352 inheritable: int
11353 /
11354
11355Set the inheritable flag of the specified file descriptor.
11356[clinic start generated code]*/
11357
Larry Hastings2f936352014-08-05 14:04:04 +100011358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011359os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
11360/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020011361{
Steve Dower8fc89802015-04-12 00:26:27 -040011362 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011363 if (!_PyVerify_fd(fd))
11364 return posix_error();
11365
Steve Dower8fc89802015-04-12 00:26:27 -040011366 _Py_BEGIN_SUPPRESS_IPH
11367 result = _Py_set_inheritable(fd, inheritable, NULL);
11368 _Py_END_SUPPRESS_IPH
11369 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020011370 return NULL;
11371 Py_RETURN_NONE;
11372}
11373
11374
11375#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011376/*[clinic input]
11377os.get_handle_inheritable -> bool
11378 handle: Py_intptr_t
11379 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020011380
Larry Hastings2f936352014-08-05 14:04:04 +100011381Get the close-on-exe flag of the specified file descriptor.
11382[clinic start generated code]*/
11383
Larry Hastings2f936352014-08-05 14:04:04 +100011384static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011385os_get_handle_inheritable_impl(PyObject *module, Py_intptr_t handle)
11386/*[clinic end generated code: output=9e5389b0aa0916ce input=5f7759443aae3dc5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011387{
11388 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011389
11390 if (!GetHandleInformation((HANDLE)handle, &flags)) {
11391 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100011392 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011393 }
11394
Larry Hastings2f936352014-08-05 14:04:04 +100011395 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011396}
11397
Victor Stinnerdaf45552013-08-28 00:53:59 +020011398
Larry Hastings2f936352014-08-05 14:04:04 +100011399/*[clinic input]
11400os.set_handle_inheritable
11401 handle: Py_intptr_t
11402 inheritable: bool
11403 /
11404
11405Set the inheritable flag of the specified handle.
11406[clinic start generated code]*/
11407
Larry Hastings2f936352014-08-05 14:04:04 +100011408static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011409os_set_handle_inheritable_impl(PyObject *module, Py_intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040011410 int inheritable)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011411/*[clinic end generated code: output=b1e67bfa3213d745 input=e64b2b2730469def]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011412{
11413 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020011414 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
11415 PyErr_SetFromWindowsErr(0);
11416 return NULL;
11417 }
11418 Py_RETURN_NONE;
11419}
Larry Hastings2f936352014-08-05 14:04:04 +100011420#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011421
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011422#ifndef MS_WINDOWS
11423PyDoc_STRVAR(get_blocking__doc__,
11424 "get_blocking(fd) -> bool\n" \
11425 "\n" \
11426 "Get the blocking mode of the file descriptor:\n" \
11427 "False if the O_NONBLOCK flag is set, True if the flag is cleared.");
11428
11429static PyObject*
11430posix_get_blocking(PyObject *self, PyObject *args)
11431{
11432 int fd;
11433 int blocking;
11434
11435 if (!PyArg_ParseTuple(args, "i:get_blocking", &fd))
11436 return NULL;
11437
11438 if (!_PyVerify_fd(fd))
11439 return posix_error();
11440
Steve Dower8fc89802015-04-12 00:26:27 -040011441 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011442 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040011443 _Py_END_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011444 if (blocking < 0)
11445 return NULL;
11446 return PyBool_FromLong(blocking);
11447}
11448
11449PyDoc_STRVAR(set_blocking__doc__,
11450 "set_blocking(fd, blocking)\n" \
11451 "\n" \
11452 "Set the blocking mode of the specified file descriptor.\n" \
11453 "Set the O_NONBLOCK flag if blocking is False,\n" \
11454 "clear the O_NONBLOCK flag otherwise.");
11455
11456static PyObject*
11457posix_set_blocking(PyObject *self, PyObject *args)
11458{
Steve Dower8fc89802015-04-12 00:26:27 -040011459 int fd, blocking, result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011460
11461 if (!PyArg_ParseTuple(args, "ii:set_blocking", &fd, &blocking))
11462 return NULL;
11463
11464 if (!_PyVerify_fd(fd))
11465 return posix_error();
11466
Steve Dower8fc89802015-04-12 00:26:27 -040011467 _Py_BEGIN_SUPPRESS_IPH
11468 result = _Py_set_blocking(fd, blocking);
11469 _Py_END_SUPPRESS_IPH
11470 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020011471 return NULL;
11472 Py_RETURN_NONE;
11473}
11474#endif /* !MS_WINDOWS */
11475
11476
Victor Stinner6036e442015-03-08 01:58:04 +010011477PyDoc_STRVAR(posix_scandir__doc__,
11478"scandir(path='.') -> iterator of DirEntry objects for given path");
11479
11480static char *follow_symlinks_keywords[] = {"follow_symlinks", NULL};
11481
11482typedef struct {
11483 PyObject_HEAD
11484 PyObject *name;
11485 PyObject *path;
11486 PyObject *stat;
11487 PyObject *lstat;
11488#ifdef MS_WINDOWS
11489 struct _Py_stat_struct win32_lstat;
11490 __int64 win32_file_index;
11491 int got_file_index;
11492#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010011493#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011494 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011495#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011496 ino_t d_ino;
11497#endif
11498} DirEntry;
11499
11500static void
11501DirEntry_dealloc(DirEntry *entry)
11502{
11503 Py_XDECREF(entry->name);
11504 Py_XDECREF(entry->path);
11505 Py_XDECREF(entry->stat);
11506 Py_XDECREF(entry->lstat);
11507 Py_TYPE(entry)->tp_free((PyObject *)entry);
11508}
11509
11510/* Forward reference */
11511static int
11512DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
11513
11514/* Set exception and return -1 on error, 0 for False, 1 for True */
11515static int
11516DirEntry_is_symlink(DirEntry *self)
11517{
11518#ifdef MS_WINDOWS
11519 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010011520#elif defined(HAVE_DIRENT_D_TYPE)
11521 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010011522 if (self->d_type != DT_UNKNOWN)
11523 return self->d_type == DT_LNK;
11524 else
11525 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010011526#else
11527 /* POSIX without d_type */
11528 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010011529#endif
11530}
11531
11532static PyObject *
11533DirEntry_py_is_symlink(DirEntry *self)
11534{
11535 int result;
11536
11537 result = DirEntry_is_symlink(self);
11538 if (result == -1)
11539 return NULL;
11540 return PyBool_FromLong(result);
11541}
11542
11543static PyObject *
11544DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
11545{
11546 int result;
11547 struct _Py_stat_struct st;
11548
11549#ifdef MS_WINDOWS
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011550 const wchar_t *path;
Victor Stinner6036e442015-03-08 01:58:04 +010011551
11552 path = PyUnicode_AsUnicode(self->path);
11553 if (!path)
11554 return NULL;
11555
11556 if (follow_symlinks)
11557 result = win32_stat_w(path, &st);
11558 else
11559 result = win32_lstat_w(path, &st);
11560
11561 if (result != 0) {
11562 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11563 0, self->path);
11564 }
11565#else /* POSIX */
11566 PyObject *bytes;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011567 const char *path;
Victor Stinner6036e442015-03-08 01:58:04 +010011568
11569 if (!PyUnicode_FSConverter(self->path, &bytes))
11570 return NULL;
11571 path = PyBytes_AS_STRING(bytes);
11572
11573 if (follow_symlinks)
11574 result = STAT(path, &st);
11575 else
11576 result = LSTAT(path, &st);
11577 Py_DECREF(bytes);
11578
11579 if (result != 0)
11580 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path);
11581#endif
11582
11583 return _pystat_fromstructstat(&st);
11584}
11585
11586static PyObject *
11587DirEntry_get_lstat(DirEntry *self)
11588{
11589 if (!self->lstat) {
11590#ifdef MS_WINDOWS
11591 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
11592#else /* POSIX */
11593 self->lstat = DirEntry_fetch_stat(self, 0);
11594#endif
11595 }
11596 Py_XINCREF(self->lstat);
11597 return self->lstat;
11598}
11599
11600static PyObject *
11601DirEntry_get_stat(DirEntry *self, int follow_symlinks)
11602{
11603 if (!follow_symlinks)
11604 return DirEntry_get_lstat(self);
11605
11606 if (!self->stat) {
11607 int result = DirEntry_is_symlink(self);
11608 if (result == -1)
11609 return NULL;
11610 else if (result)
11611 self->stat = DirEntry_fetch_stat(self, 1);
11612 else
11613 self->stat = DirEntry_get_lstat(self);
11614 }
11615
11616 Py_XINCREF(self->stat);
11617 return self->stat;
11618}
11619
11620static PyObject *
11621DirEntry_stat(DirEntry *self, PyObject *args, PyObject *kwargs)
11622{
11623 int follow_symlinks = 1;
11624
11625 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.stat",
11626 follow_symlinks_keywords, &follow_symlinks))
11627 return NULL;
11628
11629 return DirEntry_get_stat(self, follow_symlinks);
11630}
11631
11632/* Set exception and return -1 on error, 0 for False, 1 for True */
11633static int
11634DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11635{
11636 PyObject *stat = NULL;
11637 PyObject *st_mode = NULL;
11638 long mode;
11639 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010011640#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011641 int is_symlink;
11642 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010011643#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011644#ifdef MS_WINDOWS
11645 unsigned long dir_bits;
11646#endif
Victor Stinner35a97c02015-03-08 02:59:09 +010011647 _Py_IDENTIFIER(st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010011648
11649#ifdef MS_WINDOWS
11650 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
11651 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010011652#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011653 is_symlink = self->d_type == DT_LNK;
11654 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
11655#endif
11656
Victor Stinner35a97c02015-03-08 02:59:09 +010011657#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011658 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010011659#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011660 stat = DirEntry_get_stat(self, follow_symlinks);
11661 if (!stat) {
11662 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
11663 /* If file doesn't exist (anymore), then return False
11664 (i.e., say it's not a file/directory) */
11665 PyErr_Clear();
11666 return 0;
11667 }
11668 goto error;
11669 }
11670 st_mode = _PyObject_GetAttrId(stat, &PyId_st_mode);
11671 if (!st_mode)
11672 goto error;
11673
11674 mode = PyLong_AsLong(st_mode);
11675 if (mode == -1 && PyErr_Occurred())
11676 goto error;
11677 Py_CLEAR(st_mode);
11678 Py_CLEAR(stat);
11679 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010011680#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010011681 }
11682 else if (is_symlink) {
11683 assert(mode_bits != S_IFLNK);
11684 result = 0;
11685 }
11686 else {
11687 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
11688#ifdef MS_WINDOWS
11689 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
11690 if (mode_bits == S_IFDIR)
11691 result = dir_bits != 0;
11692 else
11693 result = dir_bits == 0;
11694#else /* POSIX */
11695 if (mode_bits == S_IFDIR)
11696 result = self->d_type == DT_DIR;
11697 else
11698 result = self->d_type == DT_REG;
11699#endif
11700 }
Victor Stinner35a97c02015-03-08 02:59:09 +010011701#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011702
11703 return result;
11704
11705error:
11706 Py_XDECREF(st_mode);
11707 Py_XDECREF(stat);
11708 return -1;
11709}
11710
11711static PyObject *
11712DirEntry_py_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
11713{
11714 int result;
11715
11716 result = DirEntry_test_mode(self, follow_symlinks, mode_bits);
11717 if (result == -1)
11718 return NULL;
11719 return PyBool_FromLong(result);
11720}
11721
11722static PyObject *
11723DirEntry_is_dir(DirEntry *self, PyObject *args, PyObject *kwargs)
11724{
11725 int follow_symlinks = 1;
11726
11727 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_dir",
11728 follow_symlinks_keywords, &follow_symlinks))
11729 return NULL;
11730
11731 return DirEntry_py_test_mode(self, follow_symlinks, S_IFDIR);
11732}
11733
11734static PyObject *
11735DirEntry_is_file(DirEntry *self, PyObject *args, PyObject *kwargs)
11736{
11737 int follow_symlinks = 1;
11738
11739 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|$p:DirEntry.is_file",
11740 follow_symlinks_keywords, &follow_symlinks))
11741 return NULL;
11742
11743 return DirEntry_py_test_mode(self, follow_symlinks, S_IFREG);
11744}
11745
11746static PyObject *
11747DirEntry_inode(DirEntry *self)
11748{
11749#ifdef MS_WINDOWS
11750 if (!self->got_file_index) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011751 const wchar_t *path;
Victor Stinner6036e442015-03-08 01:58:04 +010011752 struct _Py_stat_struct stat;
11753
11754 path = PyUnicode_AsUnicode(self->path);
11755 if (!path)
11756 return NULL;
11757
11758 if (win32_lstat_w(path, &stat) != 0) {
11759 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
11760 0, self->path);
11761 }
11762
11763 self->win32_file_index = stat.st_ino;
11764 self->got_file_index = 1;
11765 }
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011766 return PyLong_FromLongLong((long long)self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010011767#else /* POSIX */
11768#ifdef HAVE_LARGEFILE_SUPPORT
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011769 return PyLong_FromLongLong((long long)self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010011770#else
11771 return PyLong_FromLong((long)self->d_ino);
11772#endif
11773#endif
11774}
11775
11776static PyObject *
11777DirEntry_repr(DirEntry *self)
11778{
11779 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
11780}
11781
Brett Cannon96881cd2016-06-10 14:37:21 -070011782static PyObject *
11783DirEntry_fspath(DirEntry *self)
11784{
11785 Py_INCREF(self->path);
11786 return self->path;
11787}
11788
Victor Stinner6036e442015-03-08 01:58:04 +010011789static PyMemberDef DirEntry_members[] = {
11790 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
11791 "the entry's base filename, relative to scandir() \"path\" argument"},
11792 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
11793 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
11794 {NULL}
11795};
11796
11797static PyMethodDef DirEntry_methods[] = {
11798 {"is_dir", (PyCFunction)DirEntry_is_dir, METH_VARARGS | METH_KEYWORDS,
11799 "return True if the entry is a directory; cached per entry"
11800 },
11801 {"is_file", (PyCFunction)DirEntry_is_file, METH_VARARGS | METH_KEYWORDS,
11802 "return True if the entry is a file; cached per entry"
11803 },
11804 {"is_symlink", (PyCFunction)DirEntry_py_is_symlink, METH_NOARGS,
11805 "return True if the entry is a symbolic link; cached per entry"
11806 },
11807 {"stat", (PyCFunction)DirEntry_stat, METH_VARARGS | METH_KEYWORDS,
11808 "return stat_result object for the entry; cached per entry"
11809 },
11810 {"inode", (PyCFunction)DirEntry_inode, METH_NOARGS,
11811 "return inode of the entry; cached per entry",
11812 },
Brett Cannon96881cd2016-06-10 14:37:21 -070011813 {"__fspath__", (PyCFunction)DirEntry_fspath, METH_NOARGS,
11814 "returns the path for the entry",
11815 },
Victor Stinner6036e442015-03-08 01:58:04 +010011816 {NULL}
11817};
11818
Benjamin Peterson5646de42015-04-12 17:56:34 -040011819static PyTypeObject DirEntryType = {
Victor Stinner6036e442015-03-08 01:58:04 +010011820 PyVarObject_HEAD_INIT(NULL, 0)
11821 MODNAME ".DirEntry", /* tp_name */
11822 sizeof(DirEntry), /* tp_basicsize */
11823 0, /* tp_itemsize */
11824 /* methods */
11825 (destructor)DirEntry_dealloc, /* tp_dealloc */
11826 0, /* tp_print */
11827 0, /* tp_getattr */
11828 0, /* tp_setattr */
11829 0, /* tp_compare */
11830 (reprfunc)DirEntry_repr, /* tp_repr */
11831 0, /* tp_as_number */
11832 0, /* tp_as_sequence */
11833 0, /* tp_as_mapping */
11834 0, /* tp_hash */
11835 0, /* tp_call */
11836 0, /* tp_str */
11837 0, /* tp_getattro */
11838 0, /* tp_setattro */
11839 0, /* tp_as_buffer */
11840 Py_TPFLAGS_DEFAULT, /* tp_flags */
11841 0, /* tp_doc */
11842 0, /* tp_traverse */
11843 0, /* tp_clear */
11844 0, /* tp_richcompare */
11845 0, /* tp_weaklistoffset */
11846 0, /* tp_iter */
11847 0, /* tp_iternext */
11848 DirEntry_methods, /* tp_methods */
11849 DirEntry_members, /* tp_members */
11850};
11851
11852#ifdef MS_WINDOWS
11853
11854static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011855join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010011856{
11857 Py_ssize_t path_len;
11858 Py_ssize_t size;
11859 wchar_t *result;
11860 wchar_t ch;
11861
11862 if (!path_wide) { /* Default arg: "." */
11863 path_wide = L".";
11864 path_len = 1;
11865 }
11866 else {
11867 path_len = wcslen(path_wide);
11868 }
11869
11870 /* The +1's are for the path separator and the NUL */
11871 size = path_len + 1 + wcslen(filename) + 1;
11872 result = PyMem_New(wchar_t, size);
11873 if (!result) {
11874 PyErr_NoMemory();
11875 return NULL;
11876 }
11877 wcscpy(result, path_wide);
11878 if (path_len > 0) {
11879 ch = result[path_len - 1];
11880 if (ch != SEP && ch != ALTSEP && ch != L':')
11881 result[path_len++] = SEP;
11882 wcscpy(result + path_len, filename);
11883 }
11884 return result;
11885}
11886
11887static PyObject *
11888DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
11889{
11890 DirEntry *entry;
11891 BY_HANDLE_FILE_INFORMATION file_info;
11892 ULONG reparse_tag;
11893 wchar_t *joined_path;
11894
11895 entry = PyObject_New(DirEntry, &DirEntryType);
11896 if (!entry)
11897 return NULL;
11898 entry->name = NULL;
11899 entry->path = NULL;
11900 entry->stat = NULL;
11901 entry->lstat = NULL;
11902 entry->got_file_index = 0;
11903
11904 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
11905 if (!entry->name)
11906 goto error;
11907
11908 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
11909 if (!joined_path)
11910 goto error;
11911
11912 entry->path = PyUnicode_FromWideChar(joined_path, -1);
11913 PyMem_Free(joined_path);
11914 if (!entry->path)
11915 goto error;
11916
11917 find_data_to_file_info_w(dataW, &file_info, &reparse_tag);
11918 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
11919
11920 return (PyObject *)entry;
11921
11922error:
11923 Py_DECREF(entry);
11924 return NULL;
11925}
11926
11927#else /* POSIX */
11928
11929static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020011930join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010011931{
11932 Py_ssize_t path_len;
11933 Py_ssize_t size;
11934 char *result;
11935
11936 if (!path_narrow) { /* Default arg: "." */
11937 path_narrow = ".";
11938 path_len = 1;
11939 }
11940 else {
11941 path_len = strlen(path_narrow);
11942 }
11943
11944 if (filename_len == -1)
11945 filename_len = strlen(filename);
11946
11947 /* The +1's are for the path separator and the NUL */
11948 size = path_len + 1 + filename_len + 1;
11949 result = PyMem_New(char, size);
11950 if (!result) {
11951 PyErr_NoMemory();
11952 return NULL;
11953 }
11954 strcpy(result, path_narrow);
11955 if (path_len > 0 && result[path_len - 1] != '/')
11956 result[path_len++] = '/';
11957 strcpy(result + path_len, filename);
11958 return result;
11959}
11960
11961static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020011962DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010011963 ino_t d_ino
11964#ifdef HAVE_DIRENT_D_TYPE
11965 , unsigned char d_type
11966#endif
11967 )
Victor Stinner6036e442015-03-08 01:58:04 +010011968{
11969 DirEntry *entry;
11970 char *joined_path;
11971
11972 entry = PyObject_New(DirEntry, &DirEntryType);
11973 if (!entry)
11974 return NULL;
11975 entry->name = NULL;
11976 entry->path = NULL;
11977 entry->stat = NULL;
11978 entry->lstat = NULL;
11979
11980 joined_path = join_path_filename(path->narrow, name, name_len);
11981 if (!joined_path)
11982 goto error;
11983
11984 if (!path->narrow || !PyBytes_Check(path->object)) {
11985 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
11986 entry->path = PyUnicode_DecodeFSDefault(joined_path);
11987 }
11988 else {
11989 entry->name = PyBytes_FromStringAndSize(name, name_len);
11990 entry->path = PyBytes_FromString(joined_path);
11991 }
11992 PyMem_Free(joined_path);
11993 if (!entry->name || !entry->path)
11994 goto error;
11995
Victor Stinner35a97c02015-03-08 02:59:09 +010011996#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010011997 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010011998#endif
Victor Stinner6036e442015-03-08 01:58:04 +010011999 entry->d_ino = d_ino;
12000
12001 return (PyObject *)entry;
12002
12003error:
12004 Py_XDECREF(entry);
12005 return NULL;
12006}
12007
12008#endif
12009
12010
12011typedef struct {
12012 PyObject_HEAD
12013 path_t path;
12014#ifdef MS_WINDOWS
12015 HANDLE handle;
12016 WIN32_FIND_DATAW file_data;
12017 int first_time;
12018#else /* POSIX */
12019 DIR *dirp;
12020#endif
12021} ScandirIterator;
12022
12023#ifdef MS_WINDOWS
12024
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012025static int
12026ScandirIterator_is_closed(ScandirIterator *iterator)
12027{
12028 return iterator->handle == INVALID_HANDLE_VALUE;
12029}
12030
Victor Stinner6036e442015-03-08 01:58:04 +010012031static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012032ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012033{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012034 HANDLE handle = iterator->handle;
12035
12036 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012037 return;
12038
Victor Stinner6036e442015-03-08 01:58:04 +010012039 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012040 Py_BEGIN_ALLOW_THREADS
12041 FindClose(handle);
12042 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012043}
12044
12045static PyObject *
12046ScandirIterator_iternext(ScandirIterator *iterator)
12047{
12048 WIN32_FIND_DATAW *file_data = &iterator->file_data;
12049 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012050 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012051
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012052 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012053 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010012054 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012055
12056 while (1) {
12057 if (!iterator->first_time) {
12058 Py_BEGIN_ALLOW_THREADS
12059 success = FindNextFileW(iterator->handle, file_data);
12060 Py_END_ALLOW_THREADS
12061 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012062 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012063 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012064 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012065 break;
12066 }
12067 }
12068 iterator->first_time = 0;
12069
12070 /* Skip over . and .. */
12071 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012072 wcscmp(file_data->cFileName, L"..") != 0) {
12073 entry = DirEntry_from_find_data(&iterator->path, file_data);
12074 if (!entry)
12075 break;
12076 return entry;
12077 }
Victor Stinner6036e442015-03-08 01:58:04 +010012078
12079 /* Loop till we get a non-dot directory or finish iterating */
12080 }
12081
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012082 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012083 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012084 return NULL;
12085}
12086
12087#else /* POSIX */
12088
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012089static int
12090ScandirIterator_is_closed(ScandirIterator *iterator)
12091{
12092 return !iterator->dirp;
12093}
12094
Victor Stinner6036e442015-03-08 01:58:04 +010012095static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012096ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010012097{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012098 DIR *dirp = iterator->dirp;
12099
12100 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012101 return;
12102
Victor Stinner6036e442015-03-08 01:58:04 +010012103 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030012104 Py_BEGIN_ALLOW_THREADS
12105 closedir(dirp);
12106 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010012107 return;
12108}
12109
12110static PyObject *
12111ScandirIterator_iternext(ScandirIterator *iterator)
12112{
12113 struct dirent *direntp;
12114 Py_ssize_t name_len;
12115 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012116 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012117
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012118 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012119 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010012120 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010012121
12122 while (1) {
12123 errno = 0;
12124 Py_BEGIN_ALLOW_THREADS
12125 direntp = readdir(iterator->dirp);
12126 Py_END_ALLOW_THREADS
12127
12128 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012129 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010012130 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012131 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012132 break;
12133 }
12134
12135 /* Skip over . and .. */
12136 name_len = NAMLEN(direntp);
12137 is_dot = direntp->d_name[0] == '.' &&
12138 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
12139 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012140 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010012141 name_len, direntp->d_ino
12142#ifdef HAVE_DIRENT_D_TYPE
12143 , direntp->d_type
12144#endif
12145 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012146 if (!entry)
12147 break;
12148 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010012149 }
12150
12151 /* Loop till we get a non-dot directory or finish iterating */
12152 }
12153
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020012154 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012155 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010012156 return NULL;
12157}
12158
12159#endif
12160
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012161static PyObject *
12162ScandirIterator_close(ScandirIterator *self, PyObject *args)
12163{
12164 ScandirIterator_closedir(self);
12165 Py_RETURN_NONE;
12166}
12167
12168static PyObject *
12169ScandirIterator_enter(PyObject *self, PyObject *args)
12170{
12171 Py_INCREF(self);
12172 return self;
12173}
12174
12175static PyObject *
12176ScandirIterator_exit(ScandirIterator *self, PyObject *args)
12177{
12178 ScandirIterator_closedir(self);
12179 Py_RETURN_NONE;
12180}
12181
Victor Stinner6036e442015-03-08 01:58:04 +010012182static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010012183ScandirIterator_finalize(ScandirIterator *iterator)
12184{
12185 PyObject *error_type, *error_value, *error_traceback;
12186
12187 /* Save the current exception, if any. */
12188 PyErr_Fetch(&error_type, &error_value, &error_traceback);
12189
12190 if (!ScandirIterator_is_closed(iterator)) {
12191 ScandirIterator_closedir(iterator);
12192
12193 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
12194 "unclosed scandir iterator %R", iterator)) {
12195 /* Spurious errors can appear at shutdown */
12196 if (PyErr_ExceptionMatches(PyExc_Warning)) {
12197 PyErr_WriteUnraisable((PyObject *) iterator);
12198 }
12199 }
12200 }
12201
12202 Py_CLEAR(iterator->path.object);
12203 path_cleanup(&iterator->path);
12204
12205 /* Restore the saved exception. */
12206 PyErr_Restore(error_type, error_value, error_traceback);
12207}
12208
12209static void
Victor Stinner6036e442015-03-08 01:58:04 +010012210ScandirIterator_dealloc(ScandirIterator *iterator)
12211{
Victor Stinner7bfa4092016-03-23 00:43:54 +010012212 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
12213 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012214
Victor Stinner6036e442015-03-08 01:58:04 +010012215 Py_TYPE(iterator)->tp_free((PyObject *)iterator);
12216}
12217
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012218static PyMethodDef ScandirIterator_methods[] = {
12219 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
12220 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
12221 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
12222 {NULL}
12223};
12224
Benjamin Peterson5646de42015-04-12 17:56:34 -040012225static PyTypeObject ScandirIteratorType = {
Victor Stinner6036e442015-03-08 01:58:04 +010012226 PyVarObject_HEAD_INIT(NULL, 0)
12227 MODNAME ".ScandirIterator", /* tp_name */
12228 sizeof(ScandirIterator), /* tp_basicsize */
12229 0, /* tp_itemsize */
12230 /* methods */
12231 (destructor)ScandirIterator_dealloc, /* tp_dealloc */
12232 0, /* tp_print */
12233 0, /* tp_getattr */
12234 0, /* tp_setattr */
12235 0, /* tp_compare */
12236 0, /* tp_repr */
12237 0, /* tp_as_number */
12238 0, /* tp_as_sequence */
12239 0, /* tp_as_mapping */
12240 0, /* tp_hash */
12241 0, /* tp_call */
12242 0, /* tp_str */
12243 0, /* tp_getattro */
12244 0, /* tp_setattro */
12245 0, /* tp_as_buffer */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012246 Py_TPFLAGS_DEFAULT
12247 | Py_TPFLAGS_HAVE_FINALIZE, /* tp_flags */
Victor Stinner6036e442015-03-08 01:58:04 +010012248 0, /* tp_doc */
12249 0, /* tp_traverse */
12250 0, /* tp_clear */
12251 0, /* tp_richcompare */
12252 0, /* tp_weaklistoffset */
12253 PyObject_SelfIter, /* tp_iter */
12254 (iternextfunc)ScandirIterator_iternext, /* tp_iternext */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020012255 ScandirIterator_methods, /* tp_methods */
Victor Stinner7bfa4092016-03-23 00:43:54 +010012256 0, /* tp_members */
12257 0, /* tp_getset */
12258 0, /* tp_base */
12259 0, /* tp_dict */
12260 0, /* tp_descr_get */
12261 0, /* tp_descr_set */
12262 0, /* tp_dictoffset */
12263 0, /* tp_init */
12264 0, /* tp_alloc */
12265 0, /* tp_new */
12266 0, /* tp_free */
12267 0, /* tp_is_gc */
12268 0, /* tp_bases */
12269 0, /* tp_mro */
12270 0, /* tp_cache */
12271 0, /* tp_subclasses */
12272 0, /* tp_weaklist */
12273 0, /* tp_del */
12274 0, /* tp_version_tag */
12275 (destructor)ScandirIterator_finalize, /* tp_finalize */
Victor Stinner6036e442015-03-08 01:58:04 +010012276};
12277
12278static PyObject *
12279posix_scandir(PyObject *self, PyObject *args, PyObject *kwargs)
12280{
12281 ScandirIterator *iterator;
12282 static char *keywords[] = {"path", NULL};
12283#ifdef MS_WINDOWS
12284 wchar_t *path_strW;
12285#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012286 const char *path;
Victor Stinner6036e442015-03-08 01:58:04 +010012287#endif
12288
12289 iterator = PyObject_New(ScandirIterator, &ScandirIteratorType);
12290 if (!iterator)
12291 return NULL;
12292 memset(&iterator->path, 0, sizeof(path_t));
12293 iterator->path.function_name = "scandir";
12294 iterator->path.nullable = 1;
12295
12296#ifdef MS_WINDOWS
12297 iterator->handle = INVALID_HANDLE_VALUE;
12298#else
12299 iterator->dirp = NULL;
12300#endif
12301
12302 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:scandir", keywords,
12303 path_converter, &iterator->path))
12304 goto error;
12305
12306 /* path_converter doesn't keep path.object around, so do it
12307 manually for the lifetime of the iterator here (the refcount
12308 is decremented in ScandirIterator_dealloc)
12309 */
12310 Py_XINCREF(iterator->path.object);
12311
12312#ifdef MS_WINDOWS
12313 if (iterator->path.narrow) {
12314 PyErr_SetString(PyExc_TypeError,
12315 "os.scandir() doesn't support bytes path on Windows, use Unicode instead");
12316 goto error;
12317 }
12318 iterator->first_time = 1;
12319
12320 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
12321 if (!path_strW)
12322 goto error;
12323
12324 Py_BEGIN_ALLOW_THREADS
12325 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
12326 Py_END_ALLOW_THREADS
12327
12328 PyMem_Free(path_strW);
12329
12330 if (iterator->handle == INVALID_HANDLE_VALUE) {
12331 path_error(&iterator->path);
12332 goto error;
12333 }
12334#else /* POSIX */
12335 if (iterator->path.narrow)
12336 path = iterator->path.narrow;
12337 else
12338 path = ".";
12339
12340 errno = 0;
12341 Py_BEGIN_ALLOW_THREADS
12342 iterator->dirp = opendir(path);
12343 Py_END_ALLOW_THREADS
12344
12345 if (!iterator->dirp) {
12346 path_error(&iterator->path);
12347 goto error;
12348 }
12349#endif
12350
12351 return (PyObject *)iterator;
12352
12353error:
12354 Py_DECREF(iterator);
12355 return NULL;
12356}
12357
Ethan Furman410ef8e2016-06-04 12:06:26 -070012358/*
12359 Return the file system path representation of the object.
12360
12361 If the object is str or bytes, then allow it to pass through with
12362 an incremented refcount. If the object defines __fspath__(), then
12363 return the result of that method. All other types raise a TypeError.
12364*/
12365PyObject *
12366PyOS_FSPath(PyObject *path)
12367{
Brett Cannon3f9183b2016-08-26 14:44:48 -070012368 /* For error message reasons, this function is manually inlined in
12369 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070012370 _Py_IDENTIFIER(__fspath__);
12371 PyObject *func = NULL;
12372 PyObject *path_repr = NULL;
12373
12374 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
12375 Py_INCREF(path);
12376 return path;
12377 }
12378
12379 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
12380 if (NULL == func) {
12381 return PyErr_Format(PyExc_TypeError,
12382 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012383 "not %.200s",
12384 Py_TYPE(path)->tp_name);
Ethan Furman410ef8e2016-06-04 12:06:26 -070012385 }
12386
12387 path_repr = PyObject_CallFunctionObjArgs(func, NULL);
12388 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070012389 if (NULL == path_repr) {
12390 return NULL;
12391 }
12392
Brett Cannonc78ca1e2016-06-24 12:03:43 -070012393 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
12394 PyErr_Format(PyExc_TypeError,
12395 "expected %.200s.__fspath__() to return str or bytes, "
12396 "not %.200s", Py_TYPE(path)->tp_name,
12397 Py_TYPE(path_repr)->tp_name);
12398 Py_DECREF(path_repr);
12399 return NULL;
12400 }
12401
Ethan Furman410ef8e2016-06-04 12:06:26 -070012402 return path_repr;
12403}
12404
12405/*[clinic input]
12406os.fspath
12407
12408 path: object
12409
12410Return the file system path representation of the object.
12411
Brett Cannonb4f43e92016-06-09 14:32:08 -070012412If the object is str or bytes, then allow it to pass through as-is. If the
12413object defines __fspath__(), then return the result of that method. All other
12414types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070012415[clinic start generated code]*/
12416
12417static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012418os_fspath_impl(PyObject *module, PyObject *path)
12419/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070012420{
12421 return PyOS_FSPath(path);
12422}
Victor Stinner6036e442015-03-08 01:58:04 +010012423
Serhiy Storchakaa4c6bad2015-04-04 23:35:52 +030012424#include "clinic/posixmodule.c.h"
12425
Larry Hastings7726ac92014-01-31 22:03:12 -080012426/*[clinic input]
12427dump buffer
12428[clinic start generated code]*/
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030012429/*[clinic end generated code: output=da39a3ee5e6b4b0d input=524ce2e021e4eba6]*/
Larry Hastings7726ac92014-01-31 22:03:12 -080012430
Larry Hastings31826802013-10-19 00:09:25 -070012431
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012432static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070012433
12434 OS_STAT_METHODDEF
12435 OS_ACCESS_METHODDEF
12436 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012437 OS_CHDIR_METHODDEF
12438 OS_CHFLAGS_METHODDEF
12439 OS_CHMOD_METHODDEF
12440 OS_FCHMOD_METHODDEF
12441 OS_LCHMOD_METHODDEF
12442 OS_CHOWN_METHODDEF
12443 OS_FCHOWN_METHODDEF
12444 OS_LCHOWN_METHODDEF
12445 OS_LCHFLAGS_METHODDEF
12446 OS_CHROOT_METHODDEF
12447 OS_CTERMID_METHODDEF
12448 OS_GETCWD_METHODDEF
12449 OS_GETCWDB_METHODDEF
12450 OS_LINK_METHODDEF
12451 OS_LISTDIR_METHODDEF
12452 OS_LSTAT_METHODDEF
12453 OS_MKDIR_METHODDEF
12454 OS_NICE_METHODDEF
12455 OS_GETPRIORITY_METHODDEF
12456 OS_SETPRIORITY_METHODDEF
Guido van Rossumb6775db1994-08-01 11:34:53 +000012457#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070012458 {"readlink", (PyCFunction)posix_readlink,
12459 METH_VARARGS | METH_KEYWORDS,
12460 readlink__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000012461#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000012462#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070012463 {"readlink", (PyCFunction)win_readlink,
12464 METH_VARARGS | METH_KEYWORDS,
12465 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000012466#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100012467 OS_RENAME_METHODDEF
12468 OS_REPLACE_METHODDEF
12469 OS_RMDIR_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012470 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings2f936352014-08-05 14:04:04 +100012471 OS_SYMLINK_METHODDEF
12472 OS_SYSTEM_METHODDEF
12473 OS_UMASK_METHODDEF
12474 OS_UNAME_METHODDEF
12475 OS_UNLINK_METHODDEF
12476 OS_REMOVE_METHODDEF
12477 OS_UTIME_METHODDEF
12478 OS_TIMES_METHODDEF
12479 OS__EXIT_METHODDEF
12480 OS_EXECV_METHODDEF
12481 OS_EXECVE_METHODDEF
12482 OS_SPAWNV_METHODDEF
12483 OS_SPAWNVE_METHODDEF
12484 OS_FORK1_METHODDEF
12485 OS_FORK_METHODDEF
12486 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
12487 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
12488 OS_SCHED_GETPARAM_METHODDEF
12489 OS_SCHED_GETSCHEDULER_METHODDEF
12490 OS_SCHED_RR_GET_INTERVAL_METHODDEF
12491 OS_SCHED_SETPARAM_METHODDEF
12492 OS_SCHED_SETSCHEDULER_METHODDEF
12493 OS_SCHED_YIELD_METHODDEF
12494 OS_SCHED_SETAFFINITY_METHODDEF
12495 OS_SCHED_GETAFFINITY_METHODDEF
12496 OS_OPENPTY_METHODDEF
12497 OS_FORKPTY_METHODDEF
12498 OS_GETEGID_METHODDEF
12499 OS_GETEUID_METHODDEF
12500 OS_GETGID_METHODDEF
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020012501#ifdef HAVE_GETGROUPLIST
12502 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
12503#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012504 OS_GETGROUPS_METHODDEF
12505 OS_GETPID_METHODDEF
12506 OS_GETPGRP_METHODDEF
12507 OS_GETPPID_METHODDEF
12508 OS_GETUID_METHODDEF
12509 OS_GETLOGIN_METHODDEF
12510 OS_KILL_METHODDEF
12511 OS_KILLPG_METHODDEF
12512 OS_PLOCK_METHODDEF
Thomas Heller8b7a9572007-08-31 06:44:36 +000012513#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000012514 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000012515#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012516 OS_SETUID_METHODDEF
12517 OS_SETEUID_METHODDEF
12518 OS_SETREUID_METHODDEF
12519 OS_SETGID_METHODDEF
12520 OS_SETEGID_METHODDEF
12521 OS_SETREGID_METHODDEF
12522 OS_SETGROUPS_METHODDEF
Antoine Pitroub7572f02009-12-02 20:46:48 +000012523#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000012524 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000012525#endif /* HAVE_INITGROUPS */
Larry Hastings2f936352014-08-05 14:04:04 +100012526 OS_GETPGID_METHODDEF
12527 OS_SETPGRP_METHODDEF
12528 OS_WAIT_METHODDEF
12529 OS_WAIT3_METHODDEF
12530 OS_WAIT4_METHODDEF
12531 OS_WAITID_METHODDEF
12532 OS_WAITPID_METHODDEF
12533 OS_GETSID_METHODDEF
12534 OS_SETSID_METHODDEF
12535 OS_SETPGID_METHODDEF
12536 OS_TCGETPGRP_METHODDEF
12537 OS_TCSETPGRP_METHODDEF
12538 OS_OPEN_METHODDEF
12539 OS_CLOSE_METHODDEF
12540 OS_CLOSERANGE_METHODDEF
12541 OS_DEVICE_ENCODING_METHODDEF
12542 OS_DUP_METHODDEF
12543 OS_DUP2_METHODDEF
12544 OS_LOCKF_METHODDEF
12545 OS_LSEEK_METHODDEF
12546 OS_READ_METHODDEF
12547 OS_READV_METHODDEF
12548 OS_PREAD_METHODDEF
12549 OS_WRITE_METHODDEF
12550 OS_WRITEV_METHODDEF
12551 OS_PWRITE_METHODDEF
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012552#ifdef HAVE_SENDFILE
12553 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
12554 posix_sendfile__doc__},
12555#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012556 OS_FSTAT_METHODDEF
12557 OS_ISATTY_METHODDEF
12558 OS_PIPE_METHODDEF
12559 OS_PIPE2_METHODDEF
12560 OS_MKFIFO_METHODDEF
12561 OS_MKNOD_METHODDEF
12562 OS_MAJOR_METHODDEF
12563 OS_MINOR_METHODDEF
12564 OS_MAKEDEV_METHODDEF
12565 OS_FTRUNCATE_METHODDEF
12566 OS_TRUNCATE_METHODDEF
12567 OS_POSIX_FALLOCATE_METHODDEF
12568 OS_POSIX_FADVISE_METHODDEF
12569 OS_PUTENV_METHODDEF
12570 OS_UNSETENV_METHODDEF
12571 OS_STRERROR_METHODDEF
12572 OS_FCHDIR_METHODDEF
12573 OS_FSYNC_METHODDEF
12574 OS_SYNC_METHODDEF
12575 OS_FDATASYNC_METHODDEF
12576 OS_WCOREDUMP_METHODDEF
12577 OS_WIFCONTINUED_METHODDEF
12578 OS_WIFSTOPPED_METHODDEF
12579 OS_WIFSIGNALED_METHODDEF
12580 OS_WIFEXITED_METHODDEF
12581 OS_WEXITSTATUS_METHODDEF
12582 OS_WTERMSIG_METHODDEF
12583 OS_WSTOPSIG_METHODDEF
12584 OS_FSTATVFS_METHODDEF
12585 OS_STATVFS_METHODDEF
12586 OS_CONFSTR_METHODDEF
12587 OS_SYSCONF_METHODDEF
12588 OS_FPATHCONF_METHODDEF
12589 OS_PATHCONF_METHODDEF
12590 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030012591 OS__GETFULLPATHNAME_METHODDEF
12592 OS__ISDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100012593 OS__GETDISKUSAGE_METHODDEF
12594 OS__GETFINALPATHNAME_METHODDEF
12595 OS__GETVOLUMEPATHNAME_METHODDEF
12596 OS_GETLOADAVG_METHODDEF
12597 OS_URANDOM_METHODDEF
12598 OS_SETRESUID_METHODDEF
12599 OS_SETRESGID_METHODDEF
12600 OS_GETRESUID_METHODDEF
12601 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012602
Larry Hastings2f936352014-08-05 14:04:04 +100012603 OS_GETXATTR_METHODDEF
12604 OS_SETXATTR_METHODDEF
12605 OS_REMOVEXATTR_METHODDEF
12606 OS_LISTXATTR_METHODDEF
12607
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012608#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
12609 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
12610#endif
Larry Hastings2f936352014-08-05 14:04:04 +100012611 OS_CPU_COUNT_METHODDEF
12612 OS_GET_INHERITABLE_METHODDEF
12613 OS_SET_INHERITABLE_METHODDEF
12614 OS_GET_HANDLE_INHERITABLE_METHODDEF
12615 OS_SET_HANDLE_INHERITABLE_METHODDEF
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012616#ifndef MS_WINDOWS
12617 {"get_blocking", posix_get_blocking, METH_VARARGS, get_blocking__doc__},
12618 {"set_blocking", posix_set_blocking, METH_VARARGS, set_blocking__doc__},
12619#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012620 {"scandir", (PyCFunction)posix_scandir,
12621 METH_VARARGS | METH_KEYWORDS,
12622 posix_scandir__doc__},
Ethan Furman410ef8e2016-06-04 12:06:26 -070012623 OS_FSPATH_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000012624 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012625};
12626
12627
Brian Curtin52173d42010-12-02 18:29:18 +000012628#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000012629static int
Brian Curtin52173d42010-12-02 18:29:18 +000012630enable_symlink()
12631{
12632 HANDLE tok;
12633 TOKEN_PRIVILEGES tok_priv;
12634 LUID luid;
Brian Curtin52173d42010-12-02 18:29:18 +000012635
12636 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012637 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012638
12639 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012640 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012641
12642 tok_priv.PrivilegeCount = 1;
12643 tok_priv.Privileges[0].Luid = luid;
12644 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
12645
12646 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
12647 sizeof(TOKEN_PRIVILEGES),
12648 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000012649 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000012650
Brian Curtin3b4499c2010-12-28 14:31:47 +000012651 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
12652 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000012653}
12654#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
12655
Barry Warsaw4a342091996-12-19 23:50:02 +000012656static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012657all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000012658{
Guido van Rossum94f6f721999-01-06 18:42:14 +000012659#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012660 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012661#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012662#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012663 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012664#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012665#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012666 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012667#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000012668#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012669 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012670#endif
Fred Drakec9680921999-12-13 16:37:25 +000012671#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012672 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000012673#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012674#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012675 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012676#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012677#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012678 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012679#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012680#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012681 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012682#endif
Fred Drake106c1a02002-04-23 15:58:02 +000012683#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012684 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000012685#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000012686#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012687 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012688#endif
12689#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012690 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012691#endif
12692#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012693 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012694#endif
12695#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012696 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012697#endif
12698#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012699 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012700#endif
12701#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012702 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012703#endif
12704#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012705 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012706#endif
12707#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012708 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012709#endif
12710#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012711 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012712#endif
12713#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012714 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012715#endif
12716#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012717 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012718#endif
12719#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012720 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012721#endif
12722#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012723 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000012724#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000012725#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012726 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012727#endif
12728#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012729 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000012730#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012731#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012732 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012733#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012734#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012735 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012736#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012737#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000012738#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012739 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012740#endif
12741#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012742 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000012743#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020012744#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012745#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012746 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012747#endif
12748#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012749 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012750#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012751#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012752 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050012753#endif
Jesus Ceacf381202012-04-24 20:44:40 +020012754#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012755 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020012756#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020012757#ifdef O_TMPFILE
12758 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
12759#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012760#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012761 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012762#endif
12763#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012764 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012765#endif
12766#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012767 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012768#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020012769#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012770 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020012771#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012772#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012773 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012774#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000012775
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012776
Jesus Cea94363612012-06-22 18:32:07 +020012777#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012778 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012779#endif
12780#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012781 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020012782#endif
12783
Tim Peters5aa91602002-01-30 05:46:57 +000012784/* MS Windows */
12785#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012786 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012787 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012788#endif
12789#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000012790 /* Optimize for short life (keep in memory). */
12791 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012792 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012793#endif
12794#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000012795 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012796 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012797#endif
12798#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000012799 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012800 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012801#endif
12802#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000012803 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012804 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000012805#endif
12806
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012807/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012808#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000012809 /* Send a SIGIO signal whenever input or output
12810 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012811 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000012812#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012813#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012814 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012815 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012816#endif
12817#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000012818 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012819 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012820#endif
12821#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000012822 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012823 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000012824#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020012825#ifdef O_NOLINKS
12826 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012827 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020012828#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012829#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012830 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012831 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000012832#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000012833
Victor Stinner8c62be82010-05-06 00:08:46 +000012834 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012835#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012836 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012837#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012838#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012839 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012840#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012841#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012842 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012843#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012844#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012845 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012846#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012847#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012848 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012849#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012850#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012851 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012852#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012853#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012854 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012855#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012856#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012857 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012858#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012859#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012860 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012861#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012862#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012863 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012864#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012865#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012866 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012867#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012868#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012869 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012870#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012871#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012872 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012873#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012874#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012875 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012876#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012877#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012878 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012879#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012880#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012881 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012882#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012883#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012884 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000012885#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000012886
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000012887 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012888#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012889 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012890#endif /* ST_RDONLY */
12891#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012892 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000012893#endif /* ST_NOSUID */
12894
doko@ubuntu.comca616a22013-12-08 15:23:07 +010012895 /* GNU extensions */
12896#ifdef ST_NODEV
12897 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
12898#endif /* ST_NODEV */
12899#ifdef ST_NOEXEC
12900 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
12901#endif /* ST_NOEXEC */
12902#ifdef ST_SYNCHRONOUS
12903 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
12904#endif /* ST_SYNCHRONOUS */
12905#ifdef ST_MANDLOCK
12906 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
12907#endif /* ST_MANDLOCK */
12908#ifdef ST_WRITE
12909 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
12910#endif /* ST_WRITE */
12911#ifdef ST_APPEND
12912 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
12913#endif /* ST_APPEND */
12914#ifdef ST_NOATIME
12915 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
12916#endif /* ST_NOATIME */
12917#ifdef ST_NODIRATIME
12918 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
12919#endif /* ST_NODIRATIME */
12920#ifdef ST_RELATIME
12921 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
12922#endif /* ST_RELATIME */
12923
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012924 /* FreeBSD sendfile() constants */
12925#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012926 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012927#endif
12928#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012929 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012930#endif
12931#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012932 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000012933#endif
12934
Ross Lagerwall7807c352011-03-17 20:20:30 +020012935 /* constants for posix_fadvise */
12936#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012937 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012938#endif
12939#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012940 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012941#endif
12942#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012943 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012944#endif
12945#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012946 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012947#endif
12948#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012949 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012950#endif
12951#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012952 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012953#endif
12954
12955 /* constants for waitid */
12956#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012957 if (PyModule_AddIntMacro(m, P_PID)) return -1;
12958 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
12959 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012960#endif
12961#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012962 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012963#endif
12964#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012965 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012966#endif
12967#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012968 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012969#endif
12970#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012971 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012972#endif
12973#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012974 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012975#endif
12976#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012977 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012978#endif
12979#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012980 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012981#endif
12982
12983 /* constants for lockf */
12984#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012985 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012986#endif
12987#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012988 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012989#endif
12990#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012991 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012992#endif
12993#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012994 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020012995#endif
12996
Guido van Rossum246bc171999-02-01 23:54:31 +000012997#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020012998 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
12999 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
13000 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
13001 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
13002 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000013003#endif
13004
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013005#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013006#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013007 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013008#endif
13009#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013010 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013011#endif
13012#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013013 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070013014#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013015#ifdef SCHED_SPORADIC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013016 if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013017#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013018#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013019 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013020#endif
13021#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013022 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013023#endif
13024#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013025 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013026#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013027#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013028 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013029#endif
13030#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013031 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013032#endif
13033#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013034 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013035#endif
13036#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013037 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020013038#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013039#endif
13040
Benjamin Peterson9428d532011-09-14 11:45:52 -040013041#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013042 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
13043 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
13044 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040013045#endif
13046
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013047#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013048 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013049#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013050#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013051 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013052#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013053#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013054 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013055#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013056#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013057 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013058#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013059#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013060 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013061#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013062#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013063 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013064#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030013065#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020013066 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020013067#endif
13068
Victor Stinner8c62be82010-05-06 00:08:46 +000013069 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000013070}
13071
13072
Martin v. Löwis1a214512008-06-11 05:26:20 +000013073static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000013074 PyModuleDef_HEAD_INIT,
13075 MODNAME,
13076 posix__doc__,
13077 -1,
13078 posix_methods,
13079 NULL,
13080 NULL,
13081 NULL,
13082 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000013083};
13084
13085
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013086static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070013087
13088#ifdef HAVE_FACCESSAT
13089 "HAVE_FACCESSAT",
13090#endif
13091
13092#ifdef HAVE_FCHDIR
13093 "HAVE_FCHDIR",
13094#endif
13095
13096#ifdef HAVE_FCHMOD
13097 "HAVE_FCHMOD",
13098#endif
13099
13100#ifdef HAVE_FCHMODAT
13101 "HAVE_FCHMODAT",
13102#endif
13103
13104#ifdef HAVE_FCHOWN
13105 "HAVE_FCHOWN",
13106#endif
13107
Larry Hastings00964ed2013-08-12 13:49:30 -040013108#ifdef HAVE_FCHOWNAT
13109 "HAVE_FCHOWNAT",
13110#endif
13111
Larry Hastings9cf065c2012-06-22 16:30:09 -070013112#ifdef HAVE_FEXECVE
13113 "HAVE_FEXECVE",
13114#endif
13115
13116#ifdef HAVE_FDOPENDIR
13117 "HAVE_FDOPENDIR",
13118#endif
13119
Georg Brandl306336b2012-06-24 12:55:33 +020013120#ifdef HAVE_FPATHCONF
13121 "HAVE_FPATHCONF",
13122#endif
13123
Larry Hastings9cf065c2012-06-22 16:30:09 -070013124#ifdef HAVE_FSTATAT
13125 "HAVE_FSTATAT",
13126#endif
13127
13128#ifdef HAVE_FSTATVFS
13129 "HAVE_FSTATVFS",
13130#endif
13131
Steve Dowerfe0a41a2015-03-20 19:50:46 -070013132#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020013133 "HAVE_FTRUNCATE",
13134#endif
13135
Larry Hastings9cf065c2012-06-22 16:30:09 -070013136#ifdef HAVE_FUTIMENS
13137 "HAVE_FUTIMENS",
13138#endif
13139
13140#ifdef HAVE_FUTIMES
13141 "HAVE_FUTIMES",
13142#endif
13143
13144#ifdef HAVE_FUTIMESAT
13145 "HAVE_FUTIMESAT",
13146#endif
13147
13148#ifdef HAVE_LINKAT
13149 "HAVE_LINKAT",
13150#endif
13151
13152#ifdef HAVE_LCHFLAGS
13153 "HAVE_LCHFLAGS",
13154#endif
13155
13156#ifdef HAVE_LCHMOD
13157 "HAVE_LCHMOD",
13158#endif
13159
13160#ifdef HAVE_LCHOWN
13161 "HAVE_LCHOWN",
13162#endif
13163
13164#ifdef HAVE_LSTAT
13165 "HAVE_LSTAT",
13166#endif
13167
13168#ifdef HAVE_LUTIMES
13169 "HAVE_LUTIMES",
13170#endif
13171
13172#ifdef HAVE_MKDIRAT
13173 "HAVE_MKDIRAT",
13174#endif
13175
13176#ifdef HAVE_MKFIFOAT
13177 "HAVE_MKFIFOAT",
13178#endif
13179
13180#ifdef HAVE_MKNODAT
13181 "HAVE_MKNODAT",
13182#endif
13183
13184#ifdef HAVE_OPENAT
13185 "HAVE_OPENAT",
13186#endif
13187
13188#ifdef HAVE_READLINKAT
13189 "HAVE_READLINKAT",
13190#endif
13191
13192#ifdef HAVE_RENAMEAT
13193 "HAVE_RENAMEAT",
13194#endif
13195
13196#ifdef HAVE_SYMLINKAT
13197 "HAVE_SYMLINKAT",
13198#endif
13199
13200#ifdef HAVE_UNLINKAT
13201 "HAVE_UNLINKAT",
13202#endif
13203
13204#ifdef HAVE_UTIMENSAT
13205 "HAVE_UTIMENSAT",
13206#endif
13207
13208#ifdef MS_WINDOWS
13209 "MS_WINDOWS",
13210#endif
13211
13212 NULL
13213};
13214
13215
Mark Hammondfe51c6d2002-08-02 02:27:13 +000013216PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000013217INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000013218{
Victor Stinner8c62be82010-05-06 00:08:46 +000013219 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070013220 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020013221 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000013222
Brian Curtin52173d42010-12-02 18:29:18 +000013223#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000013224 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000013225#endif
13226
Victor Stinner8c62be82010-05-06 00:08:46 +000013227 m = PyModule_Create(&posixmodule);
13228 if (m == NULL)
13229 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000013230
Victor Stinner8c62be82010-05-06 00:08:46 +000013231 /* Initialize environ dictionary */
13232 v = convertenviron();
13233 Py_XINCREF(v);
13234 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
13235 return NULL;
13236 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000013237
Victor Stinner8c62be82010-05-06 00:08:46 +000013238 if (all_ins(m))
13239 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000013240
Victor Stinner8c62be82010-05-06 00:08:46 +000013241 if (setup_confname_tables(m))
13242 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000013243
Victor Stinner8c62be82010-05-06 00:08:46 +000013244 Py_INCREF(PyExc_OSError);
13245 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000013246
Guido van Rossumb3d39562000-01-31 18:41:26 +000013247#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000013248 if (posix_putenv_garbage == NULL)
13249 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000013250#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000013251
Victor Stinner8c62be82010-05-06 00:08:46 +000013252 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020013253#if defined(HAVE_WAITID) && !defined(__APPLE__)
13254 waitid_result_desc.name = MODNAME ".waitid_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013255 if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
13256 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +020013257#endif
13258
Christian Heimes25827622013-10-12 01:27:08 +020013259 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
Victor Stinner8c62be82010-05-06 00:08:46 +000013260 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
13261 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
13262 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
Victor Stinner1c8f0592013-07-22 22:24:54 +020013263 if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
13264 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013265 structseq_new = StatResultType.tp_new;
13266 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013267
Christian Heimes25827622013-10-12 01:27:08 +020013268 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013269 if (PyStructSequence_InitType2(&StatVFSResultType,
13270 &statvfs_result_desc) < 0)
13271 return NULL;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013272#ifdef NEED_TICKS_PER_SECOND
13273# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000013274 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013275# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000013276 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013277# else
Victor Stinner8c62be82010-05-06 00:08:46 +000013278 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000013279# endif
13280#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013281
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050013282#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013283 sched_param_desc.name = MODNAME ".sched_param";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013284 if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
13285 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100013286 SchedParamType.tp_new = os_sched_param;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013287#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013288
13289 /* initialize TerminalSize_info */
Victor Stinner1c8f0592013-07-22 22:24:54 +020013290 if (PyStructSequence_InitType2(&TerminalSizeType,
13291 &TerminalSize_desc) < 0)
13292 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013293
13294 /* initialize scandir types */
13295 if (PyType_Ready(&ScandirIteratorType) < 0)
13296 return NULL;
13297 if (PyType_Ready(&DirEntryType) < 0)
13298 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000013299 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020013300#if defined(HAVE_WAITID) && !defined(__APPLE__)
13301 Py_INCREF((PyObject*) &WaitidResultType);
13302 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
13303#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000013304 Py_INCREF((PyObject*) &StatResultType);
13305 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
13306 Py_INCREF((PyObject*) &StatVFSResultType);
13307 PyModule_AddObject(m, "statvfs_result",
13308 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013309
13310#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050013311 Py_INCREF(&SchedParamType);
13312 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050013313#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000013314
Larry Hastings605a62d2012-06-24 04:33:36 -070013315 times_result_desc.name = MODNAME ".times_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013316 if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
13317 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013318 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
13319
13320 uname_result_desc.name = MODNAME ".uname_result";
Victor Stinner1c8f0592013-07-22 22:24:54 +020013321 if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
13322 return NULL;
Larry Hastings605a62d2012-06-24 04:33:36 -070013323 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
13324
Thomas Wouters477c8d52006-05-27 19:21:47 +000013325#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000013326 /*
13327 * Step 2 of weak-linking support on Mac OS X.
13328 *
13329 * The code below removes functions that are not available on the
13330 * currently active platform.
13331 *
13332 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070013333 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000013334 * OSX 10.4.
13335 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000013336#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013337 if (fstatvfs == NULL) {
13338 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
13339 return NULL;
13340 }
13341 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013342#endif /* HAVE_FSTATVFS */
13343
13344#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000013345 if (statvfs == NULL) {
13346 if (PyObject_DelAttrString(m, "statvfs") == -1) {
13347 return NULL;
13348 }
13349 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013350#endif /* HAVE_STATVFS */
13351
13352# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000013353 if (lchown == NULL) {
13354 if (PyObject_DelAttrString(m, "lchown") == -1) {
13355 return NULL;
13356 }
13357 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000013358#endif /* HAVE_LCHOWN */
13359
13360
13361#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013362
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020013363 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013364 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
13365
Larry Hastings6fe20b32012-04-19 15:07:49 -070013366 billion = PyLong_FromLong(1000000000);
13367 if (!billion)
13368 return NULL;
13369
Larry Hastings9cf065c2012-06-22 16:30:09 -070013370 /* suppress "function not used" warnings */
13371 {
13372 int ignored;
13373 fd_specified("", -1);
13374 follow_symlinks_specified("", 1);
13375 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
13376 dir_fd_converter(Py_None, &ignored);
13377 dir_fd_unavailable(Py_None, &ignored);
13378 }
13379
13380 /*
13381 * provide list of locally available functions
13382 * so os.py can populate support_* lists
13383 */
13384 list = PyList_New(0);
13385 if (!list)
13386 return NULL;
13387 for (trace = have_functions; *trace; trace++) {
13388 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
13389 if (!unicode)
13390 return NULL;
13391 if (PyList_Append(list, unicode))
13392 return NULL;
13393 Py_DECREF(unicode);
13394 }
13395 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040013396
13397 Py_INCREF((PyObject *) &DirEntryType);
Brett Cannona32c4d02016-06-24 14:14:44 -070013398 PyModule_AddObject(m, "DirEntry", (PyObject *)&DirEntryType);
Larry Hastings9cf065c2012-06-22 16:30:09 -070013399
13400 initialized = 1;
13401
Victor Stinner8c62be82010-05-06 00:08:46 +000013402 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000013403}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000013404
13405#ifdef __cplusplus
13406}
13407#endif