blob: 90bbb137a5cd0f79b3f2c7614dab666b7debec02 [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
Jesus Ceaab70e2a2012-10-05 01:48:08 +02009 test macro, e.g. '__BORLANDC__' or '_MSC_VER'. */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010
Thomas Wouters477c8d52006-05-27 19:21:47 +000011#ifdef __APPLE__
12 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000013 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000014 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
15 * at the end of this file for more information.
16 */
17# pragma weak lchown
18# pragma weak statvfs
19# pragma weak fstatvfs
20
21#endif /* __APPLE__ */
22
Thomas Wouters68bc4f92006-03-01 01:05:10 +000023#define PY_SSIZE_T_CLEAN
24
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000025#include "Python.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020026#ifndef MS_WINDOWS
27#include "posixmodule.h"
28#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000029
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000030#if defined(__VMS)
Victor Stinnerb90db4c2011-04-26 22:48:24 +020031# error "PEP 11: VMS is now unsupported, code will be removed in Python 3.4"
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000032# include <unixio.h>
Martin v. Löwis79acb9e2002-12-06 12:48:53 +000033#endif /* defined(__VMS) */
34
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000035#ifdef __cplusplus
36extern "C" {
37#endif
38
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000039PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000040"This module provides access to operating system functionality that is\n\
41standardized by the C Standard and the POSIX standard (a thinly\n\
42disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000043corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000045
Ross Lagerwall4d076da2011-03-18 06:56:53 +020046#ifdef HAVE_SYS_UIO_H
47#include <sys/uio.h>
48#endif
49
Thomas Wouters0e3f5912006-08-11 14:57:12 +000050#ifdef HAVE_SYS_TYPES_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000051#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000052#endif /* HAVE_SYS_TYPES_H */
53
54#ifdef HAVE_SYS_STAT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +000055#include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000056#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000057
Guido van Rossum36bc6801995-06-14 22:54:23 +000058#ifdef HAVE_SYS_WAIT_H
Victor Stinner8c62be82010-05-06 00:08:46 +000059#include <sys/wait.h> /* For WNOHANG */
Guido van Rossum36bc6801995-06-14 22:54:23 +000060#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000061
Thomas Wouters0e3f5912006-08-11 14:57:12 +000062#ifdef HAVE_SIGNAL_H
Guido van Rossuma376cc51996-12-05 23:43:35 +000063#include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000064#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000065
Guido van Rossumb6775db1994-08-01 11:34:53 +000066#ifdef HAVE_FCNTL_H
67#include <fcntl.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000068#endif /* HAVE_FCNTL_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +000069
Guido van Rossuma6535fd2001-10-18 19:44:10 +000070#ifdef HAVE_GRP_H
71#include <grp.h>
72#endif
73
Barry Warsaw5676bd12003-01-07 20:57:09 +000074#ifdef HAVE_SYSEXITS_H
75#include <sysexits.h>
76#endif /* HAVE_SYSEXITS_H */
77
Anthony Baxter8a560de2004-10-13 15:30:56 +000078#ifdef HAVE_SYS_LOADAVG_H
79#include <sys/loadavg.h>
80#endif
81
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000082#ifdef HAVE_LANGINFO_H
83#include <langinfo.h>
84#endif
85
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000086#ifdef HAVE_SYS_SENDFILE_H
87#include <sys/sendfile.h>
88#endif
89
Benjamin Peterson94b580d2011-08-02 17:30:04 -050090#ifdef HAVE_SCHED_H
91#include <sched.h>
92#endif
93
Benjamin Peterson2dbda072012-03-16 10:12:55 -050094#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -050095#undef HAVE_SCHED_SETAFFINITY
96#endif
97
Benjamin Peterson9428d532011-09-14 11:45:52 -040098#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__)
99#define USE_XATTRS
100#endif
101
102#ifdef USE_XATTRS
Benjamin Petersonb77fe172011-09-13 17:20:47 -0400103#include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400104#endif
105
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000106#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
107#ifdef HAVE_SYS_SOCKET_H
108#include <sys/socket.h>
109#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000110#endif
111
Victor Stinner8b905bd2011-10-25 13:34:04 +0200112#ifdef HAVE_DLFCN_H
113#include <dlfcn.h>
114#endif
115
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200116#ifdef __hpux
117#include <sys/mpctl.h>
118#endif
119
120#if defined(__DragonFly__) || \
121 defined(__OpenBSD__) || \
122 defined(__FreeBSD__) || \
123 defined(__NetBSD__) || \
124 defined(__APPLE__)
125#include <sys/sysctl.h>
126#endif
127
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100128#if defined(MS_WINDOWS)
129# define TERMSIZE_USE_CONIO
130#elif defined(HAVE_SYS_IOCTL_H)
131# include <sys/ioctl.h>
132# if defined(HAVE_TERMIOS_H)
133# include <termios.h>
134# endif
135# if defined(TIOCGWINSZ)
136# define TERMSIZE_USE_IOCTL
137# endif
138#endif /* MS_WINDOWS */
139
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000140/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000141/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000142#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000143#define HAVE_GETCWD 1
144#define HAVE_OPENDIR 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000145#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000146#include <process.h>
147#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000148#ifdef __BORLANDC__ /* Borland compiler */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000149#define HAVE_EXECV 1
150#define HAVE_GETCWD 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000151#define HAVE_OPENDIR 1
152#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000153#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000154#define HAVE_WAIT 1
155#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000156#ifdef _MSC_VER /* Microsoft compiler */
Guido van Rossum8d665e61996-06-26 18:22:49 +0000157#define HAVE_GETCWD 1
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +0000158#define HAVE_GETPPID 1
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000159#define HAVE_GETLOGIN 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000160#define HAVE_SPAWNV 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000161#define HAVE_EXECV 1
162#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000163#define HAVE_SYSTEM 1
164#define HAVE_CWAIT 1
165#define HAVE_FSYNC 1
Tim Peters11b23062003-04-23 02:39:17 +0000166#define fsync _commit
Andrew MacIntyre6c73af22002-03-03 03:07:07 +0000167#else
Jesus Ceaab70e2a2012-10-05 01:48:08 +0200168#if defined(__VMS)
169/* Everything needed is defined in vms/pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000170#else /* all other compilers */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000171/* Unix functions that the configure script doesn't check for */
172#define HAVE_EXECV 1
173#define HAVE_FORK 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000174#if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
Guido van Rossum2242f2f2001-04-11 20:58:20 +0000175#define HAVE_FORK1 1
176#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177#define HAVE_GETCWD 1
178#define HAVE_GETEGID 1
179#define HAVE_GETEUID 1
180#define HAVE_GETGID 1
181#define HAVE_GETPPID 1
182#define HAVE_GETUID 1
183#define HAVE_KILL 1
184#define HAVE_OPENDIR 1
185#define HAVE_PIPE 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000186#define HAVE_SYSTEM 1
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000187#define HAVE_WAIT 1
Victor Stinner8c62be82010-05-06 00:08:46 +0000188#define HAVE_TTYNAME 1
Jesus Ceaab70e2a2012-10-05 01:48:08 +0200189#endif /* __VMS */
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000190#endif /* _MSC_VER */
191#endif /* __BORLANDC__ */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000192#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000193
Victor Stinnera2f7c002012-02-08 03:36:25 +0100194
195
196
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000197#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000198
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000199#if defined(__sgi)&&_COMPILER_VERSION>=700
200/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
201 (default) */
202extern char *ctermid_r(char *);
203#endif
204
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000205#ifndef HAVE_UNISTD_H
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000206#if defined(PYCC_VACPP)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000207extern int mkdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000208#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000209#if ( defined(__WATCOMC__) || defined(_MSC_VER) ) && !defined(__QNX__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000210extern int mkdir(const char *);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000211#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000212extern int mkdir(const char *, mode_t);
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000213#endif
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000214#endif
215#if defined(__IBMC__) || defined(__IBMCPP__)
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000216extern int chdir(char *);
217extern int rmdir(char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000218#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000219extern int chdir(const char *);
220extern int rmdir(const char *);
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +0000221#endif
Tim Peters58e0a8c2001-05-14 22:32:33 +0000222#ifdef __BORLANDC__
223extern int chmod(const char *, int);
224#else
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000225extern int chmod(const char *, mode_t);
Tim Peters58e0a8c2001-05-14 22:32:33 +0000226#endif
Christian Heimes4e30a842007-11-30 22:12:06 +0000227/*#ifdef HAVE_FCHMOD
228extern int fchmod(int, mode_t);
229#endif*/
230/*#ifdef HAVE_LCHMOD
231extern int lchmod(const char *, mode_t);
232#endif*/
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000233extern int chown(const char *, uid_t, gid_t);
234extern char *getcwd(char *, int);
235extern char *strerror(int);
236extern int link(const char *, const char *);
237extern int rename(const char *, const char *);
238extern int stat(const char *, struct stat *);
239extern int unlink(const char *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000240#ifdef HAVE_SYMLINK
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000241extern int symlink(const char *, const char *);
Guido van Rossuma38a5031995-02-17 15:11:36 +0000242#endif /* HAVE_SYMLINK */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000243#ifdef HAVE_LSTAT
Fredrik Lundhff7df9d2000-07-08 22:48:53 +0000244extern int lstat(const char *, struct stat *);
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000245#endif /* HAVE_LSTAT */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000246#endif /* !HAVE_UNISTD_H */
Guido van Rossum36bc6801995-06-14 22:54:23 +0000247
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000248#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000249
Guido van Rossumb6775db1994-08-01 11:34:53 +0000250#ifdef HAVE_UTIME_H
251#include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000252#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000253
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000254#ifdef HAVE_SYS_UTIME_H
255#include <sys/utime.h>
256#define HAVE_UTIME_H /* pretend we do for the rest of this file */
257#endif /* HAVE_SYS_UTIME_H */
258
Guido van Rossumb6775db1994-08-01 11:34:53 +0000259#ifdef HAVE_SYS_TIMES_H
260#include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000261#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000262
263#ifdef HAVE_SYS_PARAM_H
264#include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000265#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266
267#ifdef HAVE_SYS_UTSNAME_H
268#include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000271#ifdef HAVE_DIRENT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000272#include <dirent.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273#define NAMLEN(dirent) strlen((dirent)->d_name)
274#else
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000275#if defined(__WATCOMC__) && !defined(__QNX__)
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000276#include <direct.h>
277#define NAMLEN(dirent) strlen((dirent)->d_name)
278#else
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279#define dirent direct
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000280#define NAMLEN(dirent) (dirent)->d_namlen
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000281#endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000282#ifdef HAVE_SYS_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283#include <sys/ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000284#endif
285#ifdef HAVE_SYS_DIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000286#include <sys/dir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000287#endif
288#ifdef HAVE_NDIR_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000289#include <ndir.h>
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000290#endif
291#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000292
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000293#ifdef _MSC_VER
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000294#ifdef HAVE_DIRECT_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000295#include <direct.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000296#endif
297#ifdef HAVE_IO_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298#include <io.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299#endif
300#ifdef HAVE_PROCESS_H
Guido van Rossumb6775db1994-08-01 11:34:53 +0000301#include <process.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302#endif
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000303#ifndef VOLUME_NAME_DOS
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000304#define VOLUME_NAME_DOS 0x0
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000305#endif
306#ifndef VOLUME_NAME_NT
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000307#define VOLUME_NAME_NT 0x2
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000308#endif
309#ifndef IO_REPARSE_TAG_SYMLINK
Amaury Forgeot d'Arc844807e2010-08-16 22:16:51 +0000310#define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
Raymond Hettinger0291c9f2010-08-01 21:10:35 +0000311#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000312#include "osdefs.h"
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000313#include <malloc.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314#include <windows.h>
Victor Stinner8c62be82010-05-06 00:08:46 +0000315#include <shellapi.h> /* for ShellExecute() */
Brian Curtine8e4b3b2010-09-23 20:04:14 +0000316#include <lmcons.h> /* for UNLEN */
Brian Curtin52173d42010-12-02 18:29:18 +0000317#ifdef SE_CREATE_SYMBOLIC_LINK_NAME /* Available starting with Vista */
318#define HAVE_SYMLINK
Brian Curtin3b4499c2010-12-28 14:31:47 +0000319static int win32_can_symlink = 0;
Brian Curtin52173d42010-12-02 18:29:18 +0000320#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000321#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000322
Tim Petersbc2e10e2002-03-03 23:17:02 +0000323#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +0000324#if defined(PATH_MAX) && PATH_MAX > 1024
325#define MAXPATHLEN PATH_MAX
326#else
Tim Petersbc2e10e2002-03-03 23:17:02 +0000327#define MAXPATHLEN 1024
Thomas Wouters477c8d52006-05-27 19:21:47 +0000328#endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000329#endif /* MAXPATHLEN */
330
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000331#ifdef UNION_WAIT
332/* Emulate some macros on systems that have a union instead of macros */
333
334#ifndef WIFEXITED
335#define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
336#endif
337
338#ifndef WEXITSTATUS
339#define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
340#endif
341
342#ifndef WTERMSIG
343#define WTERMSIG(u_wait) ((u_wait).w_termsig)
344#endif
345
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346#define WAIT_TYPE union wait
347#define WAIT_STATUS_INT(s) (s.w_status)
348
349#else /* !UNION_WAIT */
350#define WAIT_TYPE int
351#define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000352#endif /* UNION_WAIT */
353
Greg Wardb48bc172000-03-01 21:51:56 +0000354/* Don't use the "_r" form if we don't need it (also, won't have a
355 prototype for it, at least on Solaris -- maybe others as well?). */
356#if defined(HAVE_CTERMID_R) && defined(WITH_THREAD)
357#define USE_CTERMID_R
358#endif
359
Fred Drake699f3522000-06-29 21:12:41 +0000360/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000361#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000362#undef FSTAT
363#undef STRUCT_STAT
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000364#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +0000365# define STAT win32_stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700366# define LSTAT win32_lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000367# define FSTAT win32_fstat
368# define STRUCT_STAT struct win32_stat
Fred Drake699f3522000-06-29 21:12:41 +0000369#else
Victor Stinner8c62be82010-05-06 00:08:46 +0000370# define STAT stat
Larry Hastings9cf065c2012-06-22 16:30:09 -0700371# define LSTAT lstat
Victor Stinner8c62be82010-05-06 00:08:46 +0000372# define FSTAT fstat
373# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000374#endif
375
Tim Peters11b23062003-04-23 02:39:17 +0000376#if defined(MAJOR_IN_MKDEV)
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000377#include <sys/mkdev.h>
378#else
379#if defined(MAJOR_IN_SYSMACROS)
380#include <sys/sysmacros.h>
381#endif
Neal Norwitz3d949422002-04-20 13:46:43 +0000382#if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
383#include <sys/mkdev.h>
384#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000385#endif
Fred Drake699f3522000-06-29 21:12:41 +0000386
Larry Hastings9cf065c2012-06-22 16:30:09 -0700387
388#ifdef MS_WINDOWS
389static int
390win32_warn_bytes_api()
391{
392 return PyErr_WarnEx(PyExc_DeprecationWarning,
393 "The Windows bytes API has been deprecated, "
394 "use Unicode filenames instead",
395 1);
396}
397#endif
398
399
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200400#ifndef MS_WINDOWS
401PyObject *
402_PyLong_FromUid(uid_t uid)
403{
404 if (uid == (uid_t)-1)
405 return PyLong_FromLong(-1);
406 return PyLong_FromUnsignedLong(uid);
407}
408
409PyObject *
410_PyLong_FromGid(gid_t gid)
411{
412 if (gid == (gid_t)-1)
413 return PyLong_FromLong(-1);
414 return PyLong_FromUnsignedLong(gid);
415}
416
417int
418_Py_Uid_Converter(PyObject *obj, void *p)
419{
420 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200421 long result;
422 if (PyFloat_Check(obj)) {
423 PyErr_SetString(PyExc_TypeError,
424 "integer argument expected, got float");
425 return 0;
426 }
427 result = PyLong_AsLongAndOverflow(obj, &overflow);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200428 if (overflow < 0)
429 goto OverflowDown;
430 if (!overflow && result == -1) {
431 /* error or -1 */
432 if (PyErr_Occurred())
433 return 0;
434 *(uid_t *)p = (uid_t)-1;
435 }
436 else {
437 /* unsigned uid_t */
438 unsigned long uresult;
439 if (overflow > 0) {
440 uresult = PyLong_AsUnsignedLong(obj);
441 if (PyErr_Occurred()) {
442 if (PyErr_ExceptionMatches(PyExc_OverflowError))
443 goto OverflowUp;
444 return 0;
445 }
446 if ((uid_t)uresult == (uid_t)-1)
447 goto OverflowUp;
448 } else {
449 if (result < 0)
450 goto OverflowDown;
451 uresult = result;
452 }
453 if (sizeof(uid_t) < sizeof(long) &&
454 (unsigned long)(uid_t)uresult != uresult)
455 goto OverflowUp;
456 *(uid_t *)p = (uid_t)uresult;
457 }
458 return 1;
459
460OverflowDown:
461 PyErr_SetString(PyExc_OverflowError,
462 "user id is less than minimum");
463 return 0;
464
465OverflowUp:
466 PyErr_SetString(PyExc_OverflowError,
467 "user id is greater than maximum");
468 return 0;
469}
470
471int
472_Py_Gid_Converter(PyObject *obj, void *p)
473{
474 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200475 long result;
476 if (PyFloat_Check(obj)) {
477 PyErr_SetString(PyExc_TypeError,
478 "integer argument expected, got float");
479 return 0;
480 }
481 result = PyLong_AsLongAndOverflow(obj, &overflow);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200482 if (overflow < 0)
483 goto OverflowDown;
484 if (!overflow && result == -1) {
485 /* error or -1 */
486 if (PyErr_Occurred())
487 return 0;
488 *(gid_t *)p = (gid_t)-1;
489 }
490 else {
491 /* unsigned gid_t */
492 unsigned long uresult;
493 if (overflow > 0) {
494 uresult = PyLong_AsUnsignedLong(obj);
495 if (PyErr_Occurred()) {
496 if (PyErr_ExceptionMatches(PyExc_OverflowError))
497 goto OverflowUp;
498 return 0;
499 }
500 if ((gid_t)uresult == (gid_t)-1)
501 goto OverflowUp;
502 } else {
503 if (result < 0)
504 goto OverflowDown;
505 uresult = result;
506 }
507 if (sizeof(gid_t) < sizeof(long) &&
508 (unsigned long)(gid_t)uresult != uresult)
509 goto OverflowUp;
510 *(gid_t *)p = (gid_t)uresult;
511 }
512 return 1;
513
514OverflowDown:
515 PyErr_SetString(PyExc_OverflowError,
516 "group id is less than minimum");
517 return 0;
518
519OverflowUp:
520 PyErr_SetString(PyExc_OverflowError,
521 "group id is greater than maximum");
522 return 0;
523}
524#endif /* MS_WINDOWS */
525
526
Larry Hastings9cf065c2012-06-22 16:30:09 -0700527#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400528/*
529 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
530 * without the int cast, the value gets interpreted as uint (4291925331),
531 * which doesn't play nicely with all the initializer lines in this file that
532 * look like this:
533 * int dir_fd = DEFAULT_DIR_FD;
534 */
535#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700536#else
537#define DEFAULT_DIR_FD (-100)
538#endif
539
540static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200541_fd_converter(PyObject *o, int *p, const char *allowed)
542{
543 int overflow;
544 long long_value = PyLong_AsLongAndOverflow(o, &overflow);
545 if (PyFloat_Check(o) ||
546 (long_value == -1 && !overflow && PyErr_Occurred())) {
547 PyErr_Clear();
548 PyErr_Format(PyExc_TypeError,
549 "argument should be %s, not %.200s",
550 allowed, Py_TYPE(o)->tp_name);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700551 return 0;
552 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200553 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700554 PyErr_SetString(PyExc_OverflowError,
555 "signed integer is greater than maximum");
556 return 0;
557 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200558 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700559 PyErr_SetString(PyExc_OverflowError,
560 "signed integer is less than minimum");
561 return 0;
562 }
563 *p = (int)long_value;
564 return 1;
565}
566
567static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200568dir_fd_converter(PyObject *o, void *p)
569{
570 if (o == Py_None) {
571 *(int *)p = DEFAULT_DIR_FD;
572 return 1;
573 }
574 return _fd_converter(o, (int *)p, "integer");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700575}
576
577
578
579/*
580 * A PyArg_ParseTuple "converter" function
581 * that handles filesystem paths in the manner
582 * preferred by the os module.
583 *
584 * path_converter accepts (Unicode) strings and their
585 * subclasses, and bytes and their subclasses. What
586 * it does with the argument depends on the platform:
587 *
588 * * On Windows, if we get a (Unicode) string we
589 * extract the wchar_t * and return it; if we get
590 * bytes we extract the char * and return that.
591 *
592 * * On all other platforms, strings are encoded
593 * to bytes using PyUnicode_FSConverter, then we
594 * extract the char * from the bytes object and
595 * return that.
596 *
597 * path_converter also optionally accepts signed
598 * integers (representing open file descriptors) instead
599 * of path strings.
600 *
601 * Input fields:
602 * path.nullable
603 * If nonzero, the path is permitted to be None.
604 * path.allow_fd
605 * If nonzero, the path is permitted to be a file handle
606 * (a signed int) instead of a string.
607 * path.function_name
608 * If non-NULL, path_converter will use that as the name
609 * of the function in error messages.
610 * (If path.argument_name is NULL it omits the function name.)
611 * path.argument_name
612 * If non-NULL, path_converter will use that as the name
613 * of the parameter in error messages.
614 * (If path.argument_name is NULL it uses "path".)
615 *
616 * Output fields:
617 * path.wide
618 * Points to the path if it was expressed as Unicode
619 * and was not encoded. (Only used on Windows.)
620 * path.narrow
621 * Points to the path if it was expressed as bytes,
622 * or it was Unicode and was encoded to bytes.
623 * path.fd
624 * Contains a file descriptor if path.accept_fd was true
625 * and the caller provided a signed integer instead of any
626 * sort of string.
627 *
628 * WARNING: if your "path" parameter is optional, and is
629 * unspecified, path_converter will never get called.
630 * So if you set allow_fd, you *MUST* initialize path.fd = -1
631 * yourself!
632 * path.length
633 * The length of the path in characters, if specified as
634 * a string.
635 * path.object
636 * The original object passed in.
637 * path.cleanup
638 * For internal use only. May point to a temporary object.
639 * (Pay no attention to the man behind the curtain.)
640 *
641 * At most one of path.wide or path.narrow will be non-NULL.
642 * If path was None and path.nullable was set,
643 * or if path was an integer and path.allow_fd was set,
644 * both path.wide and path.narrow will be NULL
645 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200646 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700647 * path_converter takes care to not write to the path_t
648 * unless it's successful. However it must reset the
649 * "cleanup" field each time it's called.
650 *
651 * Use as follows:
652 * path_t path;
653 * memset(&path, 0, sizeof(path));
654 * PyArg_ParseTuple(args, "O&", path_converter, &path);
655 * // ... use values from path ...
656 * path_cleanup(&path);
657 *
658 * (Note that if PyArg_Parse fails you don't need to call
659 * path_cleanup(). However it is safe to do so.)
660 */
661typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100662 const char *function_name;
663 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700664 int nullable;
665 int allow_fd;
666 wchar_t *wide;
667 char *narrow;
668 int fd;
669 Py_ssize_t length;
670 PyObject *object;
671 PyObject *cleanup;
672} path_t;
673
674static void
675path_cleanup(path_t *path) {
676 if (path->cleanup) {
677 Py_DECREF(path->cleanup);
678 path->cleanup = NULL;
679 }
680}
681
682static int
683path_converter(PyObject *o, void *p) {
684 path_t *path = (path_t *)p;
685 PyObject *unicode, *bytes;
686 Py_ssize_t length;
687 char *narrow;
688
689#define FORMAT_EXCEPTION(exc, fmt) \
690 PyErr_Format(exc, "%s%s" fmt, \
691 path->function_name ? path->function_name : "", \
692 path->function_name ? ": " : "", \
693 path->argument_name ? path->argument_name : "path")
694
695 /* Py_CLEANUP_SUPPORTED support */
696 if (o == NULL) {
697 path_cleanup(path);
698 return 1;
699 }
700
701 /* ensure it's always safe to call path_cleanup() */
702 path->cleanup = NULL;
703
704 if (o == Py_None) {
705 if (!path->nullable) {
706 FORMAT_EXCEPTION(PyExc_TypeError,
707 "can't specify None for %s argument");
708 return 0;
709 }
710 path->wide = NULL;
711 path->narrow = NULL;
712 path->length = 0;
713 path->object = o;
714 path->fd = -1;
715 return 1;
716 }
717
718 unicode = PyUnicode_FromObject(o);
719 if (unicode) {
720#ifdef MS_WINDOWS
721 wchar_t *wide;
722 length = PyUnicode_GET_SIZE(unicode);
723 if (length > 32767) {
724 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
725 Py_DECREF(unicode);
726 return 0;
727 }
728
729 wide = PyUnicode_AsUnicode(unicode);
730 if (!wide) {
731 Py_DECREF(unicode);
732 return 0;
733 }
734
735 path->wide = wide;
736 path->narrow = NULL;
737 path->length = length;
738 path->object = o;
739 path->fd = -1;
740 path->cleanup = unicode;
741 return Py_CLEANUP_SUPPORTED;
742#else
743 int converted = PyUnicode_FSConverter(unicode, &bytes);
744 Py_DECREF(unicode);
745 if (!converted)
746 bytes = NULL;
747#endif
748 }
749 else {
750 PyErr_Clear();
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200751 if (PyObject_CheckBuffer(o))
752 bytes = PyBytes_FromObject(o);
753 else
754 bytes = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700755 if (!bytes) {
756 PyErr_Clear();
757 if (path->allow_fd) {
758 int fd;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200759 int result = _fd_converter(o, &fd,
760 "string, bytes or integer");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700761 if (result) {
762 path->wide = NULL;
763 path->narrow = NULL;
764 path->length = 0;
765 path->object = o;
766 path->fd = fd;
767 return result;
768 }
769 }
770 }
771 }
772
773 if (!bytes) {
774 if (!PyErr_Occurred())
775 FORMAT_EXCEPTION(PyExc_TypeError, "illegal type for %s parameter");
776 return 0;
777 }
778
779#ifdef MS_WINDOWS
780 if (win32_warn_bytes_api()) {
781 Py_DECREF(bytes);
782 return 0;
783 }
784#endif
785
786 length = PyBytes_GET_SIZE(bytes);
787#ifdef MS_WINDOWS
788 if (length > MAX_PATH) {
789 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
790 Py_DECREF(bytes);
791 return 0;
792 }
793#endif
794
795 narrow = PyBytes_AS_STRING(bytes);
796 if (length != strlen(narrow)) {
797 FORMAT_EXCEPTION(PyExc_ValueError, "embedded NUL character in %s");
798 Py_DECREF(bytes);
799 return 0;
800 }
801
802 path->wide = NULL;
803 path->narrow = narrow;
804 path->length = length;
805 path->object = o;
806 path->fd = -1;
807 path->cleanup = bytes;
808 return Py_CLEANUP_SUPPORTED;
809}
810
811static void
812argument_unavailable_error(char *function_name, char *argument_name) {
813 PyErr_Format(PyExc_NotImplementedError,
814 "%s%s%s unavailable on this platform",
815 (function_name != NULL) ? function_name : "",
816 (function_name != NULL) ? ": ": "",
817 argument_name);
818}
819
820static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200821dir_fd_unavailable(PyObject *o, void *p)
822{
823 int dir_fd;
824 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700825 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200826 if (dir_fd != DEFAULT_DIR_FD) {
827 argument_unavailable_error(NULL, "dir_fd");
828 return 0;
829 }
830 *(int *)p = dir_fd;
831 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700832}
833
834static int
835fd_specified(char *function_name, int fd) {
836 if (fd == -1)
837 return 0;
838
839 argument_unavailable_error(function_name, "fd");
840 return 1;
841}
842
843static int
844follow_symlinks_specified(char *function_name, int follow_symlinks) {
845 if (follow_symlinks)
846 return 0;
847
848 argument_unavailable_error(function_name, "follow_symlinks");
849 return 1;
850}
851
852static int
853path_and_dir_fd_invalid(char *function_name, path_t *path, int dir_fd) {
854 if (!path->narrow && !path->wide && (dir_fd != DEFAULT_DIR_FD)) {
855 PyErr_Format(PyExc_ValueError,
856 "%s: can't specify dir_fd without matching path",
857 function_name);
858 return 1;
859 }
860 return 0;
861}
862
863static int
864dir_fd_and_fd_invalid(char *function_name, int dir_fd, int fd) {
865 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
866 PyErr_Format(PyExc_ValueError,
867 "%s: can't specify both dir_fd and fd",
868 function_name);
869 return 1;
870 }
871 return 0;
872}
873
874static int
875fd_and_follow_symlinks_invalid(char *function_name, int fd,
876 int follow_symlinks) {
877 if ((fd > 0) && (!follow_symlinks)) {
878 PyErr_Format(PyExc_ValueError,
879 "%s: cannot use fd and follow_symlinks together",
880 function_name);
881 return 1;
882 }
883 return 0;
884}
885
886static int
887dir_fd_and_follow_symlinks_invalid(char *function_name, int dir_fd,
888 int follow_symlinks) {
889 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
890 PyErr_Format(PyExc_ValueError,
891 "%s: cannot use dir_fd and follow_symlinks together",
892 function_name);
893 return 1;
894 }
895 return 0;
896}
897
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200898/* A helper used by a number of POSIX-only functions */
899#ifndef MS_WINDOWS
Georg Brandla391b112011-02-25 15:23:18 +0000900static int
901_parse_off_t(PyObject* arg, void* addr)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000902{
903#if !defined(HAVE_LARGEFILE_SUPPORT)
904 *((off_t*)addr) = PyLong_AsLong(arg);
905#else
Antoine Pitroudcc20b82011-02-26 13:38:35 +0000906 *((off_t*)addr) = PyLong_AsLongLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000907#endif
908 if (PyErr_Occurred())
909 return 0;
910 return 1;
911}
Ross Lagerwallb1e5d592011-09-19 08:30:43 +0200912#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000913
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000914#if defined _MSC_VER && _MSC_VER >= 1400
915/* Microsoft CRT in VS2005 and higher will verify that a filehandle is
Andrew Svetlov737fb892012-12-18 21:14:22 +0200916 * valid and raise an assertion if it isn't.
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000917 * Normally, an invalid fd is likely to be a C program error and therefore
918 * an assertion can be useful, but it does contradict the POSIX standard
919 * which for write(2) states:
920 * "Otherwise, -1 shall be returned and errno set to indicate the error."
921 * "[EBADF] The fildes argument is not a valid file descriptor open for
922 * writing."
923 * Furthermore, python allows the user to enter any old integer
924 * as a fd and should merely raise a python exception on error.
925 * The Microsoft CRT doesn't provide an official way to check for the
926 * validity of a file descriptor, but we can emulate its internal behaviour
Victor Stinner8c62be82010-05-06 00:08:46 +0000927 * by using the exported __pinfo data member and knowledge of the
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000928 * internal structures involved.
929 * The structures below must be updated for each version of visual studio
930 * according to the file internal.h in the CRT source, until MS comes
931 * up with a less hacky way to do this.
932 * (all of this is to avoid globally modifying the CRT behaviour using
933 * _set_invalid_parameter_handler() and _CrtSetReportMode())
934 */
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000935/* The actual size of the structure is determined at runtime.
936 * Only the first items must be present.
937 */
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000938typedef struct {
Victor Stinner8c62be82010-05-06 00:08:46 +0000939 intptr_t osfhnd;
940 char osfile;
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000941} my_ioinfo;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000942
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000943extern __declspec(dllimport) char * __pioinfo[];
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000944#define IOINFO_L2E 5
945#define IOINFO_ARRAY_ELTS (1 << IOINFO_L2E)
946#define IOINFO_ARRAYS 64
947#define _NHANDLE_ (IOINFO_ARRAYS * IOINFO_ARRAY_ELTS)
948#define FOPEN 0x01
949#define _NO_CONSOLE_FILENO (intptr_t)-2
950
951/* This function emulates what the windows CRT does to validate file handles */
952int
953_PyVerify_fd(int fd)
954{
Victor Stinner8c62be82010-05-06 00:08:46 +0000955 const int i1 = fd >> IOINFO_L2E;
956 const int i2 = fd & ((1 << IOINFO_L2E) - 1);
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000957
Antoine Pitrou22e41552010-08-15 18:07:50 +0000958 static size_t sizeof_ioinfo = 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000959
Victor Stinner8c62be82010-05-06 00:08:46 +0000960 /* Determine the actual size of the ioinfo structure,
961 * as used by the CRT loaded in memory
962 */
963 if (sizeof_ioinfo == 0 && __pioinfo[0] != NULL) {
964 sizeof_ioinfo = _msize(__pioinfo[0]) / IOINFO_ARRAY_ELTS;
965 }
966 if (sizeof_ioinfo == 0) {
967 /* This should not happen... */
968 goto fail;
969 }
970
971 /* See that it isn't a special CLEAR fileno */
972 if (fd != _NO_CONSOLE_FILENO) {
973 /* Microsoft CRT would check that 0<=fd<_nhandle but we can't do that. Instead
974 * we check pointer validity and other info
975 */
976 if (0 <= i1 && i1 < IOINFO_ARRAYS && __pioinfo[i1] != NULL) {
977 /* finally, check that the file is open */
978 my_ioinfo* info = (my_ioinfo*)(__pioinfo[i1] + i2 * sizeof_ioinfo);
979 if (info->osfile & FOPEN) {
980 return 1;
981 }
982 }
983 }
Kristján Valur Jónssonf64e6512009-04-13 10:16:14 +0000984 fail:
Victor Stinner8c62be82010-05-06 00:08:46 +0000985 errno = EBADF;
986 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +0000987}
988
989/* the special case of checking dup2. The target fd must be in a sensible range */
990static int
991_PyVerify_fd_dup2(int fd1, int fd2)
992{
Victor Stinner8c62be82010-05-06 00:08:46 +0000993 if (!_PyVerify_fd(fd1))
994 return 0;
995 if (fd2 == _NO_CONSOLE_FILENO)
996 return 0;
997 if ((unsigned)fd2 < _NHANDLE_)
998 return 1;
999 else
1000 return 0;
Amaury Forgeot d'Arc2fc224f2009-02-19 23:23:47 +00001001}
1002#else
1003/* dummy version. _PyVerify_fd() is already defined in fileobject.h */
1004#define _PyVerify_fd_dup2(A, B) (1)
1005#endif
1006
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001007#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001008/* The following structure was copied from
1009 http://msdn.microsoft.com/en-us/library/ms791514.aspx as the required
1010 include doesn't seem to be present in the Windows SDK (at least as included
1011 with Visual Studio Express). */
1012typedef struct _REPARSE_DATA_BUFFER {
1013 ULONG ReparseTag;
1014 USHORT ReparseDataLength;
1015 USHORT Reserved;
1016 union {
1017 struct {
1018 USHORT SubstituteNameOffset;
1019 USHORT SubstituteNameLength;
1020 USHORT PrintNameOffset;
1021 USHORT PrintNameLength;
1022 ULONG Flags;
1023 WCHAR PathBuffer[1];
1024 } SymbolicLinkReparseBuffer;
1025
1026 struct {
1027 USHORT SubstituteNameOffset;
1028 USHORT SubstituteNameLength;
1029 USHORT PrintNameOffset;
1030 USHORT PrintNameLength;
1031 WCHAR PathBuffer[1];
1032 } MountPointReparseBuffer;
1033
1034 struct {
1035 UCHAR DataBuffer[1];
1036 } GenericReparseBuffer;
1037 };
1038} REPARSE_DATA_BUFFER, *PREPARSE_DATA_BUFFER;
1039
1040#define REPARSE_DATA_BUFFER_HEADER_SIZE FIELD_OFFSET(REPARSE_DATA_BUFFER,\
1041 GenericReparseBuffer)
1042#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE ( 16 * 1024 )
1043
1044static int
Brian Curtind25aef52011-06-13 15:16:04 -05001045win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001046{
1047 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1048 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
1049 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001050
1051 if (0 == DeviceIoControl(
1052 reparse_point_handle,
1053 FSCTL_GET_REPARSE_POINT,
1054 NULL, 0, /* in buffer */
1055 target_buffer, sizeof(target_buffer),
1056 &n_bytes_returned,
1057 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001058 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001059
1060 if (reparse_tag)
1061 *reparse_tag = rdb->ReparseTag;
1062
Brian Curtind25aef52011-06-13 15:16:04 -05001063 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001064}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001065
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001066#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001067
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001069#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001070/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001071** environ directly, we must obtain it with _NSGetEnviron(). See also
1072** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001073*/
1074#include <crt_externs.h>
1075static char **environ;
1076#elif !defined(_MSC_VER) && ( !defined(__WATCOMC__) || defined(__QNX__) )
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001077extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001078#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001079
Barry Warsaw53699e91996-12-10 23:23:01 +00001080static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001081convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001082{
Victor Stinner8c62be82010-05-06 00:08:46 +00001083 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001084#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001085 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001086#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001087 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001088#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001089
Victor Stinner8c62be82010-05-06 00:08:46 +00001090 d = PyDict_New();
1091 if (d == NULL)
1092 return NULL;
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001093#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Victor Stinner8c62be82010-05-06 00:08:46 +00001094 if (environ == NULL)
1095 environ = *_NSGetEnviron();
1096#endif
1097#ifdef MS_WINDOWS
1098 /* _wenviron must be initialized in this way if the program is started
1099 through main() instead of wmain(). */
1100 _wgetenv(L"");
1101 if (_wenviron == NULL)
1102 return d;
1103 /* This part ignores errors */
1104 for (e = _wenviron; *e != NULL; e++) {
1105 PyObject *k;
1106 PyObject *v;
1107 wchar_t *p = wcschr(*e, L'=');
1108 if (p == NULL)
1109 continue;
1110 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1111 if (k == NULL) {
1112 PyErr_Clear();
1113 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001114 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001115 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1116 if (v == NULL) {
1117 PyErr_Clear();
1118 Py_DECREF(k);
1119 continue;
Guido van Rossumd48f2521997-12-05 22:19:34 +00001120 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001121 if (PyDict_GetItem(d, k) == NULL) {
1122 if (PyDict_SetItem(d, k, v) != 0)
1123 PyErr_Clear();
1124 }
1125 Py_DECREF(k);
1126 Py_DECREF(v);
1127 }
1128#else
1129 if (environ == NULL)
1130 return d;
1131 /* This part ignores errors */
1132 for (e = environ; *e != NULL; e++) {
1133 PyObject *k;
1134 PyObject *v;
1135 char *p = strchr(*e, '=');
1136 if (p == NULL)
1137 continue;
Victor Stinner84ae1182010-05-06 22:05:07 +00001138 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Victor Stinner8c62be82010-05-06 00:08:46 +00001139 if (k == NULL) {
1140 PyErr_Clear();
1141 continue;
1142 }
Victor Stinner84ae1182010-05-06 22:05:07 +00001143 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Victor Stinner8c62be82010-05-06 00:08:46 +00001144 if (v == NULL) {
1145 PyErr_Clear();
1146 Py_DECREF(k);
1147 continue;
1148 }
1149 if (PyDict_GetItem(d, k) == NULL) {
1150 if (PyDict_SetItem(d, k, v) != 0)
1151 PyErr_Clear();
1152 }
1153 Py_DECREF(k);
1154 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001155 }
1156#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001157 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001158}
1159
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001160/* Set a POSIX-specific error from errno, and return NULL */
1161
Barry Warsawd58d7641998-07-23 16:14:40 +00001162static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001163posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001164{
Victor Stinner8c62be82010-05-06 00:08:46 +00001165 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001166}
Mark Hammondef8b6542001-05-13 08:04:26 +00001167
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001168#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001169static PyObject *
Brian Curtind40e6f72010-07-08 21:39:08 +00001170win32_error(char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001171{
Victor Stinner8c62be82010-05-06 00:08:46 +00001172 /* XXX We should pass the function name along in the future.
1173 (winreg.c also wants to pass the function name.)
1174 This would however require an additional param to the
1175 Windows error object, which is non-trivial.
1176 */
1177 errno = GetLastError();
1178 if (filename)
1179 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1180 else
1181 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001182}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001183
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001184static PyObject *
Victor Stinnereb5657a2011-09-30 01:44:27 +02001185win32_error_object(char* function, PyObject* filename)
1186{
1187 /* XXX - see win32_error for comments on 'function' */
1188 errno = GetLastError();
1189 if (filename)
1190 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001191 PyExc_OSError,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001192 errno,
1193 filename);
1194 else
1195 return PyErr_SetFromWindowsErr(errno);
1196}
1197
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001198#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001199
Larry Hastings9cf065c2012-06-22 16:30:09 -07001200static PyObject *
Victor Stinner292c8352012-10-30 02:17:38 +01001201path_error(path_t *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001202{
1203#ifdef MS_WINDOWS
Victor Stinner292c8352012-10-30 02:17:38 +01001204 return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
1205 0, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001206#else
Victor Stinner292c8352012-10-30 02:17:38 +01001207 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001208#endif
1209}
1210
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001211/* POSIX generic methods */
1212
Barry Warsaw53699e91996-12-10 23:23:01 +00001213static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00001214posix_fildes(PyObject *fdobj, int (*func)(int))
1215{
Victor Stinner8c62be82010-05-06 00:08:46 +00001216 int fd;
1217 int res;
1218 fd = PyObject_AsFileDescriptor(fdobj);
1219 if (fd < 0)
1220 return NULL;
1221 if (!_PyVerify_fd(fd))
1222 return posix_error();
1223 Py_BEGIN_ALLOW_THREADS
1224 res = (*func)(fd);
1225 Py_END_ALLOW_THREADS
1226 if (res < 0)
1227 return posix_error();
1228 Py_INCREF(Py_None);
1229 return Py_None;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001230}
Guido van Rossum21142a01999-01-08 21:05:37 +00001231
1232static PyObject *
Victor Stinner292c8352012-10-30 02:17:38 +01001233posix_1str(const char *func_name, PyObject *args, char *format,
1234 int (*func)(const char*))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001235{
Victor Stinner292c8352012-10-30 02:17:38 +01001236 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00001237 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01001238 memset(&path, 0, sizeof(path));
1239 path.function_name = func_name;
Victor Stinner8c62be82010-05-06 00:08:46 +00001240 if (!PyArg_ParseTuple(args, format,
Victor Stinner292c8352012-10-30 02:17:38 +01001241 path_converter, &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00001242 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001243 Py_BEGIN_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01001244 res = (*func)(path.narrow);
Victor Stinner8c62be82010-05-06 00:08:46 +00001245 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01001246 if (res < 0) {
1247 path_error(&path);
1248 path_cleanup(&path);
1249 return NULL;
1250 }
1251 path_cleanup(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001252 Py_INCREF(Py_None);
1253 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001254}
1255
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001256
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001257#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001258/* This is a reimplementation of the C library's chdir function,
1259 but one that produces Win32 errors instead of DOS error codes.
1260 chdir is essentially a wrapper around SetCurrentDirectory; however,
1261 it also needs to set "magic" environment variables indicating
1262 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001263static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001264win32_chdir(LPCSTR path)
1265{
Victor Stinner8c62be82010-05-06 00:08:46 +00001266 char new_path[MAX_PATH+1];
1267 int result;
1268 char env[4] = "=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001269
Victor Stinner8c62be82010-05-06 00:08:46 +00001270 if(!SetCurrentDirectoryA(path))
1271 return FALSE;
1272 result = GetCurrentDirectoryA(MAX_PATH+1, new_path);
1273 if (!result)
1274 return FALSE;
1275 /* In the ANSI API, there should not be any paths longer
1276 than MAX_PATH. */
1277 assert(result <= MAX_PATH+1);
1278 if (strncmp(new_path, "\\\\", 2) == 0 ||
1279 strncmp(new_path, "//", 2) == 0)
1280 /* UNC path, nothing to do. */
1281 return TRUE;
1282 env[1] = new_path[0];
1283 return SetEnvironmentVariableA(env, new_path);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001284}
1285
1286/* The Unicode version differs from the ANSI version
1287 since the current directory might exceed MAX_PATH characters */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001288static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001289win32_wchdir(LPCWSTR path)
1290{
Victor Stinner8c62be82010-05-06 00:08:46 +00001291 wchar_t _new_path[MAX_PATH+1], *new_path = _new_path;
1292 int result;
1293 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001294
Victor Stinner8c62be82010-05-06 00:08:46 +00001295 if(!SetCurrentDirectoryW(path))
1296 return FALSE;
1297 result = GetCurrentDirectoryW(MAX_PATH+1, new_path);
1298 if (!result)
1299 return FALSE;
1300 if (result > MAX_PATH+1) {
1301 new_path = malloc(result * sizeof(wchar_t));
1302 if (!new_path) {
1303 SetLastError(ERROR_OUTOFMEMORY);
1304 return FALSE;
1305 }
1306 result = GetCurrentDirectoryW(result, new_path);
1307 if (!result) {
1308 free(new_path);
1309 return FALSE;
1310 }
1311 }
1312 if (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1313 wcsncmp(new_path, L"//", 2) == 0)
1314 /* UNC path, nothing to do. */
1315 return TRUE;
1316 env[1] = new_path[0];
1317 result = SetEnvironmentVariableW(env, new_path);
1318 if (new_path != _new_path)
1319 free(new_path);
1320 return result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001321}
1322#endif
1323
Martin v. Löwis14694662006-02-03 12:54:16 +00001324#ifdef MS_WINDOWS
1325/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1326 - time stamps are restricted to second resolution
1327 - file modification times suffer from forth-and-back conversions between
1328 UTC and local time
1329 Therefore, we implement our own stat, based on the Win32 API directly.
1330*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001331#define HAVE_STAT_NSEC 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001332
1333struct win32_stat{
Brian Curtin87e63a22012-12-31 11:59:48 -06001334 unsigned long st_dev;
Martin v. Löwis14694662006-02-03 12:54:16 +00001335 __int64 st_ino;
1336 unsigned short st_mode;
1337 int st_nlink;
1338 int st_uid;
1339 int st_gid;
Brian Curtin87e63a22012-12-31 11:59:48 -06001340 unsigned long st_rdev;
Martin v. Löwis14694662006-02-03 12:54:16 +00001341 __int64 st_size;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001342 time_t st_atime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001343 int st_atime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001344 time_t st_mtime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001345 int st_mtime_nsec;
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001346 time_t st_ctime;
Martin v. Löwis14694662006-02-03 12:54:16 +00001347 int st_ctime_nsec;
1348};
1349
1350static __int64 secs_between_epochs = 11644473600; /* Seconds between 1.1.1601 and 1.1.1970 */
1351
1352static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001353FILE_TIME_to_time_t_nsec(FILETIME *in_ptr, time_t *time_out, int* nsec_out)
Martin v. Löwis14694662006-02-03 12:54:16 +00001354{
Victor Stinner8c62be82010-05-06 00:08:46 +00001355 /* XXX endianness. Shouldn't matter, as all Windows implementations are little-endian */
1356 /* Cannot simply cast and dereference in_ptr,
1357 since it might not be aligned properly */
1358 __int64 in;
1359 memcpy(&in, in_ptr, sizeof(in));
1360 *nsec_out = (int)(in % 10000000) * 100; /* FILETIME is in units of 100 nsec. */
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001361 *time_out = Py_SAFE_DOWNCAST((in / 10000000) - secs_between_epochs, __int64, time_t);
Martin v. Löwis14694662006-02-03 12:54:16 +00001362}
1363
Thomas Wouters477c8d52006-05-27 19:21:47 +00001364static void
Amaury Forgeot d'Arca251a852011-01-03 00:19:11 +00001365time_t_to_FILE_TIME(time_t time_in, int nsec_in, FILETIME *out_ptr)
Thomas Wouters477c8d52006-05-27 19:21:47 +00001366{
Victor Stinner8c62be82010-05-06 00:08:46 +00001367 /* XXX endianness */
1368 __int64 out;
1369 out = time_in + secs_between_epochs;
1370 out = out * 10000000 + nsec_in / 100;
1371 memcpy(out_ptr, &out, sizeof(out));
Thomas Wouters477c8d52006-05-27 19:21:47 +00001372}
1373
Martin v. Löwis14694662006-02-03 12:54:16 +00001374/* Below, we *know* that ugo+r is 0444 */
1375#if _S_IREAD != 0400
1376#error Unsupported C library
1377#endif
1378static int
1379attributes_to_mode(DWORD attr)
1380{
Victor Stinner8c62be82010-05-06 00:08:46 +00001381 int m = 0;
1382 if (attr & FILE_ATTRIBUTE_DIRECTORY)
1383 m |= _S_IFDIR | 0111; /* IFEXEC for user,group,other */
1384 else
1385 m |= _S_IFREG;
1386 if (attr & FILE_ATTRIBUTE_READONLY)
1387 m |= 0444;
1388 else
1389 m |= 0666;
1390 return m;
Martin v. Löwis14694662006-02-03 12:54:16 +00001391}
1392
1393static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001394attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *info, ULONG reparse_tag, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001395{
Victor Stinner8c62be82010-05-06 00:08:46 +00001396 memset(result, 0, sizeof(*result));
1397 result->st_mode = attributes_to_mode(info->dwFileAttributes);
1398 result->st_size = (((__int64)info->nFileSizeHigh)<<32) + info->nFileSizeLow;
Brian Curtin490b32a2012-12-26 07:03:03 -06001399 result->st_dev = info->dwVolumeSerialNumber;
1400 result->st_rdev = result->st_dev;
Victor Stinner8c62be82010-05-06 00:08:46 +00001401 FILE_TIME_to_time_t_nsec(&info->ftCreationTime, &result->st_ctime, &result->st_ctime_nsec);
1402 FILE_TIME_to_time_t_nsec(&info->ftLastWriteTime, &result->st_mtime, &result->st_mtime_nsec);
1403 FILE_TIME_to_time_t_nsec(&info->ftLastAccessTime, &result->st_atime, &result->st_atime_nsec);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001404 result->st_nlink = info->nNumberOfLinks;
Brian Curtin1b9df392010-11-24 20:24:31 +00001405 result->st_ino = (((__int64)info->nFileIndexHigh)<<32) + info->nFileIndexLow;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001406 if (reparse_tag == IO_REPARSE_TAG_SYMLINK) {
1407 /* first clear the S_IFMT bits */
1408 result->st_mode ^= (result->st_mode & 0170000);
1409 /* now set the bits that make this a symlink */
1410 result->st_mode |= 0120000;
1411 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001412
Victor Stinner8c62be82010-05-06 00:08:46 +00001413 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001414}
1415
Guido van Rossumd8faa362007-04-27 19:54:29 +00001416static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001417attributes_from_dir(LPCSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001418{
Victor Stinner8c62be82010-05-06 00:08:46 +00001419 HANDLE hFindFile;
1420 WIN32_FIND_DATAA FileData;
1421 hFindFile = FindFirstFileA(pszFile, &FileData);
1422 if (hFindFile == INVALID_HANDLE_VALUE)
1423 return FALSE;
1424 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001425 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001426 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001427 info->dwFileAttributes = FileData.dwFileAttributes;
1428 info->ftCreationTime = FileData.ftCreationTime;
1429 info->ftLastAccessTime = FileData.ftLastAccessTime;
1430 info->ftLastWriteTime = FileData.ftLastWriteTime;
1431 info->nFileSizeHigh = FileData.nFileSizeHigh;
1432 info->nFileSizeLow = FileData.nFileSizeLow;
1433/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001434 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1435 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001437}
1438
1439static BOOL
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001440attributes_from_dir_w(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001441{
Victor Stinner8c62be82010-05-06 00:08:46 +00001442 HANDLE hFindFile;
1443 WIN32_FIND_DATAW FileData;
1444 hFindFile = FindFirstFileW(pszFile, &FileData);
1445 if (hFindFile == INVALID_HANDLE_VALUE)
1446 return FALSE;
1447 FindClose(hFindFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001448 memset(info, 0, sizeof(*info));
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001449 *reparse_tag = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001450 info->dwFileAttributes = FileData.dwFileAttributes;
1451 info->ftCreationTime = FileData.ftCreationTime;
1452 info->ftLastAccessTime = FileData.ftLastAccessTime;
1453 info->ftLastWriteTime = FileData.ftLastWriteTime;
1454 info->nFileSizeHigh = FileData.nFileSizeHigh;
1455 info->nFileSizeLow = FileData.nFileSizeLow;
1456/* info->nNumberOfLinks = 1; */
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001457 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1458 *reparse_tag = FileData.dwReserved0;
Victor Stinner8c62be82010-05-06 00:08:46 +00001459 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001460}
1461
Brian Curtind25aef52011-06-13 15:16:04 -05001462/* Grab GetFinalPathNameByHandle dynamically from kernel32 */
Antoine Pitrou06eecea2012-10-21 16:33:33 +02001463static int has_GetFinalPathNameByHandle = -1;
Brian Curtind25aef52011-06-13 15:16:04 -05001464static DWORD (CALLBACK *Py_GetFinalPathNameByHandleW)(HANDLE, LPWSTR, DWORD,
1465 DWORD);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001466static int
Brian Curtind25aef52011-06-13 15:16:04 -05001467check_GetFinalPathNameByHandle()
Brian Curtinf5e76d02010-11-24 13:14:05 +00001468{
Brian Curtind25aef52011-06-13 15:16:04 -05001469 HINSTANCE hKernel32;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001470 DWORD (CALLBACK *Py_GetFinalPathNameByHandleA)(HANDLE, LPSTR, DWORD,
1471 DWORD);
1472
Brian Curtind25aef52011-06-13 15:16:04 -05001473 /* only recheck */
Antoine Pitrou06eecea2012-10-21 16:33:33 +02001474 if (-1 == has_GetFinalPathNameByHandle)
Brian Curtind25aef52011-06-13 15:16:04 -05001475 {
Martin v. Löwis50590f12012-01-14 17:54:09 +01001476 hKernel32 = GetModuleHandleW(L"KERNEL32");
Brian Curtind25aef52011-06-13 15:16:04 -05001477 *(FARPROC*)&Py_GetFinalPathNameByHandleA = GetProcAddress(hKernel32,
1478 "GetFinalPathNameByHandleA");
1479 *(FARPROC*)&Py_GetFinalPathNameByHandleW = GetProcAddress(hKernel32,
1480 "GetFinalPathNameByHandleW");
1481 has_GetFinalPathNameByHandle = Py_GetFinalPathNameByHandleA &&
1482 Py_GetFinalPathNameByHandleW;
1483 }
1484 return has_GetFinalPathNameByHandle;
1485}
1486
1487static BOOL
1488get_target_path(HANDLE hdl, wchar_t **target_path)
1489{
1490 int buf_size, result_length;
1491 wchar_t *buf;
1492
1493 /* We have a good handle to the target, use it to determine
1494 the target path name (then we'll call lstat on it). */
1495 buf_size = Py_GetFinalPathNameByHandleW(hdl, 0, 0,
1496 VOLUME_NAME_DOS);
1497 if(!buf_size)
1498 return FALSE;
1499
1500 buf = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
Brian Curtinc8be8402011-06-14 09:52:50 -05001501 if (!buf) {
1502 SetLastError(ERROR_OUTOFMEMORY);
1503 return FALSE;
1504 }
1505
Brian Curtind25aef52011-06-13 15:16:04 -05001506 result_length = Py_GetFinalPathNameByHandleW(hdl,
1507 buf, buf_size, VOLUME_NAME_DOS);
1508
1509 if(!result_length) {
1510 free(buf);
1511 return FALSE;
1512 }
1513
1514 if(!CloseHandle(hdl)) {
1515 free(buf);
1516 return FALSE;
1517 }
1518
1519 buf[result_length] = 0;
1520
1521 *target_path = buf;
1522 return TRUE;
1523}
1524
1525static int
1526win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1527 BOOL traverse);
1528static int
1529win32_xstat_impl(const char *path, struct win32_stat *result,
1530 BOOL traverse)
1531{
Victor Stinner26de69d2011-06-17 15:15:38 +02001532 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001533 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001534 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001535 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001536 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001537 const char *dot;
1538
Brian Curtind25aef52011-06-13 15:16:04 -05001539 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001540 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1541 traverse reparse point. */
1542 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001543 }
1544
Brian Curtinf5e76d02010-11-24 13:14:05 +00001545 hFile = CreateFileA(
1546 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001547 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001548 0, /* share mode */
1549 NULL, /* security attributes */
1550 OPEN_EXISTING,
1551 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001552 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1553 Because of this, calls like GetFinalPathNameByHandle will return
1554 the symlink path agin and not the actual final path. */
1555 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
1556 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001557 NULL);
1558
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001559 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001560 /* Either the target doesn't exist, or we don't have access to
1561 get a handle to it. If the former, we need to return an error.
1562 If the latter, we can use attributes_from_dir. */
1563 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001564 return -1;
1565 /* Could not get attributes on open file. Fall back to
1566 reading the directory. */
1567 if (!attributes_from_dir(path, &info, &reparse_tag))
1568 /* Very strange. This should not fail now */
1569 return -1;
1570 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1571 if (traverse) {
1572 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001573 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001574 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001575 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001576 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001577 } else {
1578 if (!GetFileInformationByHandle(hFile, &info)) {
1579 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001580 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001581 }
1582 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001583 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1584 return -1;
1585
1586 /* Close the outer open file handle now that we're about to
1587 reopen it with different flags. */
1588 if (!CloseHandle(hFile))
1589 return -1;
1590
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001591 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001592 /* In order to call GetFinalPathNameByHandle we need to open
1593 the file without the reparse handling flag set. */
1594 hFile2 = CreateFileA(
1595 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1596 NULL, OPEN_EXISTING,
1597 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1598 NULL);
1599 if (hFile2 == INVALID_HANDLE_VALUE)
1600 return -1;
1601
1602 if (!get_target_path(hFile2, &target_path))
1603 return -1;
1604
1605 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001606 free(target_path);
1607 return code;
1608 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001609 } else
1610 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001611 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001612 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001613
1614 /* Set S_IEXEC if it is an .exe, .bat, ... */
1615 dot = strrchr(path, '.');
1616 if (dot) {
1617 if (stricmp(dot, ".bat") == 0 || stricmp(dot, ".cmd") == 0 ||
1618 stricmp(dot, ".exe") == 0 || stricmp(dot, ".com") == 0)
1619 result->st_mode |= 0111;
1620 }
1621 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001622}
1623
1624static int
Brian Curtind25aef52011-06-13 15:16:04 -05001625win32_xstat_impl_w(const wchar_t *path, struct win32_stat *result,
1626 BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001627{
1628 int code;
Brian Curtind25aef52011-06-13 15:16:04 -05001629 HANDLE hFile, hFile2;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001630 BY_HANDLE_FILE_INFORMATION info;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001631 ULONG reparse_tag = 0;
Victor Stinner26de69d2011-06-17 15:15:38 +02001632 wchar_t *target_path;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001633 const wchar_t *dot;
1634
Brian Curtind25aef52011-06-13 15:16:04 -05001635 if(!check_GetFinalPathNameByHandle()) {
Brian Curtinc8be8402011-06-14 09:52:50 -05001636 /* If the OS doesn't have GetFinalPathNameByHandle, don't
1637 traverse reparse point. */
1638 traverse = FALSE;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001639 }
1640
Brian Curtinf5e76d02010-11-24 13:14:05 +00001641 hFile = CreateFileW(
1642 path,
Brian Curtind25aef52011-06-13 15:16:04 -05001643 FILE_READ_ATTRIBUTES, /* desired access */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001644 0, /* share mode */
1645 NULL, /* security attributes */
1646 OPEN_EXISTING,
1647 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
Brian Curtind25aef52011-06-13 15:16:04 -05001648 /* FILE_FLAG_OPEN_REPARSE_POINT does not follow the symlink.
1649 Because of this, calls like GetFinalPathNameByHandle will return
1650 the symlink path agin and not the actual final path. */
Victor Stinner26de69d2011-06-17 15:15:38 +02001651 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS|
Brian Curtind25aef52011-06-13 15:16:04 -05001652 FILE_FLAG_OPEN_REPARSE_POINT,
Brian Curtinf5e76d02010-11-24 13:14:05 +00001653 NULL);
1654
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001655 if (hFile == INVALID_HANDLE_VALUE) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001656 /* Either the target doesn't exist, or we don't have access to
1657 get a handle to it. If the former, we need to return an error.
1658 If the latter, we can use attributes_from_dir. */
1659 if (GetLastError() != ERROR_SHARING_VIOLATION)
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001660 return -1;
1661 /* Could not get attributes on open file. Fall back to
1662 reading the directory. */
1663 if (!attributes_from_dir_w(path, &info, &reparse_tag))
1664 /* Very strange. This should not fail now */
1665 return -1;
1666 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1667 if (traverse) {
1668 /* Should traverse, but could not open reparse point handle */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001669 SetLastError(ERROR_SHARING_VIOLATION);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001670 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001671 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001672 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001673 } else {
1674 if (!GetFileInformationByHandle(hFile, &info)) {
1675 CloseHandle(hFile);
Brian Curtind25aef52011-06-13 15:16:04 -05001676 return -1;
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001677 }
1678 if (info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
Brian Curtind25aef52011-06-13 15:16:04 -05001679 if (!win32_get_reparse_tag(hFile, &reparse_tag))
1680 return -1;
1681
1682 /* Close the outer open file handle now that we're about to
1683 reopen it with different flags. */
1684 if (!CloseHandle(hFile))
1685 return -1;
1686
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001687 if (traverse) {
Brian Curtind25aef52011-06-13 15:16:04 -05001688 /* In order to call GetFinalPathNameByHandle we need to open
1689 the file without the reparse handling flag set. */
1690 hFile2 = CreateFileW(
1691 path, FILE_READ_ATTRIBUTES, FILE_SHARE_READ,
1692 NULL, OPEN_EXISTING,
1693 FILE_ATTRIBUTE_NORMAL|FILE_FLAG_BACKUP_SEMANTICS,
1694 NULL);
1695 if (hFile2 == INVALID_HANDLE_VALUE)
1696 return -1;
1697
1698 if (!get_target_path(hFile2, &target_path))
1699 return -1;
1700
1701 code = win32_xstat_impl_w(target_path, result, FALSE);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001702 free(target_path);
1703 return code;
1704 }
Hirokazu Yamamoto7ed117a2010-12-07 10:24:37 +00001705 } else
1706 CloseHandle(hFile);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001707 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001708 attribute_data_to_stat(&info, reparse_tag, result);
Brian Curtinf5e76d02010-11-24 13:14:05 +00001709
1710 /* Set S_IEXEC if it is an .exe, .bat, ... */
1711 dot = wcsrchr(path, '.');
1712 if (dot) {
1713 if (_wcsicmp(dot, L".bat") == 0 || _wcsicmp(dot, L".cmd") == 0 ||
1714 _wcsicmp(dot, L".exe") == 0 || _wcsicmp(dot, L".com") == 0)
1715 result->st_mode |= 0111;
1716 }
1717 return 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001718}
1719
1720static int
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001721win32_xstat(const char *path, struct win32_stat *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001722{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001723 /* Protocol violation: we explicitly clear errno, instead of
1724 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001725 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001726 errno = 0;
1727 return code;
1728}
Brian Curtinf5e76d02010-11-24 13:14:05 +00001729
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001730static int
1731win32_xstat_w(const wchar_t *path, struct win32_stat *result, BOOL traverse)
1732{
1733 /* Protocol violation: we explicitly clear errno, instead of
1734 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001735 int code = win32_xstat_impl_w(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001736 errno = 0;
1737 return code;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001738}
Brian Curtind25aef52011-06-13 15:16:04 -05001739/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001740
1741 In Posix, stat automatically traverses symlinks and returns the stat
1742 structure for the target. In Windows, the equivalent GetFileAttributes by
1743 default does not traverse symlinks and instead returns attributes for
1744 the symlink.
1745
1746 Therefore, win32_lstat will get the attributes traditionally, and
1747 win32_stat will first explicitly resolve the symlink target and then will
1748 call win32_lstat on that result.
1749
Ezio Melotti4969f702011-03-15 05:59:46 +02001750 The _w represent Unicode equivalents of the aforementioned ANSI functions. */
Brian Curtind40e6f72010-07-08 21:39:08 +00001751
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001752static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001753win32_lstat(const char* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001754{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001755 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001756}
1757
Victor Stinner8c62be82010-05-06 00:08:46 +00001758static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001759win32_lstat_w(const wchar_t* path, struct win32_stat *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001760{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001761 return win32_xstat_w(path, result, FALSE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001762}
1763
1764static int
1765win32_stat(const char* path, struct win32_stat *result)
1766{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001767 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001768}
1769
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001770static int
Brian Curtind40e6f72010-07-08 21:39:08 +00001771win32_stat_w(const wchar_t* path, struct win32_stat *result)
1772{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001773 return win32_xstat_w(path, result, TRUE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001774}
1775
1776static int
1777win32_fstat(int file_number, struct win32_stat *result)
1778{
Victor Stinner8c62be82010-05-06 00:08:46 +00001779 BY_HANDLE_FILE_INFORMATION info;
1780 HANDLE h;
1781 int type;
Martin v. Löwis14694662006-02-03 12:54:16 +00001782
Richard Oudkerk2240ac12012-07-06 12:05:32 +01001783 if (!_PyVerify_fd(file_number))
1784 h = INVALID_HANDLE_VALUE;
1785 else
1786 h = (HANDLE)_get_osfhandle(file_number);
Martin v. Löwis14694662006-02-03 12:54:16 +00001787
Victor Stinner8c62be82010-05-06 00:08:46 +00001788 /* Protocol violation: we explicitly clear errno, instead of
1789 setting it to a POSIX error. Callers should use GetLastError. */
1790 errno = 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001791
Victor Stinner8c62be82010-05-06 00:08:46 +00001792 if (h == INVALID_HANDLE_VALUE) {
1793 /* This is really a C library error (invalid file handle).
1794 We set the Win32 error to the closes one matching. */
1795 SetLastError(ERROR_INVALID_HANDLE);
1796 return -1;
1797 }
1798 memset(result, 0, sizeof(*result));
Martin v. Löwis14694662006-02-03 12:54:16 +00001799
Victor Stinner8c62be82010-05-06 00:08:46 +00001800 type = GetFileType(h);
1801 if (type == FILE_TYPE_UNKNOWN) {
1802 DWORD error = GetLastError();
1803 if (error != 0) {
Brian Curtinf5e76d02010-11-24 13:14:05 +00001804 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00001805 }
1806 /* else: valid but unknown file */
1807 }
Martin v. Löwis14694662006-02-03 12:54:16 +00001808
Victor Stinner8c62be82010-05-06 00:08:46 +00001809 if (type != FILE_TYPE_DISK) {
1810 if (type == FILE_TYPE_CHAR)
1811 result->st_mode = _S_IFCHR;
1812 else if (type == FILE_TYPE_PIPE)
1813 result->st_mode = _S_IFIFO;
1814 return 0;
1815 }
1816
1817 if (!GetFileInformationByHandle(h, &info)) {
1818 return -1;
1819 }
1820
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001821 attribute_data_to_stat(&info, 0, result);
Victor Stinner8c62be82010-05-06 00:08:46 +00001822 /* specific to fstat() */
Victor Stinner8c62be82010-05-06 00:08:46 +00001823 result->st_ino = (((__int64)info.nFileIndexHigh)<<32) + info.nFileIndexLow;
1824 return 0;
Martin v. Löwis14694662006-02-03 12:54:16 +00001825}
1826
1827#endif /* MS_WINDOWS */
1828
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001829PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001830"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001831This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001832 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001833or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1834\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001835Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1836or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001837\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001838See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001839
1840static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001841 {"st_mode", "protection bits"},
1842 {"st_ino", "inode"},
1843 {"st_dev", "device"},
1844 {"st_nlink", "number of hard links"},
1845 {"st_uid", "user ID of owner"},
1846 {"st_gid", "group ID of owner"},
1847 {"st_size", "total size, in bytes"},
1848 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1849 {NULL, "integer time of last access"},
1850 {NULL, "integer time of last modification"},
1851 {NULL, "integer time of last change"},
1852 {"st_atime", "time of last access"},
1853 {"st_mtime", "time of last modification"},
1854 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001855 {"st_atime_ns", "time of last access in nanoseconds"},
1856 {"st_mtime_ns", "time of last modification in nanoseconds"},
1857 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001858#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001859 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001860#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001861#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001862 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001863#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001864#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001865 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001866#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001867#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001868 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001869#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001870#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001871 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001872#endif
1873#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001874 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001875#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001876 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001877};
1878
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001879#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001880#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001881#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001882#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001883#endif
1884
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001885#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001886#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1887#else
1888#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1889#endif
1890
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001891#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001892#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1893#else
1894#define ST_RDEV_IDX ST_BLOCKS_IDX
1895#endif
1896
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001897#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1898#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1899#else
1900#define ST_FLAGS_IDX ST_RDEV_IDX
1901#endif
1902
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001903#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001904#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001905#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001906#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001907#endif
1908
1909#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1910#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1911#else
1912#define ST_BIRTHTIME_IDX ST_GEN_IDX
1913#endif
1914
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001915static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001916 "stat_result", /* name */
1917 stat_result__doc__, /* doc */
1918 stat_result_fields,
1919 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001920};
1921
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001922PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001923"statvfs_result: Result from statvfs or fstatvfs.\n\n\
1924This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001925 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00001926or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001927\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001928See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001929
1930static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001931 {"f_bsize", },
1932 {"f_frsize", },
1933 {"f_blocks", },
1934 {"f_bfree", },
1935 {"f_bavail", },
1936 {"f_files", },
1937 {"f_ffree", },
1938 {"f_favail", },
1939 {"f_flag", },
1940 {"f_namemax",},
1941 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942};
1943
1944static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001945 "statvfs_result", /* name */
1946 statvfs_result__doc__, /* doc */
1947 statvfs_result_fields,
1948 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001949};
1950
Ross Lagerwall7807c352011-03-17 20:20:30 +02001951#if defined(HAVE_WAITID) && !defined(__APPLE__)
1952PyDoc_STRVAR(waitid_result__doc__,
1953"waitid_result: Result from waitid.\n\n\
1954This object may be accessed either as a tuple of\n\
1955 (si_pid, si_uid, si_signo, si_status, si_code),\n\
1956or via the attributes si_pid, si_uid, and so on.\n\
1957\n\
1958See os.waitid for more information.");
1959
1960static PyStructSequence_Field waitid_result_fields[] = {
1961 {"si_pid", },
1962 {"si_uid", },
1963 {"si_signo", },
1964 {"si_status", },
1965 {"si_code", },
1966 {0}
1967};
1968
1969static PyStructSequence_Desc waitid_result_desc = {
1970 "waitid_result", /* name */
1971 waitid_result__doc__, /* doc */
1972 waitid_result_fields,
1973 5
1974};
1975static PyTypeObject WaitidResultType;
1976#endif
1977
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001978static int initialized;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001979static PyTypeObject StatResultType;
1980static PyTypeObject StatVFSResultType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001981#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05001982static PyTypeObject SchedParamType;
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05001983#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001984static newfunc structseq_new;
1985
1986static PyObject *
1987statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1988{
Victor Stinner8c62be82010-05-06 00:08:46 +00001989 PyStructSequence *result;
1990 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00001991
Victor Stinner8c62be82010-05-06 00:08:46 +00001992 result = (PyStructSequence*)structseq_new(type, args, kwds);
1993 if (!result)
1994 return NULL;
1995 /* If we have been initialized from a tuple,
1996 st_?time might be set to None. Initialize it
1997 from the int slots. */
1998 for (i = 7; i <= 9; i++) {
1999 if (result->ob_item[i+3] == Py_None) {
2000 Py_DECREF(Py_None);
2001 Py_INCREF(result->ob_item[i]);
2002 result->ob_item[i+3] = result->ob_item[i];
2003 }
2004 }
2005 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002006}
2007
2008
2009
2010/* If true, st_?time is float. */
Martin v. Löwisfe33d0b2005-01-16 08:57:39 +00002011static int _stat_float_times = 1;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002012
2013PyDoc_STRVAR(stat_float_times__doc__,
2014"stat_float_times([newval]) -> oldval\n\n\
2015Determine whether os.[lf]stat represents time stamps as float objects.\n\
2016If newval is True, future calls to stat() return floats, if it is False,\n\
2017future calls return ints. \n\
2018If newval is omitted, return the current setting.\n");
2019
2020static PyObject*
2021stat_float_times(PyObject* self, PyObject *args)
2022{
Victor Stinner8c62be82010-05-06 00:08:46 +00002023 int newval = -1;
2024 if (!PyArg_ParseTuple(args, "|i:stat_float_times", &newval))
2025 return NULL;
Victor Stinner034d0aa2012-06-05 01:22:15 +02002026 if (PyErr_WarnEx(PyExc_DeprecationWarning,
2027 "stat_float_times() is deprecated",
2028 1))
2029 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002030 if (newval == -1)
2031 /* Return old value */
2032 return PyBool_FromLong(_stat_float_times);
2033 _stat_float_times = newval;
2034 Py_INCREF(Py_None);
2035 return Py_None;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002036}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002037
Larry Hastings6fe20b32012-04-19 15:07:49 -07002038static PyObject *billion = NULL;
2039
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002040static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002041fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002042{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002043 PyObject *s = _PyLong_FromTime_t(sec);
2044 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2045 PyObject *s_in_ns = NULL;
2046 PyObject *ns_total = NULL;
2047 PyObject *float_s = NULL;
2048
2049 if (!(s && ns_fractional))
2050 goto exit;
2051
2052 s_in_ns = PyNumber_Multiply(s, billion);
2053 if (!s_in_ns)
2054 goto exit;
2055
2056 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2057 if (!ns_total)
2058 goto exit;
2059
Victor Stinner4195b5c2012-02-08 23:03:19 +01002060 if (_stat_float_times) {
Larry Hastings6fe20b32012-04-19 15:07:49 -07002061 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2062 if (!float_s)
2063 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00002064 }
Larry Hastings6fe20b32012-04-19 15:07:49 -07002065 else {
2066 float_s = s;
2067 Py_INCREF(float_s);
2068 }
2069
2070 PyStructSequence_SET_ITEM(v, index, s);
2071 PyStructSequence_SET_ITEM(v, index+3, float_s);
2072 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2073 s = NULL;
2074 float_s = NULL;
2075 ns_total = NULL;
2076exit:
2077 Py_XDECREF(s);
2078 Py_XDECREF(ns_fractional);
2079 Py_XDECREF(s_in_ns);
2080 Py_XDECREF(ns_total);
2081 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002082}
2083
Tim Peters5aa91602002-01-30 05:46:57 +00002084/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002085 (used by posix_stat() and posix_fstat()) */
2086static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002087_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002088{
Victor Stinner8c62be82010-05-06 00:08:46 +00002089 unsigned long ansec, mnsec, cnsec;
2090 PyObject *v = PyStructSequence_New(&StatResultType);
2091 if (v == NULL)
2092 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002093
Victor Stinner8c62be82010-05-06 00:08:46 +00002094 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Fred Drake699f3522000-06-29 21:12:41 +00002095#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002096 PyStructSequence_SET_ITEM(v, 1,
2097 PyLong_FromLongLong((PY_LONG_LONG)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002098#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002099 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long)st->st_ino));
Fred Drake699f3522000-06-29 21:12:41 +00002100#endif
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002101#ifdef MS_WINDOWS
2102 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
2103#elif defined(HAVE_LONG_LONG)
Victor Stinner8c62be82010-05-06 00:08:46 +00002104 PyStructSequence_SET_ITEM(v, 2,
2105 PyLong_FromLongLong((PY_LONG_LONG)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002106#else
Brian Curtin9cc43212013-01-01 12:31:06 -06002107 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long)st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002108#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002109 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002110#if defined(MS_WINDOWS)
2111 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2112 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2113#else
2114 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2115 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2116#endif
Fred Drake699f3522000-06-29 21:12:41 +00002117#ifdef HAVE_LARGEFILE_SUPPORT
Victor Stinner8c62be82010-05-06 00:08:46 +00002118 PyStructSequence_SET_ITEM(v, 6,
2119 PyLong_FromLongLong((PY_LONG_LONG)st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002120#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002121 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong(st->st_size));
Fred Drake699f3522000-06-29 21:12:41 +00002122#endif
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002123
Martin v. Löwis14694662006-02-03 12:54:16 +00002124#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002125 ansec = st->st_atim.tv_nsec;
2126 mnsec = st->st_mtim.tv_nsec;
2127 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002128#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002129 ansec = st->st_atimespec.tv_nsec;
2130 mnsec = st->st_mtimespec.tv_nsec;
2131 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002132#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002133 ansec = st->st_atime_nsec;
2134 mnsec = st->st_mtime_nsec;
2135 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002136#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002137 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002138#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002139 fill_time(v, 7, st->st_atime, ansec);
2140 fill_time(v, 8, st->st_mtime, mnsec);
2141 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002142
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002143#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002144 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2145 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002146#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002147#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002148 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2149 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002150#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002151#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002152 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2153 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002154#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002155#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002156 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2157 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002158#endif
2159#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002160 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002161 PyObject *val;
2162 unsigned long bsec,bnsec;
2163 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002164#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002165 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002166#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002167 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002168#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002169 if (_stat_float_times) {
2170 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
2171 } else {
2172 val = PyLong_FromLong((long)bsec);
2173 }
2174 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2175 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002176 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002177#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002178#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002179 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2180 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002181#endif
Fred Drake699f3522000-06-29 21:12:41 +00002182
Victor Stinner8c62be82010-05-06 00:08:46 +00002183 if (PyErr_Occurred()) {
2184 Py_DECREF(v);
2185 return NULL;
2186 }
Fred Drake699f3522000-06-29 21:12:41 +00002187
Victor Stinner8c62be82010-05-06 00:08:46 +00002188 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002189}
2190
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002191/* POSIX methods */
2192
Guido van Rossum94f6f721999-01-06 18:42:14 +00002193
2194static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002195posix_do_stat(char *function_name, path_t *path,
2196 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002197{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002198 STRUCT_STAT st;
2199 int result;
2200
2201#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2202 if (follow_symlinks_specified(function_name, follow_symlinks))
2203 return NULL;
2204#endif
2205
2206 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2207 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2208 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2209 return NULL;
2210
2211 Py_BEGIN_ALLOW_THREADS
2212 if (path->fd != -1)
2213 result = FSTAT(path->fd, &st);
2214 else
2215#ifdef MS_WINDOWS
2216 if (path->wide) {
2217 if (follow_symlinks)
2218 result = win32_stat_w(path->wide, &st);
2219 else
2220 result = win32_lstat_w(path->wide, &st);
2221 }
2222 else
2223#endif
2224#if defined(HAVE_LSTAT) || defined(MS_WINDOWS)
2225 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2226 result = LSTAT(path->narrow, &st);
2227 else
2228#endif
2229#ifdef HAVE_FSTATAT
2230 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2231 result = fstatat(dir_fd, path->narrow, &st,
2232 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2233 else
2234#endif
2235 result = STAT(path->narrow, &st);
2236 Py_END_ALLOW_THREADS
2237
Victor Stinner292c8352012-10-30 02:17:38 +01002238 if (result != 0) {
2239 return path_error(path);
2240 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002241
2242 return _pystat_fromstructstat(&st);
2243}
2244
2245PyDoc_STRVAR(posix_stat__doc__,
2246"stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result\n\n\
2247Perform a stat system call on the given path.\n\
2248\n\
2249path may be specified as either a string or as an open file descriptor.\n\
2250\n\
2251If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2252 and path should be relative; path will then be relative to that directory.\n\
2253 dir_fd may not be supported on your platform; if it is unavailable, using\n\
2254 it will raise a NotImplementedError.\n\
2255If follow_symlinks is False, and the last element of the path is a symbolic\n\
2256 link, stat will examine the symbolic link itself instead of the file the\n\
2257 link points to.\n\
2258It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2259 an open file descriptor.");
2260
2261static PyObject *
2262posix_stat(PyObject *self, PyObject *args, PyObject *kwargs)
2263{
2264 static char *keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
2265 path_t path;
2266 int dir_fd = DEFAULT_DIR_FD;
2267 int follow_symlinks = 1;
2268 PyObject *return_value;
2269
2270 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002271 path.function_name = "stat";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002272 path.allow_fd = 1;
2273 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&p:stat", keywords,
2274 path_converter, &path,
2275#ifdef HAVE_FSTATAT
2276 dir_fd_converter, &dir_fd,
2277#else
2278 dir_fd_unavailable, &dir_fd,
2279#endif
2280 &follow_symlinks))
2281 return NULL;
2282 return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks);
2283 path_cleanup(&path);
2284 return return_value;
2285}
2286
2287PyDoc_STRVAR(posix_lstat__doc__,
2288"lstat(path, *, dir_fd=None) -> stat result\n\n\
2289Like stat(), but do not follow symbolic links.\n\
2290Equivalent to stat(path, follow_symlinks=False).");
2291
2292static PyObject *
2293posix_lstat(PyObject *self, PyObject *args, PyObject *kwargs)
2294{
2295 static char *keywords[] = {"path", "dir_fd", NULL};
2296 path_t path;
2297 int dir_fd = DEFAULT_DIR_FD;
2298 int follow_symlinks = 0;
2299 PyObject *return_value;
2300
2301 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002302 path.function_name = "lstat";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002303 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:lstat", keywords,
2304 path_converter, &path,
2305#ifdef HAVE_FSTATAT
2306 dir_fd_converter, &dir_fd
2307#else
2308 dir_fd_unavailable, &dir_fd
2309#endif
2310 ))
2311 return NULL;
2312 return_value = posix_do_stat("stat", &path, dir_fd, follow_symlinks);
2313 path_cleanup(&path);
2314 return return_value;
2315}
2316
2317PyDoc_STRVAR(posix_access__doc__,
2318"access(path, mode, *, dir_fd=None, effective_ids=False,\
2319 follow_symlinks=True)\n\n\
2320Use the real uid/gid to test for access to a path. Returns True if granted,\n\
2321False otherwise.\n\
2322\n\
2323If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2324 and path should be relative; path will then be relative to that directory.\n\
2325If effective_ids is True, access will use the effective uid/gid instead of\n\
2326 the real uid/gid.\n\
2327If follow_symlinks is False, and the last element of the path is a symbolic\n\
2328 link, access will examine the symbolic link itself instead of the file the\n\
2329 link points to.\n\
2330dir_fd, effective_ids, and follow_symlinks may not be implemented\n\
2331 on your platform. If they are unavailable, using them will raise a\n\
2332 NotImplementedError.\n\
2333\n\
2334Note that most operations will use the effective uid/gid, therefore this\n\
2335 routine can be used in a suid/sgid environment to test if the invoking user\n\
2336 has the specified access to the path.\n\
2337The mode argument can be F_OK to test existence, or the inclusive-OR\n\
2338 of R_OK, W_OK, and X_OK.");
2339
2340static PyObject *
2341posix_access(PyObject *self, PyObject *args, PyObject *kwargs)
2342{
2343 static char *keywords[] = {"path", "mode", "dir_fd", "effective_ids",
2344 "follow_symlinks", NULL};
2345 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002346 int mode;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002347 int dir_fd = DEFAULT_DIR_FD;
2348 int effective_ids = 0;
2349 int follow_symlinks = 1;
2350 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002351
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002352#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002353 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002354#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002355 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002356#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002357
2358 memset(&path, 0, sizeof(path));
2359 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&pp:access", keywords,
2360 path_converter, &path, &mode,
2361#ifdef HAVE_FACCESSAT
2362 dir_fd_converter, &dir_fd,
2363#else
2364 dir_fd_unavailable, &dir_fd,
2365#endif
2366 &effective_ids, &follow_symlinks))
2367 return NULL;
2368
2369#ifndef HAVE_FACCESSAT
2370 if (follow_symlinks_specified("access", follow_symlinks))
2371 goto exit;
2372
2373 if (effective_ids) {
2374 argument_unavailable_error("access", "effective_ids");
2375 goto exit;
2376 }
2377#endif
2378
2379#ifdef MS_WINDOWS
2380 Py_BEGIN_ALLOW_THREADS
2381 if (path.wide != NULL)
2382 attr = GetFileAttributesW(path.wide);
2383 else
2384 attr = GetFileAttributesA(path.narrow);
2385 Py_END_ALLOW_THREADS
2386
2387 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002388 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002389 * * we didn't get a -1, and
2390 * * write access wasn't requested,
2391 * * or the file isn't read-only,
2392 * * or it's a directory.
2393 * (Directories cannot be read-only on Windows.)
2394 */
2395 return_value = PyBool_FromLong(
2396 (attr != 0xFFFFFFFF) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002397 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002398 !(attr & FILE_ATTRIBUTE_READONLY) ||
2399 (attr & FILE_ATTRIBUTE_DIRECTORY)));
2400#else
2401
2402 Py_BEGIN_ALLOW_THREADS
2403#ifdef HAVE_FACCESSAT
2404 if ((dir_fd != DEFAULT_DIR_FD) ||
2405 effective_ids ||
2406 !follow_symlinks) {
2407 int flags = 0;
2408 if (!follow_symlinks)
2409 flags |= AT_SYMLINK_NOFOLLOW;
2410 if (effective_ids)
2411 flags |= AT_EACCESS;
2412 result = faccessat(dir_fd, path.narrow, mode, flags);
2413 }
2414 else
2415#endif
2416 result = access(path.narrow, mode);
2417 Py_END_ALLOW_THREADS
2418 return_value = PyBool_FromLong(!result);
2419#endif
2420
2421#ifndef HAVE_FACCESSAT
2422exit:
2423#endif
2424 path_cleanup(&path);
2425 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002426}
2427
Guido van Rossumd371ff11999-01-25 16:12:23 +00002428#ifndef F_OK
2429#define F_OK 0
2430#endif
2431#ifndef R_OK
2432#define R_OK 4
2433#endif
2434#ifndef W_OK
2435#define W_OK 2
2436#endif
2437#ifndef X_OK
2438#define X_OK 1
2439#endif
2440
2441#ifdef HAVE_TTYNAME
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002442PyDoc_STRVAR(posix_ttyname__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002443"ttyname(fd) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002444Return the name of the terminal device connected to 'fd'.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00002445
2446static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00002447posix_ttyname(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002448{
Victor Stinner8c62be82010-05-06 00:08:46 +00002449 int id;
2450 char *ret;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002451
Victor Stinner8c62be82010-05-06 00:08:46 +00002452 if (!PyArg_ParseTuple(args, "i:ttyname", &id))
2453 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002454
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002455#if defined(__VMS)
Victor Stinner8c62be82010-05-06 00:08:46 +00002456 /* file descriptor 0 only, the default input device (stdin) */
2457 if (id == 0) {
2458 ret = ttyname();
2459 }
2460 else {
2461 ret = NULL;
2462 }
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002463#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002464 ret = ttyname(id);
Martin v. Löwis79acb9e2002-12-06 12:48:53 +00002465#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002466 if (ret == NULL)
2467 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002468 return PyUnicode_DecodeFSDefault(ret);
Guido van Rossum94f6f721999-01-06 18:42:14 +00002469}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002470#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002471
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002472#ifdef HAVE_CTERMID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002473PyDoc_STRVAR(posix_ctermid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002474"ctermid() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002475Return the name of the controlling terminal for this process.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002476
2477static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00002478posix_ctermid(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002479{
Victor Stinner8c62be82010-05-06 00:08:46 +00002480 char *ret;
2481 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002482
Greg Wardb48bc172000-03-01 21:51:56 +00002483#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002484 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002485#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002486 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002487#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002488 if (ret == NULL)
2489 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002490 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002491}
2492#endif
2493
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002494PyDoc_STRVAR(posix_chdir__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002495"chdir(path)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002496Change the current working directory to the specified path.\n\
2497\n\
2498path may always be specified as a string.\n\
2499On some platforms, path may also be specified as an open file descriptor.\n\
2500 If this functionality is unavailable, using it raises an exception.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002501
Barry Warsaw53699e91996-12-10 23:23:01 +00002502static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002503posix_chdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002504{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002505 path_t path;
2506 int result;
2507 PyObject *return_value = NULL;
2508 static char *keywords[] = {"path", NULL};
2509
2510 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002511 path.function_name = "chdir";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002512#ifdef HAVE_FCHDIR
2513 path.allow_fd = 1;
Andrew MacIntyre6c73af22002-03-03 03:07:07 +00002514#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002515 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:chdir", keywords,
2516 path_converter, &path
2517 ))
2518 return NULL;
2519
2520 Py_BEGIN_ALLOW_THREADS
2521#ifdef MS_WINDOWS
2522 if (path.wide)
2523 result = win32_wchdir(path.wide);
2524 else
2525 result = win32_chdir(path.narrow);
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03002526 result = !result; /* on unix, success = 0, on windows, success = !0 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002527#else
2528#ifdef HAVE_FCHDIR
2529 if (path.fd != -1)
2530 result = fchdir(path.fd);
2531 else
2532#endif
2533 result = chdir(path.narrow);
2534#endif
2535 Py_END_ALLOW_THREADS
2536
2537 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01002538 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002539 goto exit;
2540 }
2541
2542 return_value = Py_None;
2543 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02002544
Larry Hastings9cf065c2012-06-22 16:30:09 -07002545exit:
2546 path_cleanup(&path);
2547 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002548}
2549
Fred Drake4d1e64b2002-04-15 19:40:07 +00002550#ifdef HAVE_FCHDIR
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002551PyDoc_STRVAR(posix_fchdir__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002552"fchdir(fd)\n\n\
2553Change to the directory of the given file descriptor. fd must be\n\
2554opened on a directory, not a file. Equivalent to os.chdir(fd).");
Fred Drake4d1e64b2002-04-15 19:40:07 +00002555
2556static PyObject *
2557posix_fchdir(PyObject *self, PyObject *fdobj)
2558{
Victor Stinner8c62be82010-05-06 00:08:46 +00002559 return posix_fildes(fdobj, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002560}
2561#endif /* HAVE_FCHDIR */
2562
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002563
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002564PyDoc_STRVAR(posix_chmod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002565"chmod(path, mode, *, dir_fd=None, follow_symlinks=True)\n\n\
2566Change the access permissions of a file.\n\
2567\n\
2568path may always be specified as a string.\n\
2569On some platforms, path may also be specified as an open file descriptor.\n\
2570 If this functionality is unavailable, using it raises an exception.\n\
2571If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2572 and path should be relative; path will then be relative to that directory.\n\
2573If follow_symlinks is False, and the last element of the path is a symbolic\n\
2574 link, chmod will modify the symbolic link itself instead of the file the\n\
2575 link points to.\n\
2576It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2577 an open file descriptor.\n\
2578dir_fd and follow_symlinks may not be implemented on your platform.\n\
2579 If they are unavailable, using them will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002580
Barry Warsaw53699e91996-12-10 23:23:01 +00002581static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002582posix_chmod(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002583{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002584 path_t path;
2585 int mode;
2586 int dir_fd = DEFAULT_DIR_FD;
2587 int follow_symlinks = 1;
2588 int result;
2589 PyObject *return_value = NULL;
2590 static char *keywords[] = {"path", "mode", "dir_fd",
2591 "follow_symlinks", NULL};
2592
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002593#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002594 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002595#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00002596
Larry Hastings9cf065c2012-06-22 16:30:09 -07002597#ifdef HAVE_FCHMODAT
2598 int fchmodat_nofollow_unsupported = 0;
2599#endif
2600
2601 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002602 path.function_name = "chmod";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002603#ifdef HAVE_FCHMOD
2604 path.allow_fd = 1;
2605#endif
2606 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|$O&p:chmod", keywords,
2607 path_converter, &path,
2608 &mode,
2609#ifdef HAVE_FCHMODAT
2610 dir_fd_converter, &dir_fd,
2611#else
2612 dir_fd_unavailable, &dir_fd,
2613#endif
2614 &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002615 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002616
2617#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
2618 if (follow_symlinks_specified("chmod", follow_symlinks))
2619 goto exit;
2620#endif
2621
2622#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002623 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002624 if (path.wide)
2625 attr = GetFileAttributesW(path.wide);
2626 else
2627 attr = GetFileAttributesA(path.narrow);
2628 if (attr == 0xFFFFFFFF)
2629 result = 0;
2630 else {
2631 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00002632 attr &= ~FILE_ATTRIBUTE_READONLY;
2633 else
2634 attr |= FILE_ATTRIBUTE_READONLY;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002635 if (path.wide)
2636 result = SetFileAttributesW(path.wide, attr);
2637 else
2638 result = SetFileAttributesA(path.narrow, attr);
2639 }
2640 Py_END_ALLOW_THREADS
2641
2642 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01002643 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002644 goto exit;
2645 }
2646#else /* MS_WINDOWS */
2647 Py_BEGIN_ALLOW_THREADS
2648#ifdef HAVE_FCHMOD
2649 if (path.fd != -1)
2650 result = fchmod(path.fd, mode);
2651 else
2652#endif
2653#ifdef HAVE_LCHMOD
2654 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2655 result = lchmod(path.narrow, mode);
2656 else
2657#endif
2658#ifdef HAVE_FCHMODAT
2659 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2660 /*
2661 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
2662 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002663 * and then says it isn't implemented yet.
2664 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002665 *
2666 * Once it is supported, os.chmod will automatically
2667 * support dir_fd and follow_symlinks=False. (Hopefully.)
2668 * Until then, we need to be careful what exception we raise.
2669 */
2670 result = fchmodat(dir_fd, path.narrow, mode,
2671 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2672 /*
2673 * But wait! We can't throw the exception without allowing threads,
2674 * and we can't do that in this nested scope. (Macro trickery, sigh.)
2675 */
2676 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07002677 result &&
2678 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
2679 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00002680 }
2681 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00002682#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002683 result = chmod(path.narrow, mode);
2684 Py_END_ALLOW_THREADS
2685
2686 if (result) {
2687#ifdef HAVE_FCHMODAT
2688 if (fchmodat_nofollow_unsupported) {
2689 if (dir_fd != DEFAULT_DIR_FD)
2690 dir_fd_and_follow_symlinks_invalid("chmod",
2691 dir_fd, follow_symlinks);
2692 else
2693 follow_symlinks_specified("chmod", follow_symlinks);
2694 }
2695 else
2696#endif
Victor Stinner292c8352012-10-30 02:17:38 +01002697 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002698 goto exit;
2699 }
2700#endif
2701
2702 Py_INCREF(Py_None);
2703 return_value = Py_None;
2704exit:
2705 path_cleanup(&path);
2706 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002707}
2708
Larry Hastings9cf065c2012-06-22 16:30:09 -07002709
Christian Heimes4e30a842007-11-30 22:12:06 +00002710#ifdef HAVE_FCHMOD
2711PyDoc_STRVAR(posix_fchmod__doc__,
2712"fchmod(fd, mode)\n\n\
2713Change the access permissions of the file given by file\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002714descriptor fd. Equivalent to os.chmod(fd, mode).");
Christian Heimes4e30a842007-11-30 22:12:06 +00002715
2716static PyObject *
2717posix_fchmod(PyObject *self, PyObject *args)
2718{
Victor Stinner8c62be82010-05-06 00:08:46 +00002719 int fd, mode, res;
2720 if (!PyArg_ParseTuple(args, "ii:fchmod", &fd, &mode))
2721 return NULL;
2722 Py_BEGIN_ALLOW_THREADS
2723 res = fchmod(fd, mode);
2724 Py_END_ALLOW_THREADS
2725 if (res < 0)
2726 return posix_error();
2727 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002728}
2729#endif /* HAVE_FCHMOD */
2730
2731#ifdef HAVE_LCHMOD
2732PyDoc_STRVAR(posix_lchmod__doc__,
2733"lchmod(path, mode)\n\n\
2734Change the access permissions of a file. If path is a symlink, this\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002735affects the link itself rather than the target.\n\
2736Equivalent to chmod(path, mode, follow_symlinks=False).");
Christian Heimes4e30a842007-11-30 22:12:06 +00002737
2738static PyObject *
2739posix_lchmod(PyObject *self, PyObject *args)
2740{
Victor Stinner292c8352012-10-30 02:17:38 +01002741 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002742 int i;
2743 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01002744 memset(&path, 0, sizeof(path));
2745 path.function_name = "lchmod";
2746 if (!PyArg_ParseTuple(args, "O&i:lchmod",
2747 path_converter, &path, &i))
Victor Stinner8c62be82010-05-06 00:08:46 +00002748 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002749 Py_BEGIN_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002750 res = lchmod(path.narrow, i);
Victor Stinner8c62be82010-05-06 00:08:46 +00002751 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002752 if (res < 0) {
2753 path_error(&path);
2754 path_cleanup(&path);
2755 return NULL;
2756 }
2757 path_cleanup(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00002758 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00002759}
2760#endif /* HAVE_LCHMOD */
2761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002762
Thomas Wouterscf297e42007-02-23 15:07:44 +00002763#ifdef HAVE_CHFLAGS
2764PyDoc_STRVAR(posix_chflags__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002765"chflags(path, flags, *, follow_symlinks=True)\n\n\
2766Set file flags.\n\
2767\n\
2768If follow_symlinks is False, and the last element of the path is a symbolic\n\
2769 link, chflags will change flags on the symbolic link itself instead of the\n\
2770 file the link points to.\n\
2771follow_symlinks may not be implemented on your platform. If it is\n\
2772unavailable, using it will raise a NotImplementedError.");
Thomas Wouterscf297e42007-02-23 15:07:44 +00002773
2774static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002775posix_chflags(PyObject *self, PyObject *args, PyObject *kwargs)
Thomas Wouterscf297e42007-02-23 15:07:44 +00002776{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002777 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002778 unsigned long flags;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002779 int follow_symlinks = 1;
2780 int result;
2781 PyObject *return_value;
2782 static char *keywords[] = {"path", "flags", "follow_symlinks", NULL};
2783
2784 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002785 path.function_name = "chflags";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002786 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&k|$i:chflags", keywords,
2787 path_converter, &path,
2788 &flags, &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002789 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002790
2791#ifndef HAVE_LCHFLAGS
2792 if (follow_symlinks_specified("chflags", follow_symlinks))
2793 goto exit;
2794#endif
2795
Victor Stinner8c62be82010-05-06 00:08:46 +00002796 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002797#ifdef HAVE_LCHFLAGS
2798 if (!follow_symlinks)
2799 result = lchflags(path.narrow, flags);
2800 else
2801#endif
2802 result = chflags(path.narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002803 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002804
2805 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01002806 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002807 goto exit;
2808 }
2809
2810 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00002811 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002812
2813exit:
2814 path_cleanup(&path);
2815 return return_value;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002816}
2817#endif /* HAVE_CHFLAGS */
2818
2819#ifdef HAVE_LCHFLAGS
2820PyDoc_STRVAR(posix_lchflags__doc__,
2821"lchflags(path, flags)\n\n\
2822Set file flags.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07002823This function will not follow symbolic links.\n\
2824Equivalent to chflags(path, flags, follow_symlinks=False).");
Thomas Wouterscf297e42007-02-23 15:07:44 +00002825
2826static PyObject *
2827posix_lchflags(PyObject *self, PyObject *args)
2828{
Victor Stinner292c8352012-10-30 02:17:38 +01002829 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00002830 unsigned long flags;
2831 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01002832 memset(&path, 0, sizeof(path));
2833 path.function_name = "lchflags";
Victor Stinner8c62be82010-05-06 00:08:46 +00002834 if (!PyArg_ParseTuple(args, "O&k:lchflags",
Victor Stinner292c8352012-10-30 02:17:38 +01002835 path_converter, &path, &flags))
Victor Stinner8c62be82010-05-06 00:08:46 +00002836 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00002837 Py_BEGIN_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002838 res = lchflags(path.narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00002839 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01002840 if (res < 0) {
2841 path_error(&path);
2842 path_cleanup(&path);
2843 return NULL;
2844 }
2845 path_cleanup(&path);
2846 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00002847}
2848#endif /* HAVE_LCHFLAGS */
2849
Martin v. Löwis244edc82001-10-04 22:44:26 +00002850#ifdef HAVE_CHROOT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002851PyDoc_STRVAR(posix_chroot__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002852"chroot(path)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002853Change root directory to path.");
Martin v. Löwis244edc82001-10-04 22:44:26 +00002854
2855static PyObject *
2856posix_chroot(PyObject *self, PyObject *args)
2857{
Victor Stinner292c8352012-10-30 02:17:38 +01002858 return posix_1str("chroot", args, "O&:chroot", chroot);
Martin v. Löwis244edc82001-10-04 22:44:26 +00002859}
2860#endif
2861
Guido van Rossum21142a01999-01-08 21:05:37 +00002862#ifdef HAVE_FSYNC
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002863PyDoc_STRVAR(posix_fsync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002864"fsync(fildes)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002865force write of file with filedescriptor to disk.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002866
2867static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002868posix_fsync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002869{
Stefan Krah0e803b32010-11-26 16:16:47 +00002870 return posix_fildes(fdobj, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002871}
2872#endif /* HAVE_FSYNC */
2873
Ross Lagerwall7807c352011-03-17 20:20:30 +02002874#ifdef HAVE_SYNC
2875PyDoc_STRVAR(posix_sync__doc__,
2876"sync()\n\n\
2877Force write of everything to disk.");
2878
2879static PyObject *
2880posix_sync(PyObject *self, PyObject *noargs)
2881{
2882 Py_BEGIN_ALLOW_THREADS
2883 sync();
2884 Py_END_ALLOW_THREADS
2885 Py_RETURN_NONE;
2886}
2887#endif
2888
Guido van Rossum21142a01999-01-08 21:05:37 +00002889#ifdef HAVE_FDATASYNC
Guido van Rossumecc23b02000-09-22 16:01:05 +00002890
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00002891#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00002892extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
2893#endif
2894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002895PyDoc_STRVAR(posix_fdatasync__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00002896"fdatasync(fildes)\n\n\
Guido van Rossum21142a01999-01-08 21:05:37 +00002897force write of file with filedescriptor to disk.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002898 does not force update of metadata.");
Guido van Rossum21142a01999-01-08 21:05:37 +00002899
2900static PyObject *
Fred Drake4d1e64b2002-04-15 19:40:07 +00002901posix_fdatasync(PyObject *self, PyObject *fdobj)
Guido van Rossum21142a01999-01-08 21:05:37 +00002902{
Stefan Krah0e803b32010-11-26 16:16:47 +00002903 return posix_fildes(fdobj, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00002904}
2905#endif /* HAVE_FDATASYNC */
2906
2907
Fredrik Lundh10723342000-07-10 16:38:09 +00002908#ifdef HAVE_CHOWN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002909PyDoc_STRVAR(posix_chown__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002910"chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)\n\n\
2911Change the owner and group id of path to the numeric uid and gid.\n\
2912\n\
2913path may always be specified as a string.\n\
2914On some platforms, path may also be specified as an open file descriptor.\n\
2915 If this functionality is unavailable, using it raises an exception.\n\
2916If dir_fd is not None, it should be a file descriptor open to a directory,\n\
2917 and path should be relative; path will then be relative to that directory.\n\
2918If follow_symlinks is False, and the last element of the path is a symbolic\n\
2919 link, chown will modify the symbolic link itself instead of the file the\n\
2920 link points to.\n\
2921It is an error to use dir_fd or follow_symlinks when specifying path as\n\
2922 an open file descriptor.\n\
2923dir_fd and follow_symlinks may not be implemented on your platform.\n\
2924 If they are unavailable, using them will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002925
Barry Warsaw53699e91996-12-10 23:23:01 +00002926static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07002927posix_chown(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumb6775db1994-08-01 11:34:53 +00002928{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002929 path_t path;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930 uid_t uid;
2931 gid_t gid;
2932 int dir_fd = DEFAULT_DIR_FD;
2933 int follow_symlinks = 1;
2934 int result;
2935 PyObject *return_value = NULL;
2936 static char *keywords[] = {"path", "uid", "gid", "dir_fd",
2937 "follow_symlinks", NULL};
2938
2939 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01002940 path.function_name = "chown";
Larry Hastings9cf065c2012-06-22 16:30:09 -07002941#ifdef HAVE_FCHOWN
2942 path.allow_fd = 1;
2943#endif
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002944 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&O&|$O&p:chown", keywords,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002945 path_converter, &path,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002946 _Py_Uid_Converter, &uid,
2947 _Py_Gid_Converter, &gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002948#ifdef HAVE_FCHOWNAT
2949 dir_fd_converter, &dir_fd,
2950#else
2951 dir_fd_unavailable, &dir_fd,
2952#endif
2953 &follow_symlinks))
Victor Stinner8c62be82010-05-06 00:08:46 +00002954 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002955
2956#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
2957 if (follow_symlinks_specified("chown", follow_symlinks))
2958 goto exit;
2959#endif
2960 if (dir_fd_and_fd_invalid("chown", dir_fd, path.fd) ||
2961 fd_and_follow_symlinks_invalid("chown", path.fd, follow_symlinks))
2962 goto exit;
2963
2964#ifdef __APPLE__
2965 /*
2966 * This is for Mac OS X 10.3, which doesn't have lchown.
2967 * (But we still have an lchown symbol because of weak-linking.)
2968 * It doesn't have fchownat either. So there's no possibility
2969 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02002970 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002971 if ((!follow_symlinks) && (lchown == NULL)) {
2972 follow_symlinks_specified("chown", follow_symlinks);
2973 goto exit;
2974 }
2975#endif
2976
Victor Stinner8c62be82010-05-06 00:08:46 +00002977 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002978#ifdef HAVE_FCHOWN
2979 if (path.fd != -1)
2980 result = fchown(path.fd, uid, gid);
2981 else
2982#endif
2983#ifdef HAVE_LCHOWN
2984 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2985 result = lchown(path.narrow, uid, gid);
2986 else
2987#endif
2988#ifdef HAVE_FCHOWNAT
2989 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
2990 result = fchownat(dir_fd, path.narrow, uid, gid,
2991 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2992 else
2993#endif
2994 result = chown(path.narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00002995 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07002996
2997 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01002998 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002999 goto exit;
3000 }
3001
3002 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00003003 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003004
3005exit:
3006 path_cleanup(&path);
3007 return return_value;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003008}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003009#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003010
Christian Heimes4e30a842007-11-30 22:12:06 +00003011#ifdef HAVE_FCHOWN
3012PyDoc_STRVAR(posix_fchown__doc__,
3013"fchown(fd, uid, gid)\n\n\
3014Change the owner and group id of the file given by file descriptor\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003015fd to the numeric uid and gid. Equivalent to os.chown(fd, uid, gid).");
Christian Heimes4e30a842007-11-30 22:12:06 +00003016
3017static PyObject *
3018posix_fchown(PyObject *self, PyObject *args)
3019{
Victor Stinner8c62be82010-05-06 00:08:46 +00003020 int fd;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003021 uid_t uid;
3022 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00003023 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003024 if (!PyArg_ParseTuple(args, "iO&O&:fchown", &fd,
3025 _Py_Uid_Converter, &uid,
3026 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00003027 return NULL;
3028 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003029 res = fchown(fd, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003030 Py_END_ALLOW_THREADS
3031 if (res < 0)
3032 return posix_error();
3033 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003034}
3035#endif /* HAVE_FCHOWN */
3036
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003037#ifdef HAVE_LCHOWN
3038PyDoc_STRVAR(posix_lchown__doc__,
3039"lchown(path, uid, gid)\n\n\
3040Change the owner and group id of path to the numeric uid and gid.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003041This function will not follow symbolic links.\n\
3042Equivalent to os.chown(path, uid, gid, follow_symlinks=False).");
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003043
3044static PyObject *
3045posix_lchown(PyObject *self, PyObject *args)
3046{
Victor Stinner292c8352012-10-30 02:17:38 +01003047 path_t path;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003048 uid_t uid;
3049 gid_t gid;
Victor Stinner8c62be82010-05-06 00:08:46 +00003050 int res;
Victor Stinner292c8352012-10-30 02:17:38 +01003051 memset(&path, 0, sizeof(path));
3052 path.function_name = "lchown";
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003053 if (!PyArg_ParseTuple(args, "O&O&O&:lchown",
Victor Stinner292c8352012-10-30 02:17:38 +01003054 path_converter, &path,
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02003055 _Py_Uid_Converter, &uid,
3056 _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00003057 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003058 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakac2d02002013-02-10 22:03:08 +02003059 res = lchown(path.narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003060 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003061 if (res < 0) {
3062 path_error(&path);
3063 path_cleanup(&path);
3064 return NULL;
3065 }
3066 path_cleanup(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003067 Py_INCREF(Py_None);
3068 return Py_None;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003069}
3070#endif /* HAVE_LCHOWN */
3071
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003072
Guido van Rossum36bc6801995-06-14 22:54:23 +00003073#ifdef HAVE_GETCWD
Barry Warsaw53699e91996-12-10 23:23:01 +00003074static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003075posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003076{
Victor Stinner8c62be82010-05-06 00:08:46 +00003077 char buf[1026];
3078 char *res;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003079
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003080#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003081 if (!use_bytes) {
3082 wchar_t wbuf[1026];
3083 wchar_t *wbuf2 = wbuf;
3084 PyObject *resobj;
3085 DWORD len;
3086 Py_BEGIN_ALLOW_THREADS
3087 len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf);
3088 /* If the buffer is large enough, len does not include the
3089 terminating \0. If the buffer is too small, len includes
3090 the space needed for the terminator. */
3091 if (len >= sizeof wbuf/ sizeof wbuf[0]) {
3092 wbuf2 = malloc(len * sizeof(wchar_t));
3093 if (wbuf2)
3094 len = GetCurrentDirectoryW(len, wbuf2);
3095 }
3096 Py_END_ALLOW_THREADS
3097 if (!wbuf2) {
3098 PyErr_NoMemory();
3099 return NULL;
3100 }
3101 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003102 if (wbuf2 != wbuf)
3103 free(wbuf2);
3104 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003105 }
3106 resobj = PyUnicode_FromWideChar(wbuf2, len);
Victor Stinnerb024e842012-10-31 22:24:06 +01003107 if (wbuf2 != wbuf)
3108 free(wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003109 return resobj;
3110 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003111
3112 if (win32_warn_bytes_api())
3113 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003114#endif
3115
Victor Stinner8c62be82010-05-06 00:08:46 +00003116 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003117 res = getcwd(buf, sizeof buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003118 Py_END_ALLOW_THREADS
3119 if (res == NULL)
3120 return posix_error();
3121 if (use_bytes)
3122 return PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner97c18ab2010-05-07 16:34:53 +00003123 return PyUnicode_DecodeFSDefault(buf);
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003124}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003125
3126PyDoc_STRVAR(posix_getcwd__doc__,
3127"getcwd() -> path\n\n\
3128Return a unicode string representing the current working directory.");
3129
3130static PyObject *
3131posix_getcwd_unicode(PyObject *self)
3132{
3133 return posix_getcwd(0);
3134}
3135
3136PyDoc_STRVAR(posix_getcwdb__doc__,
3137"getcwdb() -> path\n\n\
3138Return a bytes string representing the current working directory.");
3139
3140static PyObject *
3141posix_getcwd_bytes(PyObject *self)
3142{
3143 return posix_getcwd(1);
3144}
Guido van Rossum36bc6801995-06-14 22:54:23 +00003145#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003146
Larry Hastings9cf065c2012-06-22 16:30:09 -07003147#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3148#define HAVE_LINK 1
3149#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003150
Guido van Rossumb6775db1994-08-01 11:34:53 +00003151#ifdef HAVE_LINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003152PyDoc_STRVAR(posix_link__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003153"link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True)\n\n\
3154Create a hard link to a file.\n\
3155\n\
3156If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3157 descriptor open to a directory, and the respective path string (src or dst)\n\
3158 should be relative; the path will then be relative to that directory.\n\
3159If follow_symlinks is False, and the last element of src is a symbolic\n\
3160 link, link will create a link to the symbolic link itself instead of the\n\
3161 file the link points to.\n\
3162src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your\n\
3163 platform. If they are unavailable, using them will raise a\n\
3164 NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003165
Barry Warsaw53699e91996-12-10 23:23:01 +00003166static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003167posix_link(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003168{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003169 path_t src, dst;
3170 int src_dir_fd = DEFAULT_DIR_FD;
3171 int dst_dir_fd = DEFAULT_DIR_FD;
3172 int follow_symlinks = 1;
3173 PyObject *return_value = NULL;
3174 static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd",
3175 "follow_symlinks", NULL};
3176#ifdef MS_WINDOWS
3177 BOOL result;
3178#else
3179 int result;
3180#endif
3181
3182 memset(&src, 0, sizeof(src));
3183 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01003184 src.function_name = "link";
3185 dst.function_name = "link";
Larry Hastings9cf065c2012-06-22 16:30:09 -07003186 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|O&O&p:link", keywords,
3187 path_converter, &src,
3188 path_converter, &dst,
3189 dir_fd_converter, &src_dir_fd,
3190 dir_fd_converter, &dst_dir_fd,
3191 &follow_symlinks))
3192 return NULL;
3193
3194#ifndef HAVE_LINKAT
3195 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3196 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
3197 goto exit;
3198 }
3199#endif
3200
3201 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
3202 PyErr_SetString(PyExc_NotImplementedError,
3203 "link: src and dst must be the same type");
3204 goto exit;
3205 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003206
Brian Curtin1b9df392010-11-24 20:24:31 +00003207#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003208 Py_BEGIN_ALLOW_THREADS
3209 if (src.wide)
3210 result = CreateHardLinkW(dst.wide, src.wide, NULL);
3211 else
3212 result = CreateHardLinkA(dst.narrow, src.narrow, NULL);
3213 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003214
Larry Hastings9cf065c2012-06-22 16:30:09 -07003215 if (!result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01003216 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003217 goto exit;
Brian Curtinfc889c42010-11-28 23:59:46 +00003218 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003219#else
3220 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003221#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003222 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3223 (dst_dir_fd != DEFAULT_DIR_FD) ||
3224 (!follow_symlinks))
3225 result = linkat(src_dir_fd, src.narrow,
3226 dst_dir_fd, dst.narrow,
3227 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3228 else
3229#endif
3230 result = link(src.narrow, dst.narrow);
3231 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003232
Larry Hastings9cf065c2012-06-22 16:30:09 -07003233 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01003234 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003235 goto exit;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003236 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003237#endif
3238
3239 return_value = Py_None;
3240 Py_INCREF(Py_None);
3241
3242exit:
3243 path_cleanup(&src);
3244 path_cleanup(&dst);
3245 return return_value;
Brian Curtin1b9df392010-11-24 20:24:31 +00003246}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003247#endif
3248
Brian Curtin1b9df392010-11-24 20:24:31 +00003249
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003251PyDoc_STRVAR(posix_listdir__doc__,
Larry Hastingsfdaea062012-06-25 04:42:23 -07003252"listdir(path='.') -> list_of_filenames\n\n\
3253Return a list containing the names of the files in the directory.\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003254The list is in arbitrary order. It does not include the special\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003255entries '.' and '..' even if they are present in the directory.\n\
3256\n\
Larry Hastingsfdaea062012-06-25 04:42:23 -07003257path can be specified as either str or bytes. If path is bytes,\n\
3258 the filenames returned will also be bytes; in all other circumstances\n\
3259 the filenames returned will be str.\n\
3260On some platforms, path may also be specified as an open file descriptor;\n\
3261 the file descriptor must refer to a directory.\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07003262 If this functionality is unavailable, using it raises NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003263
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003264#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003265static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003266_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003267{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003268 static char *keywords[] = {"path", NULL};
Larry Hastings9cf065c2012-06-22 16:30:09 -07003269 PyObject *v;
3270 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3271 BOOL result;
3272 WIN32_FIND_DATA FileData;
3273 char namebuf[MAX_PATH+5]; /* Overallocate for \\*.*\0 */
3274 char *bufptr = namebuf;
3275 /* only claim to have space for MAX_PATH */
3276 Py_ssize_t len = sizeof(namebuf)-5;
3277 PyObject *po = NULL;
3278 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003279
Gregory P. Smith40a21602013-03-20 20:52:50 -07003280 if (!path->narrow) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003281 WIN32_FIND_DATAW wFileData;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003282 wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003283
Gregory P. Smith40a21602013-03-20 20:52:50 -07003284 if (!path->wide) { /* Default arg: "." */
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003285 po_wchars = L".";
3286 len = 1;
3287 } else {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003288 po_wchars = path->wide;
3289 len = wcslen(path->wide);
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003290 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003291 /* The +5 is so we can append "\\*.*\0" */
Victor Stinner8c62be82010-05-06 00:08:46 +00003292 wnamebuf = malloc((len + 5) * sizeof(wchar_t));
3293 if (!wnamebuf) {
3294 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003295 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003296 }
Martin v. Löwisc9e1c7d2010-07-23 12:16:41 +00003297 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003298 if (len > 0) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003299 wchar_t wch = wnamebuf[len-1];
Victor Stinner8c62be82010-05-06 00:08:46 +00003300 if (wch != L'/' && wch != L'\\' && wch != L':')
3301 wnamebuf[len++] = L'\\';
3302 wcscpy(wnamebuf + len, L"*.*");
3303 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003304 if ((list = PyList_New(0)) == NULL) {
3305 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003306 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003307 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003308 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003309 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003310 if (hFindFile == INVALID_HANDLE_VALUE) {
3311 int error = GetLastError();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003312 if (error == ERROR_FILE_NOT_FOUND)
3313 goto exit;
3314 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003315 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003316 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003317 }
3318 do {
3319 /* Skip over . and .. */
3320 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3321 wcscmp(wFileData.cFileName, L"..") != 0) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003322 v = PyUnicode_FromWideChar(wFileData.cFileName,
3323 wcslen(wFileData.cFileName));
Victor Stinner8c62be82010-05-06 00:08:46 +00003324 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003325 Py_DECREF(list);
3326 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003327 break;
3328 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003329 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003330 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003331 Py_DECREF(list);
3332 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003333 break;
3334 }
3335 Py_DECREF(v);
3336 }
3337 Py_BEGIN_ALLOW_THREADS
3338 result = FindNextFileW(hFindFile, &wFileData);
3339 Py_END_ALLOW_THREADS
3340 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3341 it got to the end of the directory. */
3342 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003343 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003344 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003345 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003346 }
3347 } while (result == TRUE);
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003348
Larry Hastings9cf065c2012-06-22 16:30:09 -07003349 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003350 }
Gregory P. Smith40a21602013-03-20 20:52:50 -07003351 strcpy(namebuf, path->narrow);
3352 len = path->length;
Victor Stinner8c62be82010-05-06 00:08:46 +00003353 if (len > 0) {
3354 char ch = namebuf[len-1];
3355 if (ch != SEP && ch != ALTSEP && ch != ':')
3356 namebuf[len++] = '/';
3357 strcpy(namebuf + len, "*.*");
3358 }
Guido van Rossumb6775db1994-08-01 11:34:53 +00003359
Larry Hastings9cf065c2012-06-22 16:30:09 -07003360 if ((list = PyList_New(0)) == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00003361 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003362
Antoine Pitroub73caab2010-08-09 23:39:31 +00003363 Py_BEGIN_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003364 hFindFile = FindFirstFile(namebuf, &FileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003365 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 if (hFindFile == INVALID_HANDLE_VALUE) {
3367 int error = GetLastError();
3368 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003369 goto exit;
3370 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003371 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003372 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 }
3374 do {
3375 /* Skip over . and .. */
3376 if (strcmp(FileData.cFileName, ".") != 0 &&
3377 strcmp(FileData.cFileName, "..") != 0) {
3378 v = PyBytes_FromString(FileData.cFileName);
3379 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003380 Py_DECREF(list);
3381 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003382 break;
3383 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003384 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003385 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003386 Py_DECREF(list);
3387 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003388 break;
3389 }
3390 Py_DECREF(v);
3391 }
3392 Py_BEGIN_ALLOW_THREADS
3393 result = FindNextFile(hFindFile, &FileData);
3394 Py_END_ALLOW_THREADS
3395 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3396 it got to the end of the directory. */
3397 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003398 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003399 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003400 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003401 }
3402 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003403
Larry Hastings9cf065c2012-06-22 16:30:09 -07003404exit:
3405 if (hFindFile != INVALID_HANDLE_VALUE) {
3406 if (FindClose(hFindFile) == FALSE) {
3407 if (list != NULL) {
3408 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003409 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003410 }
3411 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003413 if (wnamebuf)
3414 free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003415
Larry Hastings9cf065c2012-06-22 16:30:09 -07003416 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003417} /* end of _listdir_windows_no_opendir */
3418
3419#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3420
3421static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003422_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003423{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003424 int fd = -1;
3425
3426 PyObject *v;
3427 DIR *dirp = NULL;
3428 struct dirent *ep;
3429 int return_str; /* if false, return bytes */
3430
Victor Stinner8c62be82010-05-06 00:08:46 +00003431 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003432#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003433 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003434 /* closedir() closes the FD, so we duplicate it */
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003435 Py_BEGIN_ALLOW_THREADS
Gregory P. Smith40a21602013-03-20 20:52:50 -07003436 fd = dup(path->fd);
Antoine Pitroud3ccde82010-09-04 17:21:57 +00003437 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003438
3439 if (fd == -1) {
3440 list = posix_error();
3441 goto exit;
3442 }
3443
Larry Hastingsfdaea062012-06-25 04:42:23 -07003444 return_str = 1;
3445
Larry Hastings9cf065c2012-06-22 16:30:09 -07003446 Py_BEGIN_ALLOW_THREADS
3447 dirp = fdopendir(fd);
3448 Py_END_ALLOW_THREADS
3449 }
3450 else
3451#endif
3452 {
Larry Hastingsfdaea062012-06-25 04:42:23 -07003453 char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003454 if (path->narrow) {
3455 name = path->narrow;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003456 /* only return bytes if they specified a bytes object */
Gregory P. Smith40a21602013-03-20 20:52:50 -07003457 return_str = !(PyBytes_Check(path->object));
Larry Hastingsfdaea062012-06-25 04:42:23 -07003458 }
3459 else {
3460 name = ".";
3461 return_str = 1;
3462 }
3463
Larry Hastings9cf065c2012-06-22 16:30:09 -07003464 Py_BEGIN_ALLOW_THREADS
3465 dirp = opendir(name);
3466 Py_END_ALLOW_THREADS
3467 }
3468
3469 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003470 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003471 goto exit;
3472 }
3473 if ((list = PyList_New(0)) == NULL) {
3474 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003475 }
3476 for (;;) {
3477 errno = 0;
3478 Py_BEGIN_ALLOW_THREADS
3479 ep = readdir(dirp);
3480 Py_END_ALLOW_THREADS
3481 if (ep == NULL) {
3482 if (errno == 0) {
3483 break;
3484 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003485 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003486 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003487 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003488 }
3489 }
3490 if (ep->d_name[0] == '.' &&
3491 (NAMLEN(ep) == 1 ||
3492 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3493 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003494 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003495 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3496 else
3497 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003499 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003500 break;
3501 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003502 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003503 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003504 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003505 break;
3506 }
3507 Py_DECREF(v);
3508 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003509
Larry Hastings9cf065c2012-06-22 16:30:09 -07003510exit:
3511 if (dirp != NULL) {
3512 Py_BEGIN_ALLOW_THREADS
3513 if (fd > -1)
3514 rewinddir(dirp);
3515 closedir(dirp);
3516 Py_END_ALLOW_THREADS
3517 }
3518
Larry Hastings9cf065c2012-06-22 16:30:09 -07003519 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003520} /* end of _posix_listdir */
3521#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003522
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003523static PyObject *
3524posix_listdir(PyObject *self, PyObject *args, PyObject *kwargs)
3525{
Gregory P. Smith40a21602013-03-20 20:52:50 -07003526 path_t path;
3527 PyObject *list = NULL;
3528 static char *keywords[] = {"path", NULL};
3529 PyObject *return_value;
3530
3531 memset(&path, 0, sizeof(path));
3532 path.function_name = "listdir";
3533 path.nullable = 1;
3534#ifdef HAVE_FDOPENDIR
3535 path.allow_fd = 1;
3536 path.fd = -1;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003537#endif
Gregory P. Smith40a21602013-03-20 20:52:50 -07003538
3539 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&:listdir", keywords,
3540 path_converter, &path)) {
3541 return NULL;
3542 }
3543
3544#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3545 return_value = _listdir_windows_no_opendir(&path, list);
3546#else
3547 return_value = _posix_listdir(&path, list);
3548#endif
3549 path_cleanup(&path);
3550 return return_value;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003551}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003552
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003553#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003554/* A helper function for abspath on win32 */
3555static PyObject *
3556posix__getfullpathname(PyObject *self, PyObject *args)
3557{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003558 const char *path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003559 char outbuf[MAX_PATH*2];
3560 char *temp;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003561 PyObject *po;
3562
3563 if (PyArg_ParseTuple(args, "U|:_getfullpathname", &po))
3564 {
3565 wchar_t *wpath;
3566 wchar_t woutbuf[MAX_PATH*2], *woutbufp = woutbuf;
3567 wchar_t *wtemp;
Victor Stinner8c62be82010-05-06 00:08:46 +00003568 DWORD result;
3569 PyObject *v;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003570
3571 wpath = PyUnicode_AsUnicode(po);
3572 if (wpath == NULL)
3573 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003574 result = GetFullPathNameW(wpath,
Victor Stinner63941882011-09-29 00:42:28 +02003575 Py_ARRAY_LENGTH(woutbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003576 woutbuf, &wtemp);
Victor Stinner63941882011-09-29 00:42:28 +02003577 if (result > Py_ARRAY_LENGTH(woutbuf)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003578 woutbufp = malloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003579 if (!woutbufp)
3580 return PyErr_NoMemory();
3581 result = GetFullPathNameW(wpath, result, woutbufp, &wtemp);
3582 }
3583 if (result)
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003584 v = PyUnicode_FromWideChar(woutbufp, wcslen(woutbufp));
Victor Stinner8c62be82010-05-06 00:08:46 +00003585 else
Victor Stinnereb5657a2011-09-30 01:44:27 +02003586 v = win32_error_object("GetFullPathNameW", po);
Victor Stinner8c62be82010-05-06 00:08:46 +00003587 if (woutbufp != woutbuf)
3588 free(woutbufp);
3589 return v;
3590 }
3591 /* Drop the argument parsing error as narrow strings
3592 are also valid. */
3593 PyErr_Clear();
Victor Stinnereb5657a2011-09-30 01:44:27 +02003594
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003595 if (!PyArg_ParseTuple (args, "y:_getfullpathname",
3596 &path))
Victor Stinner8c62be82010-05-06 00:08:46 +00003597 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003598 if (win32_warn_bytes_api())
3599 return NULL;
Victor Stinner63941882011-09-29 00:42:28 +02003600 if (!GetFullPathName(path, Py_ARRAY_LENGTH(outbuf),
Victor Stinner8c62be82010-05-06 00:08:46 +00003601 outbuf, &temp)) {
3602 win32_error("GetFullPathName", path);
Victor Stinner8c62be82010-05-06 00:08:46 +00003603 return NULL;
3604 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003605 if (PyUnicode_Check(PyTuple_GetItem(args, 0))) {
3606 return PyUnicode_Decode(outbuf, strlen(outbuf),
3607 Py_FileSystemDefaultEncoding, NULL);
3608 }
3609 return PyBytes_FromString(outbuf);
Mark Hammondef8b6542001-05-13 08:04:26 +00003610} /* end of posix__getfullpathname */
Brian Curtind40e6f72010-07-08 21:39:08 +00003611
Brian Curtind25aef52011-06-13 15:16:04 -05003612
Brian Curtinf5e76d02010-11-24 13:14:05 +00003613
Brian Curtind40e6f72010-07-08 21:39:08 +00003614/* A helper function for samepath on windows */
3615static PyObject *
3616posix__getfinalpathname(PyObject *self, PyObject *args)
3617{
3618 HANDLE hFile;
3619 int buf_size;
3620 wchar_t *target_path;
3621 int result_length;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003622 PyObject *po, *result;
Brian Curtind40e6f72010-07-08 21:39:08 +00003623 wchar_t *path;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003624
Victor Stinnereb5657a2011-09-30 01:44:27 +02003625 if (!PyArg_ParseTuple(args, "U|:_getfinalpathname", &po))
Brian Curtind40e6f72010-07-08 21:39:08 +00003626 return NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003627 path = PyUnicode_AsUnicode(po);
3628 if (path == NULL)
3629 return NULL;
Brian Curtind40e6f72010-07-08 21:39:08 +00003630
3631 if(!check_GetFinalPathNameByHandle()) {
3632 /* If the OS doesn't have GetFinalPathNameByHandle, return a
3633 NotImplementedError. */
3634 return PyErr_Format(PyExc_NotImplementedError,
3635 "GetFinalPathNameByHandle not available on this platform");
3636 }
3637
3638 hFile = CreateFileW(
3639 path,
3640 0, /* desired access */
3641 0, /* share mode */
3642 NULL, /* security attributes */
3643 OPEN_EXISTING,
3644 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
3645 FILE_FLAG_BACKUP_SEMANTICS,
3646 NULL);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003647
Victor Stinnereb5657a2011-09-30 01:44:27 +02003648 if(hFile == INVALID_HANDLE_VALUE)
3649 return win32_error_object("CreateFileW", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003650
3651 /* We have a good handle to the target, use it to determine the
3652 target path name. */
3653 buf_size = Py_GetFinalPathNameByHandleW(hFile, 0, 0, VOLUME_NAME_NT);
3654
3655 if(!buf_size)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003656 return win32_error_object("GetFinalPathNameByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003657
3658 target_path = (wchar_t *)malloc((buf_size+1)*sizeof(wchar_t));
3659 if(!target_path)
3660 return PyErr_NoMemory();
3661
3662 result_length = Py_GetFinalPathNameByHandleW(hFile, target_path,
3663 buf_size, VOLUME_NAME_DOS);
3664 if(!result_length)
Victor Stinnereb5657a2011-09-30 01:44:27 +02003665 return win32_error_object("GetFinalPathNamyByHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003666
3667 if(!CloseHandle(hFile))
Victor Stinnereb5657a2011-09-30 01:44:27 +02003668 return win32_error_object("CloseHandle", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00003669
3670 target_path[result_length] = 0;
Victor Stinner9d3b93b2011-11-22 02:27:30 +01003671 result = PyUnicode_FromWideChar(target_path, result_length);
Brian Curtind40e6f72010-07-08 21:39:08 +00003672 free(target_path);
3673 return result;
3674
3675} /* end of posix__getfinalpathname */
Brian Curtin62857742010-09-06 17:07:27 +00003676
Brian Curtin95d028f2011-06-09 09:10:38 -05003677PyDoc_STRVAR(posix__isdir__doc__,
3678"Return true if the pathname refers to an existing directory.");
3679
Brian Curtin9c669cc2011-06-08 18:17:18 -05003680static PyObject *
3681posix__isdir(PyObject *self, PyObject *args)
3682{
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003683 const char *path;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003684 PyObject *po;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003685 DWORD attributes;
3686
3687 if (PyArg_ParseTuple(args, "U|:_isdir", &po)) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02003688 wchar_t *wpath = PyUnicode_AsUnicode(po);
3689 if (wpath == NULL)
3690 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003691
3692 attributes = GetFileAttributesW(wpath);
3693 if (attributes == INVALID_FILE_ATTRIBUTES)
3694 Py_RETURN_FALSE;
3695 goto check;
3696 }
3697 /* Drop the argument parsing error as narrow strings
3698 are also valid. */
3699 PyErr_Clear();
3700
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003701 if (!PyArg_ParseTuple(args, "y:_isdir", &path))
Brian Curtin9c669cc2011-06-08 18:17:18 -05003702 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01003703 if (win32_warn_bytes_api())
3704 return NULL;
Brian Curtin9c669cc2011-06-08 18:17:18 -05003705 attributes = GetFileAttributesA(path);
3706 if (attributes == INVALID_FILE_ATTRIBUTES)
3707 Py_RETURN_FALSE;
3708
3709check:
3710 if (attributes & FILE_ATTRIBUTE_DIRECTORY)
3711 Py_RETURN_TRUE;
3712 else
3713 Py_RETURN_FALSE;
3714}
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003715#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00003716
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003717PyDoc_STRVAR(posix_mkdir__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003718"mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
3719Create a directory.\n\
3720\n\
3721If dir_fd is not None, it should be a file descriptor open to a directory,\n\
3722 and path should be relative; path will then be relative to that directory.\n\
3723dir_fd may not be implemented on your platform.\n\
3724 If it is unavailable, using it will raise a NotImplementedError.\n\
3725\n\
3726The mode argument is ignored on Windows.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003727
Barry Warsaw53699e91996-12-10 23:23:01 +00003728static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003729posix_mkdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003730{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003731 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00003732 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003733 int dir_fd = DEFAULT_DIR_FD;
3734 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
3735 PyObject *return_value = NULL;
3736 int result;
3737
3738 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01003739 path.function_name = "mkdir";
Larry Hastings9cf065c2012-06-22 16:30:09 -07003740 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkdir", keywords,
3741 path_converter, &path, &mode,
3742#ifdef HAVE_MKDIRAT
3743 dir_fd_converter, &dir_fd
3744#else
3745 dir_fd_unavailable, &dir_fd
3746#endif
3747 ))
3748 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003749
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003750#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003751 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003752 if (path.wide)
3753 result = CreateDirectoryW(path.wide, NULL);
3754 else
3755 result = CreateDirectoryA(path.narrow, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00003756 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003757
Larry Hastings9cf065c2012-06-22 16:30:09 -07003758 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003759 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003760 goto exit;
3761 }
3762#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003763 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003764#if HAVE_MKDIRAT
3765 if (dir_fd != DEFAULT_DIR_FD)
3766 result = mkdirat(dir_fd, path.narrow, mode);
3767 else
3768#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +00003769#if ( defined(__WATCOMC__) || defined(PYCC_VACPP) ) && !defined(__QNX__)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003770 result = mkdir(path.narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003771#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003772 result = mkdir(path.narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00003773#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003774 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003775 if (result < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01003776 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003777 goto exit;
3778 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003779#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003780 return_value = Py_None;
3781 Py_INCREF(Py_None);
3782exit:
3783 path_cleanup(&path);
3784 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003785}
3786
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003787
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003788/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
3789#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003790#include <sys/resource.h>
3791#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003792
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003793
3794#ifdef HAVE_NICE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003795PyDoc_STRVAR(posix_nice__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00003796"nice(inc) -> new_priority\n\n\
3797Decrease the priority of process by inc and return the new priority.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003798
Barry Warsaw53699e91996-12-10 23:23:01 +00003799static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00003800posix_nice(PyObject *self, PyObject *args)
Guido van Rossum775f4da1993-01-09 17:18:52 +00003801{
Victor Stinner8c62be82010-05-06 00:08:46 +00003802 int increment, value;
Guido van Rossum775f4da1993-01-09 17:18:52 +00003803
Victor Stinner8c62be82010-05-06 00:08:46 +00003804 if (!PyArg_ParseTuple(args, "i:nice", &increment))
3805 return NULL;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003806
Victor Stinner8c62be82010-05-06 00:08:46 +00003807 /* There are two flavours of 'nice': one that returns the new
3808 priority (as required by almost all standards out there) and the
3809 Linux/FreeBSD/BSDI one, which returns '0' on success and advices
3810 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00003811
Victor Stinner8c62be82010-05-06 00:08:46 +00003812 If we are of the nice family that returns the new priority, we
3813 need to clear errno before the call, and check if errno is filled
3814 before calling posix_error() on a returnvalue of -1, because the
3815 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003816
Victor Stinner8c62be82010-05-06 00:08:46 +00003817 errno = 0;
3818 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00003819#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00003820 if (value == 0)
3821 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00003822#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003823 if (value == -1 && errno != 0)
3824 /* either nice() or getpriority() returned an error */
3825 return posix_error();
3826 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00003827}
Guido van Rossumb6775db1994-08-01 11:34:53 +00003828#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00003829
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003830
3831#ifdef HAVE_GETPRIORITY
3832PyDoc_STRVAR(posix_getpriority__doc__,
3833"getpriority(which, who) -> current_priority\n\n\
3834Get program scheduling priority.");
3835
3836static PyObject *
3837posix_getpriority(PyObject *self, PyObject *args)
3838{
3839 int which, who, retval;
3840
3841 if (!PyArg_ParseTuple(args, "ii", &which, &who))
3842 return NULL;
3843 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003844 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003845 if (errno != 0)
3846 return posix_error();
3847 return PyLong_FromLong((long)retval);
3848}
3849#endif /* HAVE_GETPRIORITY */
3850
3851
3852#ifdef HAVE_SETPRIORITY
3853PyDoc_STRVAR(posix_setpriority__doc__,
3854"setpriority(which, who, prio) -> None\n\n\
3855Set program scheduling priority.");
3856
3857static PyObject *
3858posix_setpriority(PyObject *self, PyObject *args)
3859{
3860 int which, who, prio, retval;
3861
3862 if (!PyArg_ParseTuple(args, "iii", &which, &who, &prio))
3863 return NULL;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003864 retval = setpriority(which, who, prio);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00003865 if (retval == -1)
3866 return posix_error();
3867 Py_RETURN_NONE;
3868}
3869#endif /* HAVE_SETPRIORITY */
3870
3871
Barry Warsaw53699e91996-12-10 23:23:01 +00003872static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003873internal_rename(PyObject *args, PyObject *kwargs, int is_replace)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003874{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003875 char *function_name = is_replace ? "replace" : "rename";
3876 path_t src;
3877 path_t dst;
3878 int src_dir_fd = DEFAULT_DIR_FD;
3879 int dst_dir_fd = DEFAULT_DIR_FD;
3880 int dir_fd_specified;
3881 PyObject *return_value = NULL;
3882 char format[24];
3883 static char *keywords[] = {"src", "dst", "src_dir_fd", "dst_dir_fd", NULL};
3884
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003885#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003886 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003887 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003888#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003889 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003890#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003891
3892 memset(&src, 0, sizeof(src));
3893 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01003894 src.function_name = function_name;
3895 dst.function_name = function_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003896 strcpy(format, "O&O&|$O&O&:");
3897 strcat(format, function_name);
3898 if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, keywords,
3899 path_converter, &src,
3900 path_converter, &dst,
3901 dir_fd_converter, &src_dir_fd,
3902 dir_fd_converter, &dst_dir_fd))
3903 return NULL;
3904
3905 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
3906 (dst_dir_fd != DEFAULT_DIR_FD);
3907#ifndef HAVE_RENAMEAT
3908 if (dir_fd_specified) {
3909 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
3910 goto exit;
3911 }
3912#endif
3913
3914 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
3915 PyErr_Format(PyExc_ValueError,
3916 "%s: src and dst must be the same type", function_name);
3917 goto exit;
3918 }
3919
3920#ifdef MS_WINDOWS
3921 Py_BEGIN_ALLOW_THREADS
3922 if (src.wide)
3923 result = MoveFileExW(src.wide, dst.wide, flags);
3924 else
3925 result = MoveFileExA(src.narrow, dst.narrow, flags);
3926 Py_END_ALLOW_THREADS
3927
3928 if (!result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01003929 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003930 goto exit;
3931 }
3932
3933#else
3934 Py_BEGIN_ALLOW_THREADS
3935#ifdef HAVE_RENAMEAT
3936 if (dir_fd_specified)
3937 result = renameat(src_dir_fd, src.narrow, dst_dir_fd, dst.narrow);
3938 else
3939#endif
3940 result = rename(src.narrow, dst.narrow);
3941 Py_END_ALLOW_THREADS
3942
3943 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01003944 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003945 goto exit;
3946 }
3947#endif
3948
3949 Py_INCREF(Py_None);
3950 return_value = Py_None;
3951exit:
3952 path_cleanup(&src);
3953 path_cleanup(&dst);
3954 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003955}
3956
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003957PyDoc_STRVAR(posix_rename__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003958"rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\
3959Rename a file or directory.\n\
3960\n\
3961If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3962 descriptor open to a directory, and the respective path string (src or dst)\n\
3963 should be relative; the path will then be relative to that directory.\n\
3964src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\
3965 If they are unavailable, using them will raise a NotImplementedError.");
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003966
3967static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003968posix_rename(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003969{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003970 return internal_rename(args, kwargs, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003971}
3972
3973PyDoc_STRVAR(posix_replace__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003974"replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None)\n\n\
3975Rename a file or directory, overwriting the destination.\n\
3976\n\
3977If either src_dir_fd or dst_dir_fd is not None, it should be a file\n\
3978 descriptor open to a directory, and the respective path string (src or dst)\n\
3979 should be relative; the path will then be relative to that directory.\n\
3980src_dir_fd and dst_dir_fd, may not be implemented on your platform.\n\
3981 If they are unavailable, using them will raise a NotImplementedError.");
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003982
3983static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07003984posix_replace(PyObject *self, PyObject *args, PyObject *kwargs)
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003985{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003986 return internal_rename(args, kwargs, 1);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01003987}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003988
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00003989PyDoc_STRVAR(posix_rmdir__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07003990"rmdir(path, *, dir_fd=None)\n\n\
3991Remove a directory.\n\
3992\n\
3993If dir_fd is not None, it should be a file descriptor open to a directory,\n\
3994 and path should be relative; path will then be relative to that directory.\n\
3995dir_fd may not be implemented on your platform.\n\
3996 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003997
Barry Warsaw53699e91996-12-10 23:23:01 +00003998static PyObject *
Larry Hastingsb698d8e2012-06-23 16:55:07 -07003999posix_rmdir(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004000{
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004001 path_t path;
4002 int dir_fd = DEFAULT_DIR_FD;
4003 static char *keywords[] = {"path", "dir_fd", NULL};
4004 int result;
4005 PyObject *return_value = NULL;
4006
4007 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01004008 path.function_name = "rmdir";
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004009 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:rmdir", keywords,
4010 path_converter, &path,
4011#ifdef HAVE_UNLINKAT
4012 dir_fd_converter, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004013#else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004014 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004015#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004016 ))
4017 return NULL;
4018
4019 Py_BEGIN_ALLOW_THREADS
4020#ifdef MS_WINDOWS
4021 if (path.wide)
4022 result = RemoveDirectoryW(path.wide);
4023 else
4024 result = RemoveDirectoryA(path.narrow);
4025 result = !result; /* Windows, success=1, UNIX, success=0 */
4026#else
4027#ifdef HAVE_UNLINKAT
4028 if (dir_fd != DEFAULT_DIR_FD)
4029 result = unlinkat(dir_fd, path.narrow, AT_REMOVEDIR);
4030 else
4031#endif
4032 result = rmdir(path.narrow);
4033#endif
4034 Py_END_ALLOW_THREADS
4035
4036 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01004037 return_value = path_error(&path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004038 goto exit;
4039 }
4040
4041 return_value = Py_None;
4042 Py_INCREF(Py_None);
4043
4044exit:
4045 path_cleanup(&path);
4046 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004047}
4048
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004049
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004050#ifdef HAVE_SYSTEM
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004051PyDoc_STRVAR(posix_system__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004052"system(command) -> exit_status\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004053Execute the command (a string) in a subshell.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004054
Barry Warsaw53699e91996-12-10 23:23:01 +00004055static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004056posix_system(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004057{
Victor Stinner8c62be82010-05-06 00:08:46 +00004058 long sts;
Amaury Forgeot d'Arc90ebd3e2007-11-20 01:52:14 +00004059#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004060 wchar_t *command;
4061 if (!PyArg_ParseTuple(args, "u:system", &command))
4062 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00004063
Victor Stinner8c62be82010-05-06 00:08:46 +00004064 Py_BEGIN_ALLOW_THREADS
4065 sts = _wsystem(command);
4066 Py_END_ALLOW_THREADS
Victor Stinnercfa72782010-04-16 11:45:13 +00004067#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004068 PyObject *command_obj;
4069 char *command;
4070 if (!PyArg_ParseTuple(args, "O&:system",
4071 PyUnicode_FSConverter, &command_obj))
4072 return NULL;
Victor Stinnercfa72782010-04-16 11:45:13 +00004073
Victor Stinner8c62be82010-05-06 00:08:46 +00004074 command = PyBytes_AsString(command_obj);
4075 Py_BEGIN_ALLOW_THREADS
4076 sts = system(command);
4077 Py_END_ALLOW_THREADS
4078 Py_DECREF(command_obj);
Victor Stinnercfa72782010-04-16 11:45:13 +00004079#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004080 return PyLong_FromLong(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004081}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004082#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004083
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004084
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004085PyDoc_STRVAR(posix_umask__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004086"umask(new_mask) -> old_mask\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004087Set the current numeric umask and return the previous umask.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004088
Barry Warsaw53699e91996-12-10 23:23:01 +00004089static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004090posix_umask(PyObject *self, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004091{
Victor Stinner8c62be82010-05-06 00:08:46 +00004092 int i;
4093 if (!PyArg_ParseTuple(args, "i:umask", &i))
4094 return NULL;
4095 i = (int)umask(i);
4096 if (i < 0)
4097 return posix_error();
4098 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004099}
4100
Brian Curtind40e6f72010-07-08 21:39:08 +00004101#ifdef MS_WINDOWS
4102
4103/* override the default DeleteFileW behavior so that directory
4104symlinks can be removed with this function, the same as with
4105Unix symlinks */
4106BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4107{
4108 WIN32_FILE_ATTRIBUTE_DATA info;
4109 WIN32_FIND_DATAW find_data;
4110 HANDLE find_data_handle;
4111 int is_directory = 0;
4112 int is_link = 0;
4113
4114 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4115 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004116
Brian Curtind40e6f72010-07-08 21:39:08 +00004117 /* Get WIN32_FIND_DATA structure for the path to determine if
4118 it is a symlink */
4119 if(is_directory &&
4120 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4121 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4122
4123 if(find_data_handle != INVALID_HANDLE_VALUE) {
4124 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK;
4125 FindClose(find_data_handle);
4126 }
4127 }
4128 }
4129
4130 if (is_directory && is_link)
4131 return RemoveDirectoryW(lpFileName);
4132
4133 return DeleteFileW(lpFileName);
4134}
4135#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004136
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004137PyDoc_STRVAR(posix_unlink__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004138"unlink(path, *, dir_fd=None)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004139Remove a file (same as remove()).\n\
4140\n\
4141If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4142 and path should be relative; path will then be relative to that directory.\n\
4143dir_fd may not be implemented on your platform.\n\
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004144 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004145
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004146PyDoc_STRVAR(posix_remove__doc__,
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004147"remove(path, *, dir_fd=None)\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004148Remove a file (same as unlink()).\n\
4149\n\
4150If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4151 and path should be relative; path will then be relative to that directory.\n\
4152dir_fd may not be implemented on your platform.\n\
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004153 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004154
Barry Warsaw53699e91996-12-10 23:23:01 +00004155static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004156posix_unlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004157{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004158 path_t path;
4159 int dir_fd = DEFAULT_DIR_FD;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004160 static char *keywords[] = {"path", "dir_fd", NULL};
Larry Hastings9cf065c2012-06-22 16:30:09 -07004161 int result;
4162 PyObject *return_value = NULL;
4163
4164 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01004165 path.function_name = "unlink";
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004166 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:unlink", keywords,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004167 path_converter, &path,
4168#ifdef HAVE_UNLINKAT
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004169 dir_fd_converter, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004170#else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004171 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004172#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004173 ))
Larry Hastings9cf065c2012-06-22 16:30:09 -07004174 return NULL;
4175
4176 Py_BEGIN_ALLOW_THREADS
4177#ifdef MS_WINDOWS
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004178 if (path.wide)
4179 result = Py_DeleteFileW(path.wide);
4180 else
4181 result = DeleteFileA(path.narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004182 result = !result; /* Windows, success=1, UNIX, success=0 */
4183#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004184#ifdef HAVE_UNLINKAT
4185 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004186 result = unlinkat(dir_fd, path.narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004187 else
4188#endif /* HAVE_UNLINKAT */
4189 result = unlink(path.narrow);
4190#endif
4191 Py_END_ALLOW_THREADS
4192
4193 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01004194 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004195 goto exit;
4196 }
4197
4198 return_value = Py_None;
4199 Py_INCREF(Py_None);
4200
4201exit:
4202 path_cleanup(&path);
4203 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004204}
4205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004206
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004207PyDoc_STRVAR(posix_uname__doc__,
Larry Hastings605a62d2012-06-24 04:33:36 -07004208"uname() -> uname_result\n\n\
4209Return an object identifying the current operating system.\n\
4210The object behaves like a named tuple with the following fields:\n\
4211 (sysname, nodename, release, version, machine)");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004212
Larry Hastings605a62d2012-06-24 04:33:36 -07004213static PyStructSequence_Field uname_result_fields[] = {
4214 {"sysname", "operating system name"},
4215 {"nodename", "name of machine on network (implementation-defined)"},
4216 {"release", "operating system release"},
4217 {"version", "operating system version"},
4218 {"machine", "hardware identifier"},
4219 {NULL}
4220};
4221
4222PyDoc_STRVAR(uname_result__doc__,
4223"uname_result: Result from os.uname().\n\n\
4224This object may be accessed either as a tuple of\n\
4225 (sysname, nodename, release, version, machine),\n\
4226or via the attributes sysname, nodename, release, version, and machine.\n\
4227\n\
4228See os.uname for more information.");
4229
4230static PyStructSequence_Desc uname_result_desc = {
4231 "uname_result", /* name */
4232 uname_result__doc__, /* doc */
4233 uname_result_fields,
4234 5
4235};
4236
4237static PyTypeObject UnameResultType;
4238
4239
4240#ifdef HAVE_UNAME
Barry Warsaw53699e91996-12-10 23:23:01 +00004241static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00004242posix_uname(PyObject *self, PyObject *noargs)
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004243{
Victor Stinner8c62be82010-05-06 00:08:46 +00004244 struct utsname u;
4245 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004246 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004247
Victor Stinner8c62be82010-05-06 00:08:46 +00004248 Py_BEGIN_ALLOW_THREADS
4249 res = uname(&u);
4250 Py_END_ALLOW_THREADS
4251 if (res < 0)
4252 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004253
4254 value = PyStructSequence_New(&UnameResultType);
4255 if (value == NULL)
4256 return NULL;
4257
4258#define SET(i, field) \
4259 { \
4260 PyObject *o = PyUnicode_DecodeASCII(field, strlen(field), NULL); \
4261 if (!o) { \
4262 Py_DECREF(value); \
4263 return NULL; \
4264 } \
4265 PyStructSequence_SET_ITEM(value, i, o); \
4266 } \
4267
4268 SET(0, u.sysname);
4269 SET(1, u.nodename);
4270 SET(2, u.release);
4271 SET(3, u.version);
4272 SET(4, u.machine);
4273
4274#undef SET
4275
4276 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004277}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004278#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004279
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004280
Larry Hastings9cf065c2012-06-22 16:30:09 -07004281PyDoc_STRVAR(posix_utime__doc__,
4282"utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True)\n\
4283Set the access and modified time of path.\n\
4284\n\
4285path may always be specified as a string.\n\
4286On some platforms, path may also be specified as an open file descriptor.\n\
4287 If this functionality is unavailable, using it raises an exception.\n\
4288\n\
4289If times is not None, it must be a tuple (atime, mtime);\n\
4290 atime and mtime should be expressed as float seconds since the epoch.\n\
4291If ns is not None, it must be a tuple (atime_ns, mtime_ns);\n\
4292 atime_ns and mtime_ns should be expressed as integer nanoseconds\n\
4293 since the epoch.\n\
4294If both times and ns are None, utime uses the current time.\n\
4295Specifying tuples for both times and ns is an error.\n\
4296\n\
4297If dir_fd is not None, it should be a file descriptor open to a directory,\n\
4298 and path should be relative; path will then be relative to that directory.\n\
4299If follow_symlinks is False, and the last element of the path is a symbolic\n\
4300 link, utime will modify the symbolic link itself instead of the file the\n\
4301 link points to.\n\
4302It is an error to use dir_fd or follow_symlinks when specifying path\n\
4303 as an open file descriptor.\n\
4304dir_fd and follow_symlinks may not be available on your platform.\n\
4305 If they are unavailable, using them will raise a NotImplementedError.");
4306
4307typedef struct {
4308 int now;
4309 time_t atime_s;
4310 long atime_ns;
4311 time_t mtime_s;
4312 long mtime_ns;
4313} utime_t;
4314
4315/*
4316 * these macros assume that "utime" is a pointer to a utime_t
4317 * they also intentionally leak the declaration of a pointer named "time"
4318 */
4319#define UTIME_TO_TIMESPEC \
4320 struct timespec ts[2]; \
4321 struct timespec *time; \
4322 if (utime->now) \
4323 time = NULL; \
4324 else { \
4325 ts[0].tv_sec = utime->atime_s; \
4326 ts[0].tv_nsec = utime->atime_ns; \
4327 ts[1].tv_sec = utime->mtime_s; \
4328 ts[1].tv_nsec = utime->mtime_ns; \
4329 time = ts; \
4330 } \
4331
4332#define UTIME_TO_TIMEVAL \
4333 struct timeval tv[2]; \
4334 struct timeval *time; \
4335 if (utime->now) \
4336 time = NULL; \
4337 else { \
4338 tv[0].tv_sec = utime->atime_s; \
4339 tv[0].tv_usec = utime->atime_ns / 1000; \
4340 tv[1].tv_sec = utime->mtime_s; \
4341 tv[1].tv_usec = utime->mtime_ns / 1000; \
4342 time = tv; \
4343 } \
4344
4345#define UTIME_TO_UTIMBUF \
4346 struct utimbuf u[2]; \
4347 struct utimbuf *time; \
4348 if (utime->now) \
4349 time = NULL; \
4350 else { \
4351 u.actime = utime->atime_s; \
4352 u.modtime = utime->mtime_s; \
4353 time = u; \
4354 }
4355
4356#define UTIME_TO_TIME_T \
4357 time_t timet[2]; \
4358 struct timet time; \
4359 if (utime->now) \
4360 time = NULL; \
4361 else { \
4362 timet[0] = utime->atime_s; \
4363 timet[1] = utime->mtime_s; \
4364 time = &timet; \
4365 } \
4366
4367
4368#define UTIME_HAVE_DIR_FD (defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT))
4369
4370#if UTIME_HAVE_DIR_FD
4371
4372static int
4373utime_dir_fd(utime_t *utime, int dir_fd, char *path, int follow_symlinks)
4374{
4375#ifdef HAVE_UTIMENSAT
4376 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4377 UTIME_TO_TIMESPEC;
4378 return utimensat(dir_fd, path, time, flags);
4379#elif defined(HAVE_FUTIMESAT)
4380 UTIME_TO_TIMEVAL;
4381 /*
4382 * follow_symlinks will never be false here;
4383 * we only allow !follow_symlinks and dir_fd together
4384 * if we have utimensat()
4385 */
4386 assert(follow_symlinks);
4387 return futimesat(dir_fd, path, time);
4388#endif
4389}
4390
4391#endif
4392
4393#define UTIME_HAVE_FD (defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS))
4394
4395#if UTIME_HAVE_FD
4396
4397static int
4398utime_fd(utime_t *utime, int fd)
4399{
4400#ifdef HAVE_FUTIMENS
4401 UTIME_TO_TIMESPEC;
4402 return futimens(fd, time);
4403#else
4404 UTIME_TO_TIMEVAL;
4405 return futimes(fd, time);
4406#endif
4407}
4408
4409#endif
4410
4411
4412#define UTIME_HAVE_NOFOLLOW_SYMLINKS \
4413 (defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES))
4414
4415#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4416
4417static int
4418utime_nofollow_symlinks(utime_t *utime, char *path)
4419{
4420#ifdef HAVE_UTIMENSAT
4421 UTIME_TO_TIMESPEC;
4422 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4423#else
4424 UTIME_TO_TIMEVAL;
4425 return lutimes(path, time);
4426#endif
4427}
4428
4429#endif
4430
4431#ifndef MS_WINDOWS
4432
4433static int
4434utime_default(utime_t *utime, char *path)
4435{
4436#ifdef HAVE_UTIMENSAT
4437 UTIME_TO_TIMESPEC;
4438 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4439#elif defined(HAVE_UTIMES)
4440 UTIME_TO_TIMEVAL;
4441 return utimes(path, time);
4442#elif defined(HAVE_UTIME_H)
4443 UTIME_TO_UTIMBUF;
4444 return utime(path, time);
4445#else
4446 UTIME_TO_TIME_T;
4447 return utime(path, time);
4448#endif
4449}
4450
4451#endif
4452
Larry Hastings76ad59b2012-05-03 00:30:07 -07004453static int
4454split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4455{
4456 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004457 PyObject *divmod;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004458 divmod = PyNumber_Divmod(py_long, billion);
4459 if (!divmod)
4460 goto exit;
4461 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4462 if ((*s == -1) && PyErr_Occurred())
4463 goto exit;
4464 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004465 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004466 goto exit;
4467
4468 result = 1;
4469exit:
4470 Py_XDECREF(divmod);
4471 return result;
4472}
4473
Larry Hastings9cf065c2012-06-22 16:30:09 -07004474static PyObject *
4475posix_utime(PyObject *self, PyObject *args, PyObject *kwargs)
Larry Hastings76ad59b2012-05-03 00:30:07 -07004476{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004477 path_t path;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004478 PyObject *times = NULL;
4479 PyObject *ns = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004480 int dir_fd = DEFAULT_DIR_FD;
4481 int follow_symlinks = 1;
4482 char *keywords[] = {"path", "times", "ns", "dir_fd",
4483 "follow_symlinks", NULL};
Larry Hastings76ad59b2012-05-03 00:30:07 -07004484
Larry Hastings9cf065c2012-06-22 16:30:09 -07004485 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004486
Larry Hastings9cf065c2012-06-22 16:30:09 -07004487#ifdef MS_WINDOWS
4488 HANDLE hFile;
4489 FILETIME atime, mtime;
4490#else
4491 int result;
4492#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004493
Larry Hastings9cf065c2012-06-22 16:30:09 -07004494 PyObject *return_value = NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004495
Larry Hastings9cf065c2012-06-22 16:30:09 -07004496 memset(&path, 0, sizeof(path));
Victor Stinnerb024e842012-10-31 22:24:06 +01004497 path.function_name = "utime";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004498#if UTIME_HAVE_FD
4499 path.allow_fd = 1;
4500#endif
4501 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
4502 "O&|O$OO&p:utime", keywords,
4503 path_converter, &path,
4504 &times, &ns,
4505#if UTIME_HAVE_DIR_FD
4506 dir_fd_converter, &dir_fd,
4507#else
4508 dir_fd_unavailable, &dir_fd,
4509#endif
4510 &follow_symlinks
4511 ))
4512 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004513
Larry Hastings9cf065c2012-06-22 16:30:09 -07004514 if (times && (times != Py_None) && ns) {
4515 PyErr_SetString(PyExc_ValueError,
4516 "utime: you may specify either 'times'"
4517 " or 'ns' but not both");
4518 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004519 }
4520
4521 if (times && (times != Py_None)) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004522 time_t a_sec, m_sec;
4523 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004524 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004525 PyErr_SetString(PyExc_TypeError,
4526 "utime: 'times' must be either"
4527 " a tuple of two ints or None");
4528 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004529 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004530 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004531 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004532 &a_sec, &a_nsec) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004533 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004534 &m_sec, &m_nsec) == -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004535 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004536 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004537 utime.atime_s = a_sec;
4538 utime.atime_ns = a_nsec;
4539 utime.mtime_s = m_sec;
4540 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004541 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004542 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004543 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004544 PyErr_SetString(PyExc_TypeError,
4545 "utime: 'ns' must be a tuple of two ints");
4546 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004547 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004548 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004549 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004550 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004551 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004552 &utime.mtime_s, &utime.mtime_ns)) {
4553 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004554 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004555 }
4556 else {
4557 /* times and ns are both None/unspecified. use "now". */
4558 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004559 }
4560
Larry Hastings9cf065c2012-06-22 16:30:09 -07004561#if !UTIME_HAVE_NOFOLLOW_SYMLINKS
4562 if (follow_symlinks_specified("utime", follow_symlinks))
4563 goto exit;
4564#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004565
Larry Hastings9cf065c2012-06-22 16:30:09 -07004566 if (path_and_dir_fd_invalid("utime", &path, dir_fd) ||
4567 dir_fd_and_fd_invalid("utime", dir_fd, path.fd) ||
4568 fd_and_follow_symlinks_invalid("utime", path.fd, follow_symlinks))
4569 goto exit;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004570
Larry Hastings9cf065c2012-06-22 16:30:09 -07004571#if !defined(HAVE_UTIMENSAT)
4572 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004573 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004574 "utime: cannot use dir_fd and follow_symlinks "
4575 "together on this platform");
4576 goto exit;
4577 }
4578#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004579
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004580#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004581 Py_BEGIN_ALLOW_THREADS
4582 if (path.wide)
4583 hFile = CreateFileW(path.wide, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004584 NULL, OPEN_EXISTING,
4585 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004586 else
4587 hFile = CreateFileA(path.narrow, FILE_WRITE_ATTRIBUTES, 0,
Victor Stinner8c62be82010-05-06 00:08:46 +00004588 NULL, OPEN_EXISTING,
4589 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004590 Py_END_ALLOW_THREADS
4591 if (hFile == INVALID_HANDLE_VALUE) {
Victor Stinnerb024e842012-10-31 22:24:06 +01004592 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004593 goto exit;
Larry Hastingsb3336402012-05-04 02:31:57 -07004594 }
4595
Larry Hastings9cf065c2012-06-22 16:30:09 -07004596 if (utime.now) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004597 SYSTEMTIME now;
4598 GetSystemTime(&now);
4599 if (!SystemTimeToFileTime(&now, &mtime) ||
4600 !SystemTimeToFileTime(&now, &atime)) {
Victor Stinnerb024e842012-10-31 22:24:06 +01004601 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004602 goto exit;
Stefan Krah0e803b32010-11-26 16:16:47 +00004603 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004604 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004605 else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004606 time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
4607 time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00004608 }
4609 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
4610 /* Avoid putting the file name into the error here,
4611 as that may confuse the user into believing that
4612 something is wrong with the file, when it also
4613 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01004614 PyErr_SetFromWindowsErr(0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004615 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004616 }
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004617#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004618 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004619
Larry Hastings9cf065c2012-06-22 16:30:09 -07004620#if UTIME_HAVE_NOFOLLOW_SYMLINKS
4621 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
4622 result = utime_nofollow_symlinks(&utime, path.narrow);
4623 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004624#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004625
4626#if UTIME_HAVE_DIR_FD
4627 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
4628 result = utime_dir_fd(&utime, dir_fd, path.narrow, follow_symlinks);
4629 else
4630#endif
4631
4632#if UTIME_HAVE_FD
4633 if (path.fd != -1)
4634 result = utime_fd(&utime, path.fd);
4635 else
4636#endif
4637
4638 result = utime_default(&utime, path.narrow);
4639
4640 Py_END_ALLOW_THREADS
4641
4642 if (result < 0) {
4643 /* see previous comment about not putting filename in error here */
4644 return_value = posix_error();
4645 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004646 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004647
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004648#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07004649
4650 Py_INCREF(Py_None);
4651 return_value = Py_None;
4652
4653exit:
4654 path_cleanup(&path);
4655#ifdef MS_WINDOWS
4656 if (hFile != INVALID_HANDLE_VALUE)
4657 CloseHandle(hFile);
4658#endif
4659 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004660}
4661
Guido van Rossum3b066191991-06-04 19:40:25 +00004662/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004663
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004664PyDoc_STRVAR(posix__exit__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004665"_exit(status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004666Exit to the system with specified status, without normal exit processing.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004667
Barry Warsaw53699e91996-12-10 23:23:01 +00004668static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004669posix__exit(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00004670{
Victor Stinner8c62be82010-05-06 00:08:46 +00004671 int sts;
4672 if (!PyArg_ParseTuple(args, "i:_exit", &sts))
4673 return NULL;
4674 _exit(sts);
4675 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00004676}
4677
Martin v. Löwis114619e2002-10-07 06:44:21 +00004678#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV)
4679static void
Martin v. Löwis725507b2006-03-07 12:08:51 +00004680free_string_array(char **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00004681{
Victor Stinner8c62be82010-05-06 00:08:46 +00004682 Py_ssize_t i;
4683 for (i = 0; i < count; i++)
4684 PyMem_Free(array[i]);
4685 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00004686}
Martin v. Löwis011e8422009-05-05 04:43:17 +00004687
Antoine Pitrou69f71142009-05-24 21:25:49 +00004688static
Martin v. Löwis011e8422009-05-05 04:43:17 +00004689int fsconvert_strdup(PyObject *o, char**out)
4690{
Victor Stinner8c62be82010-05-06 00:08:46 +00004691 PyObject *bytes;
4692 Py_ssize_t size;
4693 if (!PyUnicode_FSConverter(o, &bytes))
4694 return 0;
4695 size = PyBytes_GET_SIZE(bytes);
4696 *out = PyMem_Malloc(size+1);
4697 if (!*out)
4698 return 0;
4699 memcpy(*out, PyBytes_AsString(bytes), size+1);
4700 Py_DECREF(bytes);
4701 return 1;
Martin v. Löwis011e8422009-05-05 04:43:17 +00004702}
Martin v. Löwis114619e2002-10-07 06:44:21 +00004703#endif
4704
Ross Lagerwall7807c352011-03-17 20:20:30 +02004705#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE)
Victor Stinner13bb71c2010-04-23 21:41:56 +00004706static char**
4707parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
4708{
Victor Stinner8c62be82010-05-06 00:08:46 +00004709 char **envlist;
4710 Py_ssize_t i, pos, envc;
4711 PyObject *keys=NULL, *vals=NULL;
4712 PyObject *key, *val, *key2, *val2;
4713 char *p, *k, *v;
4714 size_t len;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004715
Victor Stinner8c62be82010-05-06 00:08:46 +00004716 i = PyMapping_Size(env);
4717 if (i < 0)
4718 return NULL;
4719 envlist = PyMem_NEW(char *, i + 1);
4720 if (envlist == NULL) {
4721 PyErr_NoMemory();
4722 return NULL;
4723 }
4724 envc = 0;
4725 keys = PyMapping_Keys(env);
4726 vals = PyMapping_Values(env);
4727 if (!keys || !vals)
4728 goto error;
4729 if (!PyList_Check(keys) || !PyList_Check(vals)) {
4730 PyErr_Format(PyExc_TypeError,
4731 "env.keys() or env.values() is not a list");
4732 goto error;
4733 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004734
Victor Stinner8c62be82010-05-06 00:08:46 +00004735 for (pos = 0; pos < i; pos++) {
4736 key = PyList_GetItem(keys, pos);
4737 val = PyList_GetItem(vals, pos);
4738 if (!key || !val)
4739 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004740
Victor Stinner8c62be82010-05-06 00:08:46 +00004741 if (PyUnicode_FSConverter(key, &key2) == 0)
4742 goto error;
4743 if (PyUnicode_FSConverter(val, &val2) == 0) {
4744 Py_DECREF(key2);
4745 goto error;
4746 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00004747
Victor Stinner8c62be82010-05-06 00:08:46 +00004748 k = PyBytes_AsString(key2);
4749 v = PyBytes_AsString(val2);
4750 len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004751
Victor Stinner8c62be82010-05-06 00:08:46 +00004752 p = PyMem_NEW(char, len);
4753 if (p == NULL) {
4754 PyErr_NoMemory();
4755 Py_DECREF(key2);
4756 Py_DECREF(val2);
4757 goto error;
4758 }
4759 PyOS_snprintf(p, len, "%s=%s", k, v);
4760 envlist[envc++] = p;
4761 Py_DECREF(key2);
4762 Py_DECREF(val2);
Victor Stinner8c62be82010-05-06 00:08:46 +00004763 }
4764 Py_DECREF(vals);
4765 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00004766
Victor Stinner8c62be82010-05-06 00:08:46 +00004767 envlist[envc] = 0;
4768 *envc_ptr = envc;
4769 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004770
4771error:
Victor Stinner8c62be82010-05-06 00:08:46 +00004772 Py_XDECREF(keys);
4773 Py_XDECREF(vals);
4774 while (--envc >= 0)
4775 PyMem_DEL(envlist[envc]);
4776 PyMem_DEL(envlist);
4777 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00004778}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004779
Ross Lagerwall7807c352011-03-17 20:20:30 +02004780static char**
4781parse_arglist(PyObject* argv, Py_ssize_t *argc)
4782{
4783 int i;
4784 char **argvlist = PyMem_NEW(char *, *argc+1);
4785 if (argvlist == NULL) {
4786 PyErr_NoMemory();
4787 return NULL;
4788 }
4789 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004790 PyObject* item = PySequence_ITEM(argv, i);
4791 if (item == NULL)
4792 goto fail;
4793 if (!fsconvert_strdup(item, &argvlist[i])) {
4794 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004795 goto fail;
4796 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004797 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004798 }
4799 argvlist[*argc] = NULL;
4800 return argvlist;
4801fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02004802 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004803 free_string_array(argvlist, *argc);
4804 return NULL;
4805}
4806#endif
4807
4808#ifdef HAVE_EXECV
4809PyDoc_STRVAR(posix_execv__doc__,
4810"execv(path, args)\n\n\
4811Execute an executable path with arguments, replacing current process.\n\
4812\n\
4813 path: path of executable file\n\
4814 args: tuple or list of strings");
4815
4816static PyObject *
4817posix_execv(PyObject *self, PyObject *args)
4818{
4819 PyObject *opath;
4820 char *path;
4821 PyObject *argv;
4822 char **argvlist;
4823 Py_ssize_t argc;
4824
4825 /* execv has two arguments: (path, argv), where
4826 argv is a list or tuple of strings. */
4827
4828 if (!PyArg_ParseTuple(args, "O&O:execv",
4829 PyUnicode_FSConverter,
4830 &opath, &argv))
4831 return NULL;
4832 path = PyBytes_AsString(opath);
4833 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
4834 PyErr_SetString(PyExc_TypeError,
4835 "execv() arg 2 must be a tuple or list");
4836 Py_DECREF(opath);
4837 return NULL;
4838 }
4839 argc = PySequence_Size(argv);
4840 if (argc < 1) {
4841 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
4842 Py_DECREF(opath);
4843 return NULL;
4844 }
4845
4846 argvlist = parse_arglist(argv, &argc);
4847 if (argvlist == NULL) {
4848 Py_DECREF(opath);
4849 return NULL;
4850 }
4851
4852 execv(path, argvlist);
4853
4854 /* If we get here it's definitely an error */
4855
4856 free_string_array(argvlist, argc);
4857 Py_DECREF(opath);
4858 return posix_error();
4859}
4860
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004861PyDoc_STRVAR(posix_execve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004862"execve(path, args, env)\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004863Execute a path with arguments and environment, replacing current process.\n\
4864\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004865 path: path of executable file\n\
4866 args: tuple or list of arguments\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07004867 env: dictionary of strings mapping to strings\n\
4868\n\
4869On some platforms, you may specify an open file descriptor for path;\n\
4870 execve will execute the program the file descriptor is open to.\n\
4871 If this functionality is unavailable, using it raises NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004872
Barry Warsaw53699e91996-12-10 23:23:01 +00004873static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07004874posix_execve(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004875{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004876 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00004877 PyObject *argv, *env;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004878 char **argvlist = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004879 char **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02004880 Py_ssize_t argc, envc;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004881 static char *keywords[] = {"path", "argv", "environment", NULL};
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004882
Victor Stinner8c62be82010-05-06 00:08:46 +00004883 /* execve has three arguments: (path, argv, env), where
4884 argv is a list or tuple of strings and env is a dictionary
4885 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004886
Larry Hastings9cf065c2012-06-22 16:30:09 -07004887 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01004888 path.function_name = "execve";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004889#ifdef HAVE_FEXECVE
4890 path.allow_fd = 1;
4891#endif
4892 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&OO:execve", keywords,
4893 path_converter, &path,
4894 &argv, &env
4895 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00004896 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004897
Ross Lagerwall7807c352011-03-17 20:20:30 +02004898 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004899 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004900 "execve: argv must be a tuple or list");
4901 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004902 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02004903 argc = PySequence_Size(argv);
Victor Stinner8c62be82010-05-06 00:08:46 +00004904 if (!PyMapping_Check(env)) {
4905 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004906 "execve: environment must be a mapping object");
4907 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004908 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004909
Ross Lagerwall7807c352011-03-17 20:20:30 +02004910 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00004911 if (argvlist == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004912 goto fail;
Victor Stinner8c62be82010-05-06 00:08:46 +00004913 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00004914
Victor Stinner8c62be82010-05-06 00:08:46 +00004915 envlist = parse_envlist(env, &envc);
4916 if (envlist == NULL)
Ross Lagerwall7807c352011-03-17 20:20:30 +02004917 goto fail;
4918
Larry Hastings9cf065c2012-06-22 16:30:09 -07004919#ifdef HAVE_FEXECVE
4920 if (path.fd > -1)
4921 fexecve(path.fd, argvlist, envlist);
4922 else
4923#endif
4924 execve(path.narrow, argvlist, envlist);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004925
4926 /* If we get here it's definitely an error */
4927
Victor Stinner292c8352012-10-30 02:17:38 +01004928 path_error(&path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004929
4930 while (--envc >= 0)
4931 PyMem_DEL(envlist[envc]);
4932 PyMem_DEL(envlist);
4933 fail:
Larry Hastings9cf065c2012-06-22 16:30:09 -07004934 if (argvlist)
4935 free_string_array(argvlist, argc);
4936 path_cleanup(&path);
Ross Lagerwall7807c352011-03-17 20:20:30 +02004937 return NULL;
4938}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004939#endif /* HAVE_EXECV */
4940
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004941
Guido van Rossuma1065681999-01-25 23:20:23 +00004942#ifdef HAVE_SPAWNV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004943PyDoc_STRVAR(posix_spawnv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00004944"spawnv(mode, path, args)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00004945Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00004946\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00004947 mode: mode of process creation\n\
4948 path: path of executable file\n\
4949 args: tuple or list of strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00004950
4951static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00004952posix_spawnv(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00004953{
Victor Stinner8c62be82010-05-06 00:08:46 +00004954 PyObject *opath;
4955 char *path;
4956 PyObject *argv;
4957 char **argvlist;
4958 int mode, i;
4959 Py_ssize_t argc;
4960 Py_intptr_t spawnval;
4961 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00004962
Victor Stinner8c62be82010-05-06 00:08:46 +00004963 /* spawnv has three arguments: (mode, path, argv), where
4964 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00004965
Victor Stinner8c62be82010-05-06 00:08:46 +00004966 if (!PyArg_ParseTuple(args, "iO&O:spawnv", &mode,
4967 PyUnicode_FSConverter,
4968 &opath, &argv))
4969 return NULL;
4970 path = PyBytes_AsString(opath);
4971 if (PyList_Check(argv)) {
4972 argc = PyList_Size(argv);
4973 getitem = PyList_GetItem;
4974 }
4975 else if (PyTuple_Check(argv)) {
4976 argc = PyTuple_Size(argv);
4977 getitem = PyTuple_GetItem;
4978 }
4979 else {
4980 PyErr_SetString(PyExc_TypeError,
4981 "spawnv() arg 2 must be a tuple or list");
4982 Py_DECREF(opath);
4983 return NULL;
4984 }
Guido van Rossuma1065681999-01-25 23:20:23 +00004985
Victor Stinner8c62be82010-05-06 00:08:46 +00004986 argvlist = PyMem_NEW(char *, argc+1);
4987 if (argvlist == NULL) {
4988 Py_DECREF(opath);
4989 return PyErr_NoMemory();
4990 }
4991 for (i = 0; i < argc; i++) {
4992 if (!fsconvert_strdup((*getitem)(argv, i),
4993 &argvlist[i])) {
4994 free_string_array(argvlist, i);
4995 PyErr_SetString(
4996 PyExc_TypeError,
4997 "spawnv() arg 2 must contain only strings");
4998 Py_DECREF(opath);
4999 return NULL;
5000 }
5001 }
5002 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005003
Victor Stinner8c62be82010-05-06 00:08:46 +00005004 if (mode == _OLD_P_OVERLAY)
5005 mode = _P_OVERLAY;
Tim Peters5aa91602002-01-30 05:46:57 +00005006
Victor Stinner8c62be82010-05-06 00:08:46 +00005007 Py_BEGIN_ALLOW_THREADS
5008 spawnval = _spawnv(mode, path, argvlist);
5009 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005010
Victor Stinner8c62be82010-05-06 00:08:46 +00005011 free_string_array(argvlist, argc);
5012 Py_DECREF(opath);
Guido van Rossuma1065681999-01-25 23:20:23 +00005013
Victor Stinner8c62be82010-05-06 00:08:46 +00005014 if (spawnval == -1)
5015 return posix_error();
5016 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00005017#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00005018 return Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005019#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005020 return Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005021#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00005022}
5023
5024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005025PyDoc_STRVAR(posix_spawnve__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005026"spawnve(mode, path, args, env)\n\n\
Tim Peters25059d32001-12-07 20:35:43 +00005027Execute the program 'path' in a new process.\n\
Guido van Rossuma1065681999-01-25 23:20:23 +00005028\n\
Victor Stinner8c62be82010-05-06 00:08:46 +00005029 mode: mode of process creation\n\
5030 path: path of executable file\n\
5031 args: tuple or list of arguments\n\
5032 env: dictionary of strings mapping to strings");
Guido van Rossuma1065681999-01-25 23:20:23 +00005033
5034static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00005035posix_spawnve(PyObject *self, PyObject *args)
Guido van Rossuma1065681999-01-25 23:20:23 +00005036{
Victor Stinner8c62be82010-05-06 00:08:46 +00005037 PyObject *opath;
5038 char *path;
5039 PyObject *argv, *env;
5040 char **argvlist;
5041 char **envlist;
5042 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00005043 int mode;
5044 Py_ssize_t argc, i, envc;
Victor Stinner8c62be82010-05-06 00:08:46 +00005045 Py_intptr_t spawnval;
5046 PyObject *(*getitem)(PyObject *, Py_ssize_t);
5047 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00005048
Victor Stinner8c62be82010-05-06 00:08:46 +00005049 /* spawnve has four arguments: (mode, path, argv, env), where
5050 argv is a list or tuple of strings and env is a dictionary
5051 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005052
Victor Stinner8c62be82010-05-06 00:08:46 +00005053 if (!PyArg_ParseTuple(args, "iO&OO:spawnve", &mode,
5054 PyUnicode_FSConverter,
5055 &opath, &argv, &env))
5056 return NULL;
5057 path = PyBytes_AsString(opath);
5058 if (PyList_Check(argv)) {
5059 argc = PyList_Size(argv);
5060 getitem = PyList_GetItem;
5061 }
5062 else if (PyTuple_Check(argv)) {
5063 argc = PyTuple_Size(argv);
5064 getitem = PyTuple_GetItem;
5065 }
5066 else {
5067 PyErr_SetString(PyExc_TypeError,
5068 "spawnve() arg 2 must be a tuple or list");
5069 goto fail_0;
5070 }
5071 if (!PyMapping_Check(env)) {
5072 PyErr_SetString(PyExc_TypeError,
5073 "spawnve() arg 3 must be a mapping object");
5074 goto fail_0;
5075 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005076
Victor Stinner8c62be82010-05-06 00:08:46 +00005077 argvlist = PyMem_NEW(char *, argc+1);
5078 if (argvlist == NULL) {
5079 PyErr_NoMemory();
5080 goto fail_0;
5081 }
5082 for (i = 0; i < argc; i++) {
5083 if (!fsconvert_strdup((*getitem)(argv, i),
5084 &argvlist[i]))
5085 {
5086 lastarg = i;
5087 goto fail_1;
5088 }
5089 }
5090 lastarg = argc;
5091 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005092
Victor Stinner8c62be82010-05-06 00:08:46 +00005093 envlist = parse_envlist(env, &envc);
5094 if (envlist == NULL)
5095 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00005096
Victor Stinner8c62be82010-05-06 00:08:46 +00005097 if (mode == _OLD_P_OVERLAY)
5098 mode = _P_OVERLAY;
Tim Peters25059d32001-12-07 20:35:43 +00005099
Victor Stinner8c62be82010-05-06 00:08:46 +00005100 Py_BEGIN_ALLOW_THREADS
5101 spawnval = _spawnve(mode, path, argvlist, envlist);
5102 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00005103
Victor Stinner8c62be82010-05-06 00:08:46 +00005104 if (spawnval == -1)
5105 (void) posix_error();
5106 else
Fredrik Lundhe25cfd82000-07-09 13:10:40 +00005107#if SIZEOF_LONG == SIZEOF_VOID_P
Victor Stinner8c62be82010-05-06 00:08:46 +00005108 res = Py_BuildValue("l", (long) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005109#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 res = Py_BuildValue("L", (PY_LONG_LONG) spawnval);
Fred Drake699f3522000-06-29 21:12:41 +00005111#endif
Guido van Rossuma1065681999-01-25 23:20:23 +00005112
Victor Stinner8c62be82010-05-06 00:08:46 +00005113 while (--envc >= 0)
5114 PyMem_DEL(envlist[envc]);
5115 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00005116 fail_1:
Victor Stinner8c62be82010-05-06 00:08:46 +00005117 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005118 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00005119 Py_DECREF(opath);
5120 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00005121}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00005122
Guido van Rossuma1065681999-01-25 23:20:23 +00005123#endif /* HAVE_SPAWNV */
5124
5125
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005126#ifdef HAVE_FORK1
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005127PyDoc_STRVAR(posix_fork1__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005128"fork1() -> pid\n\n\
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005129Fork a child process with a single multiplexed (i.e., not bound) thread.\n\
5130\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005131Return 0 to child process and PID of child to parent process.");
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005132
5133static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005134posix_fork1(PyObject *self, PyObject *noargs)
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005135{
Victor Stinner8c62be82010-05-06 00:08:46 +00005136 pid_t pid;
5137 int result = 0;
5138 _PyImport_AcquireLock();
5139 pid = fork1();
5140 if (pid == 0) {
5141 /* child: this clobbers and resets the import lock. */
5142 PyOS_AfterFork();
5143 } else {
5144 /* parent: release the import lock. */
5145 result = _PyImport_ReleaseLock();
5146 }
5147 if (pid == -1)
5148 return posix_error();
5149 if (result < 0) {
5150 /* Don't clobber the OSError if the fork failed. */
5151 PyErr_SetString(PyExc_RuntimeError,
5152 "not holding the import lock");
5153 return NULL;
5154 }
5155 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00005156}
5157#endif
5158
5159
Guido van Rossumad0ee831995-03-01 10:34:45 +00005160#ifdef HAVE_FORK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005161PyDoc_STRVAR(posix_fork__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005162"fork() -> pid\n\n\
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005163Fork a child process.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005164Return 0 to child process and PID of child to parent process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005165
Barry Warsaw53699e91996-12-10 23:23:01 +00005166static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005167posix_fork(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005168{
Victor Stinner8c62be82010-05-06 00:08:46 +00005169 pid_t pid;
5170 int result = 0;
5171 _PyImport_AcquireLock();
5172 pid = fork();
5173 if (pid == 0) {
5174 /* child: this clobbers and resets the import lock. */
5175 PyOS_AfterFork();
5176 } else {
5177 /* parent: release the import lock. */
5178 result = _PyImport_ReleaseLock();
5179 }
5180 if (pid == -1)
5181 return posix_error();
5182 if (result < 0) {
5183 /* Don't clobber the OSError if the fork failed. */
5184 PyErr_SetString(PyExc_RuntimeError,
5185 "not holding the import lock");
5186 return NULL;
5187 }
5188 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00005189}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005190#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00005191
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005192#ifdef HAVE_SCHED_H
5193
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005194#ifdef HAVE_SCHED_GET_PRIORITY_MAX
5195
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005196PyDoc_STRVAR(posix_sched_get_priority_max__doc__,
5197"sched_get_priority_max(policy)\n\n\
5198Get the maximum scheduling priority for *policy*.");
5199
5200static PyObject *
5201posix_sched_get_priority_max(PyObject *self, PyObject *args)
5202{
5203 int policy, max;
5204
5205 if (!PyArg_ParseTuple(args, "i:sched_get_priority_max", &policy))
5206 return NULL;
5207 max = sched_get_priority_max(policy);
5208 if (max < 0)
5209 return posix_error();
5210 return PyLong_FromLong(max);
5211}
5212
5213PyDoc_STRVAR(posix_sched_get_priority_min__doc__,
5214"sched_get_priority_min(policy)\n\n\
5215Get the minimum scheduling priority for *policy*.");
5216
5217static PyObject *
5218posix_sched_get_priority_min(PyObject *self, PyObject *args)
5219{
5220 int policy, min;
5221
5222 if (!PyArg_ParseTuple(args, "i:sched_get_priority_min", &policy))
5223 return NULL;
5224 min = sched_get_priority_min(policy);
5225 if (min < 0)
5226 return posix_error();
5227 return PyLong_FromLong(min);
5228}
5229
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02005230#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
5231
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005232#ifdef HAVE_SCHED_SETSCHEDULER
5233
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005234PyDoc_STRVAR(posix_sched_getscheduler__doc__,
5235"sched_getscheduler(pid)\n\n\
5236Get the scheduling policy for the process with a PID of *pid*.\n\
5237Passing a PID of 0 returns the scheduling policy for the calling process.");
5238
5239static PyObject *
5240posix_sched_getscheduler(PyObject *self, PyObject *args)
5241{
5242 pid_t pid;
5243 int policy;
5244
5245 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getscheduler", &pid))
5246 return NULL;
5247 policy = sched_getscheduler(pid);
5248 if (policy < 0)
5249 return posix_error();
5250 return PyLong_FromLong(policy);
5251}
5252
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005253#endif
5254
5255#if defined(HAVE_SCHED_SETSCHEDULER) || defined(HAVE_SCHED_SETPARAM)
5256
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005257static PyObject *
5258sched_param_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
5259{
5260 PyObject *res, *priority;
Benjamin Peterson4e36d5a2011-08-02 19:56:11 -05005261 static char *kwlist[] = {"sched_priority", NULL};
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005262
5263 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:sched_param", kwlist, &priority))
5264 return NULL;
5265 res = PyStructSequence_New(type);
5266 if (!res)
5267 return NULL;
5268 Py_INCREF(priority);
5269 PyStructSequence_SET_ITEM(res, 0, priority);
5270 return res;
5271}
5272
5273PyDoc_STRVAR(sched_param__doc__,
5274"sched_param(sched_priority): A scheduling parameter.\n\n\
5275Current has only one field: sched_priority");
5276
5277static PyStructSequence_Field sched_param_fields[] = {
5278 {"sched_priority", "the scheduling priority"},
5279 {0}
5280};
5281
5282static PyStructSequence_Desc sched_param_desc = {
5283 "sched_param", /* name */
5284 sched_param__doc__, /* doc */
5285 sched_param_fields,
5286 1
5287};
5288
5289static int
5290convert_sched_param(PyObject *param, struct sched_param *res)
5291{
5292 long priority;
5293
5294 if (Py_TYPE(param) != &SchedParamType) {
5295 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
5296 return 0;
5297 }
5298 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
5299 if (priority == -1 && PyErr_Occurred())
5300 return 0;
5301 if (priority > INT_MAX || priority < INT_MIN) {
5302 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
5303 return 0;
5304 }
5305 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
5306 return 1;
5307}
5308
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005309#endif
5310
5311#ifdef HAVE_SCHED_SETSCHEDULER
5312
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005313PyDoc_STRVAR(posix_sched_setscheduler__doc__,
5314"sched_setscheduler(pid, policy, param)\n\n\
5315Set the scheduling policy, *policy*, for *pid*.\n\
5316If *pid* is 0, the calling process is changed.\n\
5317*param* is an instance of sched_param.");
5318
5319static PyObject *
5320posix_sched_setscheduler(PyObject *self, PyObject *args)
5321{
5322 pid_t pid;
5323 int policy;
5324 struct sched_param param;
5325
5326 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "iO&:sched_setscheduler",
5327 &pid, &policy, &convert_sched_param, &param))
5328 return NULL;
Jesus Cea9c822272011-09-10 01:40:52 +02005329
5330 /*
Jesus Cea54b01492011-09-10 01:53:19 +02005331 ** sched_setscheduler() returns 0 in Linux, but the previous
5332 ** scheduling policy under Solaris/Illumos, and others.
5333 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02005334 */
5335 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005336 return posix_error();
5337 Py_RETURN_NONE;
5338}
5339
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005340#endif
5341
5342#ifdef HAVE_SCHED_SETPARAM
5343
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005344PyDoc_STRVAR(posix_sched_getparam__doc__,
5345"sched_getparam(pid) -> sched_param\n\n\
5346Returns scheduling parameters for the process with *pid* as an instance of the\n\
5347sched_param class. A PID of 0 means the calling process.");
5348
5349static PyObject *
5350posix_sched_getparam(PyObject *self, PyObject *args)
5351{
5352 pid_t pid;
5353 struct sched_param param;
5354 PyObject *res, *priority;
5355
5356 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getparam", &pid))
5357 return NULL;
5358 if (sched_getparam(pid, &param))
5359 return posix_error();
5360 res = PyStructSequence_New(&SchedParamType);
5361 if (!res)
5362 return NULL;
5363 priority = PyLong_FromLong(param.sched_priority);
5364 if (!priority) {
5365 Py_DECREF(res);
5366 return NULL;
5367 }
5368 PyStructSequence_SET_ITEM(res, 0, priority);
5369 return res;
5370}
5371
5372PyDoc_STRVAR(posix_sched_setparam__doc__,
5373"sched_setparam(pid, param)\n\n\
5374Set scheduling parameters for a process with PID *pid*.\n\
5375A PID of 0 means the calling process.");
5376
5377static PyObject *
5378posix_sched_setparam(PyObject *self, PyObject *args)
5379{
5380 pid_t pid;
5381 struct sched_param param;
5382
5383 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O&:sched_setparam",
5384 &pid, &convert_sched_param, &param))
5385 return NULL;
5386 if (sched_setparam(pid, &param))
5387 return posix_error();
5388 Py_RETURN_NONE;
5389}
5390
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005391#endif
5392
5393#ifdef HAVE_SCHED_RR_GET_INTERVAL
5394
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005395PyDoc_STRVAR(posix_sched_rr_get_interval__doc__,
5396"sched_rr_get_interval(pid) -> float\n\n\
5397Return the round-robin quantum for the process with PID *pid* in seconds.");
5398
5399static PyObject *
5400posix_sched_rr_get_interval(PyObject *self, PyObject *args)
5401{
5402 pid_t pid;
5403 struct timespec interval;
5404
5405 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_rr_get_interval", &pid))
5406 return NULL;
5407 if (sched_rr_get_interval(pid, &interval))
5408 return posix_error();
5409 return PyFloat_FromDouble((double)interval.tv_sec + 1e-9*interval.tv_nsec);
5410}
5411
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05005412#endif
5413
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005414PyDoc_STRVAR(posix_sched_yield__doc__,
5415"sched_yield()\n\n\
5416Voluntarily relinquish the CPU.");
5417
5418static PyObject *
5419posix_sched_yield(PyObject *self, PyObject *noargs)
5420{
5421 if (sched_yield())
5422 return posix_error();
5423 Py_RETURN_NONE;
5424}
5425
Benjamin Peterson2740af82011-08-02 17:41:34 -05005426#ifdef HAVE_SCHED_SETAFFINITY
5427
Antoine Pitrou84869872012-08-04 16:16:35 +02005428/* The minimum number of CPUs allocated in a cpu_set_t */
5429static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005430
5431PyDoc_STRVAR(posix_sched_setaffinity__doc__,
5432"sched_setaffinity(pid, cpu_set)\n\n\
5433Set the affinity of the process with PID *pid* to *cpu_set*.");
5434
5435static PyObject *
5436posix_sched_setaffinity(PyObject *self, PyObject *args)
5437{
5438 pid_t pid;
Antoine Pitrou84869872012-08-04 16:16:35 +02005439 int ncpus;
5440 size_t setsize;
5441 cpu_set_t *mask = NULL;
5442 PyObject *iterable, *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005443
Antoine Pitrou84869872012-08-04 16:16:35 +02005444 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "O:sched_setaffinity",
5445 &pid, &iterable))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005446 return NULL;
Antoine Pitrou84869872012-08-04 16:16:35 +02005447
5448 iterator = PyObject_GetIter(iterable);
5449 if (iterator == NULL)
5450 return NULL;
5451
5452 ncpus = NCPUS_START;
5453 setsize = CPU_ALLOC_SIZE(ncpus);
5454 mask = CPU_ALLOC(ncpus);
5455 if (mask == NULL) {
5456 PyErr_NoMemory();
5457 goto error;
5458 }
5459 CPU_ZERO_S(setsize, mask);
5460
5461 while ((item = PyIter_Next(iterator))) {
5462 long cpu;
5463 if (!PyLong_Check(item)) {
5464 PyErr_Format(PyExc_TypeError,
5465 "expected an iterator of ints, "
5466 "but iterator yielded %R",
5467 Py_TYPE(item));
5468 Py_DECREF(item);
5469 goto error;
5470 }
5471 cpu = PyLong_AsLong(item);
5472 Py_DECREF(item);
5473 if (cpu < 0) {
5474 if (!PyErr_Occurred())
5475 PyErr_SetString(PyExc_ValueError, "negative CPU number");
5476 goto error;
5477 }
5478 if (cpu > INT_MAX - 1) {
5479 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
5480 goto error;
5481 }
5482 if (cpu >= ncpus) {
5483 /* Grow CPU mask to fit the CPU number */
5484 int newncpus = ncpus;
5485 cpu_set_t *newmask;
5486 size_t newsetsize;
5487 while (newncpus <= cpu) {
5488 if (newncpus > INT_MAX / 2)
5489 newncpus = cpu + 1;
5490 else
5491 newncpus = newncpus * 2;
5492 }
5493 newmask = CPU_ALLOC(newncpus);
5494 if (newmask == NULL) {
5495 PyErr_NoMemory();
5496 goto error;
5497 }
5498 newsetsize = CPU_ALLOC_SIZE(newncpus);
5499 CPU_ZERO_S(newsetsize, newmask);
5500 memcpy(newmask, mask, setsize);
5501 CPU_FREE(mask);
5502 setsize = newsetsize;
5503 mask = newmask;
5504 ncpus = newncpus;
5505 }
5506 CPU_SET_S(cpu, setsize, mask);
5507 }
5508 Py_CLEAR(iterator);
5509
5510 if (sched_setaffinity(pid, setsize, mask)) {
5511 posix_error();
5512 goto error;
5513 }
5514 CPU_FREE(mask);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005515 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02005516
5517error:
5518 if (mask)
5519 CPU_FREE(mask);
5520 Py_XDECREF(iterator);
5521 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005522}
5523
5524PyDoc_STRVAR(posix_sched_getaffinity__doc__,
5525"sched_getaffinity(pid, ncpus) -> cpu_set\n\n\
5526Return the affinity of the process with PID *pid*.\n\
5527The returned cpu_set will be of size *ncpus*.");
5528
5529static PyObject *
5530posix_sched_getaffinity(PyObject *self, PyObject *args)
5531{
5532 pid_t pid;
Antoine Pitrou84869872012-08-04 16:16:35 +02005533 int cpu, ncpus, count;
5534 size_t setsize;
5535 cpu_set_t *mask = NULL;
5536 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005537
Antoine Pitrou84869872012-08-04 16:16:35 +02005538 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":sched_getaffinity",
5539 &pid))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005540 return NULL;
Antoine Pitrou84869872012-08-04 16:16:35 +02005541
5542 ncpus = NCPUS_START;
5543 while (1) {
5544 setsize = CPU_ALLOC_SIZE(ncpus);
5545 mask = CPU_ALLOC(ncpus);
5546 if (mask == NULL)
5547 return PyErr_NoMemory();
5548 if (sched_getaffinity(pid, setsize, mask) == 0)
5549 break;
5550 CPU_FREE(mask);
5551 if (errno != EINVAL)
5552 return posix_error();
5553 if (ncpus > INT_MAX / 2) {
5554 PyErr_SetString(PyExc_OverflowError, "could not allocate "
5555 "a large enough CPU set");
5556 return NULL;
5557 }
5558 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005559 }
Antoine Pitrou84869872012-08-04 16:16:35 +02005560
5561 res = PySet_New(NULL);
5562 if (res == NULL)
5563 goto error;
5564 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
5565 if (CPU_ISSET_S(cpu, setsize, mask)) {
5566 PyObject *cpu_num = PyLong_FromLong(cpu);
5567 --count;
5568 if (cpu_num == NULL)
5569 goto error;
5570 if (PySet_Add(res, cpu_num)) {
5571 Py_DECREF(cpu_num);
5572 goto error;
5573 }
5574 Py_DECREF(cpu_num);
5575 }
5576 }
5577 CPU_FREE(mask);
5578 return res;
5579
5580error:
5581 if (mask)
5582 CPU_FREE(mask);
5583 Py_XDECREF(res);
5584 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005585}
5586
Benjamin Peterson2740af82011-08-02 17:41:34 -05005587#endif /* HAVE_SCHED_SETAFFINITY */
5588
Benjamin Peterson94b580d2011-08-02 17:30:04 -05005589#endif /* HAVE_SCHED_H */
5590
Neal Norwitzb59798b2003-03-21 01:43:31 +00005591/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00005592/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
5593#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00005594#define DEV_PTY_FILE "/dev/ptc"
5595#define HAVE_DEV_PTMX
5596#else
5597#define DEV_PTY_FILE "/dev/ptmx"
5598#endif
5599
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005600#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005601#ifdef HAVE_PTY_H
5602#include <pty.h>
5603#else
5604#ifdef HAVE_LIBUTIL_H
5605#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00005606#else
5607#ifdef HAVE_UTIL_H
5608#include <util.h>
5609#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005610#endif /* HAVE_LIBUTIL_H */
5611#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00005612#ifdef HAVE_STROPTS_H
5613#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005614#endif
5615#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005616
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005617#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005618PyDoc_STRVAR(posix_openpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005619"openpty() -> (master_fd, slave_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005620Open a pseudo-terminal, returning open fd's for both master and slave end.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005621
5622static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005623posix_openpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005624{
Victor Stinner8c62be82010-05-06 00:08:46 +00005625 int master_fd, slave_fd;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005626#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005627 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00005628#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005629#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005630 PyOS_sighandler_t sig_saved;
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005631#ifdef sun
Victor Stinner8c62be82010-05-06 00:08:46 +00005632 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005633#endif
5634#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00005635
Thomas Wouters70c21a12000-07-14 14:28:33 +00005636#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00005637 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
5638 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005639#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00005640 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
5641 if (slave_name == NULL)
5642 return posix_error();
Thomas Wouters70c21a12000-07-14 14:28:33 +00005643
Victor Stinner8c62be82010-05-06 00:08:46 +00005644 slave_fd = open(slave_name, O_RDWR);
5645 if (slave_fd < 0)
5646 return posix_error();
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005647#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005648 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
5649 if (master_fd < 0)
5650 return posix_error();
5651 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
5652 /* change permission of slave */
5653 if (grantpt(master_fd) < 0) {
5654 PyOS_setsig(SIGCHLD, sig_saved);
5655 return posix_error();
5656 }
5657 /* unlock slave */
5658 if (unlockpt(master_fd) < 0) {
5659 PyOS_setsig(SIGCHLD, sig_saved);
5660 return posix_error();
5661 }
5662 PyOS_setsig(SIGCHLD, sig_saved);
5663 slave_name = ptsname(master_fd); /* get name of slave */
5664 if (slave_name == NULL)
5665 return posix_error();
5666 slave_fd = open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
5667 if (slave_fd < 0)
5668 return posix_error();
Neal Norwitzb59798b2003-03-21 01:43:31 +00005669#if !defined(__CYGWIN__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00005670 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
5671 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00005672#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00005673 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00005674#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005675#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00005676#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00005677
Victor Stinner8c62be82010-05-06 00:08:46 +00005678 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00005679
Fred Drake8cef4cf2000-06-28 16:40:38 +00005680}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00005681#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00005682
5683#ifdef HAVE_FORKPTY
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005684PyDoc_STRVAR(posix_forkpty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005685"forkpty() -> (pid, master_fd)\n\n\
Fred Drake8cef4cf2000-06-28 16:40:38 +00005686Fork a new process with a new pseudo-terminal as controlling tty.\n\n\
5687Like fork(), return 0 as pid to child process, and PID of child to parent.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005688To both, return fd of newly opened pseudo-terminal.\n");
Fred Drake8cef4cf2000-06-28 16:40:38 +00005689
5690static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005691posix_forkpty(PyObject *self, PyObject *noargs)
Fred Drake8cef4cf2000-06-28 16:40:38 +00005692{
Victor Stinner8c62be82010-05-06 00:08:46 +00005693 int master_fd = -1, result = 0;
5694 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00005695
Victor Stinner8c62be82010-05-06 00:08:46 +00005696 _PyImport_AcquireLock();
5697 pid = forkpty(&master_fd, NULL, NULL, NULL);
5698 if (pid == 0) {
5699 /* child: this clobbers and resets the import lock. */
5700 PyOS_AfterFork();
5701 } else {
5702 /* parent: release the import lock. */
5703 result = _PyImport_ReleaseLock();
5704 }
5705 if (pid == -1)
5706 return posix_error();
5707 if (result < 0) {
5708 /* Don't clobber the OSError if the fork failed. */
5709 PyErr_SetString(PyExc_RuntimeError,
5710 "not holding the import lock");
5711 return NULL;
5712 }
5713 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00005714}
5715#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005716
Ross Lagerwall7807c352011-03-17 20:20:30 +02005717
Guido van Rossumad0ee831995-03-01 10:34:45 +00005718#ifdef HAVE_GETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005719PyDoc_STRVAR(posix_getegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005720"getegid() -> egid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005721Return the current process's effective group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005722
Barry Warsaw53699e91996-12-10 23:23:01 +00005723static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005724posix_getegid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005725{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005726 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005727}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005728#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005729
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005730
Guido van Rossumad0ee831995-03-01 10:34:45 +00005731#ifdef HAVE_GETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005732PyDoc_STRVAR(posix_geteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005733"geteuid() -> euid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005734Return the current process's effective user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005735
Barry Warsaw53699e91996-12-10 23:23:01 +00005736static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005737posix_geteuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005738{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005739 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005740}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005741#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005742
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005743
Guido van Rossumad0ee831995-03-01 10:34:45 +00005744#ifdef HAVE_GETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005745PyDoc_STRVAR(posix_getgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005746"getgid() -> gid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005747Return the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005748
Barry Warsaw53699e91996-12-10 23:23:01 +00005749static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005750posix_getgid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00005751{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005752 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00005753}
Guido van Rossumad0ee831995-03-01 10:34:45 +00005754#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00005755
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005756
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005757PyDoc_STRVAR(posix_getpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005758"getpid() -> pid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005759Return the current process id");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005760
Barry Warsaw53699e91996-12-10 23:23:01 +00005761static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005762posix_getpid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00005763{
Victor Stinner8c62be82010-05-06 00:08:46 +00005764 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00005765}
5766
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005767#ifdef HAVE_GETGROUPLIST
5768PyDoc_STRVAR(posix_getgrouplist__doc__,
5769"getgrouplist(user, group) -> list of groups to which a user belongs\n\n\
5770Returns a list of groups to which a user belongs.\n\n\
5771 user: username to lookup\n\
5772 group: base group id of the user");
5773
5774static PyObject *
5775posix_getgrouplist(PyObject *self, PyObject *args)
5776{
5777#ifdef NGROUPS_MAX
5778#define MAX_GROUPS NGROUPS_MAX
5779#else
5780 /* defined to be 16 on Solaris7, so this should be a small number */
5781#define MAX_GROUPS 64
5782#endif
5783
5784 const char *user;
5785 int i, ngroups;
5786 PyObject *list;
5787#ifdef __APPLE__
5788 int *groups, basegid;
5789#else
5790 gid_t *groups, basegid;
5791#endif
5792 ngroups = MAX_GROUPS;
5793
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005794#ifdef __APPLE__
5795 if (!PyArg_ParseTuple(args, "si:getgrouplist", &user, &basegid))
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005796 return NULL;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005797#else
5798 if (!PyArg_ParseTuple(args, "sO&:getgrouplist", &user,
5799 _Py_Gid_Converter, &basegid))
5800 return NULL;
5801#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005802
5803#ifdef __APPLE__
5804 groups = PyMem_Malloc(ngroups * sizeof(int));
5805#else
5806 groups = PyMem_Malloc(ngroups * sizeof(gid_t));
5807#endif
5808 if (groups == NULL)
5809 return PyErr_NoMemory();
5810
5811 if (getgrouplist(user, basegid, groups, &ngroups) == -1) {
5812 PyMem_Del(groups);
5813 return posix_error();
5814 }
5815
5816 list = PyList_New(ngroups);
5817 if (list == NULL) {
5818 PyMem_Del(groups);
5819 return NULL;
5820 }
5821
5822 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005823#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005824 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005825#else
5826 PyObject *o = _PyLong_FromGid(groups[i]);
5827#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02005828 if (o == NULL) {
5829 Py_DECREF(list);
5830 PyMem_Del(groups);
5831 return NULL;
5832 }
5833 PyList_SET_ITEM(list, i, o);
5834 }
5835
5836 PyMem_Del(groups);
5837
5838 return list;
5839}
5840#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005841
Fred Drakec9680921999-12-13 16:37:25 +00005842#ifdef HAVE_GETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005843PyDoc_STRVAR(posix_getgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005844"getgroups() -> list of group IDs\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005845Return list of supplemental group IDs for the process.");
Fred Drakec9680921999-12-13 16:37:25 +00005846
5847static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005848posix_getgroups(PyObject *self, PyObject *noargs)
Fred Drakec9680921999-12-13 16:37:25 +00005849{
5850 PyObject *result = NULL;
5851
Fred Drakec9680921999-12-13 16:37:25 +00005852#ifdef NGROUPS_MAX
5853#define MAX_GROUPS NGROUPS_MAX
5854#else
Victor Stinner8c62be82010-05-06 00:08:46 +00005855 /* defined to be 16 on Solaris7, so this should be a small number */
Fred Drakec9680921999-12-13 16:37:25 +00005856#define MAX_GROUPS 64
5857#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005858 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005859
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00005860 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005861 * This is a helper variable to store the intermediate result when
5862 * that happens.
5863 *
5864 * To keep the code readable the OSX behaviour is unconditional,
5865 * according to the POSIX spec this should be safe on all unix-y
5866 * systems.
5867 */
5868 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00005869 int n;
Fred Drakec9680921999-12-13 16:37:25 +00005870
Victor Stinner8c62be82010-05-06 00:08:46 +00005871 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005872 if (n < 0) {
5873 if (errno == EINVAL) {
5874 n = getgroups(0, NULL);
5875 if (n == -1) {
5876 return posix_error();
5877 }
5878 if (n == 0) {
5879 /* Avoid malloc(0) */
5880 alt_grouplist = grouplist;
5881 } else {
5882 alt_grouplist = PyMem_Malloc(n * sizeof(gid_t));
5883 if (alt_grouplist == NULL) {
5884 errno = EINVAL;
5885 return posix_error();
5886 }
5887 n = getgroups(n, alt_grouplist);
5888 if (n == -1) {
5889 PyMem_Free(alt_grouplist);
5890 return posix_error();
5891 }
5892 }
5893 } else {
5894 return posix_error();
5895 }
5896 }
5897 result = PyList_New(n);
5898 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005899 int i;
5900 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005901 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00005902 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00005903 Py_DECREF(result);
5904 result = NULL;
5905 break;
Fred Drakec9680921999-12-13 16:37:25 +00005906 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005907 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00005908 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00005909 }
5910
5911 if (alt_grouplist != grouplist) {
5912 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00005913 }
Neal Norwitze241ce82003-02-17 18:17:05 +00005914
Fred Drakec9680921999-12-13 16:37:25 +00005915 return result;
5916}
5917#endif
5918
Antoine Pitroub7572f02009-12-02 20:46:48 +00005919#ifdef HAVE_INITGROUPS
5920PyDoc_STRVAR(posix_initgroups__doc__,
5921"initgroups(username, gid) -> None\n\n\
5922Call the system initgroups() to initialize the group access list with all of\n\
5923the groups of which the specified username is a member, plus the specified\n\
5924group id.");
5925
5926static PyObject *
5927posix_initgroups(PyObject *self, PyObject *args)
5928{
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005929 PyObject *oname;
Victor Stinner8c62be82010-05-06 00:08:46 +00005930 char *username;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005931 int res;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005932#ifdef __APPLE__
5933 int gid;
5934#else
5935 gid_t gid;
5936#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00005937
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005938#ifdef __APPLE__
5939 if (!PyArg_ParseTuple(args, "O&i:initgroups",
5940 PyUnicode_FSConverter, &oname,
5941 &gid))
5942#else
5943 if (!PyArg_ParseTuple(args, "O&O&:initgroups",
5944 PyUnicode_FSConverter, &oname,
5945 _Py_Gid_Converter, &gid))
5946#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00005947 return NULL;
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005948 username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005949
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02005950 res = initgroups(username, gid);
Victor Stinner61ec5dc2010-08-15 09:22:44 +00005951 Py_DECREF(oname);
5952 if (res == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00005953 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00005954
Victor Stinner8c62be82010-05-06 00:08:46 +00005955 Py_INCREF(Py_None);
5956 return Py_None;
Antoine Pitroub7572f02009-12-02 20:46:48 +00005957}
5958#endif
5959
Martin v. Löwis606edc12002-06-13 21:09:11 +00005960#ifdef HAVE_GETPGID
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005961PyDoc_STRVAR(posix_getpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005962"getpgid(pid) -> pgid\n\n\
Neal Norwitz0c2c17c2002-06-13 21:22:11 +00005963Call the system call getpgid().");
Martin v. Löwis606edc12002-06-13 21:09:11 +00005964
5965static PyObject *
5966posix_getpgid(PyObject *self, PyObject *args)
5967{
Victor Stinner8c62be82010-05-06 00:08:46 +00005968 pid_t pid, pgid;
5969 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getpgid", &pid))
5970 return NULL;
5971 pgid = getpgid(pid);
5972 if (pgid < 0)
5973 return posix_error();
5974 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00005975}
5976#endif /* HAVE_GETPGID */
5977
5978
Guido van Rossumb6775db1994-08-01 11:34:53 +00005979#ifdef HAVE_GETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005980PyDoc_STRVAR(posix_getpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005981"getpgrp() -> pgrp\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005982Return the current process group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005983
Barry Warsaw53699e91996-12-10 23:23:01 +00005984static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00005985posix_getpgrp(PyObject *self, PyObject *noargs)
Guido van Rossum04814471991-06-04 20:23:49 +00005986{
Guido van Rossumb6775db1994-08-01 11:34:53 +00005987#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00005988 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005989#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00005990 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00005991#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00005992}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005993#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00005994
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005995
Guido van Rossumb6775db1994-08-01 11:34:53 +00005996#ifdef HAVE_SETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00005997PyDoc_STRVAR(posix_setpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00005998"setpgrp()\n\n\
Senthil Kumaran684760a2010-06-17 16:48:06 +00005999Make this process the process group leader.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006000
Barry Warsaw53699e91996-12-10 23:23:01 +00006001static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006002posix_setpgrp(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00006003{
Guido van Rossum64933891994-10-20 21:56:42 +00006004#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00006005 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006006#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006007 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00006008#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00006009 return posix_error();
6010 Py_INCREF(Py_None);
6011 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00006012}
6013
Guido van Rossumb6775db1994-08-01 11:34:53 +00006014#endif /* HAVE_SETPGRP */
6015
Guido van Rossumad0ee831995-03-01 10:34:45 +00006016#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006017
6018#ifdef MS_WINDOWS
6019#include <tlhelp32.h>
6020
6021static PyObject*
6022win32_getppid()
6023{
6024 HANDLE snapshot;
6025 pid_t mypid;
6026 PyObject* result = NULL;
6027 BOOL have_record;
6028 PROCESSENTRY32 pe;
6029
6030 mypid = getpid(); /* This function never fails */
6031
6032 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
6033 if (snapshot == INVALID_HANDLE_VALUE)
6034 return PyErr_SetFromWindowsErr(GetLastError());
6035
6036 pe.dwSize = sizeof(pe);
6037 have_record = Process32First(snapshot, &pe);
6038 while (have_record) {
6039 if (mypid == (pid_t)pe.th32ProcessID) {
6040 /* We could cache the ulong value in a static variable. */
6041 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
6042 break;
6043 }
6044
6045 have_record = Process32Next(snapshot, &pe);
6046 }
6047
6048 /* If our loop exits and our pid was not found (result will be NULL)
6049 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
6050 * error anyway, so let's raise it. */
6051 if (!result)
6052 result = PyErr_SetFromWindowsErr(GetLastError());
6053
6054 CloseHandle(snapshot);
6055
6056 return result;
6057}
6058#endif /*MS_WINDOWS*/
6059
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006060PyDoc_STRVAR(posix_getppid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006061"getppid() -> ppid\n\n\
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006062Return the parent's process id. If the parent process has already exited,\n\
6063Windows machines will still return its id; others systems will return the id\n\
6064of the 'init' process (1).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006065
Barry Warsaw53699e91996-12-10 23:23:01 +00006066static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006067posix_getppid(PyObject *self, PyObject *noargs)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006068{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006069#ifdef MS_WINDOWS
6070 return win32_getppid();
6071#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006072 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00006073#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00006074}
6075#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006076
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006077
Fred Drake12c6e2d1999-12-14 21:25:03 +00006078#ifdef HAVE_GETLOGIN
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006079PyDoc_STRVAR(posix_getlogin__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006080"getlogin() -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006081Return the actual login name.");
Fred Drake12c6e2d1999-12-14 21:25:03 +00006082
6083static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006084posix_getlogin(PyObject *self, PyObject *noargs)
Fred Drake12c6e2d1999-12-14 21:25:03 +00006085{
Victor Stinner8c62be82010-05-06 00:08:46 +00006086 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006087#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006088 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02006089 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006090
6091 if (GetUserNameW(user_name, &num_chars)) {
6092 /* num_chars is the number of unicode chars plus null terminator */
6093 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006094 }
6095 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006096 result = PyErr_SetFromWindowsErr(GetLastError());
6097#else
Victor Stinner8c62be82010-05-06 00:08:46 +00006098 char *name;
6099 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00006100
Victor Stinner8c62be82010-05-06 00:08:46 +00006101 errno = 0;
6102 name = getlogin();
6103 if (name == NULL) {
6104 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00006105 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00006106 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006107 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00006108 }
6109 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00006110 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006112#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00006113 return result;
6114}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00006115#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00006116
Guido van Rossumad0ee831995-03-01 10:34:45 +00006117#ifdef HAVE_GETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006118PyDoc_STRVAR(posix_getuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006119"getuid() -> uid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006120Return the current process's user id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006121
Barry Warsaw53699e91996-12-10 23:23:01 +00006122static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006123posix_getuid(PyObject *self, PyObject *noargs)
Guido van Rossum46003ff1992-05-15 11:05:24 +00006124{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006125 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006126}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006127#endif
Guido van Rossum46003ff1992-05-15 11:05:24 +00006128
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006129
Guido van Rossumad0ee831995-03-01 10:34:45 +00006130#ifdef HAVE_KILL
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006131PyDoc_STRVAR(posix_kill__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006132"kill(pid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006133Kill a process with a signal.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006134
Barry Warsaw53699e91996-12-10 23:23:01 +00006135static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006136posix_kill(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006137{
Victor Stinner8c62be82010-05-06 00:08:46 +00006138 pid_t pid;
6139 int sig;
6140 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:kill", &pid, &sig))
6141 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006142 if (kill(pid, sig) == -1)
6143 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006144 Py_INCREF(Py_None);
6145 return Py_None;
Guido van Rossum85e3b011991-06-03 12:42:10 +00006146}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006147#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006148
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006149#ifdef HAVE_KILLPG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006150PyDoc_STRVAR(posix_killpg__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006151"killpg(pgid, sig)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006152Kill a process group with a signal.");
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006153
6154static PyObject *
6155posix_killpg(PyObject *self, PyObject *args)
6156{
Victor Stinner8c62be82010-05-06 00:08:46 +00006157 int sig;
6158 pid_t pgid;
6159 /* XXX some man pages make the `pgid` parameter an int, others
6160 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
6161 take the same type. Moreover, pid_t is always at least as wide as
6162 int (else compilation of this module fails), which is safe. */
6163 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:killpg", &pgid, &sig))
6164 return NULL;
6165 if (killpg(pgid, sig) == -1)
6166 return posix_error();
6167 Py_INCREF(Py_None);
6168 return Py_None;
Martin v. Löwisb2c92f42002-02-16 23:35:41 +00006169}
6170#endif
6171
Brian Curtineb24d742010-04-12 17:16:38 +00006172#ifdef MS_WINDOWS
6173PyDoc_STRVAR(win32_kill__doc__,
6174"kill(pid, sig)\n\n\
6175Kill a process with a signal.");
6176
6177static PyObject *
6178win32_kill(PyObject *self, PyObject *args)
6179{
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00006180 PyObject *result;
Victor Stinner8c62be82010-05-06 00:08:46 +00006181 DWORD pid, sig, err;
6182 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00006183
Victor Stinner8c62be82010-05-06 00:08:46 +00006184 if (!PyArg_ParseTuple(args, "kk:kill", &pid, &sig))
6185 return NULL;
Brian Curtineb24d742010-04-12 17:16:38 +00006186
Victor Stinner8c62be82010-05-06 00:08:46 +00006187 /* Console processes which share a common console can be sent CTRL+C or
6188 CTRL+BREAK events, provided they handle said events. */
6189 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
6190 if (GenerateConsoleCtrlEvent(sig, pid) == 0) {
6191 err = GetLastError();
6192 PyErr_SetFromWindowsErr(err);
6193 }
6194 else
6195 Py_RETURN_NONE;
6196 }
Brian Curtineb24d742010-04-12 17:16:38 +00006197
Victor Stinner8c62be82010-05-06 00:08:46 +00006198 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
6199 attempt to open and terminate the process. */
6200 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
6201 if (handle == NULL) {
6202 err = GetLastError();
6203 return PyErr_SetFromWindowsErr(err);
6204 }
Brian Curtineb24d742010-04-12 17:16:38 +00006205
Victor Stinner8c62be82010-05-06 00:08:46 +00006206 if (TerminateProcess(handle, sig) == 0) {
6207 err = GetLastError();
6208 result = PyErr_SetFromWindowsErr(err);
6209 } else {
6210 Py_INCREF(Py_None);
6211 result = Py_None;
6212 }
Brian Curtineb24d742010-04-12 17:16:38 +00006213
Victor Stinner8c62be82010-05-06 00:08:46 +00006214 CloseHandle(handle);
6215 return result;
Brian Curtineb24d742010-04-12 17:16:38 +00006216}
6217#endif /* MS_WINDOWS */
6218
Guido van Rossumc0125471996-06-28 18:55:32 +00006219#ifdef HAVE_PLOCK
6220
6221#ifdef HAVE_SYS_LOCK_H
6222#include <sys/lock.h>
6223#endif
6224
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006225PyDoc_STRVAR(posix_plock__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006226"plock(op)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006227Lock program segments into memory.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006228
Barry Warsaw53699e91996-12-10 23:23:01 +00006229static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006230posix_plock(PyObject *self, PyObject *args)
Guido van Rossumc0125471996-06-28 18:55:32 +00006231{
Victor Stinner8c62be82010-05-06 00:08:46 +00006232 int op;
6233 if (!PyArg_ParseTuple(args, "i:plock", &op))
6234 return NULL;
6235 if (plock(op) == -1)
6236 return posix_error();
6237 Py_INCREF(Py_None);
6238 return Py_None;
Guido van Rossumc0125471996-06-28 18:55:32 +00006239}
6240#endif
6241
Guido van Rossumb6775db1994-08-01 11:34:53 +00006242#ifdef HAVE_SETUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006243PyDoc_STRVAR(posix_setuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006244"setuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006245Set the current process's user id.");
6246
Barry Warsaw53699e91996-12-10 23:23:01 +00006247static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006248posix_setuid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006249{
Victor Stinner8c62be82010-05-06 00:08:46 +00006250 uid_t uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006251 if (!PyArg_ParseTuple(args, "O&:setuid", _Py_Uid_Converter, &uid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006252 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006253 if (setuid(uid) < 0)
6254 return posix_error();
6255 Py_INCREF(Py_None);
6256 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006257}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006258#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006260
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006261#ifdef HAVE_SETEUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006262PyDoc_STRVAR(posix_seteuid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006263"seteuid(uid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006264Set the current process's effective user id.");
6265
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006266static PyObject *
6267posix_seteuid (PyObject *self, PyObject *args)
6268{
Victor Stinner8c62be82010-05-06 00:08:46 +00006269 uid_t euid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006270 if (!PyArg_ParseTuple(args, "O&:seteuid", _Py_Uid_Converter, &euid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006271 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006272 if (seteuid(euid) < 0) {
6273 return posix_error();
6274 } else {
6275 Py_INCREF(Py_None);
6276 return Py_None;
6277 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006278}
6279#endif /* HAVE_SETEUID */
6280
6281#ifdef HAVE_SETEGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006282PyDoc_STRVAR(posix_setegid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006283"setegid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006284Set the current process's effective group id.");
6285
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006286static PyObject *
6287posix_setegid (PyObject *self, PyObject *args)
6288{
Victor Stinner8c62be82010-05-06 00:08:46 +00006289 gid_t egid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006290 if (!PyArg_ParseTuple(args, "O&:setegid", _Py_Gid_Converter, &egid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006291 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006292 if (setegid(egid) < 0) {
6293 return posix_error();
6294 } else {
6295 Py_INCREF(Py_None);
6296 return Py_None;
6297 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006298}
6299#endif /* HAVE_SETEGID */
6300
6301#ifdef HAVE_SETREUID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006302PyDoc_STRVAR(posix_setreuid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006303"setreuid(ruid, euid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006304Set the current process's real and effective user ids.");
6305
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006306static PyObject *
6307posix_setreuid (PyObject *self, PyObject *args)
6308{
Victor Stinner8c62be82010-05-06 00:08:46 +00006309 uid_t ruid, euid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006310 if (!PyArg_ParseTuple(args, "O&O&:setreuid",
6311 _Py_Uid_Converter, &ruid,
6312 _Py_Uid_Converter, &euid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006313 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006314 if (setreuid(ruid, euid) < 0) {
6315 return posix_error();
6316 } else {
6317 Py_INCREF(Py_None);
6318 return Py_None;
6319 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006320}
6321#endif /* HAVE_SETREUID */
6322
6323#ifdef HAVE_SETREGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006324PyDoc_STRVAR(posix_setregid__doc__,
Neal Norwitz94f1d712004-02-16 01:26:34 +00006325"setregid(rgid, egid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006326Set the current process's real and effective group ids.");
6327
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006328static PyObject *
6329posix_setregid (PyObject *self, PyObject *args)
6330{
Victor Stinner8c62be82010-05-06 00:08:46 +00006331 gid_t rgid, egid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006332 if (!PyArg_ParseTuple(args, "O&O&:setregid",
6333 _Py_Gid_Converter, &rgid,
6334 _Py_Gid_Converter, &egid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006335 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006336 if (setregid(rgid, egid) < 0) {
6337 return posix_error();
6338 } else {
6339 Py_INCREF(Py_None);
6340 return Py_None;
6341 }
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +00006342}
6343#endif /* HAVE_SETREGID */
6344
Guido van Rossumb6775db1994-08-01 11:34:53 +00006345#ifdef HAVE_SETGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006346PyDoc_STRVAR(posix_setgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006347"setgid(gid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006348Set the current process's group id.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006349
Barry Warsaw53699e91996-12-10 23:23:01 +00006350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006351posix_setgid(PyObject *self, PyObject *args)
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006352{
Victor Stinner8c62be82010-05-06 00:08:46 +00006353 gid_t gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006354 if (!PyArg_ParseTuple(args, "O&:setgid", _Py_Gid_Converter, &gid))
Victor Stinner8c62be82010-05-06 00:08:46 +00006355 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00006356 if (setgid(gid) < 0)
6357 return posix_error();
6358 Py_INCREF(Py_None);
6359 return Py_None;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006360}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00006361#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00006362
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006363#ifdef HAVE_SETGROUPS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006364PyDoc_STRVAR(posix_setgroups__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006365"setgroups(list)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006366Set the groups of the current process to list.");
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006367
6368static PyObject *
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00006369posix_setgroups(PyObject *self, PyObject *groups)
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006370{
Victor Stinner8c62be82010-05-06 00:08:46 +00006371 int i, len;
6372 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00006373
Victor Stinner8c62be82010-05-06 00:08:46 +00006374 if (!PySequence_Check(groups)) {
6375 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
6376 return NULL;
6377 }
6378 len = PySequence_Size(groups);
6379 if (len > MAX_GROUPS) {
6380 PyErr_SetString(PyExc_ValueError, "too many groups");
6381 return NULL;
6382 }
6383 for(i = 0; i < len; i++) {
6384 PyObject *elem;
6385 elem = PySequence_GetItem(groups, i);
6386 if (!elem)
6387 return NULL;
6388 if (!PyLong_Check(elem)) {
6389 PyErr_SetString(PyExc_TypeError,
6390 "groups must be integers");
6391 Py_DECREF(elem);
6392 return NULL;
6393 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006394 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006395 Py_DECREF(elem);
6396 return NULL;
6397 }
6398 }
6399 Py_DECREF(elem);
6400 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006401
Victor Stinner8c62be82010-05-06 00:08:46 +00006402 if (setgroups(len, grouplist) < 0)
6403 return posix_error();
6404 Py_INCREF(Py_None);
6405 return Py_None;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00006406}
6407#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006408
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006409#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
6410static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006411wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006412{
Victor Stinner8c62be82010-05-06 00:08:46 +00006413 PyObject *result;
6414 static PyObject *struct_rusage;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02006415 _Py_IDENTIFIER(struct_rusage);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006416
Victor Stinner8c62be82010-05-06 00:08:46 +00006417 if (pid == -1)
6418 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006419
Victor Stinner8c62be82010-05-06 00:08:46 +00006420 if (struct_rusage == NULL) {
6421 PyObject *m = PyImport_ImportModuleNoBlock("resource");
6422 if (m == NULL)
6423 return NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +02006424 struct_rusage = _PyObject_GetAttrId(m, &PyId_struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 Py_DECREF(m);
6426 if (struct_rusage == NULL)
6427 return NULL;
6428 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006429
Victor Stinner8c62be82010-05-06 00:08:46 +00006430 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
6431 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
6432 if (!result)
6433 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006434
6435#ifndef doubletime
6436#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
6437#endif
6438
Victor Stinner8c62be82010-05-06 00:08:46 +00006439 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006440 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00006441 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006442 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006443#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00006444 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
6445 SET_INT(result, 2, ru->ru_maxrss);
6446 SET_INT(result, 3, ru->ru_ixrss);
6447 SET_INT(result, 4, ru->ru_idrss);
6448 SET_INT(result, 5, ru->ru_isrss);
6449 SET_INT(result, 6, ru->ru_minflt);
6450 SET_INT(result, 7, ru->ru_majflt);
6451 SET_INT(result, 8, ru->ru_nswap);
6452 SET_INT(result, 9, ru->ru_inblock);
6453 SET_INT(result, 10, ru->ru_oublock);
6454 SET_INT(result, 11, ru->ru_msgsnd);
6455 SET_INT(result, 12, ru->ru_msgrcv);
6456 SET_INT(result, 13, ru->ru_nsignals);
6457 SET_INT(result, 14, ru->ru_nvcsw);
6458 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006459#undef SET_INT
6460
Victor Stinner8c62be82010-05-06 00:08:46 +00006461 if (PyErr_Occurred()) {
6462 Py_DECREF(result);
6463 return NULL;
6464 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006465
Victor Stinner8c62be82010-05-06 00:08:46 +00006466 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006467}
6468#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
6469
6470#ifdef HAVE_WAIT3
6471PyDoc_STRVAR(posix_wait3__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006472"wait3(options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006473Wait for completion of a child process.");
6474
6475static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006476posix_wait3(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006477{
Victor Stinner8c62be82010-05-06 00:08:46 +00006478 pid_t pid;
6479 int options;
6480 struct rusage ru;
6481 WAIT_TYPE status;
6482 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006483
Victor Stinner4195b5c2012-02-08 23:03:19 +01006484 if (!PyArg_ParseTuple(args, "i:wait3", &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006485 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006486
Victor Stinner8c62be82010-05-06 00:08:46 +00006487 Py_BEGIN_ALLOW_THREADS
6488 pid = wait3(&status, options, &ru);
6489 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006490
Victor Stinner4195b5c2012-02-08 23:03:19 +01006491 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006492}
6493#endif /* HAVE_WAIT3 */
6494
6495#ifdef HAVE_WAIT4
6496PyDoc_STRVAR(posix_wait4__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01006497"wait4(pid, options) -> (pid, status, rusage)\n\n\
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006498Wait for completion of a given child process.");
6499
6500static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01006501posix_wait4(PyObject *self, PyObject *args)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006502{
Victor Stinner8c62be82010-05-06 00:08:46 +00006503 pid_t pid;
6504 int options;
6505 struct rusage ru;
6506 WAIT_TYPE status;
6507 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006508
Victor Stinner4195b5c2012-02-08 23:03:19 +01006509 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:wait4", &pid, &options))
Victor Stinner8c62be82010-05-06 00:08:46 +00006510 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006511
Victor Stinner8c62be82010-05-06 00:08:46 +00006512 Py_BEGIN_ALLOW_THREADS
6513 pid = wait4(pid, &status, options, &ru);
6514 Py_END_ALLOW_THREADS
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006515
Victor Stinner4195b5c2012-02-08 23:03:19 +01006516 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006517}
6518#endif /* HAVE_WAIT4 */
6519
Ross Lagerwall7807c352011-03-17 20:20:30 +02006520#if defined(HAVE_WAITID) && !defined(__APPLE__)
6521PyDoc_STRVAR(posix_waitid__doc__,
6522"waitid(idtype, id, options) -> waitid_result\n\n\
6523Wait for the completion of one or more child processes.\n\n\
6524idtype can be P_PID, P_PGID or P_ALL.\n\
6525id specifies the pid to wait on.\n\
6526options is constructed from the ORing of one or more of WEXITED, WSTOPPED\n\
6527or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.\n\
6528Returns either waitid_result or None if WNOHANG is specified and there are\n\
6529no children in a waitable state.");
6530
6531static PyObject *
6532posix_waitid(PyObject *self, PyObject *args)
6533{
6534 PyObject *result;
6535 idtype_t idtype;
6536 id_t id;
6537 int options, res;
6538 siginfo_t si;
6539 si.si_pid = 0;
6540 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID "i:waitid", &idtype, &id, &options))
6541 return NULL;
6542 Py_BEGIN_ALLOW_THREADS
6543 res = waitid(idtype, id, &si, options);
6544 Py_END_ALLOW_THREADS
6545 if (res == -1)
6546 return posix_error();
6547
6548 if (si.si_pid == 0)
6549 Py_RETURN_NONE;
6550
6551 result = PyStructSequence_New(&WaitidResultType);
6552 if (!result)
6553 return NULL;
6554
6555 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006556 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02006557 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
6558 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
6559 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
6560 if (PyErr_Occurred()) {
6561 Py_DECREF(result);
6562 return NULL;
6563 }
6564
6565 return result;
6566}
6567#endif
6568
Guido van Rossumb6775db1994-08-01 11:34:53 +00006569#ifdef HAVE_WAITPID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006570PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006571"waitpid(pid, options) -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006572Wait for completion of a given child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006573
Barry Warsaw53699e91996-12-10 23:23:01 +00006574static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00006575posix_waitpid(PyObject *self, PyObject *args)
Guido van Rossum85e3b011991-06-03 12:42:10 +00006576{
Victor Stinner8c62be82010-05-06 00:08:46 +00006577 pid_t pid;
6578 int options;
6579 WAIT_TYPE status;
6580 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00006581
Victor Stinner8c62be82010-05-06 00:08:46 +00006582 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6583 return NULL;
6584 Py_BEGIN_ALLOW_THREADS
6585 pid = waitpid(pid, &status, options);
6586 Py_END_ALLOW_THREADS
6587 if (pid == -1)
6588 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006589
Victor Stinner8c62be82010-05-06 00:08:46 +00006590 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00006591}
6592
Tim Petersab034fa2002-02-01 11:27:43 +00006593#elif defined(HAVE_CWAIT)
6594
6595/* MS C has a variant of waitpid() that's usable for most purposes. */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006596PyDoc_STRVAR(posix_waitpid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006597"waitpid(pid, options) -> (pid, status << 8)\n\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006598"Wait for completion of a given process. options is ignored on Windows.");
Tim Petersab034fa2002-02-01 11:27:43 +00006599
6600static PyObject *
6601posix_waitpid(PyObject *self, PyObject *args)
6602{
Victor Stinner8c62be82010-05-06 00:08:46 +00006603 Py_intptr_t pid;
6604 int status, options;
Tim Petersab034fa2002-02-01 11:27:43 +00006605
Victor Stinner8c62be82010-05-06 00:08:46 +00006606 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:waitpid", &pid, &options))
6607 return NULL;
6608 Py_BEGIN_ALLOW_THREADS
6609 pid = _cwait(&status, pid, options);
6610 Py_END_ALLOW_THREADS
6611 if (pid == -1)
6612 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006613
Victor Stinner8c62be82010-05-06 00:08:46 +00006614 /* shift the status left a byte so this is more like the POSIX waitpid */
6615 return Py_BuildValue("Ni", PyLong_FromPid(pid), status << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00006616}
6617#endif /* HAVE_WAITPID || HAVE_CWAIT */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006618
Guido van Rossumad0ee831995-03-01 10:34:45 +00006619#ifdef HAVE_WAIT
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006620PyDoc_STRVAR(posix_wait__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00006621"wait() -> (pid, status)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006622Wait for completion of a child process.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006623
Barry Warsaw53699e91996-12-10 23:23:01 +00006624static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006625posix_wait(PyObject *self, PyObject *noargs)
Guido van Rossum21803b81992-08-09 12:55:27 +00006626{
Victor Stinner8c62be82010-05-06 00:08:46 +00006627 pid_t pid;
6628 WAIT_TYPE status;
6629 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00006630
Victor Stinner8c62be82010-05-06 00:08:46 +00006631 Py_BEGIN_ALLOW_THREADS
6632 pid = wait(&status);
6633 Py_END_ALLOW_THREADS
6634 if (pid == -1)
6635 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006636
Victor Stinner8c62be82010-05-06 00:08:46 +00006637 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00006638}
Guido van Rossumad0ee831995-03-01 10:34:45 +00006639#endif
Guido van Rossum85e3b011991-06-03 12:42:10 +00006640
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006641
Larry Hastings9cf065c2012-06-22 16:30:09 -07006642#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
6643PyDoc_STRVAR(readlink__doc__,
6644"readlink(path, *, dir_fd=None) -> path\n\n\
6645Return a string representing the path to which the symbolic link points.\n\
6646\n\
6647If dir_fd is not None, it should be a file descriptor open to a directory,\n\
6648 and path should be relative; path will then be relative to that directory.\n\
6649dir_fd may not be implemented on your platform.\n\
6650 If it is unavailable, using it will raise a NotImplementedError.");
Mark Hammondc2e85bd2002-10-03 05:10:39 +00006651#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006652
Guido van Rossumb6775db1994-08-01 11:34:53 +00006653#ifdef HAVE_READLINK
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006654
Barry Warsaw53699e91996-12-10 23:23:01 +00006655static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006656posix_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006657{
Larry Hastings9cf065c2012-06-22 16:30:09 -07006658 path_t path;
6659 int dir_fd = DEFAULT_DIR_FD;
6660 char buffer[MAXPATHLEN];
6661 ssize_t length;
6662 PyObject *return_value = NULL;
6663 static char *keywords[] = {"path", "dir_fd", NULL};
Thomas Wouters89f507f2006-12-13 04:49:30 +00006664
Larry Hastings9cf065c2012-06-22 16:30:09 -07006665 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01006666 path.function_name = "readlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006667 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|$O&:readlink", keywords,
6668 path_converter, &path,
6669#ifdef HAVE_READLINKAT
6670 dir_fd_converter, &dir_fd
6671#else
6672 dir_fd_unavailable, &dir_fd
6673#endif
6674 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00006675 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00006676
Victor Stinner8c62be82010-05-06 00:08:46 +00006677 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07006678#ifdef HAVE_READLINKAT
6679 if (dir_fd != DEFAULT_DIR_FD)
6680 length = readlinkat(dir_fd, path.narrow, buffer, sizeof(buffer));
Victor Stinnera45598a2010-05-14 16:35:39 +00006681 else
Larry Hastings9cf065c2012-06-22 16:30:09 -07006682#endif
6683 length = readlink(path.narrow, buffer, sizeof(buffer));
6684 Py_END_ALLOW_THREADS
6685
6686 if (length < 0) {
Victor Stinner292c8352012-10-30 02:17:38 +01006687 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006688 goto exit;
6689 }
6690
6691 if (PyUnicode_Check(path.object))
6692 return_value = PyUnicode_DecodeFSDefaultAndSize(buffer, length);
6693 else
6694 return_value = PyBytes_FromStringAndSize(buffer, length);
6695exit:
6696 path_cleanup(&path);
6697 return return_value;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006698}
Larry Hastings9cf065c2012-06-22 16:30:09 -07006699
6700
Guido van Rossumb6775db1994-08-01 11:34:53 +00006701#endif /* HAVE_READLINK */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006702
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006703
Larry Hastings9cf065c2012-06-22 16:30:09 -07006704#ifdef HAVE_SYMLINK
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00006705PyDoc_STRVAR(posix_symlink__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07006706"symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
6707Create a symbolic link pointing to src named dst.\n\n\
6708target_is_directory is required on Windows if the target is to be\n\
6709 interpreted as a directory. (On Windows, symlink requires\n\
6710 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)\n\
6711 target_is_directory is ignored on non-Windows platforms.\n\
6712\n\
6713If dir_fd is not None, it should be a file descriptor open to a directory,\n\
6714 and path should be relative; path will then be relative to that directory.\n\
6715dir_fd may not be implemented on your platform.\n\
6716 If it is unavailable, using it will raise a NotImplementedError.");
6717
6718#if defined(MS_WINDOWS)
6719
6720/* Grab CreateSymbolicLinkW dynamically from kernel32 */
6721static DWORD (CALLBACK *Py_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD) = NULL;
6722static DWORD (CALLBACK *Py_CreateSymbolicLinkA)(LPSTR, LPSTR, DWORD) = NULL;
6723static int
6724check_CreateSymbolicLink()
6725{
6726 HINSTANCE hKernel32;
6727 /* only recheck */
6728 if (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA)
6729 return 1;
6730 hKernel32 = GetModuleHandleW(L"KERNEL32");
6731 *(FARPROC*)&Py_CreateSymbolicLinkW = GetProcAddress(hKernel32,
6732 "CreateSymbolicLinkW");
6733 *(FARPROC*)&Py_CreateSymbolicLinkA = GetProcAddress(hKernel32,
6734 "CreateSymbolicLinkA");
6735 return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA);
6736}
6737
Jason R. Coombs3a092862013-05-27 23:21:28 -04006738void _dirnameW(WCHAR *path) {
6739 /* Remove the last portion of the path */
6740
6741 WCHAR *ptr;
6742
6743 /* walk the path from the end until a backslash is encountered */
6744 for(ptr = path + wcslen(path); ptr != path; ptr--)
6745 {
6746 if(*ptr == *L"\\" || *ptr == *L"/") {
6747 break;
6748 }
6749 }
6750 *ptr = 0;
6751}
6752
6753void _dirnameA(char *path) {
6754 /* Remove the last portion of the path */
6755
6756 char *ptr;
6757
6758 /* walk the path from the end until a backslash is encountered */
6759 for(ptr = path + strlen(path); ptr != path; ptr--)
6760 {
6761 if(*ptr == '\\' || *ptr == '/') {
6762 break;
6763 }
6764 }
6765 *ptr = 0;
6766}
6767
6768int _is_absW(WCHAR *path) {
6769 /* Is this path absolute? */
6770
6771 return path[0] == L'\\' || path[0] == L'/' || path[1] == L':';
6772
6773}
6774
6775int _is_absA(char *path) {
6776 /* Is this path absolute? */
6777
6778 return path[0] == '\\' || path[0] == '/' || path[1] == ':';
6779
6780}
6781
6782void _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest) {
6783 /* join root and rest with a backslash */
6784 int root_len;
6785
6786 if(_is_absW(rest)) {
6787 wcscpy(dest_path, rest);
6788 return;
6789 }
6790
6791 root_len = wcslen(root);
6792
6793 wcscpy(dest_path, root);
6794 if(root_len) {
6795 dest_path[root_len] = *L"\\";
6796 root_len += 1;
6797 }
6798 wcscpy(dest_path+root_len, rest);
6799}
6800
6801void _joinA(char *dest_path, const char *root, const char *rest) {
6802 /* join root and rest with a backslash */
6803 int root_len;
6804
6805 if(_is_absA(rest)) {
6806 strcpy(dest_path, rest);
6807 return;
6808 }
6809
6810 root_len = strlen(root);
6811
6812 strcpy(dest_path, root);
6813 if(root_len) {
6814 dest_path[root_len] = '\\';
6815 root_len += 1;
6816 }
6817 strcpy(dest_path+root_len, rest);
6818}
6819
6820int _check_dirW(WCHAR *src, WCHAR *dest)
6821{
6822 /* Return True if the path at src relative to dest is a directory */
6823 WIN32_FILE_ATTRIBUTE_DATA src_info;
6824 WCHAR dest_parent[MAX_PATH];
6825 WCHAR src_resolved[MAX_PATH] = L"";
6826
6827 /* dest_parent = os.path.dirname(dest) */
6828 wcscpy(dest_parent, dest);
6829 _dirnameW(dest_parent);
6830 /* src_resolved = os.path.join(dest_parent, src) */
6831 _joinW(src_resolved, dest_parent, src);
6832 return (
6833 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
6834 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
6835 );
6836}
6837
6838int _check_dirA(char *src, char *dest)
6839{
6840 /* Return True if the path at src relative to dest is a directory */
6841 WIN32_FILE_ATTRIBUTE_DATA src_info;
6842 char dest_parent[MAX_PATH];
6843 char src_resolved[MAX_PATH] = "";
6844
6845 /* dest_parent = os.path.dirname(dest) */
6846 strcpy(dest_parent, dest);
6847 _dirnameW(dest_parent);
6848 /* src_resolved = os.path.join(dest_parent, src) */
6849 _joinW(src_resolved, dest_parent, src);
6850 return (
6851 GetFileAttributesExA(src_resolved, GetFileExInfoStandard, &src_info)
6852 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
6853 );
6854}
6855
Larry Hastings9cf065c2012-06-22 16:30:09 -07006856#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006857
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006858static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006859posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006860{
Larry Hastings9cf065c2012-06-22 16:30:09 -07006861 path_t src;
6862 path_t dst;
6863 int dir_fd = DEFAULT_DIR_FD;
6864 int target_is_directory = 0;
6865 static char *keywords[] = {"src", "dst", "target_is_directory",
6866 "dir_fd", NULL};
6867 PyObject *return_value;
6868#ifdef MS_WINDOWS
6869 DWORD result;
6870#else
6871 int result;
6872#endif
6873
6874 memset(&src, 0, sizeof(src));
Victor Stinner292c8352012-10-30 02:17:38 +01006875 src.function_name = "symlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006876 src.argument_name = "src";
6877 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01006878 dst.function_name = "symlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006879 dst.argument_name = "dst";
6880
6881#ifdef MS_WINDOWS
6882 if (!check_CreateSymbolicLink()) {
6883 PyErr_SetString(PyExc_NotImplementedError,
6884 "CreateSymbolicLink functions not found");
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006885 return NULL;
6886 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006887 if (!win32_can_symlink) {
6888 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006889 return NULL;
6890 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006891#endif
6892
6893 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink",
6894 keywords,
6895 path_converter, &src,
6896 path_converter, &dst,
6897 &target_is_directory,
6898#ifdef HAVE_SYMLINKAT
6899 dir_fd_converter, &dir_fd
6900#else
6901 dir_fd_unavailable, &dir_fd
6902#endif
6903 ))
6904 return NULL;
6905
6906 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
6907 PyErr_SetString(PyExc_ValueError,
6908 "symlink: src and dst must be the same type");
6909 return_value = NULL;
6910 goto exit;
6911 }
6912
6913#ifdef MS_WINDOWS
Jason R. Coombs3a092862013-05-27 23:21:28 -04006914
Larry Hastings9cf065c2012-06-22 16:30:09 -07006915 Py_BEGIN_ALLOW_THREADS
Jason R. Coombs3a092862013-05-27 23:21:28 -04006916 if (dst.wide) {
6917 /* if src is a directory, ensure target_is_directory==1 */
6918 target_is_directory |= _check_dirW(src.wide, dst.wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006919 result = Py_CreateSymbolicLinkW(dst.wide, src.wide,
6920 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04006921 }
6922 else {
6923 /* if src is a directory, ensure target_is_directory==1 */
6924 target_is_directory |= _check_dirA(src.narrow, dst.narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006925 result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow,
6926 target_is_directory);
Jason R. Coombs3a092862013-05-27 23:21:28 -04006927 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006928 Py_END_ALLOW_THREADS
6929
6930 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01006931 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006932 goto exit;
6933 }
6934
6935#else
6936
6937 Py_BEGIN_ALLOW_THREADS
6938#if HAVE_SYMLINKAT
6939 if (dir_fd != DEFAULT_DIR_FD)
6940 result = symlinkat(src.narrow, dir_fd, dst.narrow);
6941 else
6942#endif
6943 result = symlink(src.narrow, dst.narrow);
6944 Py_END_ALLOW_THREADS
6945
6946 if (result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01006947 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006948 goto exit;
6949 }
6950#endif
6951
6952 return_value = Py_None;
6953 Py_INCREF(Py_None);
6954 goto exit; /* silence "unused label" warning */
6955exit:
6956 path_cleanup(&src);
6957 path_cleanup(&dst);
6958 return return_value;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006959}
Larry Hastings9cf065c2012-06-22 16:30:09 -07006960
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006961#endif /* HAVE_SYMLINK */
6962
Larry Hastings9cf065c2012-06-22 16:30:09 -07006963
Brian Curtind40e6f72010-07-08 21:39:08 +00006964#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6965
Brian Curtind40e6f72010-07-08 21:39:08 +00006966static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006967win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtind40e6f72010-07-08 21:39:08 +00006968{
6969 wchar_t *path;
6970 DWORD n_bytes_returned;
6971 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006972 PyObject *po, *result;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006973 int dir_fd;
Brian Curtind40e6f72010-07-08 21:39:08 +00006974 HANDLE reparse_point_handle;
6975
6976 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6977 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6978 wchar_t *print_name;
6979
Larry Hastings9cf065c2012-06-22 16:30:09 -07006980 static char *keywords[] = {"path", "dir_fd", NULL};
6981
6982 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
6983 &po,
6984 dir_fd_unavailable, &dir_fd
6985 ))
Victor Stinnereb5657a2011-09-30 01:44:27 +02006986 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07006987
Victor Stinnereb5657a2011-09-30 01:44:27 +02006988 path = PyUnicode_AsUnicode(po);
6989 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006990 return NULL;
6991
6992 /* First get a handle to the reparse point */
6993 Py_BEGIN_ALLOW_THREADS
6994 reparse_point_handle = CreateFileW(
6995 path,
6996 0,
6997 0,
6998 0,
6999 OPEN_EXISTING,
7000 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
7001 0);
7002 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007003
Brian Curtind40e6f72010-07-08 21:39:08 +00007004 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02007005 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007006
Brian Curtind40e6f72010-07-08 21:39:08 +00007007 Py_BEGIN_ALLOW_THREADS
7008 /* New call DeviceIoControl to read the reparse point */
7009 io_result = DeviceIoControl(
7010 reparse_point_handle,
7011 FSCTL_GET_REPARSE_POINT,
7012 0, 0, /* in buffer */
7013 target_buffer, sizeof(target_buffer),
7014 &n_bytes_returned,
7015 0 /* we're not using OVERLAPPED_IO */
7016 );
7017 CloseHandle(reparse_point_handle);
7018 Py_END_ALLOW_THREADS
7019
7020 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02007021 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00007022
7023 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
7024 {
7025 PyErr_SetString(PyExc_ValueError,
7026 "not a symbolic link");
7027 return NULL;
7028 }
Brian Curtin74e45612010-07-09 15:58:59 +00007029 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
7030 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
7031
7032 result = PyUnicode_FromWideChar(print_name,
7033 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00007034 return result;
7035}
7036
7037#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
7038
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007039
Larry Hastings605a62d2012-06-24 04:33:36 -07007040static PyStructSequence_Field times_result_fields[] = {
7041 {"user", "user time"},
7042 {"system", "system time"},
7043 {"children_user", "user time of children"},
7044 {"children_system", "system time of children"},
7045 {"elapsed", "elapsed time since an arbitrary point in the past"},
7046 {NULL}
7047};
7048
7049PyDoc_STRVAR(times_result__doc__,
7050"times_result: Result from os.times().\n\n\
7051This object may be accessed either as a tuple of\n\
7052 (user, system, children_user, children_system, elapsed),\n\
7053or via the attributes user, system, children_user, children_system,\n\
7054and elapsed.\n\
7055\n\
7056See os.times for more information.");
7057
7058static PyStructSequence_Desc times_result_desc = {
7059 "times_result", /* name */
7060 times_result__doc__, /* doc */
7061 times_result_fields,
7062 5
7063};
7064
7065static PyTypeObject TimesResultType;
7066
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007067#ifdef MS_WINDOWS
7068#define HAVE_TIMES /* mandatory, for the method table */
7069#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07007070
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007071#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07007072
7073static PyObject *
7074build_times_result(double user, double system,
7075 double children_user, double children_system,
7076 double elapsed)
7077{
7078 PyObject *value = PyStructSequence_New(&TimesResultType);
7079 if (value == NULL)
7080 return NULL;
7081
7082#define SET(i, field) \
7083 { \
7084 PyObject *o = PyFloat_FromDouble(field); \
7085 if (!o) { \
7086 Py_DECREF(value); \
7087 return NULL; \
7088 } \
7089 PyStructSequence_SET_ITEM(value, i, o); \
7090 } \
7091
7092 SET(0, user);
7093 SET(1, system);
7094 SET(2, children_user);
7095 SET(3, children_system);
7096 SET(4, elapsed);
7097
7098#undef SET
7099
7100 return value;
7101}
7102
7103PyDoc_STRVAR(posix_times__doc__,
7104"times() -> times_result\n\n\
7105Return an object containing floating point numbers indicating process\n\
7106times. The object behaves like a named tuple with these fields:\n\
7107 (utime, stime, cutime, cstime, elapsed_time)");
7108
Jesus Ceaab70e2a2012-10-05 01:48:08 +02007109#if defined(MS_WINDOWS)
Barry Warsaw53699e91996-12-10 23:23:01 +00007110static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007111posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007112{
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 FILETIME create, exit, kernel, user;
7114 HANDLE hProc;
7115 hProc = GetCurrentProcess();
7116 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
7117 /* The fields of a FILETIME structure are the hi and lo part
7118 of a 64-bit value expressed in 100 nanosecond units.
7119 1e7 is one second in such units; 1e-7 the inverse.
7120 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
7121 */
Larry Hastings605a62d2012-06-24 04:33:36 -07007122 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00007123 (double)(user.dwHighDateTime*429.4967296 +
7124 user.dwLowDateTime*1e-7),
7125 (double)(kernel.dwHighDateTime*429.4967296 +
7126 kernel.dwLowDateTime*1e-7),
7127 (double)0,
7128 (double)0,
7129 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007130}
Jesus Ceaab70e2a2012-10-05 01:48:08 +02007131#else /* Not Windows */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007132#define NEED_TICKS_PER_SECOND
7133static long ticks_per_second = -1;
7134static PyObject *
7135posix_times(PyObject *self, PyObject *noargs)
7136{
7137 struct tms t;
7138 clock_t c;
7139 errno = 0;
7140 c = times(&t);
7141 if (c == (clock_t) -1)
7142 return posix_error();
7143 return build_times_result(
7144 (double)t.tms_utime / ticks_per_second,
7145 (double)t.tms_stime / ticks_per_second,
7146 (double)t.tms_cutime / ticks_per_second,
7147 (double)t.tms_cstime / ticks_per_second,
7148 (double)c / ticks_per_second);
7149}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007150#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007151
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007152#endif /* HAVE_TIMES */
7153
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007154
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007155#ifdef HAVE_GETSID
7156PyDoc_STRVAR(posix_getsid__doc__,
7157"getsid(pid) -> sid\n\n\
7158Call the system call getsid().");
7159
7160static PyObject *
7161posix_getsid(PyObject *self, PyObject *args)
7162{
Victor Stinner8c62be82010-05-06 00:08:46 +00007163 pid_t pid;
7164 int sid;
7165 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
7166 return NULL;
7167 sid = getsid(pid);
7168 if (sid < 0)
7169 return posix_error();
7170 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007171}
7172#endif /* HAVE_GETSID */
7173
7174
Guido van Rossumb6775db1994-08-01 11:34:53 +00007175#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007176PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007177"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007178Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007179
Barry Warsaw53699e91996-12-10 23:23:01 +00007180static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007181posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007182{
Victor Stinner8c62be82010-05-06 00:08:46 +00007183 if (setsid() < 0)
7184 return posix_error();
7185 Py_INCREF(Py_None);
7186 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007187}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007188#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007189
Guido van Rossumb6775db1994-08-01 11:34:53 +00007190#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007191PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007192"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007193Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007194
Barry Warsaw53699e91996-12-10 23:23:01 +00007195static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007196posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007197{
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 pid_t pid;
7199 int pgrp;
7200 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
7201 return NULL;
7202 if (setpgid(pid, pgrp) < 0)
7203 return posix_error();
7204 Py_INCREF(Py_None);
7205 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007206}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007207#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007208
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007209
Guido van Rossumb6775db1994-08-01 11:34:53 +00007210#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007211PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007212"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007213Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007214
Barry Warsaw53699e91996-12-10 23:23:01 +00007215static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007216posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007217{
Victor Stinner8c62be82010-05-06 00:08:46 +00007218 int fd;
7219 pid_t pgid;
7220 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
7221 return NULL;
7222 pgid = tcgetpgrp(fd);
7223 if (pgid < 0)
7224 return posix_error();
7225 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007226}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007227#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007228
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007229
Guido van Rossumb6775db1994-08-01 11:34:53 +00007230#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007231PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007232"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007233Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007234
Barry Warsaw53699e91996-12-10 23:23:01 +00007235static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007236posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007237{
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 int fd;
7239 pid_t pgid;
7240 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
7241 return NULL;
7242 if (tcsetpgrp(fd, pgid) < 0)
7243 return posix_error();
7244 Py_INCREF(Py_None);
7245 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007246}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007247#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007248
Guido van Rossum687dd131993-05-17 08:34:16 +00007249/* Functions acting on file descriptors */
7250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007251PyDoc_STRVAR(posix_open__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007252"open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7253Open a file for low level IO. Returns a file handle (integer).\n\
7254\n\
7255If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7256 and path should be relative; path will then be relative to that directory.\n\
7257dir_fd may not be implemented on your platform.\n\
7258 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007259
Barry Warsaw53699e91996-12-10 23:23:01 +00007260static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007261posix_open(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007262{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007263 path_t path;
7264 int flags;
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007266 int dir_fd = DEFAULT_DIR_FD;
Victor Stinner8c62be82010-05-06 00:08:46 +00007267 int fd;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007268 PyObject *return_value = NULL;
7269 static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007270
Larry Hastings9cf065c2012-06-22 16:30:09 -07007271 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007272 path.function_name = "open";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007273 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords,
7274 path_converter, &path,
7275 &flags, &mode,
7276#ifdef HAVE_OPENAT
7277 dir_fd_converter, &dir_fd
7278#else
7279 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007280#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07007281 ))
7282 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007283
Victor Stinner8c62be82010-05-06 00:08:46 +00007284 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007285#ifdef MS_WINDOWS
7286 if (path.wide)
7287 fd = _wopen(path.wide, flags, mode);
7288 else
7289#endif
7290#ifdef HAVE_OPENAT
7291 if (dir_fd != DEFAULT_DIR_FD)
7292 fd = openat(dir_fd, path.narrow, flags, mode);
7293 else
7294#endif
7295 fd = open(path.narrow, flags, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00007296 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00007297
Larry Hastings9cf065c2012-06-22 16:30:09 -07007298 if (fd == -1) {
Victor Stinner4e7d2d42012-11-05 01:20:58 +01007299 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007300 goto exit;
7301 }
7302
7303 return_value = PyLong_FromLong((long)fd);
7304
7305exit:
7306 path_cleanup(&path);
7307 return return_value;
7308}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007309
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007310PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007311"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007312Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007313
Barry Warsaw53699e91996-12-10 23:23:01 +00007314static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007315posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007316{
Victor Stinner8c62be82010-05-06 00:08:46 +00007317 int fd, res;
7318 if (!PyArg_ParseTuple(args, "i:close", &fd))
7319 return NULL;
7320 if (!_PyVerify_fd(fd))
7321 return posix_error();
7322 Py_BEGIN_ALLOW_THREADS
7323 res = close(fd);
7324 Py_END_ALLOW_THREADS
7325 if (res < 0)
7326 return posix_error();
7327 Py_INCREF(Py_None);
7328 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007329}
7330
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007331
Victor Stinner8c62be82010-05-06 00:08:46 +00007332PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00007333"closerange(fd_low, fd_high)\n\n\
7334Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
7335
7336static PyObject *
7337posix_closerange(PyObject *self, PyObject *args)
7338{
Victor Stinner8c62be82010-05-06 00:08:46 +00007339 int fd_from, fd_to, i;
7340 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
7341 return NULL;
7342 Py_BEGIN_ALLOW_THREADS
7343 for (i = fd_from; i < fd_to; i++)
7344 if (_PyVerify_fd(i))
7345 close(i);
7346 Py_END_ALLOW_THREADS
7347 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007348}
7349
7350
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007351PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007352"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007353Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007354
Barry Warsaw53699e91996-12-10 23:23:01 +00007355static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007356posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007357{
Victor Stinner8c62be82010-05-06 00:08:46 +00007358 int fd;
7359 if (!PyArg_ParseTuple(args, "i:dup", &fd))
7360 return NULL;
7361 if (!_PyVerify_fd(fd))
7362 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007363 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007364 if (fd < 0)
7365 return posix_error();
7366 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007367}
7368
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007369
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007370PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00007371"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007372Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007373
Barry Warsaw53699e91996-12-10 23:23:01 +00007374static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007375posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007376{
Victor Stinner8c62be82010-05-06 00:08:46 +00007377 int fd, fd2, res;
7378 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
7379 return NULL;
7380 if (!_PyVerify_fd_dup2(fd, fd2))
7381 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00007383 if (res < 0)
7384 return posix_error();
7385 Py_INCREF(Py_None);
7386 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007387}
7388
Ross Lagerwall7807c352011-03-17 20:20:30 +02007389#ifdef HAVE_LOCKF
7390PyDoc_STRVAR(posix_lockf__doc__,
7391"lockf(fd, cmd, len)\n\n\
7392Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
7393fd is an open file descriptor.\n\
7394cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
7395F_TEST.\n\
7396len specifies the section of the file to lock.");
7397
7398static PyObject *
7399posix_lockf(PyObject *self, PyObject *args)
7400{
7401 int fd, cmd, res;
7402 off_t len;
7403 if (!PyArg_ParseTuple(args, "iiO&:lockf",
7404 &fd, &cmd, _parse_off_t, &len))
7405 return NULL;
7406
7407 Py_BEGIN_ALLOW_THREADS
7408 res = lockf(fd, cmd, len);
7409 Py_END_ALLOW_THREADS
7410
7411 if (res < 0)
7412 return posix_error();
7413
7414 Py_RETURN_NONE;
7415}
7416#endif
7417
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007418
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007419PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007420"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01007421Set the current position of a file descriptor.\n\
7422Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007423
Barry Warsaw53699e91996-12-10 23:23:01 +00007424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007425posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007426{
Victor Stinner8c62be82010-05-06 00:08:46 +00007427 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007428#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007429 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007430#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007431 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007432#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02007433 PyObject *posobj;
7434 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00007435 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00007436#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007437 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7438 switch (how) {
7439 case 0: how = SEEK_SET; break;
7440 case 1: how = SEEK_CUR; break;
7441 case 2: how = SEEK_END; break;
7442 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007443#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007444
Ross Lagerwall8e749672011-03-17 21:54:07 +02007445#if !defined(HAVE_LARGEFILE_SUPPORT)
7446 pos = PyLong_AsLong(posobj);
7447#else
7448 pos = PyLong_AsLongLong(posobj);
7449#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007450 if (PyErr_Occurred())
7451 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007452
Victor Stinner8c62be82010-05-06 00:08:46 +00007453 if (!_PyVerify_fd(fd))
7454 return posix_error();
7455 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007456#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007457 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007458#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007460#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007461 Py_END_ALLOW_THREADS
7462 if (res < 0)
7463 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007464
7465#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007466 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007467#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007468 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007469#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007470}
7471
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007472
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007473PyDoc_STRVAR(posix_read__doc__,
Benjamin Peterson3b08a292013-05-24 14:35:57 -07007474"read(fd, buffersize) -> bytes\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007475Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007476
Barry Warsaw53699e91996-12-10 23:23:01 +00007477static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007478posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007479{
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 int fd, size;
7481 Py_ssize_t n;
7482 PyObject *buffer;
7483 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7484 return NULL;
7485 if (size < 0) {
7486 errno = EINVAL;
7487 return posix_error();
7488 }
7489 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7490 if (buffer == NULL)
7491 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007492 if (!_PyVerify_fd(fd)) {
7493 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007494 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007495 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007496 Py_BEGIN_ALLOW_THREADS
7497 n = read(fd, PyBytes_AS_STRING(buffer), size);
7498 Py_END_ALLOW_THREADS
7499 if (n < 0) {
7500 Py_DECREF(buffer);
7501 return posix_error();
7502 }
7503 if (n != size)
7504 _PyBytes_Resize(&buffer, n);
7505 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007506}
7507
Ross Lagerwall7807c352011-03-17 20:20:30 +02007508#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7509 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007510static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007511iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7512{
7513 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007514 Py_ssize_t blen, total = 0;
7515
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007516 *iov = PyMem_New(struct iovec, cnt);
7517 if (*iov == NULL) {
7518 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007519 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007520 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007521
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007522 *buf = PyMem_New(Py_buffer, cnt);
7523 if (*buf == NULL) {
7524 PyMem_Del(*iov);
7525 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007526 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007527 }
7528
7529 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007530 PyObject *item = PySequence_GetItem(seq, i);
7531 if (item == NULL)
7532 goto fail;
7533 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7534 Py_DECREF(item);
7535 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007536 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007537 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007538 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007539 blen = (*buf)[i].len;
7540 (*iov)[i].iov_len = blen;
7541 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007542 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007543 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007544
7545fail:
7546 PyMem_Del(*iov);
7547 for (j = 0; j < i; j++) {
7548 PyBuffer_Release(&(*buf)[j]);
7549 }
7550 PyMem_Del(*buf);
7551 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007552}
7553
7554static void
7555iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7556{
7557 int i;
7558 PyMem_Del(iov);
7559 for (i = 0; i < cnt; i++) {
7560 PyBuffer_Release(&buf[i]);
7561 }
7562 PyMem_Del(buf);
7563}
7564#endif
7565
Ross Lagerwall7807c352011-03-17 20:20:30 +02007566#ifdef HAVE_READV
7567PyDoc_STRVAR(posix_readv__doc__,
7568"readv(fd, buffers) -> bytesread\n\n\
7569Read from a file descriptor into a number of writable buffers. buffers\n\
7570is an arbitrary sequence of writable buffers.\n\
7571Returns the total number of bytes read.");
7572
7573static PyObject *
7574posix_readv(PyObject *self, PyObject *args)
7575{
7576 int fd, cnt;
7577 Py_ssize_t n;
7578 PyObject *seq;
7579 struct iovec *iov;
7580 Py_buffer *buf;
7581
7582 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7583 return NULL;
7584 if (!PySequence_Check(seq)) {
7585 PyErr_SetString(PyExc_TypeError,
7586 "readv() arg 2 must be a sequence");
7587 return NULL;
7588 }
7589 cnt = PySequence_Size(seq);
7590
7591 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7592 return NULL;
7593
7594 Py_BEGIN_ALLOW_THREADS
7595 n = readv(fd, iov, cnt);
7596 Py_END_ALLOW_THREADS
7597
7598 iov_cleanup(iov, buf, cnt);
7599 return PyLong_FromSsize_t(n);
7600}
7601#endif
7602
7603#ifdef HAVE_PREAD
7604PyDoc_STRVAR(posix_pread__doc__,
7605"pread(fd, buffersize, offset) -> string\n\n\
7606Read from a file descriptor, fd, at a position of offset. It will read up\n\
7607to buffersize number of bytes. The file offset remains unchanged.");
7608
7609static PyObject *
7610posix_pread(PyObject *self, PyObject *args)
7611{
7612 int fd, size;
7613 off_t offset;
7614 Py_ssize_t n;
7615 PyObject *buffer;
7616 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7617 return NULL;
7618
7619 if (size < 0) {
7620 errno = EINVAL;
7621 return posix_error();
7622 }
7623 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7624 if (buffer == NULL)
7625 return NULL;
7626 if (!_PyVerify_fd(fd)) {
7627 Py_DECREF(buffer);
7628 return posix_error();
7629 }
7630 Py_BEGIN_ALLOW_THREADS
7631 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7632 Py_END_ALLOW_THREADS
7633 if (n < 0) {
7634 Py_DECREF(buffer);
7635 return posix_error();
7636 }
7637 if (n != size)
7638 _PyBytes_Resize(&buffer, n);
7639 return buffer;
7640}
7641#endif
7642
7643PyDoc_STRVAR(posix_write__doc__,
Benjamin Peterson3b08a292013-05-24 14:35:57 -07007644"write(fd, data) -> byteswritten\n\n\
7645Write bytes to a file descriptor.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02007646
7647static PyObject *
7648posix_write(PyObject *self, PyObject *args)
7649{
7650 Py_buffer pbuf;
7651 int fd;
7652 Py_ssize_t size, len;
7653
7654 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7655 return NULL;
7656 if (!_PyVerify_fd(fd)) {
7657 PyBuffer_Release(&pbuf);
7658 return posix_error();
7659 }
7660 len = pbuf.len;
7661 Py_BEGIN_ALLOW_THREADS
7662#if defined(MS_WIN64) || defined(MS_WINDOWS)
7663 if (len > INT_MAX)
7664 len = INT_MAX;
7665 size = write(fd, pbuf.buf, (int)len);
7666#else
7667 size = write(fd, pbuf.buf, len);
7668#endif
7669 Py_END_ALLOW_THREADS
7670 PyBuffer_Release(&pbuf);
7671 if (size < 0)
7672 return posix_error();
7673 return PyLong_FromSsize_t(size);
7674}
7675
7676#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007677PyDoc_STRVAR(posix_sendfile__doc__,
7678"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7679sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7680 -> byteswritten\n\
7681Copy nbytes bytes from file descriptor in to file descriptor out.");
7682
7683static PyObject *
7684posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7685{
7686 int in, out;
7687 Py_ssize_t ret;
7688 off_t offset;
7689
7690#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7691#ifndef __APPLE__
7692 Py_ssize_t len;
7693#endif
7694 PyObject *headers = NULL, *trailers = NULL;
7695 Py_buffer *hbuf, *tbuf;
7696 off_t sbytes;
7697 struct sf_hdtr sf;
7698 int flags = 0;
7699 sf.headers = NULL;
7700 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007701 static char *keywords[] = {"out", "in",
7702 "offset", "count",
7703 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007704
7705#ifdef __APPLE__
7706 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007707 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007708#else
7709 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007710 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007711#endif
7712 &headers, &trailers, &flags))
7713 return NULL;
7714 if (headers != NULL) {
7715 if (!PySequence_Check(headers)) {
7716 PyErr_SetString(PyExc_TypeError,
7717 "sendfile() headers must be a sequence or None");
7718 return NULL;
7719 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007720 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007721 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007722 if (sf.hdr_cnt > 0 &&
7723 !(i = iov_setup(&(sf.headers), &hbuf,
7724 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007725 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007726#ifdef __APPLE__
7727 sbytes += i;
7728#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007729 }
7730 }
7731 if (trailers != NULL) {
7732 if (!PySequence_Check(trailers)) {
7733 PyErr_SetString(PyExc_TypeError,
7734 "sendfile() trailers must be a sequence or None");
7735 return NULL;
7736 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007737 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007738 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007739 if (sf.trl_cnt > 0 &&
7740 !(i = iov_setup(&(sf.trailers), &tbuf,
7741 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007742 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007743#ifdef __APPLE__
7744 sbytes += i;
7745#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007746 }
7747 }
7748
7749 Py_BEGIN_ALLOW_THREADS
7750#ifdef __APPLE__
7751 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7752#else
7753 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7754#endif
7755 Py_END_ALLOW_THREADS
7756
7757 if (sf.headers != NULL)
7758 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7759 if (sf.trailers != NULL)
7760 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7761
7762 if (ret < 0) {
7763 if ((errno == EAGAIN) || (errno == EBUSY)) {
7764 if (sbytes != 0) {
7765 // some data has been sent
7766 goto done;
7767 }
7768 else {
7769 // no data has been sent; upper application is supposed
7770 // to retry on EAGAIN or EBUSY
7771 return posix_error();
7772 }
7773 }
7774 return posix_error();
7775 }
7776 goto done;
7777
7778done:
7779 #if !defined(HAVE_LARGEFILE_SUPPORT)
7780 return Py_BuildValue("l", sbytes);
7781 #else
7782 return Py_BuildValue("L", sbytes);
7783 #endif
7784
7785#else
7786 Py_ssize_t count;
7787 PyObject *offobj;
7788 static char *keywords[] = {"out", "in",
7789 "offset", "count", NULL};
7790 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7791 keywords, &out, &in, &offobj, &count))
7792 return NULL;
7793#ifdef linux
7794 if (offobj == Py_None) {
7795 Py_BEGIN_ALLOW_THREADS
7796 ret = sendfile(out, in, NULL, count);
7797 Py_END_ALLOW_THREADS
7798 if (ret < 0)
7799 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007800 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007801 }
7802#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007803 if (!_parse_off_t(offobj, &offset))
7804 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007805 Py_BEGIN_ALLOW_THREADS
7806 ret = sendfile(out, in, &offset, count);
7807 Py_END_ALLOW_THREADS
7808 if (ret < 0)
7809 return posix_error();
7810 return Py_BuildValue("n", ret);
7811#endif
7812}
7813#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007814
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007815PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007816"fstat(fd) -> stat result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07007817Like stat(), but for an open file descriptor.\n\
7818Equivalent to stat(fd=fd).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007819
Barry Warsaw53699e91996-12-10 23:23:01 +00007820static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007821posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007822{
Victor Stinner8c62be82010-05-06 00:08:46 +00007823 int fd;
7824 STRUCT_STAT st;
7825 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01007826 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007827 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007828#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 /* on OpenVMS we must ensure that all bytes are written to the file */
7830 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007831#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007832 Py_BEGIN_ALLOW_THREADS
7833 res = FSTAT(fd, &st);
7834 Py_END_ALLOW_THREADS
7835 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007836#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01007837 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00007838#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007839 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007840#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007841 }
Tim Peters5aa91602002-01-30 05:46:57 +00007842
Victor Stinner4195b5c2012-02-08 23:03:19 +01007843 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007844}
7845
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007846PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007847"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007848Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007849connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007850
7851static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007852posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007853{
Victor Stinner8c62be82010-05-06 00:08:46 +00007854 int fd;
7855 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7856 return NULL;
7857 if (!_PyVerify_fd(fd))
7858 return PyBool_FromLong(0);
7859 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007860}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007861
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007862#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007863PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007864"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007865Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007866
Barry Warsaw53699e91996-12-10 23:23:01 +00007867static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007868posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007869{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007870#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007871 int fds[2];
7872 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007873 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 if (res != 0)
7875 return posix_error();
7876 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007877#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007878 HANDLE read, write;
7879 int read_fd, write_fd;
7880 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007881 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007882 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01007883 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007884 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7885 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7886 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007887#endif /* MS_WINDOWS */
Guido van Rossum687dd131993-05-17 08:34:16 +00007888}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007889#endif /* HAVE_PIPE */
7890
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007891#ifdef HAVE_PIPE2
7892PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007893"pipe2(flags) -> (read_end, write_end)\n\n\
7894Create a pipe with flags set atomically.\n\
7895flags can be constructed by ORing together one or more of these values:\n\
7896O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007897");
7898
7899static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007900posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007901{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007902 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007903 int fds[2];
7904 int res;
7905
Serhiy Storchaka78980432013-01-15 01:12:17 +02007906 flags = _PyLong_AsInt(arg);
Charles-François Natali368f34b2011-06-06 19:49:47 +02007907 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007908 return NULL;
7909
7910 res = pipe2(fds, flags);
7911 if (res != 0)
7912 return posix_error();
7913 return Py_BuildValue("(ii)", fds[0], fds[1]);
7914}
7915#endif /* HAVE_PIPE2 */
7916
Ross Lagerwall7807c352011-03-17 20:20:30 +02007917#ifdef HAVE_WRITEV
7918PyDoc_STRVAR(posix_writev__doc__,
7919"writev(fd, buffers) -> byteswritten\n\n\
7920Write the contents of buffers to a file descriptor, where buffers is an\n\
7921arbitrary sequence of buffers.\n\
7922Returns the total bytes written.");
7923
7924static PyObject *
7925posix_writev(PyObject *self, PyObject *args)
7926{
7927 int fd, cnt;
7928 Py_ssize_t res;
7929 PyObject *seq;
7930 struct iovec *iov;
7931 Py_buffer *buf;
7932 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7933 return NULL;
7934 if (!PySequence_Check(seq)) {
7935 PyErr_SetString(PyExc_TypeError,
7936 "writev() arg 2 must be a sequence");
7937 return NULL;
7938 }
7939 cnt = PySequence_Size(seq);
7940
7941 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7942 return NULL;
7943 }
7944
7945 Py_BEGIN_ALLOW_THREADS
7946 res = writev(fd, iov, cnt);
7947 Py_END_ALLOW_THREADS
7948
7949 iov_cleanup(iov, buf, cnt);
7950 return PyLong_FromSsize_t(res);
7951}
7952#endif
7953
7954#ifdef HAVE_PWRITE
7955PyDoc_STRVAR(posix_pwrite__doc__,
7956"pwrite(fd, string, offset) -> byteswritten\n\n\
7957Write string to a file descriptor, fd, from offset, leaving the file\n\
7958offset unchanged.");
7959
7960static PyObject *
7961posix_pwrite(PyObject *self, PyObject *args)
7962{
7963 Py_buffer pbuf;
7964 int fd;
7965 off_t offset;
7966 Py_ssize_t size;
7967
7968 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7969 return NULL;
7970
7971 if (!_PyVerify_fd(fd)) {
7972 PyBuffer_Release(&pbuf);
7973 return posix_error();
7974 }
7975 Py_BEGIN_ALLOW_THREADS
7976 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7977 Py_END_ALLOW_THREADS
7978 PyBuffer_Release(&pbuf);
7979 if (size < 0)
7980 return posix_error();
7981 return PyLong_FromSsize_t(size);
7982}
7983#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007984
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007985#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007986PyDoc_STRVAR(posix_mkfifo__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007987"mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\
7988Create a FIFO (a POSIX named pipe).\n\
7989\n\
7990If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7991 and path should be relative; path will then be relative to that directory.\n\
7992dir_fd may not be implemented on your platform.\n\
7993 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007994
Barry Warsaw53699e91996-12-10 23:23:01 +00007995static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007996posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007997{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007998 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 int mode = 0666;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008000 int dir_fd = DEFAULT_DIR_FD;
8001 int result;
8002 PyObject *return_value = NULL;
8003 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
8004
8005 memset(&path, 0, sizeof(path));
8006 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords,
8007 path_converter, &path,
8008 &mode,
8009#ifdef HAVE_MKFIFOAT
8010 dir_fd_converter, &dir_fd
8011#else
8012 dir_fd_unavailable, &dir_fd
8013#endif
8014 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00008015 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008016
Victor Stinner8c62be82010-05-06 00:08:46 +00008017 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008018#ifdef HAVE_MKFIFOAT
8019 if (dir_fd != DEFAULT_DIR_FD)
8020 result = mkfifoat(dir_fd, path.narrow, mode);
8021 else
8022#endif
8023 result = mkfifo(path.narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00008024 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008025
8026 if (result < 0) {
8027 return_value = posix_error();
8028 goto exit;
8029 }
8030
8031 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008033
8034exit:
8035 path_cleanup(&path);
8036 return return_value;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008037}
8038#endif
8039
Neal Norwitz11690112002-07-30 01:08:28 +00008040#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008041PyDoc_STRVAR(posix_mknod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008042"mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008043Create a filesystem node (file, device special file or named pipe)\n\
8044named filename. mode specifies both the permissions to use and the\n\
8045type of node to be created, being combined (bitwise OR) with one of\n\
8046S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008047device defines the newly created device special file (probably using\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008048os.makedev()), otherwise it is ignored.\n\
8049\n\
8050If dir_fd is not None, it should be a file descriptor open to a directory,\n\
8051 and path should be relative; path will then be relative to that directory.\n\
8052dir_fd may not be implemented on your platform.\n\
8053 If it is unavailable, using it will raise a NotImplementedError.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008054
8055
8056static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008057posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00008058{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008059 path_t path;
8060 int mode = 0666;
Victor Stinner8c62be82010-05-06 00:08:46 +00008061 int device = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008062 int dir_fd = DEFAULT_DIR_FD;
8063 int result;
8064 PyObject *return_value = NULL;
8065 static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL};
8066
8067 memset(&path, 0, sizeof(path));
8068 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords,
8069 path_converter, &path,
8070 &mode, &device,
8071#ifdef HAVE_MKNODAT
8072 dir_fd_converter, &dir_fd
8073#else
8074 dir_fd_unavailable, &dir_fd
8075#endif
8076 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00008077 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008078
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008080#ifdef HAVE_MKNODAT
8081 if (dir_fd != DEFAULT_DIR_FD)
8082 result = mknodat(dir_fd, path.narrow, mode, device);
8083 else
8084#endif
8085 result = mknod(path.narrow, mode, device);
Victor Stinner8c62be82010-05-06 00:08:46 +00008086 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008087
8088 if (result < 0) {
8089 return_value = posix_error();
8090 goto exit;
8091 }
8092
8093 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00008094 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02008095
Larry Hastings9cf065c2012-06-22 16:30:09 -07008096exit:
8097 path_cleanup(&path);
8098 return return_value;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008099}
8100#endif
8101
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008102#ifdef HAVE_DEVICE_MACROS
8103PyDoc_STRVAR(posix_major__doc__,
8104"major(device) -> major number\n\
8105Extracts a device major number from a raw device number.");
8106
8107static PyObject *
8108posix_major(PyObject *self, PyObject *args)
8109{
Victor Stinner8c62be82010-05-06 00:08:46 +00008110 int device;
8111 if (!PyArg_ParseTuple(args, "i:major", &device))
8112 return NULL;
8113 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008114}
8115
8116PyDoc_STRVAR(posix_minor__doc__,
8117"minor(device) -> minor number\n\
8118Extracts a device minor number from a raw device number.");
8119
8120static PyObject *
8121posix_minor(PyObject *self, PyObject *args)
8122{
Victor Stinner8c62be82010-05-06 00:08:46 +00008123 int device;
8124 if (!PyArg_ParseTuple(args, "i:minor", &device))
8125 return NULL;
8126 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008127}
8128
8129PyDoc_STRVAR(posix_makedev__doc__,
8130"makedev(major, minor) -> device number\n\
8131Composes a raw device number from the major and minor device numbers.");
8132
8133static PyObject *
8134posix_makedev(PyObject *self, PyObject *args)
8135{
Victor Stinner8c62be82010-05-06 00:08:46 +00008136 int major, minor;
8137 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
8138 return NULL;
8139 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008140}
8141#endif /* device macros */
8142
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008143
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008144#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008145PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008146"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008147Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008148
Barry Warsaw53699e91996-12-10 23:23:01 +00008149static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008150posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008151{
Victor Stinner8c62be82010-05-06 00:08:46 +00008152 int fd;
8153 off_t length;
8154 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008155
Ross Lagerwall7807c352011-03-17 20:20:30 +02008156 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00008157 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008158
Victor Stinner8c62be82010-05-06 00:08:46 +00008159 Py_BEGIN_ALLOW_THREADS
8160 res = ftruncate(fd, length);
8161 Py_END_ALLOW_THREADS
8162 if (res < 0)
8163 return posix_error();
8164 Py_INCREF(Py_None);
8165 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008166}
8167#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00008168
Ross Lagerwall7807c352011-03-17 20:20:30 +02008169#ifdef HAVE_TRUNCATE
8170PyDoc_STRVAR(posix_truncate__doc__,
8171"truncate(path, length)\n\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008172Truncate the file given by path to length bytes.\n\
8173On some platforms, path may also be specified as an open file descriptor.\n\
8174 If this functionality is unavailable, using it raises an exception.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02008175
8176static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008177posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008178{
Georg Brandl306336b2012-06-24 12:55:33 +02008179 path_t path;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008180 off_t length;
8181 int res;
Georg Brandl306336b2012-06-24 12:55:33 +02008182 PyObject *result = NULL;
8183 static char *keywords[] = {"path", "length", NULL};
Ross Lagerwall7807c352011-03-17 20:20:30 +02008184
Georg Brandl306336b2012-06-24 12:55:33 +02008185 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008186 path.function_name = "truncate";
Georg Brandl306336b2012-06-24 12:55:33 +02008187#ifdef HAVE_FTRUNCATE
8188 path.allow_fd = 1;
8189#endif
8190 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords,
8191 path_converter, &path,
8192 _parse_off_t, &length))
Ross Lagerwall7807c352011-03-17 20:20:30 +02008193 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008194
8195 Py_BEGIN_ALLOW_THREADS
Georg Brandl306336b2012-06-24 12:55:33 +02008196#ifdef HAVE_FTRUNCATE
8197 if (path.fd != -1)
8198 res = ftruncate(path.fd, length);
8199 else
8200#endif
8201 res = truncate(path.narrow, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008202 Py_END_ALLOW_THREADS
Ross Lagerwall7807c352011-03-17 20:20:30 +02008203 if (res < 0)
Victor Stinner292c8352012-10-30 02:17:38 +01008204 result = path_error(&path);
Georg Brandl306336b2012-06-24 12:55:33 +02008205 else {
8206 Py_INCREF(Py_None);
8207 result = Py_None;
8208 }
8209 path_cleanup(&path);
8210 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008211}
8212#endif
8213
8214#ifdef HAVE_POSIX_FALLOCATE
8215PyDoc_STRVAR(posix_posix_fallocate__doc__,
8216"posix_fallocate(fd, offset, len)\n\n\
8217Ensures that enough disk space is allocated for the file specified by fd\n\
8218starting from offset and continuing for len bytes.");
8219
8220static PyObject *
8221posix_posix_fallocate(PyObject *self, PyObject *args)
8222{
8223 off_t len, offset;
8224 int res, fd;
8225
8226 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
8227 &fd, _parse_off_t, &offset, _parse_off_t, &len))
8228 return NULL;
8229
8230 Py_BEGIN_ALLOW_THREADS
8231 res = posix_fallocate(fd, offset, len);
8232 Py_END_ALLOW_THREADS
8233 if (res != 0) {
8234 errno = res;
8235 return posix_error();
8236 }
8237 Py_RETURN_NONE;
8238}
8239#endif
8240
8241#ifdef HAVE_POSIX_FADVISE
8242PyDoc_STRVAR(posix_posix_fadvise__doc__,
8243"posix_fadvise(fd, offset, len, advice)\n\n\
8244Announces an intention to access data in a specific pattern thus allowing\n\
8245the kernel to make optimizations.\n\
8246The advice applies to the region of the file specified by fd starting at\n\
8247offset and continuing for len bytes.\n\
8248advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
8249POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
8250POSIX_FADV_DONTNEED.");
8251
8252static PyObject *
8253posix_posix_fadvise(PyObject *self, PyObject *args)
8254{
8255 off_t len, offset;
8256 int res, fd, advice;
8257
8258 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
8259 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
8260 return NULL;
8261
8262 Py_BEGIN_ALLOW_THREADS
8263 res = posix_fadvise(fd, offset, len, advice);
8264 Py_END_ALLOW_THREADS
8265 if (res != 0) {
8266 errno = res;
8267 return posix_error();
8268 }
8269 Py_RETURN_NONE;
8270}
8271#endif
8272
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008273#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008274PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008275"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008276Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008277
Fred Drake762e2061999-08-26 17:23:54 +00008278/* Save putenv() parameters as values here, so we can collect them when they
8279 * get re-set with another call for the same key. */
8280static PyObject *posix_putenv_garbage;
8281
Tim Peters5aa91602002-01-30 05:46:57 +00008282static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008283posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008284{
Victor Stinner84ae1182010-05-06 22:05:07 +00008285 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008286#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01008287 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008288 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01008289
Victor Stinner8c62be82010-05-06 00:08:46 +00008290 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008291 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01008292 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00008293 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008294
Victor Stinner65170952011-11-22 22:16:17 +01008295 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00008296 if (newstr == NULL) {
8297 PyErr_NoMemory();
8298 goto error;
8299 }
Victor Stinner65170952011-11-22 22:16:17 +01008300 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
8301 PyErr_Format(PyExc_ValueError,
8302 "the environment variable is longer than %u characters",
8303 _MAX_ENV);
8304 goto error;
8305 }
8306
Victor Stinner8c62be82010-05-06 00:08:46 +00008307 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02008308 if (newenv == NULL)
8309 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008310 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008311 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008312 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008313 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008314#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008315 PyObject *os1, *os2;
8316 char *s1, *s2;
8317 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008318
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008319 if (!PyArg_ParseTuple(args,
8320 "O&O&:putenv",
8321 PyUnicode_FSConverter, &os1,
8322 PyUnicode_FSConverter, &os2))
8323 return NULL;
8324 s1 = PyBytes_AsString(os1);
8325 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00008326
Victor Stinner65170952011-11-22 22:16:17 +01008327 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008328 if (newstr == NULL) {
8329 PyErr_NoMemory();
8330 goto error;
8331 }
8332
Victor Stinner8c62be82010-05-06 00:08:46 +00008333 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00008334 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008336 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008337 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008338#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008339
Victor Stinner8c62be82010-05-06 00:08:46 +00008340 /* Install the first arg and newstr in posix_putenv_garbage;
8341 * this will cause previous value to be collected. This has to
8342 * happen after the real putenv() call because the old value
8343 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01008344 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008345 /* really not much we can do; just leak */
8346 PyErr_Clear();
8347 }
8348 else {
8349 Py_DECREF(newstr);
8350 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00008351
Martin v. Löwis011e8422009-05-05 04:43:17 +00008352#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 Py_DECREF(os1);
8354 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00008355#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008356 Py_RETURN_NONE;
8357
8358error:
8359#ifndef MS_WINDOWS
8360 Py_DECREF(os1);
8361 Py_DECREF(os2);
8362#endif
8363 Py_XDECREF(newstr);
8364 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00008365}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008366#endif /* putenv */
8367
Guido van Rossumc524d952001-10-19 01:31:59 +00008368#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008369PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008370"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008371Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00008372
8373static PyObject *
8374posix_unsetenv(PyObject *self, PyObject *args)
8375{
Victor Stinner65170952011-11-22 22:16:17 +01008376 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01008377#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01008378 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01008379#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008380
8381 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00008382
Victor Stinner65170952011-11-22 22:16:17 +01008383 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00008384 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00008385
Victor Stinner984890f2011-11-24 13:53:38 +01008386#ifdef HAVE_BROKEN_UNSETENV
8387 unsetenv(PyBytes_AS_STRING(name));
8388#else
Victor Stinner65170952011-11-22 22:16:17 +01008389 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008390 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06008391 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01008392 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008393 }
Victor Stinner984890f2011-11-24 13:53:38 +01008394#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008395
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 /* Remove the key from posix_putenv_garbage;
8397 * this will cause it to be collected. This has to
8398 * happen after the real unsetenv() call because the
8399 * old value was still accessible until then.
8400 */
Victor Stinner65170952011-11-22 22:16:17 +01008401 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008402 /* really not much we can do; just leak */
8403 PyErr_Clear();
8404 }
Victor Stinner65170952011-11-22 22:16:17 +01008405 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00008406 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00008407}
8408#endif /* unsetenv */
8409
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008410PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008411"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008412Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008413
Guido van Rossumf68d8e52001-04-14 17:55:09 +00008414static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008415posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00008416{
Victor Stinner8c62be82010-05-06 00:08:46 +00008417 int code;
8418 char *message;
8419 if (!PyArg_ParseTuple(args, "i:strerror", &code))
8420 return NULL;
8421 message = strerror(code);
8422 if (message == NULL) {
8423 PyErr_SetString(PyExc_ValueError,
8424 "strerror() argument out of range");
8425 return NULL;
8426 }
Victor Stinner1b579672011-12-17 05:47:23 +01008427 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008428}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008429
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008430
Guido van Rossumc9641791998-08-04 15:26:23 +00008431#ifdef HAVE_SYS_WAIT_H
8432
Fred Drake106c1a02002-04-23 15:58:02 +00008433#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008434PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008435"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008436Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00008437
8438static PyObject *
8439posix_WCOREDUMP(PyObject *self, PyObject *args)
8440{
Victor Stinner8c62be82010-05-06 00:08:46 +00008441 WAIT_TYPE status;
8442 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008443
Victor Stinner8c62be82010-05-06 00:08:46 +00008444 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
8445 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008446
Victor Stinner8c62be82010-05-06 00:08:46 +00008447 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008448}
8449#endif /* WCOREDUMP */
8450
8451#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008452PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008453"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00008454Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008455job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00008456
8457static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008458posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00008459{
Victor Stinner8c62be82010-05-06 00:08:46 +00008460 WAIT_TYPE status;
8461 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008462
Victor Stinner8c62be82010-05-06 00:08:46 +00008463 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
8464 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008465
Victor Stinner8c62be82010-05-06 00:08:46 +00008466 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008467}
8468#endif /* WIFCONTINUED */
8469
Guido van Rossumc9641791998-08-04 15:26:23 +00008470#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008471PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008472"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008473Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008474
8475static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008476posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008477{
Victor Stinner8c62be82010-05-06 00:08:46 +00008478 WAIT_TYPE status;
8479 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008480
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
8482 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008483
Victor Stinner8c62be82010-05-06 00:08:46 +00008484 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008485}
8486#endif /* WIFSTOPPED */
8487
8488#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008489PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008490"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008491Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008492
8493static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008494posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008495{
Victor Stinner8c62be82010-05-06 00:08:46 +00008496 WAIT_TYPE status;
8497 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008498
Victor Stinner8c62be82010-05-06 00:08:46 +00008499 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
8500 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008501
Victor Stinner8c62be82010-05-06 00:08:46 +00008502 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008503}
8504#endif /* WIFSIGNALED */
8505
8506#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008507PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008508"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008509Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008510system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008511
8512static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008513posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008514{
Victor Stinner8c62be82010-05-06 00:08:46 +00008515 WAIT_TYPE status;
8516 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008517
Victor Stinner8c62be82010-05-06 00:08:46 +00008518 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
8519 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008520
Victor Stinner8c62be82010-05-06 00:08:46 +00008521 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008522}
8523#endif /* WIFEXITED */
8524
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008525#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008526PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008527"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008528Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008529
8530static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008531posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008532{
Victor Stinner8c62be82010-05-06 00:08:46 +00008533 WAIT_TYPE status;
8534 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008535
Victor Stinner8c62be82010-05-06 00:08:46 +00008536 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8537 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008538
Victor Stinner8c62be82010-05-06 00:08:46 +00008539 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008540}
8541#endif /* WEXITSTATUS */
8542
8543#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008544PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008545"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008546Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008547value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008548
8549static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008550posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008551{
Victor Stinner8c62be82010-05-06 00:08:46 +00008552 WAIT_TYPE status;
8553 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008554
Victor Stinner8c62be82010-05-06 00:08:46 +00008555 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8556 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008557
Victor Stinner8c62be82010-05-06 00:08:46 +00008558 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008559}
8560#endif /* WTERMSIG */
8561
8562#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008563PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008564"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008565Return the signal that stopped the process that provided\n\
8566the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008567
8568static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008569posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008570{
Victor Stinner8c62be82010-05-06 00:08:46 +00008571 WAIT_TYPE status;
8572 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008573
Victor Stinner8c62be82010-05-06 00:08:46 +00008574 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8575 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008576
Victor Stinner8c62be82010-05-06 00:08:46 +00008577 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008578}
8579#endif /* WSTOPSIG */
8580
8581#endif /* HAVE_SYS_WAIT_H */
8582
8583
Thomas Wouters477c8d52006-05-27 19:21:47 +00008584#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008585#ifdef _SCO_DS
8586/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8587 needed definitions in sys/statvfs.h */
8588#define _SVID3
8589#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008590#include <sys/statvfs.h>
8591
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008592static PyObject*
8593_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008594 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8595 if (v == NULL)
8596 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008597
8598#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008599 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8600 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8601 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8602 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8603 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8604 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8605 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8606 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8607 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8608 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008609#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008610 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8611 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8612 PyStructSequence_SET_ITEM(v, 2,
8613 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8614 PyStructSequence_SET_ITEM(v, 3,
8615 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8616 PyStructSequence_SET_ITEM(v, 4,
8617 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8618 PyStructSequence_SET_ITEM(v, 5,
8619 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8620 PyStructSequence_SET_ITEM(v, 6,
8621 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8622 PyStructSequence_SET_ITEM(v, 7,
8623 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8624 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8625 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008626#endif
8627
Victor Stinner8c62be82010-05-06 00:08:46 +00008628 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008629}
8630
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008631PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008632"fstatvfs(fd) -> statvfs result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008633Perform an fstatvfs system call on the given fd.\n\
8634Equivalent to statvfs(fd).");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008635
8636static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008637posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008638{
Victor Stinner8c62be82010-05-06 00:08:46 +00008639 int fd, res;
8640 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008641
Victor Stinner8c62be82010-05-06 00:08:46 +00008642 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8643 return NULL;
8644 Py_BEGIN_ALLOW_THREADS
8645 res = fstatvfs(fd, &st);
8646 Py_END_ALLOW_THREADS
8647 if (res != 0)
8648 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008649
Victor Stinner8c62be82010-05-06 00:08:46 +00008650 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008651}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008652#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008653
8654
Thomas Wouters477c8d52006-05-27 19:21:47 +00008655#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008656#include <sys/statvfs.h>
8657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008658PyDoc_STRVAR(posix_statvfs__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008659"statvfs(path)\n\n\
8660Perform a statvfs system call on the given path.\n\
8661\n\
8662path may always be specified as a string.\n\
8663On some platforms, path may also be specified as an open file descriptor.\n\
8664 If this functionality is unavailable, using it raises an exception.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008665
8666static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008667posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008668{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008669 static char *keywords[] = {"path", NULL};
8670 path_t path;
8671 int result;
8672 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008673 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008674
Larry Hastings9cf065c2012-06-22 16:30:09 -07008675 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008676 path.function_name = "statvfs";
Larry Hastings9cf065c2012-06-22 16:30:09 -07008677#ifdef HAVE_FSTATVFS
8678 path.allow_fd = 1;
8679#endif
8680 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords,
8681 path_converter, &path
8682 ))
8683 return NULL;
8684
8685 Py_BEGIN_ALLOW_THREADS
8686#ifdef HAVE_FSTATVFS
8687 if (path.fd != -1) {
8688#ifdef __APPLE__
8689 /* handle weak-linking on Mac OS X 10.3 */
8690 if (fstatvfs == NULL) {
8691 fd_specified("statvfs", path.fd);
8692 goto exit;
8693 }
8694#endif
8695 result = fstatvfs(path.fd, &st);
8696 }
8697 else
8698#endif
8699 result = statvfs(path.narrow, &st);
8700 Py_END_ALLOW_THREADS
8701
8702 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01008703 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008704 goto exit;
8705 }
8706
8707 return_value = _pystatvfs_fromstructstatvfs(st);
8708
8709exit:
8710 path_cleanup(&path);
8711 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00008712}
8713#endif /* HAVE_STATVFS */
8714
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008715#ifdef MS_WINDOWS
8716PyDoc_STRVAR(win32__getdiskusage__doc__,
8717"_getdiskusage(path) -> (total, free)\n\n\
8718Return disk usage statistics about the given path as (total, free) tuple.");
8719
8720static PyObject *
8721win32__getdiskusage(PyObject *self, PyObject *args)
8722{
8723 BOOL retval;
8724 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008725 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008726
Victor Stinner6139c1b2011-11-09 22:14:14 +01008727 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008728 return NULL;
8729
8730 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008731 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008732 Py_END_ALLOW_THREADS
8733 if (retval == 0)
8734 return PyErr_SetFromWindowsErr(0);
8735
8736 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8737}
8738#endif
8739
8740
Fred Drakec9680921999-12-13 16:37:25 +00008741/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8742 * It maps strings representing configuration variable names to
8743 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008744 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008745 * rarely-used constants. There are three separate tables that use
8746 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008747 *
8748 * This code is always included, even if none of the interfaces that
8749 * need it are included. The #if hackery needed to avoid it would be
8750 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008751 */
8752struct constdef {
8753 char *name;
8754 long value;
8755};
8756
Fred Drake12c6e2d1999-12-14 21:25:03 +00008757static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008758conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008759 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008760{
Christian Heimes217cfd12007-12-02 14:31:20 +00008761 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008762 *valuep = PyLong_AS_LONG(arg);
8763 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008764 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008765 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008766 /* look up the value in the table using a binary search */
8767 size_t lo = 0;
8768 size_t mid;
8769 size_t hi = tablesize;
8770 int cmp;
8771 const char *confname;
8772 if (!PyUnicode_Check(arg)) {
8773 PyErr_SetString(PyExc_TypeError,
8774 "configuration names must be strings or integers");
8775 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008776 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008777 confname = _PyUnicode_AsString(arg);
8778 if (confname == NULL)
8779 return 0;
8780 while (lo < hi) {
8781 mid = (lo + hi) / 2;
8782 cmp = strcmp(confname, table[mid].name);
8783 if (cmp < 0)
8784 hi = mid;
8785 else if (cmp > 0)
8786 lo = mid + 1;
8787 else {
8788 *valuep = table[mid].value;
8789 return 1;
8790 }
8791 }
8792 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8793 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008794 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008795}
8796
8797
8798#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8799static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008800#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008801 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008802#endif
8803#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008804 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008805#endif
Fred Drakec9680921999-12-13 16:37:25 +00008806#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008807 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008808#endif
8809#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008810 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008811#endif
8812#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008813 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008814#endif
8815#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008816 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008817#endif
8818#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008819 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008820#endif
8821#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008823#endif
8824#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008825 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008826#endif
8827#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008828 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008829#endif
8830#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008832#endif
8833#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008834 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008835#endif
8836#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008838#endif
8839#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008840 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008841#endif
8842#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008843 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008844#endif
8845#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008846 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008847#endif
8848#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008850#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008851#ifdef _PC_ACL_ENABLED
8852 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8853#endif
8854#ifdef _PC_MIN_HOLE_SIZE
8855 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8856#endif
8857#ifdef _PC_ALLOC_SIZE_MIN
8858 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8859#endif
8860#ifdef _PC_REC_INCR_XFER_SIZE
8861 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8862#endif
8863#ifdef _PC_REC_MAX_XFER_SIZE
8864 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8865#endif
8866#ifdef _PC_REC_MIN_XFER_SIZE
8867 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8868#endif
8869#ifdef _PC_REC_XFER_ALIGN
8870 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8871#endif
8872#ifdef _PC_SYMLINK_MAX
8873 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8874#endif
8875#ifdef _PC_XATTR_ENABLED
8876 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8877#endif
8878#ifdef _PC_XATTR_EXISTS
8879 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8880#endif
8881#ifdef _PC_TIMESTAMP_RESOLUTION
8882 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8883#endif
Fred Drakec9680921999-12-13 16:37:25 +00008884};
8885
Fred Drakec9680921999-12-13 16:37:25 +00008886static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008887conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008888{
8889 return conv_confname(arg, valuep, posix_constants_pathconf,
8890 sizeof(posix_constants_pathconf)
8891 / sizeof(struct constdef));
8892}
8893#endif
8894
8895#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008896PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008897"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008898Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008899If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008900
8901static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008902posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008903{
8904 PyObject *result = NULL;
8905 int name, fd;
8906
Fred Drake12c6e2d1999-12-14 21:25:03 +00008907 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8908 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008909 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008910
Stefan Krah0e803b32010-11-26 16:16:47 +00008911 errno = 0;
8912 limit = fpathconf(fd, name);
8913 if (limit == -1 && errno != 0)
8914 posix_error();
8915 else
8916 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008917 }
8918 return result;
8919}
8920#endif
8921
8922
8923#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008924PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008925"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008926Return the configuration limit name for the file or directory path.\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008927If there is no limit, return -1.\n\
8928On some platforms, path may also be specified as an open file descriptor.\n\
8929 If this functionality is unavailable, using it raises an exception.");
Fred Drakec9680921999-12-13 16:37:25 +00008930
8931static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008932posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs)
Fred Drakec9680921999-12-13 16:37:25 +00008933{
Georg Brandl306336b2012-06-24 12:55:33 +02008934 path_t path;
Fred Drakec9680921999-12-13 16:37:25 +00008935 PyObject *result = NULL;
8936 int name;
Georg Brandl306336b2012-06-24 12:55:33 +02008937 static char *keywords[] = {"path", "name", NULL};
Fred Drakec9680921999-12-13 16:37:25 +00008938
Georg Brandl306336b2012-06-24 12:55:33 +02008939 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008940 path.function_name = "pathconf";
Georg Brandl306336b2012-06-24 12:55:33 +02008941#ifdef HAVE_FPATHCONF
8942 path.allow_fd = 1;
8943#endif
8944 if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords,
8945 path_converter, &path,
8946 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008947 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008948
Victor Stinner8c62be82010-05-06 00:08:46 +00008949 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02008950#ifdef HAVE_FPATHCONF
8951 if (path.fd != -1)
8952 limit = fpathconf(path.fd, name);
8953 else
8954#endif
8955 limit = pathconf(path.narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00008956 if (limit == -1 && errno != 0) {
8957 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008958 /* could be a path or name problem */
8959 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008960 else
Victor Stinner292c8352012-10-30 02:17:38 +01008961 result = path_error(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008962 }
8963 else
8964 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008965 }
Georg Brandl306336b2012-06-24 12:55:33 +02008966 path_cleanup(&path);
Fred Drakec9680921999-12-13 16:37:25 +00008967 return result;
8968}
8969#endif
8970
8971#ifdef HAVE_CONFSTR
8972static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008973#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008974 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008975#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008976#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008977 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008978#endif
8979#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008980 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008981#endif
Fred Draked86ed291999-12-15 15:34:33 +00008982#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008984#endif
8985#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008986 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008987#endif
8988#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008989 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008990#endif
8991#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008992 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008993#endif
Fred Drakec9680921999-12-13 16:37:25 +00008994#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008995 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008996#endif
8997#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008998 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008999#endif
9000#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009001 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009002#endif
9003#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009004 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009005#endif
9006#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009007 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009008#endif
9009#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009010 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009011#endif
9012#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009013 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009014#endif
9015#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009016 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009017#endif
Fred Draked86ed291999-12-15 15:34:33 +00009018#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00009019 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00009020#endif
Fred Drakec9680921999-12-13 16:37:25 +00009021#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00009022 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00009023#endif
Fred Draked86ed291999-12-15 15:34:33 +00009024#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009025 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00009026#endif
9027#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009028 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00009029#endif
9030#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009031 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00009032#endif
9033#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009034 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00009035#endif
Fred Drakec9680921999-12-13 16:37:25 +00009036#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009037 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009038#endif
9039#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009040 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009041#endif
9042#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009043 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009044#endif
9045#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009046 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009047#endif
9048#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009049 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009050#endif
9051#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009052 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009053#endif
9054#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009055 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009056#endif
9057#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009058 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009059#endif
9060#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009061 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009062#endif
9063#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009064 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009065#endif
9066#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009067 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009068#endif
9069#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009070 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009071#endif
9072#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009073 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009074#endif
9075#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009076 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009077#endif
9078#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00009079 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00009080#endif
9081#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00009082 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00009083#endif
Fred Draked86ed291999-12-15 15:34:33 +00009084#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009085 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009086#endif
9087#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00009088 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00009089#endif
9090#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00009091 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00009092#endif
9093#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009094 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009095#endif
9096#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009097 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009098#endif
9099#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00009100 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00009101#endif
9102#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009103 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00009104#endif
9105#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00009106 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00009107#endif
9108#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00009109 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00009110#endif
9111#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00009112 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00009113#endif
9114#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00009115 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00009116#endif
9117#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00009119#endif
9120#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00009121 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00009122#endif
Fred Drakec9680921999-12-13 16:37:25 +00009123};
9124
9125static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009126conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009127{
9128 return conv_confname(arg, valuep, posix_constants_confstr,
9129 sizeof(posix_constants_confstr)
9130 / sizeof(struct constdef));
9131}
9132
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009133PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009134"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009135Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009136
9137static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009138posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009139{
9140 PyObject *result = NULL;
9141 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00009142 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00009143 int len;
Fred Drakec9680921999-12-13 16:37:25 +00009144
Victor Stinnercb043522010-09-10 23:49:04 +00009145 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
9146 return NULL;
9147
9148 errno = 0;
9149 len = confstr(name, buffer, sizeof(buffer));
9150 if (len == 0) {
9151 if (errno) {
9152 posix_error();
9153 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009154 }
9155 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009156 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009157 }
9158 }
Victor Stinnercb043522010-09-10 23:49:04 +00009159
9160 if ((unsigned int)len >= sizeof(buffer)) {
9161 char *buf = PyMem_Malloc(len);
9162 if (buf == NULL)
9163 return PyErr_NoMemory();
9164 confstr(name, buf, len);
9165 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
9166 PyMem_Free(buf);
9167 }
9168 else
9169 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009170 return result;
9171}
9172#endif
9173
9174
9175#ifdef HAVE_SYSCONF
9176static struct constdef posix_constants_sysconf[] = {
9177#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009178 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009179#endif
9180#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009181 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009182#endif
9183#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009184 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009185#endif
9186#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009187 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009188#endif
9189#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009190 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009191#endif
9192#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009193 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009194#endif
9195#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009196 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009197#endif
9198#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009199 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009200#endif
9201#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009202 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009203#endif
9204#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009205 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009206#endif
Fred Draked86ed291999-12-15 15:34:33 +00009207#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009208 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009209#endif
9210#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009211 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009212#endif
Fred Drakec9680921999-12-13 16:37:25 +00009213#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009214 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009215#endif
Fred Drakec9680921999-12-13 16:37:25 +00009216#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009217 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009218#endif
9219#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009220 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009221#endif
9222#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009223 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009224#endif
9225#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009226 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009227#endif
9228#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009229 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009230#endif
Fred Draked86ed291999-12-15 15:34:33 +00009231#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009232 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009233#endif
Fred Drakec9680921999-12-13 16:37:25 +00009234#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009235 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009236#endif
9237#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009238 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009239#endif
9240#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009241 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009242#endif
9243#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009244 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009245#endif
9246#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009247 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009248#endif
Fred Draked86ed291999-12-15 15:34:33 +00009249#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009250 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009251#endif
Fred Drakec9680921999-12-13 16:37:25 +00009252#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009253 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009254#endif
9255#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009256 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009257#endif
9258#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009259 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009260#endif
9261#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009262 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009263#endif
9264#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009265 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009266#endif
9267#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009268 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009269#endif
9270#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009271 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009272#endif
9273#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009274 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009275#endif
9276#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009277 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009278#endif
9279#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009280 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009281#endif
9282#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009283 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009284#endif
9285#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009286 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009287#endif
9288#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009289 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009290#endif
9291#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009292 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009293#endif
9294#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009295 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009296#endif
9297#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009298 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009299#endif
9300#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009301 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00009302#endif
9303#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009304 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009305#endif
9306#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009307 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009308#endif
9309#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009310 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009311#endif
9312#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009313 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009314#endif
9315#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009316 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009317#endif
9318#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009319 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009320#endif
Fred Draked86ed291999-12-15 15:34:33 +00009321#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00009322 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00009323#endif
Fred Drakec9680921999-12-13 16:37:25 +00009324#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009325 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009326#endif
9327#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009328 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009329#endif
9330#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009331 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009332#endif
Fred Draked86ed291999-12-15 15:34:33 +00009333#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009334 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00009335#endif
Fred Drakec9680921999-12-13 16:37:25 +00009336#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00009337 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00009338#endif
Fred Draked86ed291999-12-15 15:34:33 +00009339#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009340 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00009341#endif
9342#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00009343 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00009344#endif
Fred Drakec9680921999-12-13 16:37:25 +00009345#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009346 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009347#endif
9348#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009349 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009350#endif
9351#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009352 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009353#endif
9354#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009355 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009356#endif
Fred Draked86ed291999-12-15 15:34:33 +00009357#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00009358 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00009359#endif
Fred Drakec9680921999-12-13 16:37:25 +00009360#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00009361 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00009362#endif
9363#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009364 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00009365#endif
9366#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009367 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009368#endif
9369#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009370 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00009371#endif
9372#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009373 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00009374#endif
9375#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00009376 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00009377#endif
9378#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00009379 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00009380#endif
Fred Draked86ed291999-12-15 15:34:33 +00009381#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00009382 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00009383#endif
Fred Drakec9680921999-12-13 16:37:25 +00009384#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009385 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009386#endif
9387#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009388 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009389#endif
Fred Draked86ed291999-12-15 15:34:33 +00009390#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009391 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009392#endif
Fred Drakec9680921999-12-13 16:37:25 +00009393#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009394 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009395#endif
9396#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009397 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009398#endif
9399#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009400 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009401#endif
9402#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009403 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009404#endif
9405#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009406 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009407#endif
9408#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009409 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009410#endif
9411#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009412 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009413#endif
9414#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009415 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00009416#endif
9417#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009418 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00009419#endif
Fred Draked86ed291999-12-15 15:34:33 +00009420#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009421 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00009422#endif
9423#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009424 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00009425#endif
Fred Drakec9680921999-12-13 16:37:25 +00009426#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00009427 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00009428#endif
9429#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009430 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009431#endif
9432#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009433 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009434#endif
9435#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009436 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009437#endif
9438#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009439 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009440#endif
9441#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009442 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009443#endif
9444#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00009445 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00009446#endif
9447#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00009448 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00009449#endif
9450#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009451 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00009452#endif
9453#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009454 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00009455#endif
9456#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00009457 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00009458#endif
9459#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009460 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00009461#endif
9462#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009463 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00009464#endif
9465#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00009466 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00009467#endif
9468#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00009469 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00009470#endif
9471#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00009472 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00009473#endif
9474#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00009475 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00009476#endif
9477#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009478 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009479#endif
9480#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009481 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009482#endif
9483#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00009484 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00009485#endif
9486#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009487 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009488#endif
9489#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009490 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009491#endif
9492#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00009493 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00009494#endif
9495#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009496 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009497#endif
9498#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009499 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009500#endif
9501#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009502 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00009503#endif
9504#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00009505 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00009506#endif
9507#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009508 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009509#endif
9510#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009511 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009512#endif
9513#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009514 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00009515#endif
9516#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009517 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009518#endif
9519#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009520 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009521#endif
9522#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009523 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009524#endif
9525#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009526 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009527#endif
9528#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009529 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009530#endif
Fred Draked86ed291999-12-15 15:34:33 +00009531#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00009532 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00009533#endif
Fred Drakec9680921999-12-13 16:37:25 +00009534#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00009535 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00009536#endif
9537#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009538 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009539#endif
9540#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00009541 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00009542#endif
9543#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009544 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009545#endif
9546#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009547 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009548#endif
9549#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009550 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009551#endif
9552#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00009553 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00009554#endif
9555#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009556 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009557#endif
9558#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009559 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009560#endif
9561#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009562 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009563#endif
9564#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009565 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009566#endif
9567#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009568 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00009569#endif
9570#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009571 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00009572#endif
9573#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00009574 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00009575#endif
9576#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009577 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009578#endif
9579#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009580 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009581#endif
9582#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009583 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009584#endif
9585#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009586 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009587#endif
9588#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009589 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009590#endif
9591#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009592 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009593#endif
9594#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009595 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009596#endif
9597#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009598 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009599#endif
9600#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009601 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009602#endif
9603#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009604 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009605#endif
9606#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009607 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009608#endif
9609#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009610 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009611#endif
9612#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009613 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009614#endif
9615#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009616 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009617#endif
9618#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009619 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009620#endif
9621#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009622 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009623#endif
9624#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009625 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009626#endif
9627#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009628 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009629#endif
9630#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009631 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009632#endif
9633#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009634 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009635#endif
9636#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009637 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009638#endif
9639#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009640 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009641#endif
9642#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009643 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009644#endif
9645#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009646 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009647#endif
9648#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009649 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009650#endif
9651#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009652 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009653#endif
9654#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009655 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009656#endif
9657#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009658 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009659#endif
9660#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009661 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009662#endif
9663#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009664 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009665#endif
9666#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009667 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009668#endif
9669};
9670
9671static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009672conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009673{
9674 return conv_confname(arg, valuep, posix_constants_sysconf,
9675 sizeof(posix_constants_sysconf)
9676 / sizeof(struct constdef));
9677}
9678
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009679PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009680"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009681Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009682
9683static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009684posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009685{
9686 PyObject *result = NULL;
9687 int name;
9688
9689 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner6fdd7b82013-05-16 22:26:29 +02009690 long value;
Fred Drakec9680921999-12-13 16:37:25 +00009691
9692 errno = 0;
9693 value = sysconf(name);
9694 if (value == -1 && errno != 0)
9695 posix_error();
9696 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009697 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009698 }
9699 return result;
9700}
9701#endif
9702
9703
Fred Drakebec628d1999-12-15 18:31:10 +00009704/* This code is used to ensure that the tables of configuration value names
9705 * are in sorted order as required by conv_confname(), and also to build the
9706 * the exported dictionaries that are used to publish information about the
9707 * names available on the host platform.
9708 *
9709 * Sorting the table at runtime ensures that the table is properly ordered
9710 * when used, even for platforms we're not able to test on. It also makes
9711 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009712 */
Fred Drakebec628d1999-12-15 18:31:10 +00009713
9714static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009715cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009716{
9717 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009718 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009719 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009720 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009721
9722 return strcmp(c1->name, c2->name);
9723}
9724
9725static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009726setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009727 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009728{
Fred Drakebec628d1999-12-15 18:31:10 +00009729 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009730 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009731
9732 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9733 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009734 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009735 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009736
Barry Warsaw3155db32000-04-13 15:20:40 +00009737 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009738 PyObject *o = PyLong_FromLong(table[i].value);
9739 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9740 Py_XDECREF(o);
9741 Py_DECREF(d);
9742 return -1;
9743 }
9744 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009745 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009746 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009747}
9748
Fred Drakebec628d1999-12-15 18:31:10 +00009749/* Return -1 on failure, 0 on success. */
9750static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009751setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009752{
9753#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009754 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009755 sizeof(posix_constants_pathconf)
9756 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009757 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009758 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009759#endif
9760#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009761 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009762 sizeof(posix_constants_confstr)
9763 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009764 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009765 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009766#endif
9767#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009768 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009769 sizeof(posix_constants_sysconf)
9770 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009771 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009772 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009773#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009774 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009775}
Fred Draked86ed291999-12-15 15:34:33 +00009776
9777
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009778PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009779"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009780Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009781in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009782
9783static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009784posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009785{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009786 abort();
9787 /*NOTREACHED*/
9788 Py_FatalError("abort() called from Python code didn't abort!");
9789 return NULL;
9790}
Fred Drakebec628d1999-12-15 18:31:10 +00009791
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009792#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009793PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009794"startfile(filepath [, operation]) - Start a file with its associated\n\
9795application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009796\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009797When \"operation\" is not specified or \"open\", this acts like\n\
9798double-clicking the file in Explorer, or giving the file name as an\n\
9799argument to the DOS \"start\" command: the file is opened with whatever\n\
9800application (if any) its extension is associated.\n\
9801When another \"operation\" is given, it specifies what should be done with\n\
9802the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009803\n\
9804startfile returns as soon as the associated application is launched.\n\
9805There is no option to wait for the application to close, and no way\n\
9806to retrieve the application's exit status.\n\
9807\n\
9808The filepath is relative to the current directory. If you want to use\n\
9809an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009810the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009811
9812static PyObject *
9813win32_startfile(PyObject *self, PyObject *args)
9814{
Victor Stinner8c62be82010-05-06 00:08:46 +00009815 PyObject *ofilepath;
9816 char *filepath;
9817 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009818 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009819 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009820
Victor Stinnereb5657a2011-09-30 01:44:27 +02009821 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009822 if (!PyArg_ParseTuple(args, "U|s:startfile",
9823 &unipath, &operation)) {
9824 PyErr_Clear();
9825 goto normal;
9826 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009827
Victor Stinner8c62be82010-05-06 00:08:46 +00009828 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009829 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009830 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009831 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009832 PyErr_Clear();
9833 operation = NULL;
9834 goto normal;
9835 }
9836 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009837
Victor Stinnereb5657a2011-09-30 01:44:27 +02009838 wpath = PyUnicode_AsUnicode(unipath);
9839 if (wpath == NULL)
9840 goto normal;
9841 if (uoperation) {
9842 woperation = PyUnicode_AsUnicode(uoperation);
9843 if (woperation == NULL)
9844 goto normal;
9845 }
9846 else
9847 woperation = NULL;
9848
Victor Stinner8c62be82010-05-06 00:08:46 +00009849 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009850 rc = ShellExecuteW((HWND)0, woperation, wpath,
9851 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009852 Py_END_ALLOW_THREADS
9853
Victor Stinnereb5657a2011-09-30 01:44:27 +02009854 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009855 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009856 win32_error_object("startfile", unipath);
9857 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009858 }
9859 Py_INCREF(Py_None);
9860 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009861
9862normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009863 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9864 PyUnicode_FSConverter, &ofilepath,
9865 &operation))
9866 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009867 if (win32_warn_bytes_api()) {
9868 Py_DECREF(ofilepath);
9869 return NULL;
9870 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009871 filepath = PyBytes_AsString(ofilepath);
9872 Py_BEGIN_ALLOW_THREADS
9873 rc = ShellExecute((HWND)0, operation, filepath,
9874 NULL, NULL, SW_SHOWNORMAL);
9875 Py_END_ALLOW_THREADS
9876 if (rc <= (HINSTANCE)32) {
9877 PyObject *errval = win32_error("startfile", filepath);
9878 Py_DECREF(ofilepath);
9879 return errval;
9880 }
9881 Py_DECREF(ofilepath);
9882 Py_INCREF(Py_None);
9883 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009884}
9885#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009886
Martin v. Löwis438b5342002-12-27 10:16:42 +00009887#ifdef HAVE_GETLOADAVG
9888PyDoc_STRVAR(posix_getloadavg__doc__,
9889"getloadavg() -> (float, float, float)\n\n\
9890Return the number of processes in the system run queue averaged over\n\
9891the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9892was unobtainable");
9893
9894static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009895posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009896{
9897 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009898 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009899 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9900 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009901 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009902 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009903}
9904#endif
9905
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009906PyDoc_STRVAR(device_encoding__doc__,
9907"device_encoding(fd) -> str\n\n\
9908Return a string describing the encoding of the device\n\
9909if the output is a terminal; else return None.");
9910
9911static PyObject *
9912device_encoding(PyObject *self, PyObject *args)
9913{
Victor Stinner8c62be82010-05-06 00:08:46 +00009914 int fd;
Brett Cannonefb00c02012-02-29 18:31:31 -05009915
Victor Stinner8c62be82010-05-06 00:08:46 +00009916 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9917 return NULL;
Brett Cannonefb00c02012-02-29 18:31:31 -05009918
9919 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009920}
9921
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009922#ifdef HAVE_SETRESUID
9923PyDoc_STRVAR(posix_setresuid__doc__,
9924"setresuid(ruid, euid, suid)\n\n\
9925Set the current process's real, effective, and saved user ids.");
9926
9927static PyObject*
9928posix_setresuid (PyObject *self, PyObject *args)
9929{
Victor Stinner8c62be82010-05-06 00:08:46 +00009930 /* We assume uid_t is no larger than a long. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009931 uid_t ruid, euid, suid;
9932 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
9933 _Py_Uid_Converter, &ruid,
9934 _Py_Uid_Converter, &euid,
9935 _Py_Uid_Converter, &suid))
Victor Stinner8c62be82010-05-06 00:08:46 +00009936 return NULL;
9937 if (setresuid(ruid, euid, suid) < 0)
9938 return posix_error();
9939 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009940}
9941#endif
9942
9943#ifdef HAVE_SETRESGID
9944PyDoc_STRVAR(posix_setresgid__doc__,
9945"setresgid(rgid, egid, sgid)\n\n\
9946Set the current process's real, effective, and saved group ids.");
9947
9948static PyObject*
9949posix_setresgid (PyObject *self, PyObject *args)
9950{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009951 gid_t rgid, egid, sgid;
9952 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
9953 _Py_Gid_Converter, &rgid,
9954 _Py_Gid_Converter, &egid,
9955 _Py_Gid_Converter, &sgid))
Victor Stinner8c62be82010-05-06 00:08:46 +00009956 return NULL;
9957 if (setresgid(rgid, egid, sgid) < 0)
9958 return posix_error();
9959 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009960}
9961#endif
9962
9963#ifdef HAVE_GETRESUID
9964PyDoc_STRVAR(posix_getresuid__doc__,
9965"getresuid() -> (ruid, euid, suid)\n\n\
9966Get tuple of the current process's real, effective, and saved user ids.");
9967
9968static PyObject*
9969posix_getresuid (PyObject *self, PyObject *noargs)
9970{
Victor Stinner8c62be82010-05-06 00:08:46 +00009971 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +00009972 if (getresuid(&ruid, &euid, &suid) < 0)
9973 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009974 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
9975 _PyLong_FromUid(euid),
9976 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009977}
9978#endif
9979
9980#ifdef HAVE_GETRESGID
9981PyDoc_STRVAR(posix_getresgid__doc__,
9982"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009983Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009984
9985static PyObject*
9986posix_getresgid (PyObject *self, PyObject *noargs)
9987{
Victor Stinner8c62be82010-05-06 00:08:46 +00009988 uid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +00009989 if (getresgid(&rgid, &egid, &sgid) < 0)
9990 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009991 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
9992 _PyLong_FromGid(egid),
9993 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009994}
9995#endif
9996
Benjamin Peterson9428d532011-09-14 11:45:52 -04009997#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -04009998
Benjamin Peterson799bd802011-08-31 22:15:17 -04009999PyDoc_STRVAR(posix_getxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010000"getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\
10001Return the value of extended attribute attribute on path.\n\
10002\n\
10003path may be either a string or an open file descriptor.\n\
10004If follow_symlinks is False, and the last element of the path is a symbolic\n\
10005 link, getxattr will examine the symbolic link itself instead of the file\n\
10006 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010007
10008static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010009posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010010{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010011 path_t path;
10012 path_t attribute;
10013 int follow_symlinks = 1;
10014 PyObject *buffer = NULL;
10015 int i;
10016 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010017
Larry Hastings9cf065c2012-06-22 16:30:09 -070010018 memset(&path, 0, sizeof(path));
10019 memset(&attribute, 0, sizeof(attribute));
Victor Stinner292c8352012-10-30 02:17:38 +010010020 path.function_name = "getxattr";
10021 attribute.function_name = "getxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010022 path.allow_fd = 1;
10023 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords,
10024 path_converter, &path,
10025 path_converter, &attribute,
10026 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010027 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010028
Larry Hastings9cf065c2012-06-22 16:30:09 -070010029 if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks))
10030 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010031
Larry Hastings9cf065c2012-06-22 16:30:09 -070010032 for (i = 0; ; i++) {
10033 void *ptr;
10034 ssize_t result;
10035 static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
10036 Py_ssize_t buffer_size = buffer_sizes[i];
10037 if (!buffer_size) {
Victor Stinner292c8352012-10-30 02:17:38 +010010038 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010039 goto exit;
10040 }
10041 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
10042 if (!buffer)
10043 goto exit;
10044 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010045
Larry Hastings9cf065c2012-06-22 16:30:09 -070010046 Py_BEGIN_ALLOW_THREADS;
10047 if (path.fd >= 0)
10048 result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size);
10049 else if (follow_symlinks)
10050 result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size);
10051 else
10052 result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size);
10053 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010054
Larry Hastings9cf065c2012-06-22 16:30:09 -070010055 if (result < 0) {
10056 Py_DECREF(buffer);
10057 buffer = NULL;
10058 if (errno == ERANGE)
10059 continue;
Victor Stinner292c8352012-10-30 02:17:38 +010010060 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010061 goto exit;
10062 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010063
Larry Hastings9cf065c2012-06-22 16:30:09 -070010064 if (result != buffer_size) {
10065 /* Can only shrink. */
10066 _PyBytes_Resize(&buffer, result);
10067 }
10068 break;
10069 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010070
Larry Hastings9cf065c2012-06-22 16:30:09 -070010071exit:
10072 path_cleanup(&path);
10073 path_cleanup(&attribute);
10074 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010075}
10076
10077PyDoc_STRVAR(posix_setxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010078"setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\
10079Set extended attribute attribute on path to value.\n\
10080path may be either a string or an open file descriptor.\n\
10081If follow_symlinks is False, and the last element of the path is a symbolic\n\
10082 link, setxattr will modify the symbolic link itself instead of the file\n\
10083 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010084
10085static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010086posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010087{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010088 path_t path;
10089 path_t attribute;
10090 Py_buffer value;
10091 int flags = 0;
10092 int follow_symlinks = 1;
10093 int result;
10094 PyObject *return_value = NULL;
10095 static char *keywords[] = {"path", "attribute", "value",
10096 "flags", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010097
Larry Hastings9cf065c2012-06-22 16:30:09 -070010098 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010099 path.function_name = "setxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010100 path.allow_fd = 1;
10101 memset(&attribute, 0, sizeof(attribute));
10102 memset(&value, 0, sizeof(value));
10103 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr",
10104 keywords,
10105 path_converter, &path,
10106 path_converter, &attribute,
10107 &value, &flags,
10108 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010109 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010110
10111 if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks))
10112 goto exit;
10113
Benjamin Peterson799bd802011-08-31 22:15:17 -040010114 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010115 if (path.fd > -1)
10116 result = fsetxattr(path.fd, attribute.narrow,
10117 value.buf, value.len, flags);
10118 else if (follow_symlinks)
10119 result = setxattr(path.narrow, attribute.narrow,
10120 value.buf, value.len, flags);
10121 else
10122 result = lsetxattr(path.narrow, attribute.narrow,
10123 value.buf, value.len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010124 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010125
Larry Hastings9cf065c2012-06-22 16:30:09 -070010126 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +010010127 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010128 goto exit;
10129 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010130
Larry Hastings9cf065c2012-06-22 16:30:09 -070010131 return_value = Py_None;
10132 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010133
Larry Hastings9cf065c2012-06-22 16:30:09 -070010134exit:
10135 path_cleanup(&path);
10136 path_cleanup(&attribute);
10137 PyBuffer_Release(&value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010138
Larry Hastings9cf065c2012-06-22 16:30:09 -070010139 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010140}
10141
10142PyDoc_STRVAR(posix_removexattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010143"removexattr(path, attribute, *, follow_symlinks=True)\n\n\
10144Remove extended attribute attribute on path.\n\
10145path may be either a string or an open file descriptor.\n\
10146If follow_symlinks is False, and the last element of the path is a symbolic\n\
10147 link, removexattr will modify the symbolic link itself instead of the file\n\
10148 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010149
10150static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010151posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010152{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010153 path_t path;
10154 path_t attribute;
10155 int follow_symlinks = 1;
10156 int result;
10157 PyObject *return_value = NULL;
10158 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010159
Larry Hastings9cf065c2012-06-22 16:30:09 -070010160 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010161 path.function_name = "removexattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010162 memset(&attribute, 0, sizeof(attribute));
Victor Stinner292c8352012-10-30 02:17:38 +010010163 attribute.function_name = "removexattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010164 path.allow_fd = 1;
10165 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr",
10166 keywords,
10167 path_converter, &path,
10168 path_converter, &attribute,
10169 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010170 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010171
10172 if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks))
10173 goto exit;
10174
Benjamin Peterson799bd802011-08-31 22:15:17 -040010175 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010176 if (path.fd > -1)
10177 result = fremovexattr(path.fd, attribute.narrow);
10178 else if (follow_symlinks)
10179 result = removexattr(path.narrow, attribute.narrow);
10180 else
10181 result = lremovexattr(path.narrow, attribute.narrow);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010182 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010183
Larry Hastings9cf065c2012-06-22 16:30:09 -070010184 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +010010185 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010186 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010187 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010188
Larry Hastings9cf065c2012-06-22 16:30:09 -070010189 return_value = Py_None;
10190 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010191
Larry Hastings9cf065c2012-06-22 16:30:09 -070010192exit:
10193 path_cleanup(&path);
10194 path_cleanup(&attribute);
10195
10196 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010197}
10198
10199PyDoc_STRVAR(posix_listxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010200"listxattr(path='.', *, follow_symlinks=True)\n\n\
10201Return a list of extended attributes on path.\n\
10202\n\
10203path may be either None, a string, or an open file descriptor.\n\
10204if path is None, listxattr will examine the current directory.\n\
10205If follow_symlinks is False, and the last element of the path is a symbolic\n\
10206 link, listxattr will examine the symbolic link itself instead of the file\n\
10207 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010208
10209static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010210posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010211{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010212 path_t path;
10213 int follow_symlinks = 1;
10214 Py_ssize_t i;
10215 PyObject *result = NULL;
10216 char *buffer = NULL;
10217 char *name;
10218 static char *keywords[] = {"path", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010219
Larry Hastings9cf065c2012-06-22 16:30:09 -070010220 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010221 path.function_name = "listxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010222 path.allow_fd = 1;
10223 path.fd = -1;
10224 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords,
10225 path_converter, &path,
10226 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010227 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010228
Larry Hastings9cf065c2012-06-22 16:30:09 -070010229 if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks))
10230 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010231
Larry Hastings9cf065c2012-06-22 16:30:09 -070010232 name = path.narrow ? path.narrow : ".";
10233 for (i = 0; ; i++) {
10234 char *start, *trace, *end;
10235 ssize_t length;
10236 static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
10237 Py_ssize_t buffer_size = buffer_sizes[i];
10238 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020010239 /* ERANGE */
Victor Stinner292c8352012-10-30 02:17:38 +010010240 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010241 break;
10242 }
10243 buffer = PyMem_MALLOC(buffer_size);
10244 if (!buffer) {
10245 PyErr_NoMemory();
10246 break;
10247 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010248
Larry Hastings9cf065c2012-06-22 16:30:09 -070010249 Py_BEGIN_ALLOW_THREADS;
10250 if (path.fd > -1)
10251 length = flistxattr(path.fd, buffer, buffer_size);
10252 else if (follow_symlinks)
10253 length = listxattr(name, buffer, buffer_size);
10254 else
10255 length = llistxattr(name, buffer, buffer_size);
10256 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010257
Larry Hastings9cf065c2012-06-22 16:30:09 -070010258 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020010259 if (errno == ERANGE) {
10260 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050010261 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010262 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020010263 }
Victor Stinner292c8352012-10-30 02:17:38 +010010264 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010265 break;
10266 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010267
Larry Hastings9cf065c2012-06-22 16:30:09 -070010268 result = PyList_New(0);
10269 if (!result) {
10270 goto exit;
10271 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010272
Larry Hastings9cf065c2012-06-22 16:30:09 -070010273 end = buffer + length;
10274 for (trace = start = buffer; trace != end; trace++) {
10275 if (!*trace) {
10276 int error;
10277 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
10278 trace - start);
10279 if (!attribute) {
10280 Py_DECREF(result);
10281 result = NULL;
10282 goto exit;
10283 }
10284 error = PyList_Append(result, attribute);
10285 Py_DECREF(attribute);
10286 if (error) {
10287 Py_DECREF(result);
10288 result = NULL;
10289 goto exit;
10290 }
10291 start = trace + 1;
10292 }
10293 }
10294 break;
10295 }
10296exit:
10297 path_cleanup(&path);
10298 if (buffer)
10299 PyMem_FREE(buffer);
10300 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010301}
10302
Benjamin Peterson9428d532011-09-14 11:45:52 -040010303#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010304
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010305
Georg Brandl2fb477c2012-02-21 00:33:36 +010010306PyDoc_STRVAR(posix_urandom__doc__,
10307"urandom(n) -> str\n\n\
10308Return n random bytes suitable for cryptographic use.");
10309
10310static PyObject *
10311posix_urandom(PyObject *self, PyObject *args)
10312{
10313 Py_ssize_t size;
10314 PyObject *result;
10315 int ret;
10316
10317 /* Read arguments */
10318 if (!PyArg_ParseTuple(args, "n:urandom", &size))
10319 return NULL;
10320 if (size < 0)
10321 return PyErr_Format(PyExc_ValueError,
10322 "negative argument not allowed");
10323 result = PyBytes_FromStringAndSize(NULL, size);
10324 if (result == NULL)
10325 return NULL;
10326
10327 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
10328 PyBytes_GET_SIZE(result));
10329 if (ret == -1) {
10330 Py_DECREF(result);
10331 return NULL;
10332 }
10333 return result;
10334}
10335
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010336/* Terminal size querying */
10337
10338static PyTypeObject TerminalSizeType;
10339
10340PyDoc_STRVAR(TerminalSize_docstring,
10341 "A tuple of (columns, lines) for holding terminal window size");
10342
10343static PyStructSequence_Field TerminalSize_fields[] = {
10344 {"columns", "width of the terminal window in characters"},
10345 {"lines", "height of the terminal window in characters"},
10346 {NULL, NULL}
10347};
10348
10349static PyStructSequence_Desc TerminalSize_desc = {
10350 "os.terminal_size",
10351 TerminalSize_docstring,
10352 TerminalSize_fields,
10353 2,
10354};
10355
10356#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10357PyDoc_STRVAR(termsize__doc__,
10358 "Return the size of the terminal window as (columns, lines).\n" \
10359 "\n" \
10360 "The optional argument fd (default standard output) specifies\n" \
10361 "which file descriptor should be queried.\n" \
10362 "\n" \
10363 "If the file descriptor is not connected to a terminal, an OSError\n" \
10364 "is thrown.\n" \
10365 "\n" \
10366 "This function will only be defined if an implementation is\n" \
10367 "available for this system.\n" \
10368 "\n" \
10369 "shutil.get_terminal_size is the high-level function which should \n" \
10370 "normally be used, os.get_terminal_size is the low-level implementation.");
10371
10372static PyObject*
10373get_terminal_size(PyObject *self, PyObject *args)
10374{
10375 int columns, lines;
10376 PyObject *termsize;
10377
10378 int fd = fileno(stdout);
10379 /* Under some conditions stdout may not be connected and
10380 * fileno(stdout) may point to an invalid file descriptor. For example
10381 * GUI apps don't have valid standard streams by default.
10382 *
10383 * If this happens, and the optional fd argument is not present,
10384 * the ioctl below will fail returning EBADF. This is what we want.
10385 */
10386
10387 if (!PyArg_ParseTuple(args, "|i", &fd))
10388 return NULL;
10389
10390#ifdef TERMSIZE_USE_IOCTL
10391 {
10392 struct winsize w;
10393 if (ioctl(fd, TIOCGWINSZ, &w))
10394 return PyErr_SetFromErrno(PyExc_OSError);
10395 columns = w.ws_col;
10396 lines = w.ws_row;
10397 }
10398#endif /* TERMSIZE_USE_IOCTL */
10399
10400#ifdef TERMSIZE_USE_CONIO
10401 {
10402 DWORD nhandle;
10403 HANDLE handle;
10404 CONSOLE_SCREEN_BUFFER_INFO csbi;
10405 switch (fd) {
10406 case 0: nhandle = STD_INPUT_HANDLE;
10407 break;
10408 case 1: nhandle = STD_OUTPUT_HANDLE;
10409 break;
10410 case 2: nhandle = STD_ERROR_HANDLE;
10411 break;
10412 default:
10413 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10414 }
10415 handle = GetStdHandle(nhandle);
10416 if (handle == NULL)
10417 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10418 if (handle == INVALID_HANDLE_VALUE)
10419 return PyErr_SetFromWindowsErr(0);
10420
10421 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10422 return PyErr_SetFromWindowsErr(0);
10423
10424 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10425 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10426 }
10427#endif /* TERMSIZE_USE_CONIO */
10428
10429 termsize = PyStructSequence_New(&TerminalSizeType);
10430 if (termsize == NULL)
10431 return NULL;
10432 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10433 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10434 if (PyErr_Occurred()) {
10435 Py_DECREF(termsize);
10436 return NULL;
10437 }
10438 return termsize;
10439}
10440#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10441
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010442PyDoc_STRVAR(posix_cpu_count__doc__,
10443"cpu_count() -> integer\n\n\
10444Return the number of CPUs in the system, or None if this value cannot be\n\
10445established.");
10446
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010447static PyObject *
10448posix_cpu_count(PyObject *self)
10449{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010450 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010451#ifdef MS_WINDOWS
10452 SYSTEM_INFO sysinfo;
10453 GetSystemInfo(&sysinfo);
10454 ncpu = sysinfo.dwNumberOfProcessors;
10455#elif defined(__hpux)
10456 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
10457#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
10458 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010459#elif defined(__DragonFly__) || \
10460 defined(__OpenBSD__) || \
10461 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010462 defined(__NetBSD__) || \
10463 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020010464 int mib[2];
10465 size_t len = sizeof(ncpu);
10466 mib[0] = CTL_HW;
10467 mib[1] = HW_NCPU;
10468 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
10469 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010470#endif
10471 if (ncpu >= 1)
10472 return PyLong_FromLong(ncpu);
10473 else
10474 Py_RETURN_NONE;
10475}
10476
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010477
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010478static PyMethodDef posix_methods[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010479 {"access", (PyCFunction)posix_access,
10480 METH_VARARGS | METH_KEYWORDS,
10481 posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010482#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010483 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010484#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010485 {"chdir", (PyCFunction)posix_chdir,
10486 METH_VARARGS | METH_KEYWORDS,
10487 posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010488#ifdef HAVE_CHFLAGS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010489 {"chflags", (PyCFunction)posix_chflags,
10490 METH_VARARGS | METH_KEYWORDS,
10491 posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010492#endif /* HAVE_CHFLAGS */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010493 {"chmod", (PyCFunction)posix_chmod,
10494 METH_VARARGS | METH_KEYWORDS,
10495 posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010496#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010497 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010498#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010499#ifdef HAVE_CHOWN
Larry Hastings9cf065c2012-06-22 16:30:09 -070010500 {"chown", (PyCFunction)posix_chown,
10501 METH_VARARGS | METH_KEYWORDS,
10502 posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010503#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010504#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010505 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010506#endif /* HAVE_LCHMOD */
10507#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010508 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010509#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010510#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010511 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010512#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010513#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010514 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010515#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010516#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010517 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010518#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010519#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010520 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010521#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010522#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010523 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10524 METH_NOARGS, posix_getcwd__doc__},
10525 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10526 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010527#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010528#if defined(HAVE_LINK) || defined(MS_WINDOWS)
10529 {"link", (PyCFunction)posix_link,
10530 METH_VARARGS | METH_KEYWORDS,
10531 posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010532#endif /* HAVE_LINK */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010533 {"listdir", (PyCFunction)posix_listdir,
10534 METH_VARARGS | METH_KEYWORDS,
10535 posix_listdir__doc__},
10536 {"lstat", (PyCFunction)posix_lstat,
10537 METH_VARARGS | METH_KEYWORDS,
10538 posix_lstat__doc__},
10539 {"mkdir", (PyCFunction)posix_mkdir,
10540 METH_VARARGS | METH_KEYWORDS,
10541 posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010542#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010543 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010544#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010545#ifdef HAVE_GETPRIORITY
10546 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10547#endif /* HAVE_GETPRIORITY */
10548#ifdef HAVE_SETPRIORITY
10549 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10550#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010551#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070010552 {"readlink", (PyCFunction)posix_readlink,
10553 METH_VARARGS | METH_KEYWORDS,
10554 readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010555#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010556#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010557 {"readlink", (PyCFunction)win_readlink,
10558 METH_VARARGS | METH_KEYWORDS,
10559 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010560#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010561 {"rename", (PyCFunction)posix_rename,
10562 METH_VARARGS | METH_KEYWORDS,
10563 posix_rename__doc__},
10564 {"replace", (PyCFunction)posix_replace,
10565 METH_VARARGS | METH_KEYWORDS,
10566 posix_replace__doc__},
Larry Hastingsb698d8e2012-06-23 16:55:07 -070010567 {"rmdir", (PyCFunction)posix_rmdir,
10568 METH_VARARGS | METH_KEYWORDS,
10569 posix_rmdir__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010570 {"stat", (PyCFunction)posix_stat,
10571 METH_VARARGS | METH_KEYWORDS,
10572 posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010573 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010574#if defined(HAVE_SYMLINK)
10575 {"symlink", (PyCFunction)posix_symlink,
10576 METH_VARARGS | METH_KEYWORDS,
10577 posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010578#endif /* HAVE_SYMLINK */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010579#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010580 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010581#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010582 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010583#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010585#endif /* HAVE_UNAME */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010586 {"unlink", (PyCFunction)posix_unlink,
10587 METH_VARARGS | METH_KEYWORDS,
10588 posix_unlink__doc__},
10589 {"remove", (PyCFunction)posix_unlink,
10590 METH_VARARGS | METH_KEYWORDS,
10591 posix_remove__doc__},
Larry Hastings76ad59b2012-05-03 00:30:07 -070010592 {"utime", (PyCFunction)posix_utime,
10593 METH_VARARGS | METH_KEYWORDS, posix_utime__doc__},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010594#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010595 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010596#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010597 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010598#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010599 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010600 {"execve", (PyCFunction)posix_execve,
10601 METH_VARARGS | METH_KEYWORDS,
10602 posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010603#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +000010604#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10606 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +000010607#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010608#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010609 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010610#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010611#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010612 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010613#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010614#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010615#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010616 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10617 {"sched_get_priority_min", posix_sched_get_priority_min, METH_VARARGS, posix_sched_get_priority_min__doc__},
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010618#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010619#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010620 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010621#endif
10622#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010623 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010624#endif
10625#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010626 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010627#endif
10628#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010629 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010630#endif
10631#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010632 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010633#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010634 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010635#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010636 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10637 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10638#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010639#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010640#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010642#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010643#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010644 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010645#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010646#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010647 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010648#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010649#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010651#endif /* HAVE_GETEUID */
10652#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010654#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010655#ifdef HAVE_GETGROUPLIST
10656 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10657#endif
Fred Drakec9680921999-12-13 16:37:25 +000010658#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010659 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010660#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010661 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010662#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010663 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010664#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010665#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010666 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010667#endif /* HAVE_GETPPID */
10668#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010669 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010670#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010671#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010672 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010673#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010674#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010675 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010676#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010677#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010678 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010679#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010680#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010682#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010683#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010684 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10685 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010686#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010687#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010688 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010689#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010690#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010691 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010692#endif /* HAVE_SETEUID */
10693#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010694 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010695#endif /* HAVE_SETEGID */
10696#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010698#endif /* HAVE_SETREUID */
10699#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b2d2000-07-13 01:26:58 +000010701#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010702#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010703 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010704#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010705#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010707#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010708#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010709 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010710#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010711#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010712 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010713#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010714#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010715 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010716#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010717#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010718 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010719#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010720#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010010721 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010722#endif /* HAVE_WAIT3 */
10723#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010010724 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010725#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010726#if defined(HAVE_WAITID) && !defined(__APPLE__)
10727 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10728#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010729#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010730 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010731#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010732#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010733 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010734#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010735#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010736 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010737#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010738#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010739 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010740#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010741#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010742 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010743#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010744#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010745 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010746#endif /* HAVE_TCSETPGRP */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010747 {"open", (PyCFunction)posix_open,\
10748 METH_VARARGS | METH_KEYWORDS,
10749 posix_open__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010750 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10751 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10752 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10753 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10754 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010755#ifdef HAVE_LOCKF
10756 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10757#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10759 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010760#ifdef HAVE_READV
10761 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10762#endif
10763#ifdef HAVE_PREAD
10764 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10765#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010766 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010767#ifdef HAVE_WRITEV
10768 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10769#endif
10770#ifdef HAVE_PWRITE
10771 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10772#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010773#ifdef HAVE_SENDFILE
10774 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10775 posix_sendfile__doc__},
10776#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010777 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010778 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010779#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010780 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010781#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010782#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010783 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010784#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010785#ifdef HAVE_MKFIFO
Larry Hastings9cf065c2012-06-22 16:30:09 -070010786 {"mkfifo", (PyCFunction)posix_mkfifo,
10787 METH_VARARGS | METH_KEYWORDS,
10788 posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010789#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010790#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010791 {"mknod", (PyCFunction)posix_mknod,
10792 METH_VARARGS | METH_KEYWORDS,
10793 posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010794#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010795#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010796 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10797 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10798 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010799#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010800#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010801 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010802#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010803#ifdef HAVE_TRUNCATE
Georg Brandl306336b2012-06-24 12:55:33 +020010804 {"truncate", (PyCFunction)posix_truncate,
10805 METH_VARARGS | METH_KEYWORDS,
10806 posix_truncate__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010807#endif
10808#ifdef HAVE_POSIX_FALLOCATE
10809 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10810#endif
10811#ifdef HAVE_POSIX_FADVISE
10812 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10813#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010814#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010815 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010816#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010817#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010818 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010819#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010820 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010821#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010822 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010823#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010824#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010825 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010826#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010827#ifdef HAVE_SYNC
10828 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10829#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010830#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010831 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010832#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010833#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010834#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010835 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010836#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010837#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010838 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010839#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010840#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010841 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010842#endif /* WIFSTOPPED */
10843#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010844 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010845#endif /* WIFSIGNALED */
10846#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010847 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010848#endif /* WIFEXITED */
10849#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010850 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010851#endif /* WEXITSTATUS */
10852#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010853 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010854#endif /* WTERMSIG */
10855#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010856 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010857#endif /* WSTOPSIG */
10858#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010859#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010861#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010862#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010863 {"statvfs", (PyCFunction)posix_statvfs,
10864 METH_VARARGS | METH_KEYWORDS,
10865 posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010866#endif
Fred Drakec9680921999-12-13 16:37:25 +000010867#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010869#endif
10870#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010871 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010872#endif
10873#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010874 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010875#endif
10876#ifdef HAVE_PATHCONF
Georg Brandl306336b2012-06-24 12:55:33 +020010877 {"pathconf", (PyCFunction)posix_pathconf,
10878 METH_VARARGS | METH_KEYWORDS,
10879 posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010880#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010882#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010884 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010885 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010886 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010887#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010888#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010889 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010890#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010010891 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010892#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010894#endif
10895#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010897#endif
10898#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010899 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010900#endif
10901#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010902 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010903#endif
10904
Benjamin Peterson9428d532011-09-14 11:45:52 -040010905#ifdef USE_XATTRS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010906 {"setxattr", (PyCFunction)posix_setxattr,
10907 METH_VARARGS | METH_KEYWORDS,
10908 posix_setxattr__doc__},
10909 {"getxattr", (PyCFunction)posix_getxattr,
10910 METH_VARARGS | METH_KEYWORDS,
10911 posix_getxattr__doc__},
10912 {"removexattr", (PyCFunction)posix_removexattr,
10913 METH_VARARGS | METH_KEYWORDS,
10914 posix_removexattr__doc__},
10915 {"listxattr", (PyCFunction)posix_listxattr,
10916 METH_VARARGS | METH_KEYWORDS,
10917 posix_listxattr__doc__},
Benjamin Peterson799bd802011-08-31 22:15:17 -040010918#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010919#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10920 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
10921#endif
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010922 {"cpu_count", (PyCFunction)posix_cpu_count,
10923 METH_NOARGS, posix_cpu_count__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010924 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010925};
10926
10927
Brian Curtin52173d42010-12-02 18:29:18 +000010928#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010929static int
Brian Curtin52173d42010-12-02 18:29:18 +000010930enable_symlink()
10931{
10932 HANDLE tok;
10933 TOKEN_PRIVILEGES tok_priv;
10934 LUID luid;
10935 int meth_idx = 0;
10936
10937 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010938 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010939
10940 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010941 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010942
10943 tok_priv.PrivilegeCount = 1;
10944 tok_priv.Privileges[0].Luid = luid;
10945 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10946
10947 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10948 sizeof(TOKEN_PRIVILEGES),
10949 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010950 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010951
Brian Curtin3b4499c2010-12-28 14:31:47 +000010952 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10953 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010954}
10955#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10956
Barry Warsaw4a342091996-12-19 23:50:02 +000010957static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010958all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000010959{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010960#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010961 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010962#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010963#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010964 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010965#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010966#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010967 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010968#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010969#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010970 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010971#endif
Fred Drakec9680921999-12-13 16:37:25 +000010972#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010973 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010974#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010975#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010976 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010977#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010978#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010979 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010980#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010981#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010982 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010983#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010984#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010985 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010986#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010987#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010988 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010989#endif
10990#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010991 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010992#endif
10993#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010994 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010995#endif
10996#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010997 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010998#endif
10999#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011000 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011001#endif
11002#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011003 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011004#endif
11005#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011006 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011007#endif
11008#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011009 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011010#endif
11011#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011012 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011013#endif
11014#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011015 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011016#endif
11017#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011018 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011019#endif
11020#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011021 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011022#endif
11023#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011024 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000011025#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000011026#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011027 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011028#endif
11029#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011030 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000011031#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011032#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011033 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020011034#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011035#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011036 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011037#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000011038#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011039 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011040#endif
11041#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011042 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000011043#endif
Jesus Ceacf381202012-04-24 20:44:40 +020011044#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011045 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020011046#endif
11047#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011048 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020011049#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050011050#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011051 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050011052#endif
Jesus Ceacf381202012-04-24 20:44:40 +020011053#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011054 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020011055#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011056#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011057 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011058#endif
11059#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011060 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011061#endif
11062#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011063 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011064#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020011065#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011066 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020011067#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011068#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011069 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020011070#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000011071
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011072
Jesus Cea94363612012-06-22 18:32:07 +020011073#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011074 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020011075#endif
11076#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011077 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020011078#endif
11079
Tim Peters5aa91602002-01-30 05:46:57 +000011080/* MS Windows */
11081#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011083 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011084#endif
11085#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000011086 /* Optimize for short life (keep in memory). */
11087 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011088 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011089#endif
11090#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011092 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011093#endif
11094#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000011095 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011096 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011097#endif
11098#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011100 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000011101#endif
11102
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011103/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011104#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011105 /* Send a SIGIO signal whenever input or output
11106 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011107 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000011108#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011109#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011110 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011111 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011112#endif
11113#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011115 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011116#endif
11117#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011119 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000011120#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020011121#ifdef O_NOLINKS
11122 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011123 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020011124#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011125#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011126 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011127 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011128#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011129
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011131#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011132 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011133#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011134#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011135 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011136#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011137#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011138 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011139#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011140#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011141 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011142#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011143#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011144 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011145#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011146#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011147 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011148#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011149#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011150 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011151#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011152#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011153 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011154#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011155#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011156 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011157#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011158#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011159 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011160#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011161#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011162 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011163#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011164#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011165 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011166#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011167#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011168 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011169#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011170#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011171 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011172#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011173#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011174 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011175#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011176#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011177 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011178#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011179#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011180 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011181#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011182
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011183 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011184#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011185 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011186#endif /* ST_RDONLY */
11187#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011188 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011189#endif /* ST_NOSUID */
11190
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011191 /* FreeBSD sendfile() constants */
11192#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011193 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011194#endif
11195#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011196 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011197#endif
11198#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011199 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011200#endif
11201
Ross Lagerwall7807c352011-03-17 20:20:30 +020011202 /* constants for posix_fadvise */
11203#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011204 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011205#endif
11206#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011207 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011208#endif
11209#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011210 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011211#endif
11212#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011213 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011214#endif
11215#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011216 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011217#endif
11218#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011219 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011220#endif
11221
11222 /* constants for waitid */
11223#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011224 if (PyModule_AddIntMacro(m, P_PID)) return -1;
11225 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
11226 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011227#endif
11228#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011229 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011230#endif
11231#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011232 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011233#endif
11234#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011235 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011236#endif
11237#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011238 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011239#endif
11240#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011241 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011242#endif
11243#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011244 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011245#endif
11246#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011247 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011248#endif
11249
11250 /* constants for lockf */
11251#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011252 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011253#endif
11254#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011255 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011256#endif
11257#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011258 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011259#endif
11260#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011261 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011262#endif
11263
Guido van Rossum246bc171999-02-01 23:54:31 +000011264#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011265 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
11266 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
11267 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
11268 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
11269 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011270#endif
11271
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011272#ifdef HAVE_SCHED_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011273 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
11274 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
11275 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011276#ifdef SCHED_SPORADIC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011277 if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011278#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011279#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011280 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011281#endif
11282#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011283 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011284#endif
11285#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011286 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011287#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011288#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011289 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011290#endif
11291#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011292 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011293#endif
11294#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011295 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011296#endif
11297#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011298 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011299#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011300#endif
11301
Benjamin Peterson9428d532011-09-14 11:45:52 -040011302#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011303 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
11304 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
11305 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011306#endif
11307
Victor Stinner8b905bd2011-10-25 13:34:04 +020011308#ifdef RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011309 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011310#endif
11311#ifdef RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011312 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011313#endif
11314#ifdef RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011315 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011316#endif
11317#ifdef RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011318 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011319#endif
11320#ifdef RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011321 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011322#endif
11323#ifdef RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011324 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011325#endif
11326#ifdef RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011327 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011328#endif
11329
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011331}
11332
11333
Tim Peters5aa91602002-01-30 05:46:57 +000011334#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011335#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011336#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011337
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011338#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011339#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011340#define MODNAME "posix"
11341#endif
11342
Martin v. Löwis1a214512008-06-11 05:26:20 +000011343static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011344 PyModuleDef_HEAD_INIT,
11345 MODNAME,
11346 posix__doc__,
11347 -1,
11348 posix_methods,
11349 NULL,
11350 NULL,
11351 NULL,
11352 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011353};
11354
11355
Larry Hastings9cf065c2012-06-22 16:30:09 -070011356static char *have_functions[] = {
11357
11358#ifdef HAVE_FACCESSAT
11359 "HAVE_FACCESSAT",
11360#endif
11361
11362#ifdef HAVE_FCHDIR
11363 "HAVE_FCHDIR",
11364#endif
11365
11366#ifdef HAVE_FCHMOD
11367 "HAVE_FCHMOD",
11368#endif
11369
11370#ifdef HAVE_FCHMODAT
11371 "HAVE_FCHMODAT",
11372#endif
11373
11374#ifdef HAVE_FCHOWN
11375 "HAVE_FCHOWN",
11376#endif
11377
11378#ifdef HAVE_FEXECVE
11379 "HAVE_FEXECVE",
11380#endif
11381
11382#ifdef HAVE_FDOPENDIR
11383 "HAVE_FDOPENDIR",
11384#endif
11385
Georg Brandl306336b2012-06-24 12:55:33 +020011386#ifdef HAVE_FPATHCONF
11387 "HAVE_FPATHCONF",
11388#endif
11389
Larry Hastings9cf065c2012-06-22 16:30:09 -070011390#ifdef HAVE_FSTATAT
11391 "HAVE_FSTATAT",
11392#endif
11393
11394#ifdef HAVE_FSTATVFS
11395 "HAVE_FSTATVFS",
11396#endif
11397
Georg Brandl306336b2012-06-24 12:55:33 +020011398#ifdef HAVE_FTRUNCATE
11399 "HAVE_FTRUNCATE",
11400#endif
11401
Larry Hastings9cf065c2012-06-22 16:30:09 -070011402#ifdef HAVE_FUTIMENS
11403 "HAVE_FUTIMENS",
11404#endif
11405
11406#ifdef HAVE_FUTIMES
11407 "HAVE_FUTIMES",
11408#endif
11409
11410#ifdef HAVE_FUTIMESAT
11411 "HAVE_FUTIMESAT",
11412#endif
11413
11414#ifdef HAVE_LINKAT
11415 "HAVE_LINKAT",
11416#endif
11417
11418#ifdef HAVE_LCHFLAGS
11419 "HAVE_LCHFLAGS",
11420#endif
11421
11422#ifdef HAVE_LCHMOD
11423 "HAVE_LCHMOD",
11424#endif
11425
11426#ifdef HAVE_LCHOWN
11427 "HAVE_LCHOWN",
11428#endif
11429
11430#ifdef HAVE_LSTAT
11431 "HAVE_LSTAT",
11432#endif
11433
11434#ifdef HAVE_LUTIMES
11435 "HAVE_LUTIMES",
11436#endif
11437
11438#ifdef HAVE_MKDIRAT
11439 "HAVE_MKDIRAT",
11440#endif
11441
11442#ifdef HAVE_MKFIFOAT
11443 "HAVE_MKFIFOAT",
11444#endif
11445
11446#ifdef HAVE_MKNODAT
11447 "HAVE_MKNODAT",
11448#endif
11449
11450#ifdef HAVE_OPENAT
11451 "HAVE_OPENAT",
11452#endif
11453
11454#ifdef HAVE_READLINKAT
11455 "HAVE_READLINKAT",
11456#endif
11457
11458#ifdef HAVE_RENAMEAT
11459 "HAVE_RENAMEAT",
11460#endif
11461
11462#ifdef HAVE_SYMLINKAT
11463 "HAVE_SYMLINKAT",
11464#endif
11465
11466#ifdef HAVE_UNLINKAT
11467 "HAVE_UNLINKAT",
11468#endif
11469
11470#ifdef HAVE_UTIMENSAT
11471 "HAVE_UTIMENSAT",
11472#endif
11473
11474#ifdef MS_WINDOWS
11475 "MS_WINDOWS",
11476#endif
11477
11478 NULL
11479};
11480
11481
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011482PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011483INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011484{
Victor Stinner8c62be82010-05-06 00:08:46 +000011485 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011486 PyObject *list;
11487 char **trace;
Tim Peters5aa91602002-01-30 05:46:57 +000011488
Brian Curtin52173d42010-12-02 18:29:18 +000011489#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011490 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011491#endif
11492
Victor Stinner8c62be82010-05-06 00:08:46 +000011493 m = PyModule_Create(&posixmodule);
11494 if (m == NULL)
11495 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011496
Victor Stinner8c62be82010-05-06 00:08:46 +000011497 /* Initialize environ dictionary */
11498 v = convertenviron();
11499 Py_XINCREF(v);
11500 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11501 return NULL;
11502 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011503
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 if (all_ins(m))
11505 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011506
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 if (setup_confname_tables(m))
11508 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011509
Victor Stinner8c62be82010-05-06 00:08:46 +000011510 Py_INCREF(PyExc_OSError);
11511 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011512
Guido van Rossumb3d39562000-01-31 18:41:26 +000011513#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011514 if (posix_putenv_garbage == NULL)
11515 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011516#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011517
Victor Stinner8c62be82010-05-06 00:08:46 +000011518 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011519#if defined(HAVE_WAITID) && !defined(__APPLE__)
11520 waitid_result_desc.name = MODNAME ".waitid_result";
11521 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11522#endif
11523
Victor Stinner8c62be82010-05-06 00:08:46 +000011524 stat_result_desc.name = MODNAME ".stat_result";
11525 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11526 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11527 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11528 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11529 structseq_new = StatResultType.tp_new;
11530 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011531
Victor Stinner8c62be82010-05-06 00:08:46 +000011532 statvfs_result_desc.name = MODNAME ".statvfs_result";
11533 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011534#ifdef NEED_TICKS_PER_SECOND
11535# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011536 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011537# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011538 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011539# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011540 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011541# endif
11542#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011543
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011544#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011545 sched_param_desc.name = MODNAME ".sched_param";
11546 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11547 SchedParamType.tp_new = sched_param_new;
11548#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011549
11550 /* initialize TerminalSize_info */
11551 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011553#if defined(HAVE_WAITID) && !defined(__APPLE__)
11554 Py_INCREF((PyObject*) &WaitidResultType);
11555 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11556#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011557 Py_INCREF((PyObject*) &StatResultType);
11558 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11559 Py_INCREF((PyObject*) &StatVFSResultType);
11560 PyModule_AddObject(m, "statvfs_result",
11561 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011562
11563#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011564 Py_INCREF(&SchedParamType);
11565 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011566#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000011567
Larry Hastings605a62d2012-06-24 04:33:36 -070011568 times_result_desc.name = MODNAME ".times_result";
11569 PyStructSequence_InitType(&TimesResultType, &times_result_desc);
11570 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
11571
11572 uname_result_desc.name = MODNAME ".uname_result";
11573 PyStructSequence_InitType(&UnameResultType, &uname_result_desc);
11574 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
11575
Thomas Wouters477c8d52006-05-27 19:21:47 +000011576#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011577 /*
11578 * Step 2 of weak-linking support on Mac OS X.
11579 *
11580 * The code below removes functions that are not available on the
11581 * currently active platform.
11582 *
11583 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070011584 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000011585 * OSX 10.4.
11586 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011587#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011588 if (fstatvfs == NULL) {
11589 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11590 return NULL;
11591 }
11592 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011593#endif /* HAVE_FSTATVFS */
11594
11595#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011596 if (statvfs == NULL) {
11597 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11598 return NULL;
11599 }
11600 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011601#endif /* HAVE_STATVFS */
11602
11603# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011604 if (lchown == NULL) {
11605 if (PyObject_DelAttrString(m, "lchown") == -1) {
11606 return NULL;
11607 }
11608 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011609#endif /* HAVE_LCHOWN */
11610
11611
11612#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011613
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020011614 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011615 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
11616
Larry Hastings6fe20b32012-04-19 15:07:49 -070011617 billion = PyLong_FromLong(1000000000);
11618 if (!billion)
11619 return NULL;
11620
Larry Hastings9cf065c2012-06-22 16:30:09 -070011621 /* suppress "function not used" warnings */
11622 {
11623 int ignored;
11624 fd_specified("", -1);
11625 follow_symlinks_specified("", 1);
11626 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
11627 dir_fd_converter(Py_None, &ignored);
11628 dir_fd_unavailable(Py_None, &ignored);
11629 }
11630
11631 /*
11632 * provide list of locally available functions
11633 * so os.py can populate support_* lists
11634 */
11635 list = PyList_New(0);
11636 if (!list)
11637 return NULL;
11638 for (trace = have_functions; *trace; trace++) {
11639 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
11640 if (!unicode)
11641 return NULL;
11642 if (PyList_Append(list, unicode))
11643 return NULL;
11644 Py_DECREF(unicode);
11645 }
11646 PyModule_AddObject(m, "_have_functions", list);
11647
11648 initialized = 1;
11649
Victor Stinner8c62be82010-05-06 00:08:46 +000011650 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011651
Guido van Rossumb6775db1994-08-01 11:34:53 +000011652}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011653
11654#ifdef __cplusplus
11655}
11656#endif