blob: 615ff821cb5d28ddac0cb18e0b26edcf2a350512 [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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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. Kuchling8d2f2b22000-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
6738#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006739
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006740static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006741posix_symlink(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006742{
Larry Hastings9cf065c2012-06-22 16:30:09 -07006743 path_t src;
6744 path_t dst;
6745 int dir_fd = DEFAULT_DIR_FD;
6746 int target_is_directory = 0;
6747 static char *keywords[] = {"src", "dst", "target_is_directory",
6748 "dir_fd", NULL};
6749 PyObject *return_value;
6750#ifdef MS_WINDOWS
6751 DWORD result;
6752#else
6753 int result;
6754#endif
6755
6756 memset(&src, 0, sizeof(src));
Victor Stinner292c8352012-10-30 02:17:38 +01006757 src.function_name = "symlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006758 src.argument_name = "src";
6759 memset(&dst, 0, sizeof(dst));
Victor Stinner292c8352012-10-30 02:17:38 +01006760 dst.function_name = "symlink";
Larry Hastings9cf065c2012-06-22 16:30:09 -07006761 dst.argument_name = "dst";
6762
6763#ifdef MS_WINDOWS
6764 if (!check_CreateSymbolicLink()) {
6765 PyErr_SetString(PyExc_NotImplementedError,
6766 "CreateSymbolicLink functions not found");
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006767 return NULL;
6768 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006769 if (!win32_can_symlink) {
6770 PyErr_SetString(PyExc_OSError, "symbolic link privilege not held");
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006771 return NULL;
6772 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07006773#endif
6774
6775 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|i$O&:symlink",
6776 keywords,
6777 path_converter, &src,
6778 path_converter, &dst,
6779 &target_is_directory,
6780#ifdef HAVE_SYMLINKAT
6781 dir_fd_converter, &dir_fd
6782#else
6783 dir_fd_unavailable, &dir_fd
6784#endif
6785 ))
6786 return NULL;
6787
6788 if ((src.narrow && dst.wide) || (src.wide && dst.narrow)) {
6789 PyErr_SetString(PyExc_ValueError,
6790 "symlink: src and dst must be the same type");
6791 return_value = NULL;
6792 goto exit;
6793 }
6794
6795#ifdef MS_WINDOWS
6796 Py_BEGIN_ALLOW_THREADS
6797 if (dst.wide)
6798 result = Py_CreateSymbolicLinkW(dst.wide, src.wide,
6799 target_is_directory);
6800 else
6801 result = Py_CreateSymbolicLinkA(dst.narrow, src.narrow,
6802 target_is_directory);
6803 Py_END_ALLOW_THREADS
6804
6805 if (!result) {
Victor Stinnerb024e842012-10-31 22:24:06 +01006806 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006807 goto exit;
6808 }
6809
6810#else
6811
6812 Py_BEGIN_ALLOW_THREADS
6813#if HAVE_SYMLINKAT
6814 if (dir_fd != DEFAULT_DIR_FD)
6815 result = symlinkat(src.narrow, dir_fd, dst.narrow);
6816 else
6817#endif
6818 result = symlink(src.narrow, dst.narrow);
6819 Py_END_ALLOW_THREADS
6820
6821 if (result) {
Victor Stinnerafe17062012-10-31 22:47:43 +01006822 return_value = path_error(&src);
Larry Hastings9cf065c2012-06-22 16:30:09 -07006823 goto exit;
6824 }
6825#endif
6826
6827 return_value = Py_None;
6828 Py_INCREF(Py_None);
6829 goto exit; /* silence "unused label" warning */
6830exit:
6831 path_cleanup(&src);
6832 path_cleanup(&dst);
6833 return return_value;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006834}
Larry Hastings9cf065c2012-06-22 16:30:09 -07006835
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006836#endif /* HAVE_SYMLINK */
6837
Larry Hastings9cf065c2012-06-22 16:30:09 -07006838
Brian Curtind40e6f72010-07-08 21:39:08 +00006839#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
6840
Brian Curtind40e6f72010-07-08 21:39:08 +00006841static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07006842win_readlink(PyObject *self, PyObject *args, PyObject *kwargs)
Brian Curtind40e6f72010-07-08 21:39:08 +00006843{
6844 wchar_t *path;
6845 DWORD n_bytes_returned;
6846 DWORD io_result;
Victor Stinnereb5657a2011-09-30 01:44:27 +02006847 PyObject *po, *result;
Petri Lehtinen5445a8c2012-10-23 16:12:14 +03006848 int dir_fd;
Brian Curtind40e6f72010-07-08 21:39:08 +00006849 HANDLE reparse_point_handle;
6850
6851 char target_buffer[MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
6852 REPARSE_DATA_BUFFER *rdb = (REPARSE_DATA_BUFFER *)target_buffer;
6853 wchar_t *print_name;
6854
Larry Hastings9cf065c2012-06-22 16:30:09 -07006855 static char *keywords[] = {"path", "dir_fd", NULL};
6856
6857 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "U|$O&:readlink", keywords,
6858 &po,
6859 dir_fd_unavailable, &dir_fd
6860 ))
Victor Stinnereb5657a2011-09-30 01:44:27 +02006861 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07006862
Victor Stinnereb5657a2011-09-30 01:44:27 +02006863 path = PyUnicode_AsUnicode(po);
6864 if (path == NULL)
Brian Curtind40e6f72010-07-08 21:39:08 +00006865 return NULL;
6866
6867 /* First get a handle to the reparse point */
6868 Py_BEGIN_ALLOW_THREADS
6869 reparse_point_handle = CreateFileW(
6870 path,
6871 0,
6872 0,
6873 0,
6874 OPEN_EXISTING,
6875 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
6876 0);
6877 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006878
Brian Curtind40e6f72010-07-08 21:39:08 +00006879 if (reparse_point_handle==INVALID_HANDLE_VALUE)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006880 return win32_error_object("readlink", po);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00006881
Brian Curtind40e6f72010-07-08 21:39:08 +00006882 Py_BEGIN_ALLOW_THREADS
6883 /* New call DeviceIoControl to read the reparse point */
6884 io_result = DeviceIoControl(
6885 reparse_point_handle,
6886 FSCTL_GET_REPARSE_POINT,
6887 0, 0, /* in buffer */
6888 target_buffer, sizeof(target_buffer),
6889 &n_bytes_returned,
6890 0 /* we're not using OVERLAPPED_IO */
6891 );
6892 CloseHandle(reparse_point_handle);
6893 Py_END_ALLOW_THREADS
6894
6895 if (io_result==0)
Victor Stinnereb5657a2011-09-30 01:44:27 +02006896 return win32_error_object("readlink", po);
Brian Curtind40e6f72010-07-08 21:39:08 +00006897
6898 if (rdb->ReparseTag != IO_REPARSE_TAG_SYMLINK)
6899 {
6900 PyErr_SetString(PyExc_ValueError,
6901 "not a symbolic link");
6902 return NULL;
6903 }
Brian Curtin74e45612010-07-09 15:58:59 +00006904 print_name = rdb->SymbolicLinkReparseBuffer.PathBuffer +
6905 rdb->SymbolicLinkReparseBuffer.PrintNameOffset;
6906
6907 result = PyUnicode_FromWideChar(print_name,
6908 rdb->SymbolicLinkReparseBuffer.PrintNameLength/2);
Brian Curtind40e6f72010-07-08 21:39:08 +00006909 return result;
6910}
6911
6912#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
6913
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00006914
Larry Hastings605a62d2012-06-24 04:33:36 -07006915static PyStructSequence_Field times_result_fields[] = {
6916 {"user", "user time"},
6917 {"system", "system time"},
6918 {"children_user", "user time of children"},
6919 {"children_system", "system time of children"},
6920 {"elapsed", "elapsed time since an arbitrary point in the past"},
6921 {NULL}
6922};
6923
6924PyDoc_STRVAR(times_result__doc__,
6925"times_result: Result from os.times().\n\n\
6926This object may be accessed either as a tuple of\n\
6927 (user, system, children_user, children_system, elapsed),\n\
6928or via the attributes user, system, children_user, children_system,\n\
6929and elapsed.\n\
6930\n\
6931See os.times for more information.");
6932
6933static PyStructSequence_Desc times_result_desc = {
6934 "times_result", /* name */
6935 times_result__doc__, /* doc */
6936 times_result_fields,
6937 5
6938};
6939
6940static PyTypeObject TimesResultType;
6941
Antoine Pitrouf3923e92012-07-24 21:23:53 +02006942#ifdef MS_WINDOWS
6943#define HAVE_TIMES /* mandatory, for the method table */
6944#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07006945
Antoine Pitrouf3923e92012-07-24 21:23:53 +02006946#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07006947
6948static PyObject *
6949build_times_result(double user, double system,
6950 double children_user, double children_system,
6951 double elapsed)
6952{
6953 PyObject *value = PyStructSequence_New(&TimesResultType);
6954 if (value == NULL)
6955 return NULL;
6956
6957#define SET(i, field) \
6958 { \
6959 PyObject *o = PyFloat_FromDouble(field); \
6960 if (!o) { \
6961 Py_DECREF(value); \
6962 return NULL; \
6963 } \
6964 PyStructSequence_SET_ITEM(value, i, o); \
6965 } \
6966
6967 SET(0, user);
6968 SET(1, system);
6969 SET(2, children_user);
6970 SET(3, children_system);
6971 SET(4, elapsed);
6972
6973#undef SET
6974
6975 return value;
6976}
6977
6978PyDoc_STRVAR(posix_times__doc__,
6979"times() -> times_result\n\n\
6980Return an object containing floating point numbers indicating process\n\
6981times. The object behaves like a named tuple with these fields:\n\
6982 (utime, stime, cutime, cstime, elapsed_time)");
6983
Jesus Ceaab70e2a2012-10-05 01:48:08 +02006984#if defined(MS_WINDOWS)
Barry Warsaw53699e91996-12-10 23:23:01 +00006985static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00006986posix_times(PyObject *self, PyObject *noargs)
Guido van Rossum14ed0b21994-09-29 09:50:09 +00006987{
Victor Stinner8c62be82010-05-06 00:08:46 +00006988 FILETIME create, exit, kernel, user;
6989 HANDLE hProc;
6990 hProc = GetCurrentProcess();
6991 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
6992 /* The fields of a FILETIME structure are the hi and lo part
6993 of a 64-bit value expressed in 100 nanosecond units.
6994 1e7 is one second in such units; 1e-7 the inverse.
6995 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
6996 */
Larry Hastings605a62d2012-06-24 04:33:36 -07006997 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00006998 (double)(user.dwHighDateTime*429.4967296 +
6999 user.dwLowDateTime*1e-7),
7000 (double)(kernel.dwHighDateTime*429.4967296 +
7001 kernel.dwLowDateTime*1e-7),
7002 (double)0,
7003 (double)0,
7004 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00007005}
Jesus Ceaab70e2a2012-10-05 01:48:08 +02007006#else /* Not Windows */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007007#define NEED_TICKS_PER_SECOND
7008static long ticks_per_second = -1;
7009static PyObject *
7010posix_times(PyObject *self, PyObject *noargs)
7011{
7012 struct tms t;
7013 clock_t c;
7014 errno = 0;
7015 c = times(&t);
7016 if (c == (clock_t) -1)
7017 return posix_error();
7018 return build_times_result(
7019 (double)t.tms_utime / ticks_per_second,
7020 (double)t.tms_stime / ticks_per_second,
7021 (double)t.tms_cutime / ticks_per_second,
7022 (double)t.tms_cstime / ticks_per_second,
7023 (double)c / ticks_per_second);
7024}
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00007025#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00007026
Antoine Pitrouf3923e92012-07-24 21:23:53 +02007027#endif /* HAVE_TIMES */
7028
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007029
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007030#ifdef HAVE_GETSID
7031PyDoc_STRVAR(posix_getsid__doc__,
7032"getsid(pid) -> sid\n\n\
7033Call the system call getsid().");
7034
7035static PyObject *
7036posix_getsid(PyObject *self, PyObject *args)
7037{
Victor Stinner8c62be82010-05-06 00:08:46 +00007038 pid_t pid;
7039 int sid;
7040 if (!PyArg_ParseTuple(args, _Py_PARSE_PID ":getsid", &pid))
7041 return NULL;
7042 sid = getsid(pid);
7043 if (sid < 0)
7044 return posix_error();
7045 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00007046}
7047#endif /* HAVE_GETSID */
7048
7049
Guido van Rossumb6775db1994-08-01 11:34:53 +00007050#ifdef HAVE_SETSID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007051PyDoc_STRVAR(posix_setsid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007052"setsid()\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007053Call the system call setsid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007054
Barry Warsaw53699e91996-12-10 23:23:01 +00007055static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007056posix_setsid(PyObject *self, PyObject *noargs)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007057{
Victor Stinner8c62be82010-05-06 00:08:46 +00007058 if (setsid() < 0)
7059 return posix_error();
7060 Py_INCREF(Py_None);
7061 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007062}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007063#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007064
Guido van Rossumb6775db1994-08-01 11:34:53 +00007065#ifdef HAVE_SETPGID
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007066PyDoc_STRVAR(posix_setpgid__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007067"setpgid(pid, pgrp)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007068Call the system call setpgid().");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007069
Barry Warsaw53699e91996-12-10 23:23:01 +00007070static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007071posix_setpgid(PyObject *self, PyObject *args)
Guido van Rossumc2670a01992-09-13 20:07:29 +00007072{
Victor Stinner8c62be82010-05-06 00:08:46 +00007073 pid_t pid;
7074 int pgrp;
7075 if (!PyArg_ParseTuple(args, _Py_PARSE_PID "i:setpgid", &pid, &pgrp))
7076 return NULL;
7077 if (setpgid(pid, pgrp) < 0)
7078 return posix_error();
7079 Py_INCREF(Py_None);
7080 return Py_None;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007081}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007082#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00007083
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007084
Guido van Rossumb6775db1994-08-01 11:34:53 +00007085#ifdef HAVE_TCGETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007086PyDoc_STRVAR(posix_tcgetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007087"tcgetpgrp(fd) -> pgid\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007088Return the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007089
Barry Warsaw53699e91996-12-10 23:23:01 +00007090static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007091posix_tcgetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007092{
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 int fd;
7094 pid_t pgid;
7095 if (!PyArg_ParseTuple(args, "i:tcgetpgrp", &fd))
7096 return NULL;
7097 pgid = tcgetpgrp(fd);
7098 if (pgid < 0)
7099 return posix_error();
7100 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00007101}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007102#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00007103
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007104
Guido van Rossumb6775db1994-08-01 11:34:53 +00007105#ifdef HAVE_TCSETPGRP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007106PyDoc_STRVAR(posix_tcsetpgrp__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007107"tcsetpgrp(fd, pgid)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007108Set the process group associated with the terminal given by a fd.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007109
Barry Warsaw53699e91996-12-10 23:23:01 +00007110static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007111posix_tcsetpgrp(PyObject *self, PyObject *args)
Guido van Rossum7066dd71992-09-17 17:54:56 +00007112{
Victor Stinner8c62be82010-05-06 00:08:46 +00007113 int fd;
7114 pid_t pgid;
7115 if (!PyArg_ParseTuple(args, "i" _Py_PARSE_PID ":tcsetpgrp", &fd, &pgid))
7116 return NULL;
7117 if (tcsetpgrp(fd, pgid) < 0)
7118 return posix_error();
7119 Py_INCREF(Py_None);
7120 return Py_None;
Guido van Rossum7066dd71992-09-17 17:54:56 +00007121}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007122#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00007123
Guido van Rossum687dd131993-05-17 08:34:16 +00007124/* Functions acting on file descriptors */
7125
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007126PyDoc_STRVAR(posix_open__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007127"open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
7128Open a file for low level IO. Returns a file handle (integer).\n\
7129\n\
7130If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7131 and path should be relative; path will then be relative to that directory.\n\
7132dir_fd may not be implemented on your platform.\n\
7133 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007134
Barry Warsaw53699e91996-12-10 23:23:01 +00007135static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007136posix_open(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007137{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007138 path_t path;
7139 int flags;
Victor Stinner8c62be82010-05-06 00:08:46 +00007140 int mode = 0777;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007141 int dir_fd = DEFAULT_DIR_FD;
Victor Stinner8c62be82010-05-06 00:08:46 +00007142 int fd;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007143 PyObject *return_value = NULL;
7144 static char *keywords[] = {"path", "flags", "mode", "dir_fd", NULL};
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007145
Larry Hastings9cf065c2012-06-22 16:30:09 -07007146 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01007147 path.function_name = "open";
Larry Hastings9cf065c2012-06-22 16:30:09 -07007148 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&i|i$O&:open", keywords,
7149 path_converter, &path,
7150 &flags, &mode,
7151#ifdef HAVE_OPENAT
7152 dir_fd_converter, &dir_fd
7153#else
7154 dir_fd_unavailable, &dir_fd
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007155#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07007156 ))
7157 return NULL;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00007158
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007160#ifdef MS_WINDOWS
7161 if (path.wide)
7162 fd = _wopen(path.wide, flags, mode);
7163 else
7164#endif
7165#ifdef HAVE_OPENAT
7166 if (dir_fd != DEFAULT_DIR_FD)
7167 fd = openat(dir_fd, path.narrow, flags, mode);
7168 else
7169#endif
7170 fd = open(path.narrow, flags, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00007171 Py_END_ALLOW_THREADS
Guido van Rossum687dd131993-05-17 08:34:16 +00007172
Larry Hastings9cf065c2012-06-22 16:30:09 -07007173 if (fd == -1) {
Victor Stinner4e7d2d42012-11-05 01:20:58 +01007174 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path.object);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007175 goto exit;
7176 }
7177
7178 return_value = PyLong_FromLong((long)fd);
7179
7180exit:
7181 path_cleanup(&path);
7182 return return_value;
7183}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007184
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007185PyDoc_STRVAR(posix_close__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007186"close(fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007187Close a file descriptor (for low level IO).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007188
Barry Warsaw53699e91996-12-10 23:23:01 +00007189static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007190posix_close(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007191{
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 int fd, res;
7193 if (!PyArg_ParseTuple(args, "i:close", &fd))
7194 return NULL;
7195 if (!_PyVerify_fd(fd))
7196 return posix_error();
7197 Py_BEGIN_ALLOW_THREADS
7198 res = close(fd);
7199 Py_END_ALLOW_THREADS
7200 if (res < 0)
7201 return posix_error();
7202 Py_INCREF(Py_None);
7203 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007204}
7205
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007206
Victor Stinner8c62be82010-05-06 00:08:46 +00007207PyDoc_STRVAR(posix_closerange__doc__,
Christian Heimesfdab48e2008-01-20 09:06:41 +00007208"closerange(fd_low, fd_high)\n\n\
7209Closes all file descriptors in [fd_low, fd_high), ignoring errors.");
7210
7211static PyObject *
7212posix_closerange(PyObject *self, PyObject *args)
7213{
Victor Stinner8c62be82010-05-06 00:08:46 +00007214 int fd_from, fd_to, i;
7215 if (!PyArg_ParseTuple(args, "ii:closerange", &fd_from, &fd_to))
7216 return NULL;
7217 Py_BEGIN_ALLOW_THREADS
7218 for (i = fd_from; i < fd_to; i++)
7219 if (_PyVerify_fd(i))
7220 close(i);
7221 Py_END_ALLOW_THREADS
7222 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00007223}
7224
7225
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007226PyDoc_STRVAR(posix_dup__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007227"dup(fd) -> fd2\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007228Return a duplicate of a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007229
Barry Warsaw53699e91996-12-10 23:23:01 +00007230static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007231posix_dup(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007232{
Victor Stinner8c62be82010-05-06 00:08:46 +00007233 int fd;
7234 if (!PyArg_ParseTuple(args, "i:dup", &fd))
7235 return NULL;
7236 if (!_PyVerify_fd(fd))
7237 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007238 fd = dup(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00007239 if (fd < 0)
7240 return posix_error();
7241 return PyLong_FromLong((long)fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00007242}
7243
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007244
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007245PyDoc_STRVAR(posix_dup2__doc__,
Andrew M. Kuchling8135fd52004-01-16 13:18:42 +00007246"dup2(old_fd, new_fd)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007247Duplicate file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007248
Barry Warsaw53699e91996-12-10 23:23:01 +00007249static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007250posix_dup2(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007251{
Victor Stinner8c62be82010-05-06 00:08:46 +00007252 int fd, fd2, res;
7253 if (!PyArg_ParseTuple(args, "ii:dup2", &fd, &fd2))
7254 return NULL;
7255 if (!_PyVerify_fd_dup2(fd, fd2))
7256 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007257 res = dup2(fd, fd2);
Victor Stinner8c62be82010-05-06 00:08:46 +00007258 if (res < 0)
7259 return posix_error();
7260 Py_INCREF(Py_None);
7261 return Py_None;
Guido van Rossum687dd131993-05-17 08:34:16 +00007262}
7263
Ross Lagerwall7807c352011-03-17 20:20:30 +02007264#ifdef HAVE_LOCKF
7265PyDoc_STRVAR(posix_lockf__doc__,
7266"lockf(fd, cmd, len)\n\n\
7267Apply, test or remove a POSIX lock on an open file descriptor.\n\n\
7268fd is an open file descriptor.\n\
7269cmd specifies the command to use - one of F_LOCK, F_TLOCK, F_ULOCK or\n\
7270F_TEST.\n\
7271len specifies the section of the file to lock.");
7272
7273static PyObject *
7274posix_lockf(PyObject *self, PyObject *args)
7275{
7276 int fd, cmd, res;
7277 off_t len;
7278 if (!PyArg_ParseTuple(args, "iiO&:lockf",
7279 &fd, &cmd, _parse_off_t, &len))
7280 return NULL;
7281
7282 Py_BEGIN_ALLOW_THREADS
7283 res = lockf(fd, cmd, len);
7284 Py_END_ALLOW_THREADS
7285
7286 if (res < 0)
7287 return posix_error();
7288
7289 Py_RETURN_NONE;
7290}
7291#endif
7292
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007293
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007294PyDoc_STRVAR(posix_lseek__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007295"lseek(fd, pos, how) -> newpos\n\n\
Victor Stinnere83f8992011-12-17 23:15:09 +01007296Set the current position of a file descriptor.\n\
7297Return the new cursor position in bytes, starting from the beginning.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007298
Barry Warsaw53699e91996-12-10 23:23:01 +00007299static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007300posix_lseek(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007301{
Victor Stinner8c62be82010-05-06 00:08:46 +00007302 int fd, how;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007303#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007304 PY_LONG_LONG pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007305#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007306 off_t pos, res;
Fred Drake699f3522000-06-29 21:12:41 +00007307#endif
Ross Lagerwall8e749672011-03-17 21:54:07 +02007308 PyObject *posobj;
7309 if (!PyArg_ParseTuple(args, "iOi:lseek", &fd, &posobj, &how))
Victor Stinner8c62be82010-05-06 00:08:46 +00007310 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +00007311#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00007312 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
7313 switch (how) {
7314 case 0: how = SEEK_SET; break;
7315 case 1: how = SEEK_CUR; break;
7316 case 2: how = SEEK_END; break;
7317 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007318#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00007319
Ross Lagerwall8e749672011-03-17 21:54:07 +02007320#if !defined(HAVE_LARGEFILE_SUPPORT)
7321 pos = PyLong_AsLong(posobj);
7322#else
7323 pos = PyLong_AsLongLong(posobj);
7324#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007325 if (PyErr_Occurred())
7326 return NULL;
Guido van Rossum94f6f721999-01-06 18:42:14 +00007327
Victor Stinner8c62be82010-05-06 00:08:46 +00007328 if (!_PyVerify_fd(fd))
7329 return posix_error();
7330 Py_BEGIN_ALLOW_THREADS
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007331#if defined(MS_WIN64) || defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007332 res = _lseeki64(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007333#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007334 res = lseek(fd, pos, how);
Fred Drake699f3522000-06-29 21:12:41 +00007335#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007336 Py_END_ALLOW_THREADS
7337 if (res < 0)
7338 return posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00007339
7340#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00007341 return PyLong_FromLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007342#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 return PyLong_FromLongLong(res);
Guido van Rossum94f6f721999-01-06 18:42:14 +00007344#endif
Guido van Rossum687dd131993-05-17 08:34:16 +00007345}
7346
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007347
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007348PyDoc_STRVAR(posix_read__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007349"read(fd, buffersize) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007350Read a file descriptor.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007351
Barry Warsaw53699e91996-12-10 23:23:01 +00007352static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00007353posix_read(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007354{
Victor Stinner8c62be82010-05-06 00:08:46 +00007355 int fd, size;
7356 Py_ssize_t n;
7357 PyObject *buffer;
7358 if (!PyArg_ParseTuple(args, "ii:read", &fd, &size))
7359 return NULL;
7360 if (size < 0) {
7361 errno = EINVAL;
7362 return posix_error();
7363 }
7364 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7365 if (buffer == NULL)
7366 return NULL;
Stefan Krah99439262010-11-26 12:58:05 +00007367 if (!_PyVerify_fd(fd)) {
7368 Py_DECREF(buffer);
Victor Stinner8c62be82010-05-06 00:08:46 +00007369 return posix_error();
Stefan Krah99439262010-11-26 12:58:05 +00007370 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007371 Py_BEGIN_ALLOW_THREADS
7372 n = read(fd, PyBytes_AS_STRING(buffer), size);
7373 Py_END_ALLOW_THREADS
7374 if (n < 0) {
7375 Py_DECREF(buffer);
7376 return posix_error();
7377 }
7378 if (n != size)
7379 _PyBytes_Resize(&buffer, n);
7380 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00007381}
7382
Ross Lagerwall7807c352011-03-17 20:20:30 +02007383#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
7384 || defined(__APPLE__))) || defined(HAVE_READV) || defined(HAVE_WRITEV)
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007385static Py_ssize_t
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007386iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, int cnt, int type)
7387{
7388 int i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007389 Py_ssize_t blen, total = 0;
7390
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007391 *iov = PyMem_New(struct iovec, cnt);
7392 if (*iov == NULL) {
7393 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007394 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007395 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007396
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007397 *buf = PyMem_New(Py_buffer, cnt);
7398 if (*buf == NULL) {
7399 PyMem_Del(*iov);
7400 PyErr_NoMemory();
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007401 return total;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007402 }
7403
7404 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007405 PyObject *item = PySequence_GetItem(seq, i);
7406 if (item == NULL)
7407 goto fail;
7408 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
7409 Py_DECREF(item);
7410 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007411 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007412 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007413 (*iov)[i].iov_base = (*buf)[i].buf;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007414 blen = (*buf)[i].len;
7415 (*iov)[i].iov_len = blen;
7416 total += blen;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007417 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007418 return total;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02007419
7420fail:
7421 PyMem_Del(*iov);
7422 for (j = 0; j < i; j++) {
7423 PyBuffer_Release(&(*buf)[j]);
7424 }
7425 PyMem_Del(*buf);
7426 return 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007427}
7428
7429static void
7430iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
7431{
7432 int i;
7433 PyMem_Del(iov);
7434 for (i = 0; i < cnt; i++) {
7435 PyBuffer_Release(&buf[i]);
7436 }
7437 PyMem_Del(buf);
7438}
7439#endif
7440
Ross Lagerwall7807c352011-03-17 20:20:30 +02007441#ifdef HAVE_READV
7442PyDoc_STRVAR(posix_readv__doc__,
7443"readv(fd, buffers) -> bytesread\n\n\
7444Read from a file descriptor into a number of writable buffers. buffers\n\
7445is an arbitrary sequence of writable buffers.\n\
7446Returns the total number of bytes read.");
7447
7448static PyObject *
7449posix_readv(PyObject *self, PyObject *args)
7450{
7451 int fd, cnt;
7452 Py_ssize_t n;
7453 PyObject *seq;
7454 struct iovec *iov;
7455 Py_buffer *buf;
7456
7457 if (!PyArg_ParseTuple(args, "iO:readv", &fd, &seq))
7458 return NULL;
7459 if (!PySequence_Check(seq)) {
7460 PyErr_SetString(PyExc_TypeError,
7461 "readv() arg 2 must be a sequence");
7462 return NULL;
7463 }
7464 cnt = PySequence_Size(seq);
7465
7466 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_WRITABLE))
7467 return NULL;
7468
7469 Py_BEGIN_ALLOW_THREADS
7470 n = readv(fd, iov, cnt);
7471 Py_END_ALLOW_THREADS
7472
7473 iov_cleanup(iov, buf, cnt);
7474 return PyLong_FromSsize_t(n);
7475}
7476#endif
7477
7478#ifdef HAVE_PREAD
7479PyDoc_STRVAR(posix_pread__doc__,
7480"pread(fd, buffersize, offset) -> string\n\n\
7481Read from a file descriptor, fd, at a position of offset. It will read up\n\
7482to buffersize number of bytes. The file offset remains unchanged.");
7483
7484static PyObject *
7485posix_pread(PyObject *self, PyObject *args)
7486{
7487 int fd, size;
7488 off_t offset;
7489 Py_ssize_t n;
7490 PyObject *buffer;
7491 if (!PyArg_ParseTuple(args, "iiO&:pread", &fd, &size, _parse_off_t, &offset))
7492 return NULL;
7493
7494 if (size < 0) {
7495 errno = EINVAL;
7496 return posix_error();
7497 }
7498 buffer = PyBytes_FromStringAndSize((char *)NULL, size);
7499 if (buffer == NULL)
7500 return NULL;
7501 if (!_PyVerify_fd(fd)) {
7502 Py_DECREF(buffer);
7503 return posix_error();
7504 }
7505 Py_BEGIN_ALLOW_THREADS
7506 n = pread(fd, PyBytes_AS_STRING(buffer), size, offset);
7507 Py_END_ALLOW_THREADS
7508 if (n < 0) {
7509 Py_DECREF(buffer);
7510 return posix_error();
7511 }
7512 if (n != size)
7513 _PyBytes_Resize(&buffer, n);
7514 return buffer;
7515}
7516#endif
7517
7518PyDoc_STRVAR(posix_write__doc__,
7519"write(fd, string) -> byteswritten\n\n\
7520Write a string to a file descriptor.");
7521
7522static PyObject *
7523posix_write(PyObject *self, PyObject *args)
7524{
7525 Py_buffer pbuf;
7526 int fd;
7527 Py_ssize_t size, len;
7528
7529 if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
7530 return NULL;
7531 if (!_PyVerify_fd(fd)) {
7532 PyBuffer_Release(&pbuf);
7533 return posix_error();
7534 }
7535 len = pbuf.len;
7536 Py_BEGIN_ALLOW_THREADS
7537#if defined(MS_WIN64) || defined(MS_WINDOWS)
7538 if (len > INT_MAX)
7539 len = INT_MAX;
7540 size = write(fd, pbuf.buf, (int)len);
7541#else
7542 size = write(fd, pbuf.buf, len);
7543#endif
7544 Py_END_ALLOW_THREADS
7545 PyBuffer_Release(&pbuf);
7546 if (size < 0)
7547 return posix_error();
7548 return PyLong_FromSsize_t(size);
7549}
7550
7551#ifdef HAVE_SENDFILE
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007552PyDoc_STRVAR(posix_sendfile__doc__,
7553"sendfile(out, in, offset, nbytes) -> byteswritten\n\
7554sendfile(out, in, offset, nbytes, headers=None, trailers=None, flags=0)\n\
7555 -> byteswritten\n\
7556Copy nbytes bytes from file descriptor in to file descriptor out.");
7557
7558static PyObject *
7559posix_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
7560{
7561 int in, out;
7562 Py_ssize_t ret;
7563 off_t offset;
7564
7565#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
7566#ifndef __APPLE__
7567 Py_ssize_t len;
7568#endif
7569 PyObject *headers = NULL, *trailers = NULL;
7570 Py_buffer *hbuf, *tbuf;
7571 off_t sbytes;
7572 struct sf_hdtr sf;
7573 int flags = 0;
7574 sf.headers = NULL;
7575 sf.trailers = NULL;
Benjamin Petersond8a43b42011-02-26 21:35:16 +00007576 static char *keywords[] = {"out", "in",
7577 "offset", "count",
7578 "headers", "trailers", "flags", NULL};
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007579
7580#ifdef __APPLE__
7581 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&O&|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007582 keywords, &out, &in, _parse_off_t, &offset, _parse_off_t, &sbytes,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007583#else
7584 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiO&n|OOi:sendfile",
Georg Brandla391b112011-02-25 15:23:18 +00007585 keywords, &out, &in, _parse_off_t, &offset, &len,
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007586#endif
7587 &headers, &trailers, &flags))
7588 return NULL;
7589 if (headers != NULL) {
7590 if (!PySequence_Check(headers)) {
7591 PyErr_SetString(PyExc_TypeError,
7592 "sendfile() headers must be a sequence or None");
7593 return NULL;
7594 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007595 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007596 sf.hdr_cnt = PySequence_Size(headers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007597 if (sf.hdr_cnt > 0 &&
7598 !(i = iov_setup(&(sf.headers), &hbuf,
7599 headers, sf.hdr_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007600 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007601#ifdef __APPLE__
7602 sbytes += i;
7603#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007604 }
7605 }
7606 if (trailers != NULL) {
7607 if (!PySequence_Check(trailers)) {
7608 PyErr_SetString(PyExc_TypeError,
7609 "sendfile() trailers must be a sequence or None");
7610 return NULL;
7611 } else {
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007612 Py_ssize_t i = 0; /* Avoid uninitialized warning */
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007613 sf.trl_cnt = PySequence_Size(trailers);
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007614 if (sf.trl_cnt > 0 &&
7615 !(i = iov_setup(&(sf.trailers), &tbuf,
7616 trailers, sf.trl_cnt, PyBUF_SIMPLE)))
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007617 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00007618#ifdef __APPLE__
7619 sbytes += i;
7620#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007621 }
7622 }
7623
7624 Py_BEGIN_ALLOW_THREADS
7625#ifdef __APPLE__
7626 ret = sendfile(in, out, offset, &sbytes, &sf, flags);
7627#else
7628 ret = sendfile(in, out, offset, len, &sf, &sbytes, flags);
7629#endif
7630 Py_END_ALLOW_THREADS
7631
7632 if (sf.headers != NULL)
7633 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
7634 if (sf.trailers != NULL)
7635 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
7636
7637 if (ret < 0) {
7638 if ((errno == EAGAIN) || (errno == EBUSY)) {
7639 if (sbytes != 0) {
7640 // some data has been sent
7641 goto done;
7642 }
7643 else {
7644 // no data has been sent; upper application is supposed
7645 // to retry on EAGAIN or EBUSY
7646 return posix_error();
7647 }
7648 }
7649 return posix_error();
7650 }
7651 goto done;
7652
7653done:
7654 #if !defined(HAVE_LARGEFILE_SUPPORT)
7655 return Py_BuildValue("l", sbytes);
7656 #else
7657 return Py_BuildValue("L", sbytes);
7658 #endif
7659
7660#else
7661 Py_ssize_t count;
7662 PyObject *offobj;
7663 static char *keywords[] = {"out", "in",
7664 "offset", "count", NULL};
7665 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iiOn:sendfile",
7666 keywords, &out, &in, &offobj, &count))
7667 return NULL;
7668#ifdef linux
7669 if (offobj == Py_None) {
7670 Py_BEGIN_ALLOW_THREADS
7671 ret = sendfile(out, in, NULL, count);
7672 Py_END_ALLOW_THREADS
7673 if (ret < 0)
7674 return posix_error();
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02007675 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007676 }
7677#endif
Antoine Pitroudcc20b82011-02-26 13:38:35 +00007678 if (!_parse_off_t(offobj, &offset))
7679 return NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007680 Py_BEGIN_ALLOW_THREADS
7681 ret = sendfile(out, in, &offset, count);
7682 Py_END_ALLOW_THREADS
7683 if (ret < 0)
7684 return posix_error();
7685 return Py_BuildValue("n", ret);
7686#endif
7687}
7688#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007689
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007690PyDoc_STRVAR(posix_fstat__doc__,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007691"fstat(fd) -> stat result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07007692Like stat(), but for an open file descriptor.\n\
7693Equivalent to stat(fd=fd).");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007694
Barry Warsaw53699e91996-12-10 23:23:01 +00007695static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007696posix_fstat(PyObject *self, PyObject *args)
Guido van Rossum687dd131993-05-17 08:34:16 +00007697{
Victor Stinner8c62be82010-05-06 00:08:46 +00007698 int fd;
7699 STRUCT_STAT st;
7700 int res;
Victor Stinner4195b5c2012-02-08 23:03:19 +01007701 if (!PyArg_ParseTuple(args, "i:fstat", &fd))
Victor Stinner8c62be82010-05-06 00:08:46 +00007702 return NULL;
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007703#ifdef __VMS
Victor Stinner8c62be82010-05-06 00:08:46 +00007704 /* on OpenVMS we must ensure that all bytes are written to the file */
7705 fsync(fd);
Martin v. Löwis7a924e62003-03-05 14:15:21 +00007706#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007707 Py_BEGIN_ALLOW_THREADS
7708 res = FSTAT(fd, &st);
7709 Py_END_ALLOW_THREADS
7710 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00007711#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01007712 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00007713#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 return posix_error();
Martin v. Löwis14694662006-02-03 12:54:16 +00007715#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00007716 }
Tim Peters5aa91602002-01-30 05:46:57 +00007717
Victor Stinner4195b5c2012-02-08 23:03:19 +01007718 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00007719}
7720
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007721PyDoc_STRVAR(posix_isatty__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007722"isatty(fd) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00007723Return True if the file descriptor 'fd' is an open file descriptor\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007724connected to the slave end of a terminal.");
Skip Montanaro1517d842000-07-19 14:34:14 +00007725
7726static PyObject *
Thomas Wouters616607a2000-07-19 14:45:40 +00007727posix_isatty(PyObject *self, PyObject *args)
Skip Montanaro1517d842000-07-19 14:34:14 +00007728{
Victor Stinner8c62be82010-05-06 00:08:46 +00007729 int fd;
7730 if (!PyArg_ParseTuple(args, "i:isatty", &fd))
7731 return NULL;
7732 if (!_PyVerify_fd(fd))
7733 return PyBool_FromLong(0);
7734 return PyBool_FromLong(isatty(fd));
Skip Montanaro1517d842000-07-19 14:34:14 +00007735}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007736
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007737#ifdef HAVE_PIPE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007738PyDoc_STRVAR(posix_pipe__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00007739"pipe() -> (read_end, write_end)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007740Create a pipe.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007741
Barry Warsaw53699e91996-12-10 23:23:01 +00007742static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00007743posix_pipe(PyObject *self, PyObject *noargs)
Guido van Rossum687dd131993-05-17 08:34:16 +00007744{
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007745#if !defined(MS_WINDOWS)
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 int fds[2];
7747 int res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007748 res = pipe(fds);
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 if (res != 0)
7750 return posix_error();
7751 return Py_BuildValue("(ii)", fds[0], fds[1]);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007752#else /* MS_WINDOWS */
Victor Stinner8c62be82010-05-06 00:08:46 +00007753 HANDLE read, write;
7754 int read_fd, write_fd;
7755 BOOL ok;
Victor Stinner8c62be82010-05-06 00:08:46 +00007756 ok = CreatePipe(&read, &write, NULL, 0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007757 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01007758 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00007759 read_fd = _open_osfhandle((Py_intptr_t)read, 0);
7760 write_fd = _open_osfhandle((Py_intptr_t)write, 1);
7761 return Py_BuildValue("(ii)", read_fd, write_fd);
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00007762#endif /* MS_WINDOWS */
Guido van Rossum687dd131993-05-17 08:34:16 +00007763}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007764#endif /* HAVE_PIPE */
7765
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007766#ifdef HAVE_PIPE2
7767PyDoc_STRVAR(posix_pipe2__doc__,
Charles-François Natali368f34b2011-06-06 19:49:47 +02007768"pipe2(flags) -> (read_end, write_end)\n\n\
7769Create a pipe with flags set atomically.\n\
7770flags can be constructed by ORing together one or more of these values:\n\
7771O_NONBLOCK, O_CLOEXEC.\n\
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007772");
7773
7774static PyObject *
Charles-François Natali368f34b2011-06-06 19:49:47 +02007775posix_pipe2(PyObject *self, PyObject *arg)
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007776{
Charles-François Natali368f34b2011-06-06 19:49:47 +02007777 int flags;
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007778 int fds[2];
7779 int res;
7780
Serhiy Storchaka78980432013-01-15 01:12:17 +02007781 flags = _PyLong_AsInt(arg);
Charles-François Natali368f34b2011-06-06 19:49:47 +02007782 if (flags == -1 && PyErr_Occurred())
Charles-François Natalidaafdd52011-05-29 20:07:40 +02007783 return NULL;
7784
7785 res = pipe2(fds, flags);
7786 if (res != 0)
7787 return posix_error();
7788 return Py_BuildValue("(ii)", fds[0], fds[1]);
7789}
7790#endif /* HAVE_PIPE2 */
7791
Ross Lagerwall7807c352011-03-17 20:20:30 +02007792#ifdef HAVE_WRITEV
7793PyDoc_STRVAR(posix_writev__doc__,
7794"writev(fd, buffers) -> byteswritten\n\n\
7795Write the contents of buffers to a file descriptor, where buffers is an\n\
7796arbitrary sequence of buffers.\n\
7797Returns the total bytes written.");
7798
7799static PyObject *
7800posix_writev(PyObject *self, PyObject *args)
7801{
7802 int fd, cnt;
7803 Py_ssize_t res;
7804 PyObject *seq;
7805 struct iovec *iov;
7806 Py_buffer *buf;
7807 if (!PyArg_ParseTuple(args, "iO:writev", &fd, &seq))
7808 return NULL;
7809 if (!PySequence_Check(seq)) {
7810 PyErr_SetString(PyExc_TypeError,
7811 "writev() arg 2 must be a sequence");
7812 return NULL;
7813 }
7814 cnt = PySequence_Size(seq);
7815
7816 if (!iov_setup(&iov, &buf, seq, cnt, PyBUF_SIMPLE)) {
7817 return NULL;
7818 }
7819
7820 Py_BEGIN_ALLOW_THREADS
7821 res = writev(fd, iov, cnt);
7822 Py_END_ALLOW_THREADS
7823
7824 iov_cleanup(iov, buf, cnt);
7825 return PyLong_FromSsize_t(res);
7826}
7827#endif
7828
7829#ifdef HAVE_PWRITE
7830PyDoc_STRVAR(posix_pwrite__doc__,
7831"pwrite(fd, string, offset) -> byteswritten\n\n\
7832Write string to a file descriptor, fd, from offset, leaving the file\n\
7833offset unchanged.");
7834
7835static PyObject *
7836posix_pwrite(PyObject *self, PyObject *args)
7837{
7838 Py_buffer pbuf;
7839 int fd;
7840 off_t offset;
7841 Py_ssize_t size;
7842
7843 if (!PyArg_ParseTuple(args, "iy*O&:pwrite", &fd, &pbuf, _parse_off_t, &offset))
7844 return NULL;
7845
7846 if (!_PyVerify_fd(fd)) {
7847 PyBuffer_Release(&pbuf);
7848 return posix_error();
7849 }
7850 Py_BEGIN_ALLOW_THREADS
7851 size = pwrite(fd, pbuf.buf, (size_t)pbuf.len, offset);
7852 Py_END_ALLOW_THREADS
7853 PyBuffer_Release(&pbuf);
7854 if (size < 0)
7855 return posix_error();
7856 return PyLong_FromSsize_t(size);
7857}
7858#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007859
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007860#ifdef HAVE_MKFIFO
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007861PyDoc_STRVAR(posix_mkfifo__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007862"mkfifo(path, mode=0o666, *, dir_fd=None)\n\n\
7863Create a FIFO (a POSIX named pipe).\n\
7864\n\
7865If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7866 and path should be relative; path will then be relative to that directory.\n\
7867dir_fd may not be implemented on your platform.\n\
7868 If it is unavailable, using it will raise a NotImplementedError.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007869
Barry Warsaw53699e91996-12-10 23:23:01 +00007870static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007871posix_mkfifo(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007872{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007873 path_t path;
Victor Stinner8c62be82010-05-06 00:08:46 +00007874 int mode = 0666;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007875 int dir_fd = DEFAULT_DIR_FD;
7876 int result;
7877 PyObject *return_value = NULL;
7878 static char *keywords[] = {"path", "mode", "dir_fd", NULL};
7879
7880 memset(&path, 0, sizeof(path));
7881 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|i$O&:mkfifo", keywords,
7882 path_converter, &path,
7883 &mode,
7884#ifdef HAVE_MKFIFOAT
7885 dir_fd_converter, &dir_fd
7886#else
7887 dir_fd_unavailable, &dir_fd
7888#endif
7889 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00007890 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007891
Victor Stinner8c62be82010-05-06 00:08:46 +00007892 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007893#ifdef HAVE_MKFIFOAT
7894 if (dir_fd != DEFAULT_DIR_FD)
7895 result = mkfifoat(dir_fd, path.narrow, mode);
7896 else
7897#endif
7898 result = mkfifo(path.narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00007899 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007900
7901 if (result < 0) {
7902 return_value = posix_error();
7903 goto exit;
7904 }
7905
7906 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00007907 Py_INCREF(Py_None);
Larry Hastings9cf065c2012-06-22 16:30:09 -07007908
7909exit:
7910 path_cleanup(&path);
7911 return return_value;
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007912}
7913#endif
7914
Neal Norwitz11690112002-07-30 01:08:28 +00007915#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007916PyDoc_STRVAR(posix_mknod__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07007917"mknod(filename, mode=0o600, device=0, *, dir_fd=None)\n\n\
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007918Create a filesystem node (file, device special file or named pipe)\n\
7919named filename. mode specifies both the permissions to use and the\n\
7920type of node to be created, being combined (bitwise OR) with one of\n\
7921S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK,\n\
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007922device defines the newly created device special file (probably using\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07007923os.makedev()), otherwise it is ignored.\n\
7924\n\
7925If dir_fd is not None, it should be a file descriptor open to a directory,\n\
7926 and path should be relative; path will then be relative to that directory.\n\
7927dir_fd may not be implemented on your platform.\n\
7928 If it is unavailable, using it will raise a NotImplementedError.");
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007929
7930
7931static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07007932posix_mknod(PyObject *self, PyObject *args, PyObject *kwargs)
Martin v. Löwis06a83e92002-04-14 10:19:44 +00007933{
Larry Hastings9cf065c2012-06-22 16:30:09 -07007934 path_t path;
7935 int mode = 0666;
Victor Stinner8c62be82010-05-06 00:08:46 +00007936 int device = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007937 int dir_fd = DEFAULT_DIR_FD;
7938 int result;
7939 PyObject *return_value = NULL;
7940 static char *keywords[] = {"path", "mode", "device", "dir_fd", NULL};
7941
7942 memset(&path, 0, sizeof(path));
7943 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|ii$O&:mknod", keywords,
7944 path_converter, &path,
7945 &mode, &device,
7946#ifdef HAVE_MKNODAT
7947 dir_fd_converter, &dir_fd
7948#else
7949 dir_fd_unavailable, &dir_fd
7950#endif
7951 ))
Victor Stinner8c62be82010-05-06 00:08:46 +00007952 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07007953
Victor Stinner8c62be82010-05-06 00:08:46 +00007954 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007955#ifdef HAVE_MKNODAT
7956 if (dir_fd != DEFAULT_DIR_FD)
7957 result = mknodat(dir_fd, path.narrow, mode, device);
7958 else
7959#endif
7960 result = mknod(path.narrow, mode, device);
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07007962
7963 if (result < 0) {
7964 return_value = posix_error();
7965 goto exit;
7966 }
7967
7968 return_value = Py_None;
Victor Stinner8c62be82010-05-06 00:08:46 +00007969 Py_INCREF(Py_None);
Georg Brandlf7875592012-06-24 13:58:31 +02007970
Larry Hastings9cf065c2012-06-22 16:30:09 -07007971exit:
7972 path_cleanup(&path);
7973 return return_value;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00007974}
7975#endif
7976
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007977#ifdef HAVE_DEVICE_MACROS
7978PyDoc_STRVAR(posix_major__doc__,
7979"major(device) -> major number\n\
7980Extracts a device major number from a raw device number.");
7981
7982static PyObject *
7983posix_major(PyObject *self, PyObject *args)
7984{
Victor Stinner8c62be82010-05-06 00:08:46 +00007985 int device;
7986 if (!PyArg_ParseTuple(args, "i:major", &device))
7987 return NULL;
7988 return PyLong_FromLong((long)major(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00007989}
7990
7991PyDoc_STRVAR(posix_minor__doc__,
7992"minor(device) -> minor number\n\
7993Extracts a device minor number from a raw device number.");
7994
7995static PyObject *
7996posix_minor(PyObject *self, PyObject *args)
7997{
Victor Stinner8c62be82010-05-06 00:08:46 +00007998 int device;
7999 if (!PyArg_ParseTuple(args, "i:minor", &device))
8000 return NULL;
8001 return PyLong_FromLong((long)minor(device));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008002}
8003
8004PyDoc_STRVAR(posix_makedev__doc__,
8005"makedev(major, minor) -> device number\n\
8006Composes a raw device number from the major and minor device numbers.");
8007
8008static PyObject *
8009posix_makedev(PyObject *self, PyObject *args)
8010{
Victor Stinner8c62be82010-05-06 00:08:46 +00008011 int major, minor;
8012 if (!PyArg_ParseTuple(args, "ii:makedev", &major, &minor))
8013 return NULL;
8014 return PyLong_FromLong((long)makedev(major, minor));
Martin v. Löwisdbe3f762002-10-10 14:27:30 +00008015}
8016#endif /* device macros */
8017
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008018
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008019#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008020PyDoc_STRVAR(posix_ftruncate__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008021"ftruncate(fd, length)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008022Truncate a file to a specified length.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008023
Barry Warsaw53699e91996-12-10 23:23:01 +00008024static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008025posix_ftruncate(PyObject *self, PyObject *args)
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008026{
Victor Stinner8c62be82010-05-06 00:08:46 +00008027 int fd;
8028 off_t length;
8029 int res;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008030
Ross Lagerwall7807c352011-03-17 20:20:30 +02008031 if (!PyArg_ParseTuple(args, "iO&:ftruncate", &fd, _parse_off_t, &length))
Victor Stinner8c62be82010-05-06 00:08:46 +00008032 return NULL;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008033
Victor Stinner8c62be82010-05-06 00:08:46 +00008034 Py_BEGIN_ALLOW_THREADS
8035 res = ftruncate(fd, length);
8036 Py_END_ALLOW_THREADS
8037 if (res < 0)
8038 return posix_error();
8039 Py_INCREF(Py_None);
8040 return Py_None;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00008041}
8042#endif
Guido van Rossum22db57e1992-04-05 14:25:30 +00008043
Ross Lagerwall7807c352011-03-17 20:20:30 +02008044#ifdef HAVE_TRUNCATE
8045PyDoc_STRVAR(posix_truncate__doc__,
8046"truncate(path, length)\n\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008047Truncate the file given by path to length bytes.\n\
8048On some platforms, path may also be specified as an open file descriptor.\n\
8049 If this functionality is unavailable, using it raises an exception.");
Ross Lagerwall7807c352011-03-17 20:20:30 +02008050
8051static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008052posix_truncate(PyObject *self, PyObject *args, PyObject *kwargs)
Ross Lagerwall7807c352011-03-17 20:20:30 +02008053{
Georg Brandl306336b2012-06-24 12:55:33 +02008054 path_t path;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008055 off_t length;
8056 int res;
Georg Brandl306336b2012-06-24 12:55:33 +02008057 PyObject *result = NULL;
8058 static char *keywords[] = {"path", "length", NULL};
Ross Lagerwall7807c352011-03-17 20:20:30 +02008059
Georg Brandl306336b2012-06-24 12:55:33 +02008060 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008061 path.function_name = "truncate";
Georg Brandl306336b2012-06-24 12:55:33 +02008062#ifdef HAVE_FTRUNCATE
8063 path.allow_fd = 1;
8064#endif
8065 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:truncate", keywords,
8066 path_converter, &path,
8067 _parse_off_t, &length))
Ross Lagerwall7807c352011-03-17 20:20:30 +02008068 return NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008069
8070 Py_BEGIN_ALLOW_THREADS
Georg Brandl306336b2012-06-24 12:55:33 +02008071#ifdef HAVE_FTRUNCATE
8072 if (path.fd != -1)
8073 res = ftruncate(path.fd, length);
8074 else
8075#endif
8076 res = truncate(path.narrow, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008077 Py_END_ALLOW_THREADS
Ross Lagerwall7807c352011-03-17 20:20:30 +02008078 if (res < 0)
Victor Stinner292c8352012-10-30 02:17:38 +01008079 result = path_error(&path);
Georg Brandl306336b2012-06-24 12:55:33 +02008080 else {
8081 Py_INCREF(Py_None);
8082 result = Py_None;
8083 }
8084 path_cleanup(&path);
8085 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008086}
8087#endif
8088
8089#ifdef HAVE_POSIX_FALLOCATE
8090PyDoc_STRVAR(posix_posix_fallocate__doc__,
8091"posix_fallocate(fd, offset, len)\n\n\
8092Ensures that enough disk space is allocated for the file specified by fd\n\
8093starting from offset and continuing for len bytes.");
8094
8095static PyObject *
8096posix_posix_fallocate(PyObject *self, PyObject *args)
8097{
8098 off_t len, offset;
8099 int res, fd;
8100
8101 if (!PyArg_ParseTuple(args, "iO&O&:posix_fallocate",
8102 &fd, _parse_off_t, &offset, _parse_off_t, &len))
8103 return NULL;
8104
8105 Py_BEGIN_ALLOW_THREADS
8106 res = posix_fallocate(fd, offset, len);
8107 Py_END_ALLOW_THREADS
8108 if (res != 0) {
8109 errno = res;
8110 return posix_error();
8111 }
8112 Py_RETURN_NONE;
8113}
8114#endif
8115
8116#ifdef HAVE_POSIX_FADVISE
8117PyDoc_STRVAR(posix_posix_fadvise__doc__,
8118"posix_fadvise(fd, offset, len, advice)\n\n\
8119Announces an intention to access data in a specific pattern thus allowing\n\
8120the kernel to make optimizations.\n\
8121The advice applies to the region of the file specified by fd starting at\n\
8122offset and continuing for len bytes.\n\
8123advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,\n\
8124POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED or\n\
8125POSIX_FADV_DONTNEED.");
8126
8127static PyObject *
8128posix_posix_fadvise(PyObject *self, PyObject *args)
8129{
8130 off_t len, offset;
8131 int res, fd, advice;
8132
8133 if (!PyArg_ParseTuple(args, "iO&O&i:posix_fadvise",
8134 &fd, _parse_off_t, &offset, _parse_off_t, &len, &advice))
8135 return NULL;
8136
8137 Py_BEGIN_ALLOW_THREADS
8138 res = posix_fadvise(fd, offset, len, advice);
8139 Py_END_ALLOW_THREADS
8140 if (res != 0) {
8141 errno = res;
8142 return posix_error();
8143 }
8144 Py_RETURN_NONE;
8145}
8146#endif
8147
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008148#ifdef HAVE_PUTENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008149PyDoc_STRVAR(posix_putenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008150"putenv(key, value)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008151Change or add an environment variable.");
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008152
Fred Drake762e2061999-08-26 17:23:54 +00008153/* Save putenv() parameters as values here, so we can collect them when they
8154 * get re-set with another call for the same key. */
8155static PyObject *posix_putenv_garbage;
8156
Tim Peters5aa91602002-01-30 05:46:57 +00008157static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008158posix_putenv(PyObject *self, PyObject *args)
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008159{
Victor Stinner84ae1182010-05-06 22:05:07 +00008160 PyObject *newstr = NULL;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008161#ifdef MS_WINDOWS
Victor Stinner65170952011-11-22 22:16:17 +01008162 PyObject *os1, *os2;
Guido van Rossumf1af3fe1996-07-23 19:18:10 +00008163 wchar_t *newenv;
Victor Stinner65170952011-11-22 22:16:17 +01008164
Victor Stinner8c62be82010-05-06 00:08:46 +00008165 if (!PyArg_ParseTuple(args,
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008166 "UU:putenv",
Victor Stinner65170952011-11-22 22:16:17 +01008167 &os1, &os2))
Victor Stinner8c62be82010-05-06 00:08:46 +00008168 return NULL;
Guido van Rossumd48f2521997-12-05 22:19:34 +00008169
Victor Stinner65170952011-11-22 22:16:17 +01008170 newstr = PyUnicode_FromFormat("%U=%U", os1, os2);
Victor Stinner84ae1182010-05-06 22:05:07 +00008171 if (newstr == NULL) {
8172 PyErr_NoMemory();
8173 goto error;
8174 }
Victor Stinner65170952011-11-22 22:16:17 +01008175 if (_MAX_ENV < PyUnicode_GET_LENGTH(newstr)) {
8176 PyErr_Format(PyExc_ValueError,
8177 "the environment variable is longer than %u characters",
8178 _MAX_ENV);
8179 goto error;
8180 }
8181
Victor Stinner8c62be82010-05-06 00:08:46 +00008182 newenv = PyUnicode_AsUnicode(newstr);
Victor Stinnereb5657a2011-09-30 01:44:27 +02008183 if (newenv == NULL)
8184 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008185 if (_wputenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008186 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008187 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008188 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008189#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008190 PyObject *os1, *os2;
8191 char *s1, *s2;
8192 char *newenv;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008193
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008194 if (!PyArg_ParseTuple(args,
8195 "O&O&:putenv",
8196 PyUnicode_FSConverter, &os1,
8197 PyUnicode_FSConverter, &os2))
8198 return NULL;
8199 s1 = PyBytes_AsString(os1);
8200 s2 = PyBytes_AsString(os2);
Guido van Rossumad0ee831995-03-01 10:34:45 +00008201
Victor Stinner65170952011-11-22 22:16:17 +01008202 newstr = PyBytes_FromFormat("%s=%s", s1, s2);
Victor Stinner9d3b93b2011-11-22 02:27:30 +01008203 if (newstr == NULL) {
8204 PyErr_NoMemory();
8205 goto error;
8206 }
8207
Victor Stinner8c62be82010-05-06 00:08:46 +00008208 newenv = PyBytes_AS_STRING(newstr);
Victor Stinner8c62be82010-05-06 00:08:46 +00008209 if (putenv(newenv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008210 posix_error();
Victor Stinner84ae1182010-05-06 22:05:07 +00008211 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00008212 }
Thomas Hellerf78f12a2007-11-08 19:33:05 +00008213#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008214
Victor Stinner8c62be82010-05-06 00:08:46 +00008215 /* Install the first arg and newstr in posix_putenv_garbage;
8216 * this will cause previous value to be collected. This has to
8217 * happen after the real putenv() call because the old value
8218 * was still accessible until then. */
Victor Stinner65170952011-11-22 22:16:17 +01008219 if (PyDict_SetItem(posix_putenv_garbage, os1, newstr)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008220 /* really not much we can do; just leak */
8221 PyErr_Clear();
8222 }
8223 else {
8224 Py_DECREF(newstr);
8225 }
Guido van Rossumd48f2521997-12-05 22:19:34 +00008226
Martin v. Löwis011e8422009-05-05 04:43:17 +00008227#ifndef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00008228 Py_DECREF(os1);
8229 Py_DECREF(os2);
Martin v. Löwis011e8422009-05-05 04:43:17 +00008230#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008231 Py_RETURN_NONE;
8232
8233error:
8234#ifndef MS_WINDOWS
8235 Py_DECREF(os1);
8236 Py_DECREF(os2);
8237#endif
8238 Py_XDECREF(newstr);
8239 return NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +00008240}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008241#endif /* putenv */
8242
Guido van Rossumc524d952001-10-19 01:31:59 +00008243#ifdef HAVE_UNSETENV
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008244PyDoc_STRVAR(posix_unsetenv__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008245"unsetenv(key)\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008246Delete an environment variable.");
Guido van Rossumc524d952001-10-19 01:31:59 +00008247
8248static PyObject *
8249posix_unsetenv(PyObject *self, PyObject *args)
8250{
Victor Stinner65170952011-11-22 22:16:17 +01008251 PyObject *name;
Victor Stinner984890f2011-11-24 13:53:38 +01008252#ifndef HAVE_BROKEN_UNSETENV
Victor Stinner60b385e2011-11-22 22:01:28 +01008253 int err;
Victor Stinner984890f2011-11-24 13:53:38 +01008254#endif
Victor Stinner84ae1182010-05-06 22:05:07 +00008255
8256 if (!PyArg_ParseTuple(args, "O&:unsetenv",
Guido van Rossumc524d952001-10-19 01:31:59 +00008257
Victor Stinner65170952011-11-22 22:16:17 +01008258 PyUnicode_FSConverter, &name))
Guido van Rossumc524d952001-10-19 01:31:59 +00008259 return NULL;
Guido van Rossumc524d952001-10-19 01:31:59 +00008260
Victor Stinner984890f2011-11-24 13:53:38 +01008261#ifdef HAVE_BROKEN_UNSETENV
8262 unsetenv(PyBytes_AS_STRING(name));
8263#else
Victor Stinner65170952011-11-22 22:16:17 +01008264 err = unsetenv(PyBytes_AS_STRING(name));
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008265 if (err) {
Benjamin Petersone8eb0e82011-11-22 23:14:47 -06008266 Py_DECREF(name);
Victor Stinner60b385e2011-11-22 22:01:28 +01008267 return posix_error();
Benjamin Peterson4bb867d2011-11-22 23:12:49 -06008268 }
Victor Stinner984890f2011-11-24 13:53:38 +01008269#endif
Guido van Rossumc524d952001-10-19 01:31:59 +00008270
Victor Stinner8c62be82010-05-06 00:08:46 +00008271 /* Remove the key from posix_putenv_garbage;
8272 * this will cause it to be collected. This has to
8273 * happen after the real unsetenv() call because the
8274 * old value was still accessible until then.
8275 */
Victor Stinner65170952011-11-22 22:16:17 +01008276 if (PyDict_DelItem(posix_putenv_garbage, name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008277 /* really not much we can do; just leak */
8278 PyErr_Clear();
8279 }
Victor Stinner65170952011-11-22 22:16:17 +01008280 Py_DECREF(name);
Victor Stinner84ae1182010-05-06 22:05:07 +00008281 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +00008282}
8283#endif /* unsetenv */
8284
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008285PyDoc_STRVAR(posix_strerror__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008286"strerror(code) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008287Translate an error code to a message string.");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008288
Guido van Rossumf68d8e52001-04-14 17:55:09 +00008289static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008290posix_strerror(PyObject *self, PyObject *args)
Guido van Rossumb6a47161997-09-15 22:54:34 +00008291{
Victor Stinner8c62be82010-05-06 00:08:46 +00008292 int code;
8293 char *message;
8294 if (!PyArg_ParseTuple(args, "i:strerror", &code))
8295 return NULL;
8296 message = strerror(code);
8297 if (message == NULL) {
8298 PyErr_SetString(PyExc_ValueError,
8299 "strerror() argument out of range");
8300 return NULL;
8301 }
Victor Stinner1b579672011-12-17 05:47:23 +01008302 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +00008303}
Guido van Rossumb6a47161997-09-15 22:54:34 +00008304
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008305
Guido van Rossumc9641791998-08-04 15:26:23 +00008306#ifdef HAVE_SYS_WAIT_H
8307
Fred Drake106c1a02002-04-23 15:58:02 +00008308#ifdef WCOREDUMP
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008309PyDoc_STRVAR(posix_WCOREDUMP__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008310"WCOREDUMP(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008311Return True if the process returning 'status' was dumped to a core file.");
Fred Drake106c1a02002-04-23 15:58:02 +00008312
8313static PyObject *
8314posix_WCOREDUMP(PyObject *self, PyObject *args)
8315{
Victor Stinner8c62be82010-05-06 00:08:46 +00008316 WAIT_TYPE status;
8317 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008318
Victor Stinner8c62be82010-05-06 00:08:46 +00008319 if (!PyArg_ParseTuple(args, "i:WCOREDUMP", &WAIT_STATUS_INT(status)))
8320 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008321
Victor Stinner8c62be82010-05-06 00:08:46 +00008322 return PyBool_FromLong(WCOREDUMP(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008323}
8324#endif /* WCOREDUMP */
8325
8326#ifdef WIFCONTINUED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008327PyDoc_STRVAR(posix_WIFCONTINUED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008328"WIFCONTINUED(status) -> bool\n\n\
Fred Drake106c1a02002-04-23 15:58:02 +00008329Return True if the process returning 'status' was continued from a\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008330job control stop.");
Fred Drake106c1a02002-04-23 15:58:02 +00008331
8332static PyObject *
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +00008333posix_WIFCONTINUED(PyObject *self, PyObject *args)
Fred Drake106c1a02002-04-23 15:58:02 +00008334{
Victor Stinner8c62be82010-05-06 00:08:46 +00008335 WAIT_TYPE status;
8336 WAIT_STATUS_INT(status) = 0;
Fred Drake106c1a02002-04-23 15:58:02 +00008337
Victor Stinner8c62be82010-05-06 00:08:46 +00008338 if (!PyArg_ParseTuple(args, "i:WCONTINUED", &WAIT_STATUS_INT(status)))
8339 return NULL;
Fred Drake106c1a02002-04-23 15:58:02 +00008340
Victor Stinner8c62be82010-05-06 00:08:46 +00008341 return PyBool_FromLong(WIFCONTINUED(status));
Fred Drake106c1a02002-04-23 15:58:02 +00008342}
8343#endif /* WIFCONTINUED */
8344
Guido van Rossumc9641791998-08-04 15:26:23 +00008345#ifdef WIFSTOPPED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008346PyDoc_STRVAR(posix_WIFSTOPPED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008347"WIFSTOPPED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008348Return True if the process returning 'status' was stopped.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008349
8350static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008351posix_WIFSTOPPED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008352{
Victor Stinner8c62be82010-05-06 00:08:46 +00008353 WAIT_TYPE status;
8354 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008355
Victor Stinner8c62be82010-05-06 00:08:46 +00008356 if (!PyArg_ParseTuple(args, "i:WIFSTOPPED", &WAIT_STATUS_INT(status)))
8357 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008358
Victor Stinner8c62be82010-05-06 00:08:46 +00008359 return PyBool_FromLong(WIFSTOPPED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008360}
8361#endif /* WIFSTOPPED */
8362
8363#ifdef WIFSIGNALED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008364PyDoc_STRVAR(posix_WIFSIGNALED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008365"WIFSIGNALED(status) -> bool\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008366Return True if the process returning 'status' was terminated by a signal.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008367
8368static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008369posix_WIFSIGNALED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008370{
Victor Stinner8c62be82010-05-06 00:08:46 +00008371 WAIT_TYPE status;
8372 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008373
Victor Stinner8c62be82010-05-06 00:08:46 +00008374 if (!PyArg_ParseTuple(args, "i:WIFSIGNALED", &WAIT_STATUS_INT(status)))
8375 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008376
Victor Stinner8c62be82010-05-06 00:08:46 +00008377 return PyBool_FromLong(WIFSIGNALED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008378}
8379#endif /* WIFSIGNALED */
8380
8381#ifdef WIFEXITED
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008382PyDoc_STRVAR(posix_WIFEXITED__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008383"WIFEXITED(status) -> bool\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008384Return true if the process returning 'status' exited using the exit()\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008385system call.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008386
8387static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008388posix_WIFEXITED(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008389{
Victor Stinner8c62be82010-05-06 00:08:46 +00008390 WAIT_TYPE status;
8391 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008392
Victor Stinner8c62be82010-05-06 00:08:46 +00008393 if (!PyArg_ParseTuple(args, "i:WIFEXITED", &WAIT_STATUS_INT(status)))
8394 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008395
Victor Stinner8c62be82010-05-06 00:08:46 +00008396 return PyBool_FromLong(WIFEXITED(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008397}
8398#endif /* WIFEXITED */
8399
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008400#ifdef WEXITSTATUS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008401PyDoc_STRVAR(posix_WEXITSTATUS__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008402"WEXITSTATUS(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008403Return the process return code from 'status'.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008404
8405static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008406posix_WEXITSTATUS(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008407{
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 WAIT_TYPE status;
8409 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008410
Victor Stinner8c62be82010-05-06 00:08:46 +00008411 if (!PyArg_ParseTuple(args, "i:WEXITSTATUS", &WAIT_STATUS_INT(status)))
8412 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008413
Victor Stinner8c62be82010-05-06 00:08:46 +00008414 return Py_BuildValue("i", WEXITSTATUS(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008415}
8416#endif /* WEXITSTATUS */
8417
8418#ifdef WTERMSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008419PyDoc_STRVAR(posix_WTERMSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008420"WTERMSIG(status) -> integer\n\n\
Fred Drake7e3535c1999-02-02 16:37:11 +00008421Return the signal that terminated the process that provided the 'status'\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008422value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008423
8424static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008425posix_WTERMSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008426{
Victor Stinner8c62be82010-05-06 00:08:46 +00008427 WAIT_TYPE status;
8428 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008429
Victor Stinner8c62be82010-05-06 00:08:46 +00008430 if (!PyArg_ParseTuple(args, "i:WTERMSIG", &WAIT_STATUS_INT(status)))
8431 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008432
Victor Stinner8c62be82010-05-06 00:08:46 +00008433 return Py_BuildValue("i", WTERMSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008434}
8435#endif /* WTERMSIG */
8436
8437#ifdef WSTOPSIG
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008438PyDoc_STRVAR(posix_WSTOPSIG__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008439"WSTOPSIG(status) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008440Return the signal that stopped the process that provided\n\
8441the 'status' value.");
Guido van Rossumc9641791998-08-04 15:26:23 +00008442
8443static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008444posix_WSTOPSIG(PyObject *self, PyObject *args)
Guido van Rossumc9641791998-08-04 15:26:23 +00008445{
Victor Stinner8c62be82010-05-06 00:08:46 +00008446 WAIT_TYPE status;
8447 WAIT_STATUS_INT(status) = 0;
Tim Peters5aa91602002-01-30 05:46:57 +00008448
Victor Stinner8c62be82010-05-06 00:08:46 +00008449 if (!PyArg_ParseTuple(args, "i:WSTOPSIG", &WAIT_STATUS_INT(status)))
8450 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +00008451
Victor Stinner8c62be82010-05-06 00:08:46 +00008452 return Py_BuildValue("i", WSTOPSIG(status));
Guido van Rossumc9641791998-08-04 15:26:23 +00008453}
8454#endif /* WSTOPSIG */
8455
8456#endif /* HAVE_SYS_WAIT_H */
8457
8458
Thomas Wouters477c8d52006-05-27 19:21:47 +00008459#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +00008460#ifdef _SCO_DS
8461/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
8462 needed definitions in sys/statvfs.h */
8463#define _SVID3
8464#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00008465#include <sys/statvfs.h>
8466
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008467static PyObject*
8468_pystatvfs_fromstructstatvfs(struct statvfs st) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008469 PyObject *v = PyStructSequence_New(&StatVFSResultType);
8470 if (v == NULL)
8471 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008472
8473#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +00008474 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8475 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8476 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
8477 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
8478 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
8479 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
8480 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
8481 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
8482 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8483 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008484#else
Victor Stinner8c62be82010-05-06 00:08:46 +00008485 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
8486 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
8487 PyStructSequence_SET_ITEM(v, 2,
8488 PyLong_FromLongLong((PY_LONG_LONG) st.f_blocks));
8489 PyStructSequence_SET_ITEM(v, 3,
8490 PyLong_FromLongLong((PY_LONG_LONG) st.f_bfree));
8491 PyStructSequence_SET_ITEM(v, 4,
8492 PyLong_FromLongLong((PY_LONG_LONG) st.f_bavail));
8493 PyStructSequence_SET_ITEM(v, 5,
8494 PyLong_FromLongLong((PY_LONG_LONG) st.f_files));
8495 PyStructSequence_SET_ITEM(v, 6,
8496 PyLong_FromLongLong((PY_LONG_LONG) st.f_ffree));
8497 PyStructSequence_SET_ITEM(v, 7,
8498 PyLong_FromLongLong((PY_LONG_LONG) st.f_favail));
8499 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
8500 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008501#endif
8502
Victor Stinner8c62be82010-05-06 00:08:46 +00008503 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008504}
8505
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008506PyDoc_STRVAR(posix_fstatvfs__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008507"fstatvfs(fd) -> statvfs result\n\n\
Larry Hastings9cf065c2012-06-22 16:30:09 -07008508Perform an fstatvfs system call on the given fd.\n\
8509Equivalent to statvfs(fd).");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008510
8511static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008512posix_fstatvfs(PyObject *self, PyObject *args)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008513{
Victor Stinner8c62be82010-05-06 00:08:46 +00008514 int fd, res;
8515 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008516
Victor Stinner8c62be82010-05-06 00:08:46 +00008517 if (!PyArg_ParseTuple(args, "i:fstatvfs", &fd))
8518 return NULL;
8519 Py_BEGIN_ALLOW_THREADS
8520 res = fstatvfs(fd, &st);
8521 Py_END_ALLOW_THREADS
8522 if (res != 0)
8523 return posix_error();
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008524
Victor Stinner8c62be82010-05-06 00:08:46 +00008525 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +00008526}
Thomas Wouters477c8d52006-05-27 19:21:47 +00008527#endif /* HAVE_FSTATVFS && HAVE_SYS_STATVFS_H */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008528
8529
Thomas Wouters477c8d52006-05-27 19:21:47 +00008530#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008531#include <sys/statvfs.h>
8532
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008533PyDoc_STRVAR(posix_statvfs__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07008534"statvfs(path)\n\n\
8535Perform a statvfs system call on the given path.\n\
8536\n\
8537path may always be specified as a string.\n\
8538On some platforms, path may also be specified as an open file descriptor.\n\
8539 If this functionality is unavailable, using it raises an exception.");
Guido van Rossum94f6f721999-01-06 18:42:14 +00008540
8541static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07008542posix_statvfs(PyObject *self, PyObject *args, PyObject *kwargs)
Guido van Rossum94f6f721999-01-06 18:42:14 +00008543{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008544 static char *keywords[] = {"path", NULL};
8545 path_t path;
8546 int result;
8547 PyObject *return_value = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008548 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00008549
Larry Hastings9cf065c2012-06-22 16:30:09 -07008550 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008551 path.function_name = "statvfs";
Larry Hastings9cf065c2012-06-22 16:30:09 -07008552#ifdef HAVE_FSTATVFS
8553 path.allow_fd = 1;
8554#endif
8555 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&:statvfs", keywords,
8556 path_converter, &path
8557 ))
8558 return NULL;
8559
8560 Py_BEGIN_ALLOW_THREADS
8561#ifdef HAVE_FSTATVFS
8562 if (path.fd != -1) {
8563#ifdef __APPLE__
8564 /* handle weak-linking on Mac OS X 10.3 */
8565 if (fstatvfs == NULL) {
8566 fd_specified("statvfs", path.fd);
8567 goto exit;
8568 }
8569#endif
8570 result = fstatvfs(path.fd, &st);
8571 }
8572 else
8573#endif
8574 result = statvfs(path.narrow, &st);
8575 Py_END_ALLOW_THREADS
8576
8577 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +01008578 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008579 goto exit;
8580 }
8581
8582 return_value = _pystatvfs_fromstructstatvfs(st);
8583
8584exit:
8585 path_cleanup(&path);
8586 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00008587}
8588#endif /* HAVE_STATVFS */
8589
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008590#ifdef MS_WINDOWS
8591PyDoc_STRVAR(win32__getdiskusage__doc__,
8592"_getdiskusage(path) -> (total, free)\n\n\
8593Return disk usage statistics about the given path as (total, free) tuple.");
8594
8595static PyObject *
8596win32__getdiskusage(PyObject *self, PyObject *args)
8597{
8598 BOOL retval;
8599 ULARGE_INTEGER _, total, free;
Victor Stinner6139c1b2011-11-09 22:14:14 +01008600 const wchar_t *path;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008601
Victor Stinner6139c1b2011-11-09 22:14:14 +01008602 if (! PyArg_ParseTuple(args, "u", &path))
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008603 return NULL;
8604
8605 Py_BEGIN_ALLOW_THREADS
Victor Stinner6139c1b2011-11-09 22:14:14 +01008606 retval = GetDiskFreeSpaceExW(path, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +02008607 Py_END_ALLOW_THREADS
8608 if (retval == 0)
8609 return PyErr_SetFromWindowsErr(0);
8610
8611 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
8612}
8613#endif
8614
8615
Fred Drakec9680921999-12-13 16:37:25 +00008616/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
8617 * It maps strings representing configuration variable names to
8618 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +00008619 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +00008620 * rarely-used constants. There are three separate tables that use
8621 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +00008622 *
8623 * This code is always included, even if none of the interfaces that
8624 * need it are included. The #if hackery needed to avoid it would be
8625 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +00008626 */
8627struct constdef {
8628 char *name;
8629 long value;
8630};
8631
Fred Drake12c6e2d1999-12-14 21:25:03 +00008632static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008633conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +00008634 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +00008635{
Christian Heimes217cfd12007-12-02 14:31:20 +00008636 if (PyLong_Check(arg)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008637 *valuep = PyLong_AS_LONG(arg);
8638 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +00008639 }
Guido van Rossumbce56a62007-05-10 18:04:33 +00008640 else {
Stefan Krah0e803b32010-11-26 16:16:47 +00008641 /* look up the value in the table using a binary search */
8642 size_t lo = 0;
8643 size_t mid;
8644 size_t hi = tablesize;
8645 int cmp;
8646 const char *confname;
8647 if (!PyUnicode_Check(arg)) {
8648 PyErr_SetString(PyExc_TypeError,
8649 "configuration names must be strings or integers");
8650 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008651 }
Stefan Krah0e803b32010-11-26 16:16:47 +00008652 confname = _PyUnicode_AsString(arg);
8653 if (confname == NULL)
8654 return 0;
8655 while (lo < hi) {
8656 mid = (lo + hi) / 2;
8657 cmp = strcmp(confname, table[mid].name);
8658 if (cmp < 0)
8659 hi = mid;
8660 else if (cmp > 0)
8661 lo = mid + 1;
8662 else {
8663 *valuep = table[mid].value;
8664 return 1;
8665 }
8666 }
8667 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
8668 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008669 }
Fred Drake12c6e2d1999-12-14 21:25:03 +00008670}
8671
8672
8673#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
8674static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008675#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008676 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00008677#endif
8678#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008679 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +00008680#endif
Fred Drakec9680921999-12-13 16:37:25 +00008681#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008682 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008683#endif
8684#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +00008685 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +00008686#endif
8687#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +00008688 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +00008689#endif
8690#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +00008691 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +00008692#endif
8693#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008694 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008695#endif
8696#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +00008697 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +00008698#endif
8699#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +00008700 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +00008701#endif
8702#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008703 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008704#endif
8705#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +00008706 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +00008707#endif
8708#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00008709 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00008710#endif
8711#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008712 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +00008713#endif
8714#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008715 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008716#endif
8717#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +00008718 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +00008719#endif
8720#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00008721 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +00008722#endif
8723#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +00008724 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +00008725#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +00008726#ifdef _PC_ACL_ENABLED
8727 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
8728#endif
8729#ifdef _PC_MIN_HOLE_SIZE
8730 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
8731#endif
8732#ifdef _PC_ALLOC_SIZE_MIN
8733 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
8734#endif
8735#ifdef _PC_REC_INCR_XFER_SIZE
8736 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
8737#endif
8738#ifdef _PC_REC_MAX_XFER_SIZE
8739 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
8740#endif
8741#ifdef _PC_REC_MIN_XFER_SIZE
8742 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
8743#endif
8744#ifdef _PC_REC_XFER_ALIGN
8745 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
8746#endif
8747#ifdef _PC_SYMLINK_MAX
8748 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
8749#endif
8750#ifdef _PC_XATTR_ENABLED
8751 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
8752#endif
8753#ifdef _PC_XATTR_EXISTS
8754 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
8755#endif
8756#ifdef _PC_TIMESTAMP_RESOLUTION
8757 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
8758#endif
Fred Drakec9680921999-12-13 16:37:25 +00008759};
8760
Fred Drakec9680921999-12-13 16:37:25 +00008761static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008762conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00008763{
8764 return conv_confname(arg, valuep, posix_constants_pathconf,
8765 sizeof(posix_constants_pathconf)
8766 / sizeof(struct constdef));
8767}
8768#endif
8769
8770#ifdef HAVE_FPATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008771PyDoc_STRVAR(posix_fpathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008772"fpathconf(fd, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008773Return the configuration limit name for the file descriptor fd.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008774If there is no limit, return -1.");
Fred Drakec9680921999-12-13 16:37:25 +00008775
8776static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00008777posix_fpathconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00008778{
8779 PyObject *result = NULL;
8780 int name, fd;
8781
Fred Drake12c6e2d1999-12-14 21:25:03 +00008782 if (PyArg_ParseTuple(args, "iO&:fpathconf", &fd,
8783 conv_path_confname, &name)) {
Stefan Krah0e803b32010-11-26 16:16:47 +00008784 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008785
Stefan Krah0e803b32010-11-26 16:16:47 +00008786 errno = 0;
8787 limit = fpathconf(fd, name);
8788 if (limit == -1 && errno != 0)
8789 posix_error();
8790 else
8791 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008792 }
8793 return result;
8794}
8795#endif
8796
8797
8798#ifdef HAVE_PATHCONF
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00008799PyDoc_STRVAR(posix_pathconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00008800"pathconf(path, name) -> integer\n\n\
Fred Drakec9680921999-12-13 16:37:25 +00008801Return the configuration limit name for the file or directory path.\n\
Georg Brandl306336b2012-06-24 12:55:33 +02008802If there is no limit, return -1.\n\
8803On some platforms, path may also be specified as an open file descriptor.\n\
8804 If this functionality is unavailable, using it raises an exception.");
Fred Drakec9680921999-12-13 16:37:25 +00008805
8806static PyObject *
Georg Brandl306336b2012-06-24 12:55:33 +02008807posix_pathconf(PyObject *self, PyObject *args, PyObject *kwargs)
Fred Drakec9680921999-12-13 16:37:25 +00008808{
Georg Brandl306336b2012-06-24 12:55:33 +02008809 path_t path;
Fred Drakec9680921999-12-13 16:37:25 +00008810 PyObject *result = NULL;
8811 int name;
Georg Brandl306336b2012-06-24 12:55:33 +02008812 static char *keywords[] = {"path", "name", NULL};
Fred Drakec9680921999-12-13 16:37:25 +00008813
Georg Brandl306336b2012-06-24 12:55:33 +02008814 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01008815 path.function_name = "pathconf";
Georg Brandl306336b2012-06-24 12:55:33 +02008816#ifdef HAVE_FPATHCONF
8817 path.allow_fd = 1;
8818#endif
8819 if (PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&:pathconf", keywords,
8820 path_converter, &path,
8821 conv_path_confname, &name)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008822 long limit;
Fred Drakec9680921999-12-13 16:37:25 +00008823
Victor Stinner8c62be82010-05-06 00:08:46 +00008824 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +02008825#ifdef HAVE_FPATHCONF
8826 if (path.fd != -1)
8827 limit = fpathconf(path.fd, name);
8828 else
8829#endif
8830 limit = pathconf(path.narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +00008831 if (limit == -1 && errno != 0) {
8832 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +00008833 /* could be a path or name problem */
8834 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +00008835 else
Victor Stinner292c8352012-10-30 02:17:38 +01008836 result = path_error(&path);
Victor Stinner8c62be82010-05-06 00:08:46 +00008837 }
8838 else
8839 result = PyLong_FromLong(limit);
Fred Drakec9680921999-12-13 16:37:25 +00008840 }
Georg Brandl306336b2012-06-24 12:55:33 +02008841 path_cleanup(&path);
Fred Drakec9680921999-12-13 16:37:25 +00008842 return result;
8843}
8844#endif
8845
8846#ifdef HAVE_CONFSTR
8847static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +00008848#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +00008849 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +00008850#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +00008851#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008852 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008853#endif
8854#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008855 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +00008856#endif
Fred Draked86ed291999-12-15 15:34:33 +00008857#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008859#endif
8860#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008861 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008862#endif
8863#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008864 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008865#endif
8866#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008867 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008868#endif
Fred Drakec9680921999-12-13 16:37:25 +00008869#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008870 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008871#endif
8872#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008873 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008874#endif
8875#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008876 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008877#endif
8878#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008879 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008880#endif
8881#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008882 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008883#endif
8884#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008885 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008886#endif
8887#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008888 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008889#endif
8890#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008891 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008892#endif
Fred Draked86ed291999-12-15 15:34:33 +00008893#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +00008894 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +00008895#endif
Fred Drakec9680921999-12-13 16:37:25 +00008896#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +00008897 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +00008898#endif
Fred Draked86ed291999-12-15 15:34:33 +00008899#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008900 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +00008901#endif
8902#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008903 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +00008904#endif
8905#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008906 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +00008907#endif
8908#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00008909 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +00008910#endif
Fred Drakec9680921999-12-13 16:37:25 +00008911#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008913#endif
8914#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008915 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008916#endif
8917#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008918 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008919#endif
8920#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008921 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008922#endif
8923#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008924 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008925#endif
8926#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008927 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008928#endif
8929#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008930 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008931#endif
8932#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008933 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008934#endif
8935#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008936 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008937#endif
8938#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008940#endif
8941#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008942 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008943#endif
8944#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008945 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008946#endif
8947#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008948 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008949#endif
8950#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008951 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008952#endif
8953#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +00008954 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +00008955#endif
8956#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00008957 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +00008958#endif
Fred Draked86ed291999-12-15 15:34:33 +00008959#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008960 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008961#endif
8962#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +00008963 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +00008964#endif
8965#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +00008966 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +00008967#endif
8968#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008969 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008970#endif
8971#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008972 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008973#endif
8974#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +00008975 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +00008976#endif
8977#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00008978 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +00008979#endif
8980#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +00008981 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +00008982#endif
8983#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +00008984 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +00008985#endif
8986#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +00008987 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +00008988#endif
8989#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +00008990 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +00008991#endif
8992#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +00008993 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +00008994#endif
8995#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +00008996 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +00008997#endif
Fred Drakec9680921999-12-13 16:37:25 +00008998};
8999
9000static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009001conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009002{
9003 return conv_confname(arg, valuep, posix_constants_confstr,
9004 sizeof(posix_constants_confstr)
9005 / sizeof(struct constdef));
9006}
9007
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009008PyDoc_STRVAR(posix_confstr__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009009"confstr(name) -> string\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009010Return a string-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009011
9012static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009013posix_confstr(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009014{
9015 PyObject *result = NULL;
9016 int name;
Victor Stinnercb043522010-09-10 23:49:04 +00009017 char buffer[255];
Stefan Krah99439262010-11-26 12:58:05 +00009018 int len;
Fred Drakec9680921999-12-13 16:37:25 +00009019
Victor Stinnercb043522010-09-10 23:49:04 +00009020 if (!PyArg_ParseTuple(args, "O&:confstr", conv_confstr_confname, &name))
9021 return NULL;
9022
9023 errno = 0;
9024 len = confstr(name, buffer, sizeof(buffer));
9025 if (len == 0) {
9026 if (errno) {
9027 posix_error();
9028 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +00009029 }
9030 else {
Victor Stinnercb043522010-09-10 23:49:04 +00009031 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +00009032 }
9033 }
Victor Stinnercb043522010-09-10 23:49:04 +00009034
9035 if ((unsigned int)len >= sizeof(buffer)) {
9036 char *buf = PyMem_Malloc(len);
9037 if (buf == NULL)
9038 return PyErr_NoMemory();
9039 confstr(name, buf, len);
9040 result = PyUnicode_DecodeFSDefaultAndSize(buf, len-1);
9041 PyMem_Free(buf);
9042 }
9043 else
9044 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +00009045 return result;
9046}
9047#endif
9048
9049
9050#ifdef HAVE_SYSCONF
9051static struct constdef posix_constants_sysconf[] = {
9052#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +00009053 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +00009054#endif
9055#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +00009056 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +00009057#endif
9058#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009059 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009060#endif
9061#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009062 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009063#endif
9064#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009065 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009066#endif
9067#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +00009068 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +00009069#endif
9070#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +00009071 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +00009072#endif
9073#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +00009074 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +00009075#endif
9076#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +00009077 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +00009078#endif
9079#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009080 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009081#endif
Fred Draked86ed291999-12-15 15:34:33 +00009082#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009083 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +00009084#endif
9085#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +00009086 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +00009087#endif
Fred Drakec9680921999-12-13 16:37:25 +00009088#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009089 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009090#endif
Fred Drakec9680921999-12-13 16:37:25 +00009091#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009092 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009093#endif
9094#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009095 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009096#endif
9097#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009098 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009099#endif
9100#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009101 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009102#endif
9103#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009104 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009105#endif
Fred Draked86ed291999-12-15 15:34:33 +00009106#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009107 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +00009108#endif
Fred Drakec9680921999-12-13 16:37:25 +00009109#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009110 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009111#endif
9112#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009113 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009114#endif
9115#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009116 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009117#endif
9118#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009119 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009120#endif
9121#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009122 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009123#endif
Fred Draked86ed291999-12-15 15:34:33 +00009124#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +00009125 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +00009126#endif
Fred Drakec9680921999-12-13 16:37:25 +00009127#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009128 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009129#endif
9130#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009131 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009132#endif
9133#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009134 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009135#endif
9136#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009137 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009138#endif
9139#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009140 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009141#endif
9142#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009143 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +00009144#endif
9145#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009146 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009147#endif
9148#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009149 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009150#endif
9151#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009152 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009153#endif
9154#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009155 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009156#endif
9157#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009158 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009159#endif
9160#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009161 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009162#endif
9163#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009164 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009165#endif
9166#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009167 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009168#endif
9169#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009170 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009171#endif
9172#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009173 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009174#endif
9175#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +00009176 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +00009177#endif
9178#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009179 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009180#endif
9181#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009182 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009183#endif
9184#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +00009185 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +00009186#endif
9187#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009188 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +00009189#endif
9190#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009191 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +00009192#endif
9193#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +00009194 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +00009195#endif
Fred Draked86ed291999-12-15 15:34:33 +00009196#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +00009197 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +00009198#endif
Fred Drakec9680921999-12-13 16:37:25 +00009199#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009200 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009201#endif
9202#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009203 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009204#endif
9205#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009206 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009207#endif
Fred Draked86ed291999-12-15 15:34:33 +00009208#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009209 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +00009210#endif
Fred Drakec9680921999-12-13 16:37:25 +00009211#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +00009212 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +00009213#endif
Fred Draked86ed291999-12-15 15:34:33 +00009214#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009215 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +00009216#endif
9217#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +00009218 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +00009219#endif
Fred Drakec9680921999-12-13 16:37:25 +00009220#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009221 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009222#endif
9223#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009224 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009225#endif
9226#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009227 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009228#endif
9229#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009230 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009231#endif
Fred Draked86ed291999-12-15 15:34:33 +00009232#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +00009233 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +00009234#endif
Fred Drakec9680921999-12-13 16:37:25 +00009235#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +00009236 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +00009237#endif
9238#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +00009239 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +00009240#endif
9241#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009242 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009243#endif
9244#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +00009245 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +00009246#endif
9247#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +00009248 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +00009249#endif
9250#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +00009251 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +00009252#endif
9253#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +00009254 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +00009255#endif
Fred Draked86ed291999-12-15 15:34:33 +00009256#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +00009257 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +00009258#endif
Fred Drakec9680921999-12-13 16:37:25 +00009259#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009260 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009261#endif
9262#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009263 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009264#endif
Fred Draked86ed291999-12-15 15:34:33 +00009265#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009266 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +00009267#endif
Fred Drakec9680921999-12-13 16:37:25 +00009268#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009269 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009270#endif
9271#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009272 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009273#endif
9274#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009275 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009276#endif
9277#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009278 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009279#endif
9280#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009281 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009282#endif
9283#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009284 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009285#endif
9286#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009287 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +00009288#endif
9289#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009290 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +00009291#endif
9292#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009293 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +00009294#endif
Fred Draked86ed291999-12-15 15:34:33 +00009295#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +00009296 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +00009297#endif
9298#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +00009299 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +00009300#endif
Fred Drakec9680921999-12-13 16:37:25 +00009301#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +00009302 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +00009303#endif
9304#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009305 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009306#endif
9307#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009308 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009309#endif
9310#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009311 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009312#endif
9313#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009314 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009315#endif
9316#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +00009317 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +00009318#endif
9319#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +00009320 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +00009321#endif
9322#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +00009323 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +00009324#endif
9325#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009326 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +00009327#endif
9328#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +00009329 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +00009330#endif
9331#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +00009332 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +00009333#endif
9334#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009335 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +00009336#endif
9337#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009338 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +00009339#endif
9340#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +00009341 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +00009342#endif
9343#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +00009344 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +00009345#endif
9346#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +00009347 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +00009348#endif
9349#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +00009350 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +00009351#endif
9352#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009353 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009354#endif
9355#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009356 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009357#endif
9358#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +00009359 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +00009360#endif
9361#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009362 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009363#endif
9364#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009365 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009366#endif
9367#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +00009368 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +00009369#endif
9370#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009371 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009372#endif
9373#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009374 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009375#endif
9376#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009377 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +00009378#endif
9379#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +00009380 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +00009381#endif
9382#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009383 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009384#endif
9385#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009386 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009387#endif
9388#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +00009389 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +00009390#endif
9391#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009392 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009393#endif
9394#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009395 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009396#endif
9397#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009398 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009399#endif
9400#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009401 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009402#endif
9403#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009404 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009405#endif
Fred Draked86ed291999-12-15 15:34:33 +00009406#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +00009407 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +00009408#endif
Fred Drakec9680921999-12-13 16:37:25 +00009409#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +00009410 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +00009411#endif
9412#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009413 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009414#endif
9415#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +00009416 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +00009417#endif
9418#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009419 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009420#endif
9421#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +00009422 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +00009423#endif
9424#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009425 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009426#endif
9427#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +00009428 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +00009429#endif
9430#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00009431 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +00009432#endif
9433#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009434 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009435#endif
9436#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009437 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009438#endif
9439#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +00009440 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +00009441#endif
9442#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009443 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +00009444#endif
9445#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +00009446 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +00009447#endif
9448#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +00009449 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +00009450#endif
9451#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +00009452 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +00009453#endif
9454#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +00009455 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +00009456#endif
9457#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009458 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009459#endif
9460#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +00009461 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +00009462#endif
9463#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009464 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009465#endif
9466#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009467 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009468#endif
9469#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009470 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009471#endif
9472#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009473 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009474#endif
9475#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009476 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009477#endif
9478#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009479 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009480#endif
9481#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +00009482 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +00009483#endif
9484#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009485 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009486#endif
9487#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +00009488 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +00009489#endif
9490#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009491 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009492#endif
9493#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +00009494 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +00009495#endif
9496#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +00009497 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +00009498#endif
9499#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009500 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009501#endif
9502#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +00009503 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +00009504#endif
9505#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +00009506 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +00009507#endif
9508#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +00009509 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +00009510#endif
9511#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +00009512 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +00009513#endif
9514#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +00009515 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +00009516#endif
9517#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00009518 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +00009519#endif
9520#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00009521 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +00009522#endif
9523#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +00009524 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +00009525#endif
9526#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +00009527 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +00009528#endif
9529#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009530 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009531#endif
9532#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +00009533 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +00009534#endif
9535#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +00009536 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +00009537#endif
9538#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +00009539 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +00009540#endif
9541#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +00009542 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +00009543#endif
9544};
9545
9546static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009547conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +00009548{
9549 return conv_confname(arg, valuep, posix_constants_sysconf,
9550 sizeof(posix_constants_sysconf)
9551 / sizeof(struct constdef));
9552}
9553
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009554PyDoc_STRVAR(posix_sysconf__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009555"sysconf(name) -> integer\n\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009556Return an integer-valued system configuration variable.");
Fred Drakec9680921999-12-13 16:37:25 +00009557
9558static PyObject *
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009559posix_sysconf(PyObject *self, PyObject *args)
Fred Drakec9680921999-12-13 16:37:25 +00009560{
9561 PyObject *result = NULL;
9562 int name;
9563
9564 if (PyArg_ParseTuple(args, "O&:sysconf", conv_sysconf_confname, &name)) {
Victor Stinner6fdd7b82013-05-16 22:26:29 +02009565 long value;
Fred Drakec9680921999-12-13 16:37:25 +00009566
9567 errno = 0;
9568 value = sysconf(name);
9569 if (value == -1 && errno != 0)
9570 posix_error();
9571 else
Christian Heimes217cfd12007-12-02 14:31:20 +00009572 result = PyLong_FromLong(value);
Fred Drakec9680921999-12-13 16:37:25 +00009573 }
9574 return result;
9575}
9576#endif
9577
9578
Fred Drakebec628d1999-12-15 18:31:10 +00009579/* This code is used to ensure that the tables of configuration value names
9580 * are in sorted order as required by conv_confname(), and also to build the
9581 * the exported dictionaries that are used to publish information about the
9582 * names available on the host platform.
9583 *
9584 * Sorting the table at runtime ensures that the table is properly ordered
9585 * when used, even for platforms we're not able to test on. It also makes
9586 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +00009587 */
Fred Drakebec628d1999-12-15 18:31:10 +00009588
9589static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009590cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +00009591{
9592 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009593 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +00009594 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +00009595 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +00009596
9597 return strcmp(c1->name, c2->name);
9598}
9599
9600static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +00009601setup_confname_table(struct constdef *table, size_t tablesize,
Victor Stinner8c62be82010-05-06 00:08:46 +00009602 char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009603{
Fred Drakebec628d1999-12-15 18:31:10 +00009604 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +00009605 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +00009606
9607 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
9608 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +00009609 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00009610 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009611
Barry Warsaw3155db32000-04-13 15:20:40 +00009612 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009613 PyObject *o = PyLong_FromLong(table[i].value);
9614 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
9615 Py_XDECREF(o);
9616 Py_DECREF(d);
9617 return -1;
9618 }
9619 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +00009620 }
Fred Drake4d1e64b2002-04-15 19:40:07 +00009621 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +00009622}
9623
Fred Drakebec628d1999-12-15 18:31:10 +00009624/* Return -1 on failure, 0 on success. */
9625static int
Fred Drake4d1e64b2002-04-15 19:40:07 +00009626setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +00009627{
9628#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +00009629 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +00009630 sizeof(posix_constants_pathconf)
9631 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009632 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009633 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009634#endif
9635#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +00009636 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +00009637 sizeof(posix_constants_confstr)
9638 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009639 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009640 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009641#endif
9642#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +00009643 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +00009644 sizeof(posix_constants_sysconf)
9645 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +00009646 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +00009647 return -1;
Fred Draked86ed291999-12-15 15:34:33 +00009648#endif
Fred Drakebec628d1999-12-15 18:31:10 +00009649 return 0;
Fred Draked86ed291999-12-15 15:34:33 +00009650}
Fred Draked86ed291999-12-15 15:34:33 +00009651
9652
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009653PyDoc_STRVAR(posix_abort__doc__,
Fred Drakef7ce04d2002-06-20 18:31:21 +00009654"abort() -> does not return!\n\n\
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009655Abort the interpreter immediately. This 'dumps core' or otherwise fails\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009656in the hardest way possible on the hosting operating system.");
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009657
9658static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009659posix_abort(PyObject *self, PyObject *noargs)
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009660{
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009661 abort();
9662 /*NOTREACHED*/
9663 Py_FatalError("abort() called from Python code didn't abort!");
9664 return NULL;
9665}
Fred Drakebec628d1999-12-15 18:31:10 +00009666
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00009667#ifdef MS_WINDOWS
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009668PyDoc_STRVAR(win32_startfile__doc__,
Georg Brandlf4f44152006-02-18 22:29:33 +00009669"startfile(filepath [, operation]) - Start a file with its associated\n\
9670application.\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009671\n\
Georg Brandlf4f44152006-02-18 22:29:33 +00009672When \"operation\" is not specified or \"open\", this acts like\n\
9673double-clicking the file in Explorer, or giving the file name as an\n\
9674argument to the DOS \"start\" command: the file is opened with whatever\n\
9675application (if any) its extension is associated.\n\
9676When another \"operation\" is given, it specifies what should be done with\n\
9677the file. A typical operation is \"print\".\n\
Tim Petersf58a7aa2000-09-22 10:05:54 +00009678\n\
9679startfile returns as soon as the associated application is launched.\n\
9680There is no option to wait for the application to close, and no way\n\
9681to retrieve the application's exit status.\n\
9682\n\
9683The filepath is relative to the current directory. If you want to use\n\
9684an absolute path, make sure the first character is not a slash (\"/\");\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00009685the underlying Win32 ShellExecute function doesn't work if it is.");
Tim Petersf58a7aa2000-09-22 10:05:54 +00009686
9687static PyObject *
9688win32_startfile(PyObject *self, PyObject *args)
9689{
Victor Stinner8c62be82010-05-06 00:08:46 +00009690 PyObject *ofilepath;
9691 char *filepath;
9692 char *operation = NULL;
Victor Stinnereb5657a2011-09-30 01:44:27 +02009693 wchar_t *wpath, *woperation;
Victor Stinner8c62be82010-05-06 00:08:46 +00009694 HINSTANCE rc;
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00009695
Victor Stinnereb5657a2011-09-30 01:44:27 +02009696 PyObject *unipath, *uoperation = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009697 if (!PyArg_ParseTuple(args, "U|s:startfile",
9698 &unipath, &operation)) {
9699 PyErr_Clear();
9700 goto normal;
9701 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009702
Victor Stinner8c62be82010-05-06 00:08:46 +00009703 if (operation) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009704 uoperation = PyUnicode_DecodeASCII(operation,
Victor Stinner8c62be82010-05-06 00:08:46 +00009705 strlen(operation), NULL);
Victor Stinnereb5657a2011-09-30 01:44:27 +02009706 if (!uoperation) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009707 PyErr_Clear();
9708 operation = NULL;
9709 goto normal;
9710 }
9711 }
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00009712
Victor Stinnereb5657a2011-09-30 01:44:27 +02009713 wpath = PyUnicode_AsUnicode(unipath);
9714 if (wpath == NULL)
9715 goto normal;
9716 if (uoperation) {
9717 woperation = PyUnicode_AsUnicode(uoperation);
9718 if (woperation == NULL)
9719 goto normal;
9720 }
9721 else
9722 woperation = NULL;
9723
Victor Stinner8c62be82010-05-06 00:08:46 +00009724 Py_BEGIN_ALLOW_THREADS
Victor Stinnereb5657a2011-09-30 01:44:27 +02009725 rc = ShellExecuteW((HWND)0, woperation, wpath,
9726 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +00009727 Py_END_ALLOW_THREADS
9728
Victor Stinnereb5657a2011-09-30 01:44:27 +02009729 Py_XDECREF(uoperation);
Victor Stinner8c62be82010-05-06 00:08:46 +00009730 if (rc <= (HINSTANCE)32) {
Victor Stinnereb5657a2011-09-30 01:44:27 +02009731 win32_error_object("startfile", unipath);
9732 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009733 }
9734 Py_INCREF(Py_None);
9735 return Py_None;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009736
9737normal:
Victor Stinner8c62be82010-05-06 00:08:46 +00009738 if (!PyArg_ParseTuple(args, "O&|s:startfile",
9739 PyUnicode_FSConverter, &ofilepath,
9740 &operation))
9741 return NULL;
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01009742 if (win32_warn_bytes_api()) {
9743 Py_DECREF(ofilepath);
9744 return NULL;
9745 }
Victor Stinner8c62be82010-05-06 00:08:46 +00009746 filepath = PyBytes_AsString(ofilepath);
9747 Py_BEGIN_ALLOW_THREADS
9748 rc = ShellExecute((HWND)0, operation, filepath,
9749 NULL, NULL, SW_SHOWNORMAL);
9750 Py_END_ALLOW_THREADS
9751 if (rc <= (HINSTANCE)32) {
9752 PyObject *errval = win32_error("startfile", filepath);
9753 Py_DECREF(ofilepath);
9754 return errval;
9755 }
9756 Py_DECREF(ofilepath);
9757 Py_INCREF(Py_None);
9758 return Py_None;
Tim Petersf58a7aa2000-09-22 10:05:54 +00009759}
9760#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +00009761
Martin v. Löwis438b5342002-12-27 10:16:42 +00009762#ifdef HAVE_GETLOADAVG
9763PyDoc_STRVAR(posix_getloadavg__doc__,
9764"getloadavg() -> (float, float, float)\n\n\
9765Return the number of processes in the system run queue averaged over\n\
9766the last 1, 5, and 15 minutes or raises OSError if the load average\n\
9767was unobtainable");
9768
9769static PyObject *
Neal Norwitze241ce82003-02-17 18:17:05 +00009770posix_getloadavg(PyObject *self, PyObject *noargs)
Martin v. Löwis438b5342002-12-27 10:16:42 +00009771{
9772 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +00009773 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +00009774 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
9775 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +00009776 } else
Stefan Krah0e803b32010-11-26 16:16:47 +00009777 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +00009778}
9779#endif
9780
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009781PyDoc_STRVAR(device_encoding__doc__,
9782"device_encoding(fd) -> str\n\n\
9783Return a string describing the encoding of the device\n\
9784if the output is a terminal; else return None.");
9785
9786static PyObject *
9787device_encoding(PyObject *self, PyObject *args)
9788{
Victor Stinner8c62be82010-05-06 00:08:46 +00009789 int fd;
Brett Cannonefb00c02012-02-29 18:31:31 -05009790
Victor Stinner8c62be82010-05-06 00:08:46 +00009791 if (!PyArg_ParseTuple(args, "i:device_encoding", &fd))
9792 return NULL;
Brett Cannonefb00c02012-02-29 18:31:31 -05009793
9794 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +00009795}
9796
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009797#ifdef HAVE_SETRESUID
9798PyDoc_STRVAR(posix_setresuid__doc__,
9799"setresuid(ruid, euid, suid)\n\n\
9800Set the current process's real, effective, and saved user ids.");
9801
9802static PyObject*
9803posix_setresuid (PyObject *self, PyObject *args)
9804{
Victor Stinner8c62be82010-05-06 00:08:46 +00009805 /* We assume uid_t is no larger than a long. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009806 uid_t ruid, euid, suid;
9807 if (!PyArg_ParseTuple(args, "O&O&O&:setresuid",
9808 _Py_Uid_Converter, &ruid,
9809 _Py_Uid_Converter, &euid,
9810 _Py_Uid_Converter, &suid))
Victor Stinner8c62be82010-05-06 00:08:46 +00009811 return NULL;
9812 if (setresuid(ruid, euid, suid) < 0)
9813 return posix_error();
9814 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009815}
9816#endif
9817
9818#ifdef HAVE_SETRESGID
9819PyDoc_STRVAR(posix_setresgid__doc__,
9820"setresgid(rgid, egid, sgid)\n\n\
9821Set the current process's real, effective, and saved group ids.");
9822
9823static PyObject*
9824posix_setresgid (PyObject *self, PyObject *args)
9825{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009826 gid_t rgid, egid, sgid;
9827 if (!PyArg_ParseTuple(args, "O&O&O&:setresgid",
9828 _Py_Gid_Converter, &rgid,
9829 _Py_Gid_Converter, &egid,
9830 _Py_Gid_Converter, &sgid))
Victor Stinner8c62be82010-05-06 00:08:46 +00009831 return NULL;
9832 if (setresgid(rgid, egid, sgid) < 0)
9833 return posix_error();
9834 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009835}
9836#endif
9837
9838#ifdef HAVE_GETRESUID
9839PyDoc_STRVAR(posix_getresuid__doc__,
9840"getresuid() -> (ruid, euid, suid)\n\n\
9841Get tuple of the current process's real, effective, and saved user ids.");
9842
9843static PyObject*
9844posix_getresuid (PyObject *self, PyObject *noargs)
9845{
Victor Stinner8c62be82010-05-06 00:08:46 +00009846 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +00009847 if (getresuid(&ruid, &euid, &suid) < 0)
9848 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009849 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
9850 _PyLong_FromUid(euid),
9851 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009852}
9853#endif
9854
9855#ifdef HAVE_GETRESGID
9856PyDoc_STRVAR(posix_getresgid__doc__,
9857"getresgid() -> (rgid, egid, sgid)\n\n\
Georg Brandla9b51d22010-09-05 17:07:12 +00009858Get tuple of the current process's real, effective, and saved group ids.");
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009859
9860static PyObject*
9861posix_getresgid (PyObject *self, PyObject *noargs)
9862{
Victor Stinner8c62be82010-05-06 00:08:46 +00009863 uid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +00009864 if (getresgid(&rgid, &egid, &sgid) < 0)
9865 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02009866 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
9867 _PyLong_FromGid(egid),
9868 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +00009869}
9870#endif
9871
Benjamin Peterson9428d532011-09-14 11:45:52 -04009872#ifdef USE_XATTRS
Benjamin Peterson799bd802011-08-31 22:15:17 -04009873
Benjamin Peterson799bd802011-08-31 22:15:17 -04009874PyDoc_STRVAR(posix_getxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07009875"getxattr(path, attribute, *, follow_symlinks=True) -> value\n\n\
9876Return the value of extended attribute attribute on path.\n\
9877\n\
9878path may be either a string or an open file descriptor.\n\
9879If follow_symlinks is False, and the last element of the path is a symbolic\n\
9880 link, getxattr will examine the symbolic link itself instead of the file\n\
9881 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -04009882
9883static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07009884posix_getxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -04009885{
Larry Hastings9cf065c2012-06-22 16:30:09 -07009886 path_t path;
9887 path_t attribute;
9888 int follow_symlinks = 1;
9889 PyObject *buffer = NULL;
9890 int i;
9891 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -04009892
Larry Hastings9cf065c2012-06-22 16:30:09 -07009893 memset(&path, 0, sizeof(path));
9894 memset(&attribute, 0, sizeof(attribute));
Victor Stinner292c8352012-10-30 02:17:38 +01009895 path.function_name = "getxattr";
9896 attribute.function_name = "getxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -07009897 path.allow_fd = 1;
9898 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:getxattr", keywords,
9899 path_converter, &path,
9900 path_converter, &attribute,
9901 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -04009902 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -04009903
Larry Hastings9cf065c2012-06-22 16:30:09 -07009904 if (fd_and_follow_symlinks_invalid("getxattr", path.fd, follow_symlinks))
9905 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -04009906
Larry Hastings9cf065c2012-06-22 16:30:09 -07009907 for (i = 0; ; i++) {
9908 void *ptr;
9909 ssize_t result;
9910 static Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
9911 Py_ssize_t buffer_size = buffer_sizes[i];
9912 if (!buffer_size) {
Victor Stinner292c8352012-10-30 02:17:38 +01009913 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009914 goto exit;
9915 }
9916 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
9917 if (!buffer)
9918 goto exit;
9919 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -04009920
Larry Hastings9cf065c2012-06-22 16:30:09 -07009921 Py_BEGIN_ALLOW_THREADS;
9922 if (path.fd >= 0)
9923 result = fgetxattr(path.fd, attribute.narrow, ptr, buffer_size);
9924 else if (follow_symlinks)
9925 result = getxattr(path.narrow, attribute.narrow, ptr, buffer_size);
9926 else
9927 result = lgetxattr(path.narrow, attribute.narrow, ptr, buffer_size);
9928 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -04009929
Larry Hastings9cf065c2012-06-22 16:30:09 -07009930 if (result < 0) {
9931 Py_DECREF(buffer);
9932 buffer = NULL;
9933 if (errno == ERANGE)
9934 continue;
Victor Stinner292c8352012-10-30 02:17:38 +01009935 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07009936 goto exit;
9937 }
Benjamin Peterson799bd802011-08-31 22:15:17 -04009938
Larry Hastings9cf065c2012-06-22 16:30:09 -07009939 if (result != buffer_size) {
9940 /* Can only shrink. */
9941 _PyBytes_Resize(&buffer, result);
9942 }
9943 break;
9944 }
Benjamin Peterson799bd802011-08-31 22:15:17 -04009945
Larry Hastings9cf065c2012-06-22 16:30:09 -07009946exit:
9947 path_cleanup(&path);
9948 path_cleanup(&attribute);
9949 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -04009950}
9951
9952PyDoc_STRVAR(posix_setxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07009953"setxattr(path, attribute, value, flags=0, *, follow_symlinks=True)\n\n\
9954Set extended attribute attribute on path to value.\n\
9955path may be either a string or an open file descriptor.\n\
9956If follow_symlinks is False, and the last element of the path is a symbolic\n\
9957 link, setxattr will modify the symbolic link itself instead of the file\n\
9958 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -04009959
9960static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -07009961posix_setxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -04009962{
Larry Hastings9cf065c2012-06-22 16:30:09 -07009963 path_t path;
9964 path_t attribute;
9965 Py_buffer value;
9966 int flags = 0;
9967 int follow_symlinks = 1;
9968 int result;
9969 PyObject *return_value = NULL;
9970 static char *keywords[] = {"path", "attribute", "value",
9971 "flags", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -04009972
Larry Hastings9cf065c2012-06-22 16:30:09 -07009973 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +01009974 path.function_name = "setxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -07009975 path.allow_fd = 1;
9976 memset(&attribute, 0, sizeof(attribute));
9977 memset(&value, 0, sizeof(value));
9978 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&y*|i$p:setxattr",
9979 keywords,
9980 path_converter, &path,
9981 path_converter, &attribute,
9982 &value, &flags,
9983 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -04009984 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009985
9986 if (fd_and_follow_symlinks_invalid("setxattr", path.fd, follow_symlinks))
9987 goto exit;
9988
Benjamin Peterson799bd802011-08-31 22:15:17 -04009989 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009990 if (path.fd > -1)
9991 result = fsetxattr(path.fd, attribute.narrow,
9992 value.buf, value.len, flags);
9993 else if (follow_symlinks)
9994 result = setxattr(path.narrow, attribute.narrow,
9995 value.buf, value.len, flags);
9996 else
9997 result = lsetxattr(path.narrow, attribute.narrow,
9998 value.buf, value.len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -04009999 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010000
Larry Hastings9cf065c2012-06-22 16:30:09 -070010001 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +010010002 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010003 goto exit;
10004 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010005
Larry Hastings9cf065c2012-06-22 16:30:09 -070010006 return_value = Py_None;
10007 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010008
Larry Hastings9cf065c2012-06-22 16:30:09 -070010009exit:
10010 path_cleanup(&path);
10011 path_cleanup(&attribute);
10012 PyBuffer_Release(&value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010013
Larry Hastings9cf065c2012-06-22 16:30:09 -070010014 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010015}
10016
10017PyDoc_STRVAR(posix_removexattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010018"removexattr(path, attribute, *, follow_symlinks=True)\n\n\
10019Remove extended attribute attribute on path.\n\
10020path may be either a string or an open file descriptor.\n\
10021If follow_symlinks is False, and the last element of the path is a symbolic\n\
10022 link, removexattr will modify the symbolic link itself instead of the file\n\
10023 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010024
10025static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010026posix_removexattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010027{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010028 path_t path;
10029 path_t attribute;
10030 int follow_symlinks = 1;
10031 int result;
10032 PyObject *return_value = NULL;
10033 static char *keywords[] = {"path", "attribute", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010034
Larry Hastings9cf065c2012-06-22 16:30:09 -070010035 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010036 path.function_name = "removexattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010037 memset(&attribute, 0, sizeof(attribute));
Victor Stinner292c8352012-10-30 02:17:38 +010010038 attribute.function_name = "removexattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010039 path.allow_fd = 1;
10040 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&O&|$p:removexattr",
10041 keywords,
10042 path_converter, &path,
10043 path_converter, &attribute,
10044 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010045 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010046
10047 if (fd_and_follow_symlinks_invalid("removexattr", path.fd, follow_symlinks))
10048 goto exit;
10049
Benjamin Peterson799bd802011-08-31 22:15:17 -040010050 Py_BEGIN_ALLOW_THREADS;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010051 if (path.fd > -1)
10052 result = fremovexattr(path.fd, attribute.narrow);
10053 else if (follow_symlinks)
10054 result = removexattr(path.narrow, attribute.narrow);
10055 else
10056 result = lremovexattr(path.narrow, attribute.narrow);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010057 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010058
Larry Hastings9cf065c2012-06-22 16:30:09 -070010059 if (result) {
Victor Stinner292c8352012-10-30 02:17:38 +010010060 return_value = path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010061 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010062 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010063
Larry Hastings9cf065c2012-06-22 16:30:09 -070010064 return_value = Py_None;
10065 Py_INCREF(return_value);
Benjamin Peterson799bd802011-08-31 22:15:17 -040010066
Larry Hastings9cf065c2012-06-22 16:30:09 -070010067exit:
10068 path_cleanup(&path);
10069 path_cleanup(&attribute);
10070
10071 return return_value;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010072}
10073
10074PyDoc_STRVAR(posix_listxattr__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -070010075"listxattr(path='.', *, follow_symlinks=True)\n\n\
10076Return a list of extended attributes on path.\n\
10077\n\
10078path may be either None, a string, or an open file descriptor.\n\
10079if path is None, listxattr will examine the current directory.\n\
10080If follow_symlinks is False, and the last element of the path is a symbolic\n\
10081 link, listxattr will examine the symbolic link itself instead of the file\n\
10082 the link points to.");
Benjamin Peterson799bd802011-08-31 22:15:17 -040010083
10084static PyObject *
Larry Hastings9cf065c2012-06-22 16:30:09 -070010085posix_listxattr(PyObject *self, PyObject *args, PyObject *kwargs)
Benjamin Peterson799bd802011-08-31 22:15:17 -040010086{
Larry Hastings9cf065c2012-06-22 16:30:09 -070010087 path_t path;
10088 int follow_symlinks = 1;
10089 Py_ssize_t i;
10090 PyObject *result = NULL;
10091 char *buffer = NULL;
10092 char *name;
10093 static char *keywords[] = {"path", "follow_symlinks", NULL};
Benjamin Peterson799bd802011-08-31 22:15:17 -040010094
Larry Hastings9cf065c2012-06-22 16:30:09 -070010095 memset(&path, 0, sizeof(path));
Victor Stinner292c8352012-10-30 02:17:38 +010010096 path.function_name = "listxattr";
Larry Hastings9cf065c2012-06-22 16:30:09 -070010097 path.allow_fd = 1;
10098 path.fd = -1;
10099 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O&$p:listxattr", keywords,
10100 path_converter, &path,
10101 &follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040010102 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010103
Larry Hastings9cf065c2012-06-22 16:30:09 -070010104 if (fd_and_follow_symlinks_invalid("listxattr", path.fd, follow_symlinks))
10105 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010106
Larry Hastings9cf065c2012-06-22 16:30:09 -070010107 name = path.narrow ? path.narrow : ".";
10108 for (i = 0; ; i++) {
10109 char *start, *trace, *end;
10110 ssize_t length;
10111 static Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
10112 Py_ssize_t buffer_size = buffer_sizes[i];
10113 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020010114 /* ERANGE */
Victor Stinner292c8352012-10-30 02:17:38 +010010115 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010116 break;
10117 }
10118 buffer = PyMem_MALLOC(buffer_size);
10119 if (!buffer) {
10120 PyErr_NoMemory();
10121 break;
10122 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010123
Larry Hastings9cf065c2012-06-22 16:30:09 -070010124 Py_BEGIN_ALLOW_THREADS;
10125 if (path.fd > -1)
10126 length = flistxattr(path.fd, buffer, buffer_size);
10127 else if (follow_symlinks)
10128 length = listxattr(name, buffer, buffer_size);
10129 else
10130 length = llistxattr(name, buffer, buffer_size);
10131 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010132
Larry Hastings9cf065c2012-06-22 16:30:09 -070010133 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020010134 if (errno == ERANGE) {
10135 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050010136 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010137 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020010138 }
Victor Stinner292c8352012-10-30 02:17:38 +010010139 path_error(&path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010140 break;
10141 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010142
Larry Hastings9cf065c2012-06-22 16:30:09 -070010143 result = PyList_New(0);
10144 if (!result) {
10145 goto exit;
10146 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040010147
Larry Hastings9cf065c2012-06-22 16:30:09 -070010148 end = buffer + length;
10149 for (trace = start = buffer; trace != end; trace++) {
10150 if (!*trace) {
10151 int error;
10152 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
10153 trace - start);
10154 if (!attribute) {
10155 Py_DECREF(result);
10156 result = NULL;
10157 goto exit;
10158 }
10159 error = PyList_Append(result, attribute);
10160 Py_DECREF(attribute);
10161 if (error) {
10162 Py_DECREF(result);
10163 result = NULL;
10164 goto exit;
10165 }
10166 start = trace + 1;
10167 }
10168 }
10169 break;
10170 }
10171exit:
10172 path_cleanup(&path);
10173 if (buffer)
10174 PyMem_FREE(buffer);
10175 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040010176}
10177
Benjamin Peterson9428d532011-09-14 11:45:52 -040010178#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040010179
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010180
Georg Brandl2fb477c2012-02-21 00:33:36 +010010181PyDoc_STRVAR(posix_urandom__doc__,
10182"urandom(n) -> str\n\n\
10183Return n random bytes suitable for cryptographic use.");
10184
10185static PyObject *
10186posix_urandom(PyObject *self, PyObject *args)
10187{
10188 Py_ssize_t size;
10189 PyObject *result;
10190 int ret;
10191
10192 /* Read arguments */
10193 if (!PyArg_ParseTuple(args, "n:urandom", &size))
10194 return NULL;
10195 if (size < 0)
10196 return PyErr_Format(PyExc_ValueError,
10197 "negative argument not allowed");
10198 result = PyBytes_FromStringAndSize(NULL, size);
10199 if (result == NULL)
10200 return NULL;
10201
10202 ret = _PyOS_URandom(PyBytes_AS_STRING(result),
10203 PyBytes_GET_SIZE(result));
10204 if (ret == -1) {
10205 Py_DECREF(result);
10206 return NULL;
10207 }
10208 return result;
10209}
10210
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010211/* Terminal size querying */
10212
10213static PyTypeObject TerminalSizeType;
10214
10215PyDoc_STRVAR(TerminalSize_docstring,
10216 "A tuple of (columns, lines) for holding terminal window size");
10217
10218static PyStructSequence_Field TerminalSize_fields[] = {
10219 {"columns", "width of the terminal window in characters"},
10220 {"lines", "height of the terminal window in characters"},
10221 {NULL, NULL}
10222};
10223
10224static PyStructSequence_Desc TerminalSize_desc = {
10225 "os.terminal_size",
10226 TerminalSize_docstring,
10227 TerminalSize_fields,
10228 2,
10229};
10230
10231#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10232PyDoc_STRVAR(termsize__doc__,
10233 "Return the size of the terminal window as (columns, lines).\n" \
10234 "\n" \
10235 "The optional argument fd (default standard output) specifies\n" \
10236 "which file descriptor should be queried.\n" \
10237 "\n" \
10238 "If the file descriptor is not connected to a terminal, an OSError\n" \
10239 "is thrown.\n" \
10240 "\n" \
10241 "This function will only be defined if an implementation is\n" \
10242 "available for this system.\n" \
10243 "\n" \
10244 "shutil.get_terminal_size is the high-level function which should \n" \
10245 "normally be used, os.get_terminal_size is the low-level implementation.");
10246
10247static PyObject*
10248get_terminal_size(PyObject *self, PyObject *args)
10249{
10250 int columns, lines;
10251 PyObject *termsize;
10252
10253 int fd = fileno(stdout);
10254 /* Under some conditions stdout may not be connected and
10255 * fileno(stdout) may point to an invalid file descriptor. For example
10256 * GUI apps don't have valid standard streams by default.
10257 *
10258 * If this happens, and the optional fd argument is not present,
10259 * the ioctl below will fail returning EBADF. This is what we want.
10260 */
10261
10262 if (!PyArg_ParseTuple(args, "|i", &fd))
10263 return NULL;
10264
10265#ifdef TERMSIZE_USE_IOCTL
10266 {
10267 struct winsize w;
10268 if (ioctl(fd, TIOCGWINSZ, &w))
10269 return PyErr_SetFromErrno(PyExc_OSError);
10270 columns = w.ws_col;
10271 lines = w.ws_row;
10272 }
10273#endif /* TERMSIZE_USE_IOCTL */
10274
10275#ifdef TERMSIZE_USE_CONIO
10276 {
10277 DWORD nhandle;
10278 HANDLE handle;
10279 CONSOLE_SCREEN_BUFFER_INFO csbi;
10280 switch (fd) {
10281 case 0: nhandle = STD_INPUT_HANDLE;
10282 break;
10283 case 1: nhandle = STD_OUTPUT_HANDLE;
10284 break;
10285 case 2: nhandle = STD_ERROR_HANDLE;
10286 break;
10287 default:
10288 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
10289 }
10290 handle = GetStdHandle(nhandle);
10291 if (handle == NULL)
10292 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
10293 if (handle == INVALID_HANDLE_VALUE)
10294 return PyErr_SetFromWindowsErr(0);
10295
10296 if (!GetConsoleScreenBufferInfo(handle, &csbi))
10297 return PyErr_SetFromWindowsErr(0);
10298
10299 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
10300 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
10301 }
10302#endif /* TERMSIZE_USE_CONIO */
10303
10304 termsize = PyStructSequence_New(&TerminalSizeType);
10305 if (termsize == NULL)
10306 return NULL;
10307 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
10308 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
10309 if (PyErr_Occurred()) {
10310 Py_DECREF(termsize);
10311 return NULL;
10312 }
10313 return termsize;
10314}
10315#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
10316
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010317PyDoc_STRVAR(posix_cpu_count__doc__,
10318"cpu_count() -> integer\n\n\
10319Return the number of CPUs in the system, or None if this value cannot be\n\
10320established.");
10321
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010322static PyObject *
10323posix_cpu_count(PyObject *self)
10324{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010325 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010326#ifdef MS_WINDOWS
10327 SYSTEM_INFO sysinfo;
10328 GetSystemInfo(&sysinfo);
10329 ncpu = sysinfo.dwNumberOfProcessors;
10330#elif defined(__hpux)
10331 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
10332#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
10333 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010334#elif defined(__DragonFly__) || \
10335 defined(__OpenBSD__) || \
10336 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020010337 defined(__NetBSD__) || \
10338 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020010339 int mib[2];
10340 size_t len = sizeof(ncpu);
10341 mib[0] = CTL_HW;
10342 mib[1] = HW_NCPU;
10343 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
10344 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010345#endif
10346 if (ncpu >= 1)
10347 return PyLong_FromLong(ncpu);
10348 else
10349 Py_RETURN_NONE;
10350}
10351
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010352
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010353static PyMethodDef posix_methods[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010354 {"access", (PyCFunction)posix_access,
10355 METH_VARARGS | METH_KEYWORDS,
10356 posix_access__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010357#ifdef HAVE_TTYNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010358 {"ttyname", posix_ttyname, METH_VARARGS, posix_ttyname__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010359#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010360 {"chdir", (PyCFunction)posix_chdir,
10361 METH_VARARGS | METH_KEYWORDS,
10362 posix_chdir__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010363#ifdef HAVE_CHFLAGS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010364 {"chflags", (PyCFunction)posix_chflags,
10365 METH_VARARGS | METH_KEYWORDS,
10366 posix_chflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010367#endif /* HAVE_CHFLAGS */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010368 {"chmod", (PyCFunction)posix_chmod,
10369 METH_VARARGS | METH_KEYWORDS,
10370 posix_chmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010371#ifdef HAVE_FCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010372 {"fchmod", posix_fchmod, METH_VARARGS, posix_fchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010373#endif /* HAVE_FCHMOD */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010374#ifdef HAVE_CHOWN
Larry Hastings9cf065c2012-06-22 16:30:09 -070010375 {"chown", (PyCFunction)posix_chown,
10376 METH_VARARGS | METH_KEYWORDS,
10377 posix_chown__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010378#endif /* HAVE_CHOWN */
Christian Heimes4e30a842007-11-30 22:12:06 +000010379#ifdef HAVE_LCHMOD
Victor Stinner8c62be82010-05-06 00:08:46 +000010380 {"lchmod", posix_lchmod, METH_VARARGS, posix_lchmod__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010381#endif /* HAVE_LCHMOD */
10382#ifdef HAVE_FCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010383 {"fchown", posix_fchown, METH_VARARGS, posix_fchown__doc__},
Christian Heimes4e30a842007-11-30 22:12:06 +000010384#endif /* HAVE_FCHOWN */
Thomas Wouterscf297e42007-02-23 15:07:44 +000010385#ifdef HAVE_LCHFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000010386 {"lchflags", posix_lchflags, METH_VARARGS, posix_lchflags__doc__},
Thomas Wouterscf297e42007-02-23 15:07:44 +000010387#endif /* HAVE_LCHFLAGS */
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010388#ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000010389 {"lchown", posix_lchown, METH_VARARGS, posix_lchown__doc__},
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +000010390#endif /* HAVE_LCHOWN */
Martin v. Löwis244edc82001-10-04 22:44:26 +000010391#ifdef HAVE_CHROOT
Victor Stinner8c62be82010-05-06 00:08:46 +000010392 {"chroot", posix_chroot, METH_VARARGS, posix_chroot__doc__},
Martin v. Löwis244edc82001-10-04 22:44:26 +000010393#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010394#ifdef HAVE_CTERMID
Victor Stinner8c62be82010-05-06 00:08:46 +000010395 {"ctermid", posix_ctermid, METH_NOARGS, posix_ctermid__doc__},
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010396#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010397#ifdef HAVE_GETCWD
Victor Stinner8c62be82010-05-06 00:08:46 +000010398 {"getcwd", (PyCFunction)posix_getcwd_unicode,
10399 METH_NOARGS, posix_getcwd__doc__},
10400 {"getcwdb", (PyCFunction)posix_getcwd_bytes,
10401 METH_NOARGS, posix_getcwdb__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010402#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070010403#if defined(HAVE_LINK) || defined(MS_WINDOWS)
10404 {"link", (PyCFunction)posix_link,
10405 METH_VARARGS | METH_KEYWORDS,
10406 posix_link__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010407#endif /* HAVE_LINK */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010408 {"listdir", (PyCFunction)posix_listdir,
10409 METH_VARARGS | METH_KEYWORDS,
10410 posix_listdir__doc__},
10411 {"lstat", (PyCFunction)posix_lstat,
10412 METH_VARARGS | METH_KEYWORDS,
10413 posix_lstat__doc__},
10414 {"mkdir", (PyCFunction)posix_mkdir,
10415 METH_VARARGS | METH_KEYWORDS,
10416 posix_mkdir__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010417#ifdef HAVE_NICE
Victor Stinner8c62be82010-05-06 00:08:46 +000010418 {"nice", posix_nice, METH_VARARGS, posix_nice__doc__},
Guido van Rossum775f4da1993-01-09 17:18:52 +000010419#endif /* HAVE_NICE */
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010420#ifdef HAVE_GETPRIORITY
10421 {"getpriority", posix_getpriority, METH_VARARGS, posix_getpriority__doc__},
10422#endif /* HAVE_GETPRIORITY */
10423#ifdef HAVE_SETPRIORITY
10424 {"setpriority", posix_setpriority, METH_VARARGS, posix_setpriority__doc__},
10425#endif /* HAVE_SETPRIORITY */
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010426#ifdef HAVE_READLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -070010427 {"readlink", (PyCFunction)posix_readlink,
10428 METH_VARARGS | METH_KEYWORDS,
10429 readlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010430#endif /* HAVE_READLINK */
Brian Curtind40e6f72010-07-08 21:39:08 +000010431#if !defined(HAVE_READLINK) && defined(MS_WINDOWS)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010432 {"readlink", (PyCFunction)win_readlink,
10433 METH_VARARGS | METH_KEYWORDS,
10434 readlink__doc__},
Brian Curtind40e6f72010-07-08 21:39:08 +000010435#endif /* !defined(HAVE_READLINK) && defined(MS_WINDOWS) */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010436 {"rename", (PyCFunction)posix_rename,
10437 METH_VARARGS | METH_KEYWORDS,
10438 posix_rename__doc__},
10439 {"replace", (PyCFunction)posix_replace,
10440 METH_VARARGS | METH_KEYWORDS,
10441 posix_replace__doc__},
Larry Hastingsb698d8e2012-06-23 16:55:07 -070010442 {"rmdir", (PyCFunction)posix_rmdir,
10443 METH_VARARGS | METH_KEYWORDS,
10444 posix_rmdir__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010445 {"stat", (PyCFunction)posix_stat,
10446 METH_VARARGS | METH_KEYWORDS,
10447 posix_stat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010448 {"stat_float_times", stat_float_times, METH_VARARGS, stat_float_times__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010449#if defined(HAVE_SYMLINK)
10450 {"symlink", (PyCFunction)posix_symlink,
10451 METH_VARARGS | METH_KEYWORDS,
10452 posix_symlink__doc__},
Guido van Rossumc39de5f1992-02-05 11:15:54 +000010453#endif /* HAVE_SYMLINK */
Guido van Rossum85e3b011991-06-03 12:42:10 +000010454#ifdef HAVE_SYSTEM
Victor Stinner8c62be82010-05-06 00:08:46 +000010455 {"system", posix_system, METH_VARARGS, posix_system__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010456#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010457 {"umask", posix_umask, METH_VARARGS, posix_umask__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010458#ifdef HAVE_UNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000010459 {"uname", posix_uname, METH_NOARGS, posix_uname__doc__},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010460#endif /* HAVE_UNAME */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010461 {"unlink", (PyCFunction)posix_unlink,
10462 METH_VARARGS | METH_KEYWORDS,
10463 posix_unlink__doc__},
10464 {"remove", (PyCFunction)posix_unlink,
10465 METH_VARARGS | METH_KEYWORDS,
10466 posix_remove__doc__},
Larry Hastings76ad59b2012-05-03 00:30:07 -070010467 {"utime", (PyCFunction)posix_utime,
10468 METH_VARARGS | METH_KEYWORDS, posix_utime__doc__},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000010469#ifdef HAVE_TIMES
Victor Stinner8c62be82010-05-06 00:08:46 +000010470 {"times", posix_times, METH_NOARGS, posix_times__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010471#endif /* HAVE_TIMES */
Victor Stinner8c62be82010-05-06 00:08:46 +000010472 {"_exit", posix__exit, METH_VARARGS, posix__exit__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010473#ifdef HAVE_EXECV
Victor Stinner8c62be82010-05-06 00:08:46 +000010474 {"execv", posix_execv, METH_VARARGS, posix_execv__doc__},
Larry Hastings9cf065c2012-06-22 16:30:09 -070010475 {"execve", (PyCFunction)posix_execve,
10476 METH_VARARGS | METH_KEYWORDS,
10477 posix_execve__doc__},
Guido van Rossum3b066191991-06-04 19:40:25 +000010478#endif /* HAVE_EXECV */
Guido van Rossuma1065681999-01-25 23:20:23 +000010479#ifdef HAVE_SPAWNV
Victor Stinner8c62be82010-05-06 00:08:46 +000010480 {"spawnv", posix_spawnv, METH_VARARGS, posix_spawnv__doc__},
10481 {"spawnve", posix_spawnve, METH_VARARGS, posix_spawnve__doc__},
Guido van Rossuma1065681999-01-25 23:20:23 +000010482#endif /* HAVE_SPAWNV */
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010483#ifdef HAVE_FORK1
Victor Stinner8c62be82010-05-06 00:08:46 +000010484 {"fork1", posix_fork1, METH_NOARGS, posix_fork1__doc__},
Guido van Rossum2242f2f2001-04-11 20:58:20 +000010485#endif /* HAVE_FORK1 */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010486#ifdef HAVE_FORK
Victor Stinner8c62be82010-05-06 00:08:46 +000010487 {"fork", posix_fork, METH_NOARGS, posix_fork__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010488#endif /* HAVE_FORK */
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010489#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010490#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010491 {"sched_get_priority_max", posix_sched_get_priority_max, METH_VARARGS, posix_sched_get_priority_max__doc__},
10492 {"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 +020010493#endif
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010494#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010495 {"sched_getparam", posix_sched_getparam, METH_VARARGS, posix_sched_getparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010496#endif
10497#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010498 {"sched_getscheduler", posix_sched_getscheduler, METH_VARARGS, posix_sched_getscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010499#endif
10500#ifdef HAVE_SCHED_RR_GET_INTERVAL
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010501 {"sched_rr_get_interval", posix_sched_rr_get_interval, METH_VARARGS, posix_sched_rr_get_interval__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010502#endif
10503#ifdef HAVE_SCHED_SETPARAM
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010504 {"sched_setparam", posix_sched_setparam, METH_VARARGS, posix_sched_setparam__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010505#endif
10506#ifdef HAVE_SCHED_SETSCHEDULER
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010507 {"sched_setscheduler", posix_sched_setscheduler, METH_VARARGS, posix_sched_setscheduler__doc__},
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -050010508#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010509 {"sched_yield", posix_sched_yield, METH_NOARGS, posix_sched_yield__doc__},
Benjamin Peterson2740af82011-08-02 17:41:34 -050010510#ifdef HAVE_SCHED_SETAFFINITY
Benjamin Peterson94b580d2011-08-02 17:30:04 -050010511 {"sched_setaffinity", posix_sched_setaffinity, METH_VARARGS, posix_sched_setaffinity__doc__},
10512 {"sched_getaffinity", posix_sched_getaffinity, METH_VARARGS, posix_sched_getaffinity__doc__},
10513#endif
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +020010514#endif /* HAVE_SCHED_H */
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010515#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Victor Stinner8c62be82010-05-06 00:08:46 +000010516 {"openpty", posix_openpty, METH_NOARGS, posix_openpty__doc__},
Martin v. Löwis24a880b2002-12-31 12:55:15 +000010517#endif /* HAVE_OPENPTY || HAVE__GETPTY || HAVE_DEV_PTMX */
Fred Drake8cef4cf2000-06-28 16:40:38 +000010518#ifdef HAVE_FORKPTY
Victor Stinner8c62be82010-05-06 00:08:46 +000010519 {"forkpty", posix_forkpty, METH_NOARGS, posix_forkpty__doc__},
Fred Drake8cef4cf2000-06-28 16:40:38 +000010520#endif /* HAVE_FORKPTY */
Guido van Rossum3b066191991-06-04 19:40:25 +000010521#ifdef HAVE_GETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010522 {"getegid", posix_getegid, METH_NOARGS, posix_getegid__doc__},
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +000010523#endif /* HAVE_GETEGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010524#ifdef HAVE_GETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010525 {"geteuid", posix_geteuid, METH_NOARGS, posix_geteuid__doc__},
Guido van Rossum46003ff1992-05-15 11:05:24 +000010526#endif /* HAVE_GETEUID */
10527#ifdef HAVE_GETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010528 {"getgid", posix_getgid, METH_NOARGS, posix_getgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010529#endif /* HAVE_GETGID */
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +020010530#ifdef HAVE_GETGROUPLIST
10531 {"getgrouplist", posix_getgrouplist, METH_VARARGS, posix_getgrouplist__doc__},
10532#endif
Fred Drakec9680921999-12-13 16:37:25 +000010533#ifdef HAVE_GETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010534 {"getgroups", posix_getgroups, METH_NOARGS, posix_getgroups__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010535#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010536 {"getpid", posix_getpid, METH_NOARGS, posix_getpid__doc__},
Guido van Rossumb6775db1994-08-01 11:34:53 +000010537#ifdef HAVE_GETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010538 {"getpgrp", posix_getpgrp, METH_NOARGS, posix_getpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010539#endif /* HAVE_GETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010540#ifdef HAVE_GETPPID
Victor Stinner8c62be82010-05-06 00:08:46 +000010541 {"getppid", posix_getppid, METH_NOARGS, posix_getppid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010542#endif /* HAVE_GETPPID */
10543#ifdef HAVE_GETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010544 {"getuid", posix_getuid, METH_NOARGS, posix_getuid__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010545#endif /* HAVE_GETUID */
Fred Drake12c6e2d1999-12-14 21:25:03 +000010546#ifdef HAVE_GETLOGIN
Victor Stinner8c62be82010-05-06 00:08:46 +000010547 {"getlogin", posix_getlogin, METH_NOARGS, posix_getlogin__doc__},
Fred Drake12c6e2d1999-12-14 21:25:03 +000010548#endif
Guido van Rossumad0ee831995-03-01 10:34:45 +000010549#ifdef HAVE_KILL
Victor Stinner8c62be82010-05-06 00:08:46 +000010550 {"kill", posix_kill, METH_VARARGS, posix_kill__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010551#endif /* HAVE_KILL */
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010552#ifdef HAVE_KILLPG
Victor Stinner8c62be82010-05-06 00:08:46 +000010553 {"killpg", posix_killpg, METH_VARARGS, posix_killpg__doc__},
Martin v. Löwisb2c92f42002-02-16 23:35:41 +000010554#endif /* HAVE_KILLPG */
Guido van Rossumc0125471996-06-28 18:55:32 +000010555#ifdef HAVE_PLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000010556 {"plock", posix_plock, METH_VARARGS, posix_plock__doc__},
Guido van Rossumc0125471996-06-28 18:55:32 +000010557#endif /* HAVE_PLOCK */
Thomas Heller8b7a9572007-08-31 06:44:36 +000010558#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010559 {"startfile", win32_startfile, METH_VARARGS, win32_startfile__doc__},
10560 {"kill", win32_kill, METH_VARARGS, win32_kill__doc__},
Thomas Heller8b7a9572007-08-31 06:44:36 +000010561#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000010562#ifdef HAVE_SETUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010563 {"setuid", posix_setuid, METH_VARARGS, posix_setuid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010564#endif /* HAVE_SETUID */
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010565#ifdef HAVE_SETEUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010566 {"seteuid", posix_seteuid, METH_VARARGS, posix_seteuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010567#endif /* HAVE_SETEUID */
10568#ifdef HAVE_SETEGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010569 {"setegid", posix_setegid, METH_VARARGS, posix_setegid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010570#endif /* HAVE_SETEGID */
10571#ifdef HAVE_SETREUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010572 {"setreuid", posix_setreuid, METH_VARARGS, posix_setreuid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010573#endif /* HAVE_SETREUID */
10574#ifdef HAVE_SETREGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010575 {"setregid", posix_setregid, METH_VARARGS, posix_setregid__doc__},
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +000010576#endif /* HAVE_SETREGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010577#ifdef HAVE_SETGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010578 {"setgid", posix_setgid, METH_VARARGS, posix_setgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010579#endif /* HAVE_SETGID */
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010580#ifdef HAVE_SETGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010581 {"setgroups", posix_setgroups, METH_O, posix_setgroups__doc__},
Martin v. Löwis61c5edf2001-10-18 04:06:00 +000010582#endif /* HAVE_SETGROUPS */
Antoine Pitroub7572f02009-12-02 20:46:48 +000010583#ifdef HAVE_INITGROUPS
Victor Stinner8c62be82010-05-06 00:08:46 +000010584 {"initgroups", posix_initgroups, METH_VARARGS, posix_initgroups__doc__},
Antoine Pitroub7572f02009-12-02 20:46:48 +000010585#endif /* HAVE_INITGROUPS */
Martin v. Löwis606edc12002-06-13 21:09:11 +000010586#ifdef HAVE_GETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010587 {"getpgid", posix_getpgid, METH_VARARGS, posix_getpgid__doc__},
Martin v. Löwis606edc12002-06-13 21:09:11 +000010588#endif /* HAVE_GETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010589#ifdef HAVE_SETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010590 {"setpgrp", posix_setpgrp, METH_NOARGS, posix_setpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010591#endif /* HAVE_SETPGRP */
Guido van Rossumad0ee831995-03-01 10:34:45 +000010592#ifdef HAVE_WAIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010593 {"wait", posix_wait, METH_NOARGS, posix_wait__doc__},
Guido van Rossumad0ee831995-03-01 10:34:45 +000010594#endif /* HAVE_WAIT */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010595#ifdef HAVE_WAIT3
Victor Stinner4195b5c2012-02-08 23:03:19 +010010596 {"wait3", posix_wait3, METH_VARARGS, posix_wait3__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010597#endif /* HAVE_WAIT3 */
10598#ifdef HAVE_WAIT4
Victor Stinner4195b5c2012-02-08 23:03:19 +010010599 {"wait4", posix_wait4, METH_VARARGS, posix_wait4__doc__},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000010600#endif /* HAVE_WAIT4 */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010601#if defined(HAVE_WAITID) && !defined(__APPLE__)
10602 {"waitid", posix_waitid, METH_VARARGS, posix_waitid__doc__},
10603#endif
Tim Petersab034fa2002-02-01 11:27:43 +000010604#if defined(HAVE_WAITPID) || defined(HAVE_CWAIT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010605 {"waitpid", posix_waitpid, METH_VARARGS, posix_waitpid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010606#endif /* HAVE_WAITPID */
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010607#ifdef HAVE_GETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010608 {"getsid", posix_getsid, METH_VARARGS, posix_getsid__doc__},
Martin v. Löwis49ee14d2003-11-10 06:35:36 +000010609#endif /* HAVE_GETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010610#ifdef HAVE_SETSID
Victor Stinner8c62be82010-05-06 00:08:46 +000010611 {"setsid", posix_setsid, METH_NOARGS, posix_setsid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010612#endif /* HAVE_SETSID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010613#ifdef HAVE_SETPGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010614 {"setpgid", posix_setpgid, METH_VARARGS, posix_setpgid__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010615#endif /* HAVE_SETPGID */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010616#ifdef HAVE_TCGETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010617 {"tcgetpgrp", posix_tcgetpgrp, METH_VARARGS, posix_tcgetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010618#endif /* HAVE_TCGETPGRP */
Guido van Rossumb6775db1994-08-01 11:34:53 +000010619#ifdef HAVE_TCSETPGRP
Victor Stinner8c62be82010-05-06 00:08:46 +000010620 {"tcsetpgrp", posix_tcsetpgrp, METH_VARARGS, posix_tcsetpgrp__doc__},
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +000010621#endif /* HAVE_TCSETPGRP */
Larry Hastings9cf065c2012-06-22 16:30:09 -070010622 {"open", (PyCFunction)posix_open,\
10623 METH_VARARGS | METH_KEYWORDS,
10624 posix_open__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010625 {"close", posix_close, METH_VARARGS, posix_close__doc__},
10626 {"closerange", posix_closerange, METH_VARARGS, posix_closerange__doc__},
10627 {"device_encoding", device_encoding, METH_VARARGS, device_encoding__doc__},
10628 {"dup", posix_dup, METH_VARARGS, posix_dup__doc__},
10629 {"dup2", posix_dup2, METH_VARARGS, posix_dup2__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010630#ifdef HAVE_LOCKF
10631 {"lockf", posix_lockf, METH_VARARGS, posix_lockf__doc__},
10632#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010633 {"lseek", posix_lseek, METH_VARARGS, posix_lseek__doc__},
10634 {"read", posix_read, METH_VARARGS, posix_read__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010635#ifdef HAVE_READV
10636 {"readv", posix_readv, METH_VARARGS, posix_readv__doc__},
10637#endif
10638#ifdef HAVE_PREAD
10639 {"pread", posix_pread, METH_VARARGS, posix_pread__doc__},
10640#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010641 {"write", posix_write, METH_VARARGS, posix_write__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010642#ifdef HAVE_WRITEV
10643 {"writev", posix_writev, METH_VARARGS, posix_writev__doc__},
10644#endif
10645#ifdef HAVE_PWRITE
10646 {"pwrite", posix_pwrite, METH_VARARGS, posix_pwrite__doc__},
10647#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000010648#ifdef HAVE_SENDFILE
10649 {"sendfile", (PyCFunction)posix_sendfile, METH_VARARGS | METH_KEYWORDS,
10650 posix_sendfile__doc__},
10651#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +010010652 {"fstat", posix_fstat, METH_VARARGS, posix_fstat__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010653 {"isatty", posix_isatty, METH_VARARGS, posix_isatty__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010654#ifdef HAVE_PIPE
Victor Stinner8c62be82010-05-06 00:08:46 +000010655 {"pipe", posix_pipe, METH_NOARGS, posix_pipe__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010656#endif
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010657#ifdef HAVE_PIPE2
Charles-François Natali368f34b2011-06-06 19:49:47 +020010658 {"pipe2", posix_pipe2, METH_O, posix_pipe2__doc__},
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010659#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010660#ifdef HAVE_MKFIFO
Larry Hastings9cf065c2012-06-22 16:30:09 -070010661 {"mkfifo", (PyCFunction)posix_mkfifo,
10662 METH_VARARGS | METH_KEYWORDS,
10663 posix_mkfifo__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010664#endif
Neal Norwitz11690112002-07-30 01:08:28 +000010665#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010666 {"mknod", (PyCFunction)posix_mknod,
10667 METH_VARARGS | METH_KEYWORDS,
10668 posix_mknod__doc__},
Martin v. Löwis06a83e92002-04-14 10:19:44 +000010669#endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010670#ifdef HAVE_DEVICE_MACROS
Victor Stinner8c62be82010-05-06 00:08:46 +000010671 {"major", posix_major, METH_VARARGS, posix_major__doc__},
10672 {"minor", posix_minor, METH_VARARGS, posix_minor__doc__},
10673 {"makedev", posix_makedev, METH_VARARGS, posix_makedev__doc__},
Martin v. Löwisdbe3f762002-10-10 14:27:30 +000010674#endif
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010675#ifdef HAVE_FTRUNCATE
Victor Stinner8c62be82010-05-06 00:08:46 +000010676 {"ftruncate", posix_ftruncate, METH_VARARGS, posix_ftruncate__doc__},
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010677#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010678#ifdef HAVE_TRUNCATE
Georg Brandl306336b2012-06-24 12:55:33 +020010679 {"truncate", (PyCFunction)posix_truncate,
10680 METH_VARARGS | METH_KEYWORDS,
10681 posix_truncate__doc__},
Ross Lagerwall7807c352011-03-17 20:20:30 +020010682#endif
10683#ifdef HAVE_POSIX_FALLOCATE
10684 {"posix_fallocate", posix_posix_fallocate, METH_VARARGS, posix_posix_fallocate__doc__},
10685#endif
10686#ifdef HAVE_POSIX_FADVISE
10687 {"posix_fadvise", posix_posix_fadvise, METH_VARARGS, posix_posix_fadvise__doc__},
10688#endif
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010689#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010690 {"putenv", posix_putenv, METH_VARARGS, posix_putenv__doc__},
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010691#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010692#ifdef HAVE_UNSETENV
Victor Stinner8c62be82010-05-06 00:08:46 +000010693 {"unsetenv", posix_unsetenv, METH_VARARGS, posix_unsetenv__doc__},
Guido van Rossumc524d952001-10-19 01:31:59 +000010694#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010695 {"strerror", posix_strerror, METH_VARARGS, posix_strerror__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010696#ifdef HAVE_FCHDIR
Victor Stinner8c62be82010-05-06 00:08:46 +000010697 {"fchdir", posix_fchdir, METH_O, posix_fchdir__doc__},
Fred Drake4d1e64b2002-04-15 19:40:07 +000010698#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010699#ifdef HAVE_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010700 {"fsync", posix_fsync, METH_O, posix_fsync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010701#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020010702#ifdef HAVE_SYNC
10703 {"sync", posix_sync, METH_NOARGS, posix_sync__doc__},
10704#endif
Guido van Rossum21142a01999-01-08 21:05:37 +000010705#ifdef HAVE_FDATASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010706 {"fdatasync", posix_fdatasync, METH_O, posix_fdatasync__doc__},
Guido van Rossum21142a01999-01-08 21:05:37 +000010707#endif
Guido van Rossumc9641791998-08-04 15:26:23 +000010708#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010709#ifdef WCOREDUMP
Victor Stinner8c62be82010-05-06 00:08:46 +000010710 {"WCOREDUMP", posix_WCOREDUMP, METH_VARARGS, posix_WCOREDUMP__doc__},
Fred Drake106c1a02002-04-23 15:58:02 +000010711#endif /* WCOREDUMP */
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010712#ifdef WIFCONTINUED
Victor Stinner8c62be82010-05-06 00:08:46 +000010713 {"WIFCONTINUED",posix_WIFCONTINUED, METH_VARARGS, posix_WIFCONTINUED__doc__},
Martin v. Löwis2b41b0d2002-05-04 13:13:41 +000010714#endif /* WIFCONTINUED */
Guido van Rossumc9641791998-08-04 15:26:23 +000010715#ifdef WIFSTOPPED
Victor Stinner8c62be82010-05-06 00:08:46 +000010716 {"WIFSTOPPED", posix_WIFSTOPPED, METH_VARARGS, posix_WIFSTOPPED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010717#endif /* WIFSTOPPED */
10718#ifdef WIFSIGNALED
Victor Stinner8c62be82010-05-06 00:08:46 +000010719 {"WIFSIGNALED", posix_WIFSIGNALED, METH_VARARGS, posix_WIFSIGNALED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010720#endif /* WIFSIGNALED */
10721#ifdef WIFEXITED
Victor Stinner8c62be82010-05-06 00:08:46 +000010722 {"WIFEXITED", posix_WIFEXITED, METH_VARARGS, posix_WIFEXITED__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010723#endif /* WIFEXITED */
10724#ifdef WEXITSTATUS
Victor Stinner8c62be82010-05-06 00:08:46 +000010725 {"WEXITSTATUS", posix_WEXITSTATUS, METH_VARARGS, posix_WEXITSTATUS__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010726#endif /* WEXITSTATUS */
10727#ifdef WTERMSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010728 {"WTERMSIG", posix_WTERMSIG, METH_VARARGS, posix_WTERMSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010729#endif /* WTERMSIG */
10730#ifdef WSTOPSIG
Victor Stinner8c62be82010-05-06 00:08:46 +000010731 {"WSTOPSIG", posix_WSTOPSIG, METH_VARARGS, posix_WSTOPSIG__doc__},
Guido van Rossumc9641791998-08-04 15:26:23 +000010732#endif /* WSTOPSIG */
10733#endif /* HAVE_SYS_WAIT_H */
Thomas Wouters477c8d52006-05-27 19:21:47 +000010734#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Victor Stinner8c62be82010-05-06 00:08:46 +000010735 {"fstatvfs", posix_fstatvfs, METH_VARARGS, posix_fstatvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010736#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000010737#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Larry Hastings9cf065c2012-06-22 16:30:09 -070010738 {"statvfs", (PyCFunction)posix_statvfs,
10739 METH_VARARGS | METH_KEYWORDS,
10740 posix_statvfs__doc__},
Guido van Rossum94f6f721999-01-06 18:42:14 +000010741#endif
Fred Drakec9680921999-12-13 16:37:25 +000010742#ifdef HAVE_CONFSTR
Victor Stinner8c62be82010-05-06 00:08:46 +000010743 {"confstr", posix_confstr, METH_VARARGS, posix_confstr__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010744#endif
10745#ifdef HAVE_SYSCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010746 {"sysconf", posix_sysconf, METH_VARARGS, posix_sysconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010747#endif
10748#ifdef HAVE_FPATHCONF
Victor Stinner8c62be82010-05-06 00:08:46 +000010749 {"fpathconf", posix_fpathconf, METH_VARARGS, posix_fpathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010750#endif
10751#ifdef HAVE_PATHCONF
Georg Brandl306336b2012-06-24 12:55:33 +020010752 {"pathconf", (PyCFunction)posix_pathconf,
10753 METH_VARARGS | METH_KEYWORDS,
10754 posix_pathconf__doc__},
Fred Drakec9680921999-12-13 16:37:25 +000010755#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000010756 {"abort", posix_abort, METH_NOARGS, posix_abort__doc__},
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010757#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010758 {"_getfullpathname", posix__getfullpathname, METH_VARARGS, NULL},
Brian Curtind40e6f72010-07-08 21:39:08 +000010759 {"_getfinalpathname", posix__getfinalpathname, METH_VARARGS, NULL},
Brian Curtin95d028f2011-06-09 09:10:38 -050010760 {"_isdir", posix__isdir, METH_VARARGS, posix__isdir__doc__},
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010761 {"_getdiskusage", win32__getdiskusage, METH_VARARGS, win32__getdiskusage__doc__},
Mark Hammondef8b6542001-05-13 08:04:26 +000010762#endif
Martin v. Löwis438b5342002-12-27 10:16:42 +000010763#ifdef HAVE_GETLOADAVG
Victor Stinner8c62be82010-05-06 00:08:46 +000010764 {"getloadavg", posix_getloadavg, METH_NOARGS, posix_getloadavg__doc__},
Martin v. Löwis438b5342002-12-27 10:16:42 +000010765#endif
Georg Brandl2daf6ae2012-02-20 19:54:16 +010010766 {"urandom", posix_urandom, METH_VARARGS, posix_urandom__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010767#ifdef HAVE_SETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010768 {"setresuid", posix_setresuid, METH_VARARGS, posix_setresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010769#endif
10770#ifdef HAVE_SETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010771 {"setresgid", posix_setresgid, METH_VARARGS, posix_setresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010772#endif
10773#ifdef HAVE_GETRESUID
Victor Stinner8c62be82010-05-06 00:08:46 +000010774 {"getresuid", posix_getresuid, METH_NOARGS, posix_getresuid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010775#endif
10776#ifdef HAVE_GETRESGID
Victor Stinner8c62be82010-05-06 00:08:46 +000010777 {"getresgid", posix_getresgid, METH_NOARGS, posix_getresgid__doc__},
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000010778#endif
10779
Benjamin Peterson9428d532011-09-14 11:45:52 -040010780#ifdef USE_XATTRS
Larry Hastings9cf065c2012-06-22 16:30:09 -070010781 {"setxattr", (PyCFunction)posix_setxattr,
10782 METH_VARARGS | METH_KEYWORDS,
10783 posix_setxattr__doc__},
10784 {"getxattr", (PyCFunction)posix_getxattr,
10785 METH_VARARGS | METH_KEYWORDS,
10786 posix_getxattr__doc__},
10787 {"removexattr", (PyCFunction)posix_removexattr,
10788 METH_VARARGS | METH_KEYWORDS,
10789 posix_removexattr__doc__},
10790 {"listxattr", (PyCFunction)posix_listxattr,
10791 METH_VARARGS | METH_KEYWORDS,
10792 posix_listxattr__doc__},
Benjamin Peterson799bd802011-08-31 22:15:17 -040010793#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010010794#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
10795 {"get_terminal_size", get_terminal_size, METH_VARARGS, termsize__doc__},
10796#endif
Charles-Francois Natali44feda32013-05-20 14:40:46 +020010797 {"cpu_count", (PyCFunction)posix_cpu_count,
10798 METH_NOARGS, posix_cpu_count__doc__},
Victor Stinner8c62be82010-05-06 00:08:46 +000010799 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010800};
10801
10802
Brian Curtin52173d42010-12-02 18:29:18 +000010803#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000010804static int
Brian Curtin52173d42010-12-02 18:29:18 +000010805enable_symlink()
10806{
10807 HANDLE tok;
10808 TOKEN_PRIVILEGES tok_priv;
10809 LUID luid;
10810 int meth_idx = 0;
10811
10812 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &tok))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010813 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010814
10815 if (!LookupPrivilegeValue(NULL, SE_CREATE_SYMBOLIC_LINK_NAME, &luid))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010816 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010817
10818 tok_priv.PrivilegeCount = 1;
10819 tok_priv.Privileges[0].Luid = luid;
10820 tok_priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
10821
10822 if (!AdjustTokenPrivileges(tok, FALSE, &tok_priv,
10823 sizeof(TOKEN_PRIVILEGES),
10824 (PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL))
Brian Curtin3b4499c2010-12-28 14:31:47 +000010825 return 0;
Brian Curtin52173d42010-12-02 18:29:18 +000010826
Brian Curtin3b4499c2010-12-28 14:31:47 +000010827 /* ERROR_NOT_ALL_ASSIGNED returned when the privilege can't be assigned. */
10828 return GetLastError() == ERROR_NOT_ALL_ASSIGNED ? 0 : 1;
Brian Curtin52173d42010-12-02 18:29:18 +000010829}
10830#endif /* defined(HAVE_SYMLINK) && defined(MS_WINDOWS) */
10831
Barry Warsaw4a342091996-12-19 23:50:02 +000010832static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010833all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000010834{
Guido van Rossum94f6f721999-01-06 18:42:14 +000010835#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010836 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010837#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010838#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010839 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010840#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010841#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010842 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010843#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010844#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010845 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010846#endif
Fred Drakec9680921999-12-13 16:37:25 +000010847#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010848 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000010849#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010850#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010851 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000010852#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010853#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010854 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010855#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010856#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010857 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010858#endif
Fred Drake106c1a02002-04-23 15:58:02 +000010859#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010860 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000010861#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000010862#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010863 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010864#endif
10865#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010866 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010867#endif
10868#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010869 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010870#endif
10871#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010872 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010873#endif
10874#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010875 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010876#endif
10877#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010878 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010879#endif
10880#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010881 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010882#endif
10883#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010884 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010885#endif
10886#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010887 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010888#endif
10889#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010890 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010891#endif
10892#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010893 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010894#endif
10895#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010896 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010897#endif
10898#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010899 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000010900#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000010901#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010902 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010903#endif
10904#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010905 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000010906#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020010907#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010908 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020010909#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010910#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010911 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010912#endif
Skip Montanaro5ff14922005-05-16 02:42:22 +000010913#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010914 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010915#endif
10916#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010917 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000010918#endif
Jesus Ceacf381202012-04-24 20:44:40 +020010919#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010920 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020010921#endif
10922#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010923 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020010924#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050010925#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010926 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050010927#endif
Jesus Ceacf381202012-04-24 20:44:40 +020010928#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010929 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020010930#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010931#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010932 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010933#endif
10934#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010935 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010936#endif
10937#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010938 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010939#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020010940#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010941 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020010942#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020010943#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010944 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020010945#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000010946
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010947
Jesus Cea94363612012-06-22 18:32:07 +020010948#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010949 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020010950#endif
10951#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010952 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020010953#endif
10954
Tim Peters5aa91602002-01-30 05:46:57 +000010955/* MS Windows */
10956#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000010957 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010958 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010959#endif
10960#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000010961 /* Optimize for short life (keep in memory). */
10962 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010963 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010964#endif
10965#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000010966 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010967 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010968#endif
10969#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000010970 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010971 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010972#endif
10973#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000010974 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010975 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000010976#endif
10977
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010978/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010979#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010980 /* Send a SIGIO signal whenever input or output
10981 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010982 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000010983#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010984#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000010985 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010986 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010987#endif
10988#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010990 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010991#endif
10992#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000010993 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010994 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000010995#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020010996#ifdef O_NOLINKS
10997 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020010998 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020010999#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011000#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011001 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011002 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000011003#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000011004
Victor Stinner8c62be82010-05-06 00:08:46 +000011005 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011006#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011007 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011008#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011009#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011010 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011011#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011012#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011013 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011014#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011015#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011016 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011017#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011018#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011019 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011020#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011021#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011022 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011023#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011024#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011025 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011026#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011027#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011028 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011029#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011030#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011031 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011032#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011033#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011034 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011035#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011036#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011037 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011038#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011039#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011040 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011041#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011042#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011043 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011044#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011045#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011046 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011047#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011048#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011049 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011050#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011051#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011052 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011053#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011054#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011055 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000011056#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000011057
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000011058 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011059#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011060 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011061#endif /* ST_RDONLY */
11062#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011063 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000011064#endif /* ST_NOSUID */
11065
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011066 /* FreeBSD sendfile() constants */
11067#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011068 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011069#endif
11070#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011071 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011072#endif
11073#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011074 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000011075#endif
11076
Ross Lagerwall7807c352011-03-17 20:20:30 +020011077 /* constants for posix_fadvise */
11078#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011079 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011080#endif
11081#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011082 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011083#endif
11084#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011085 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011086#endif
11087#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011088 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011089#endif
11090#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011091 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011092#endif
11093#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011094 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011095#endif
11096
11097 /* constants for waitid */
11098#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011099 if (PyModule_AddIntMacro(m, P_PID)) return -1;
11100 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
11101 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011102#endif
11103#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011104 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011105#endif
11106#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011107 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011108#endif
11109#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011110 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011111#endif
11112#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011113 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011114#endif
11115#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011116 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011117#endif
11118#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011119 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011120#endif
11121#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011122 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011123#endif
11124
11125 /* constants for lockf */
11126#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011127 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011128#endif
11129#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011130 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011131#endif
11132#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011133 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011134#endif
11135#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011136 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020011137#endif
11138
Guido van Rossum246bc171999-02-01 23:54:31 +000011139#ifdef HAVE_SPAWNV
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011140 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
11141 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
11142 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
11143 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
11144 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000011145#endif
11146
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011147#ifdef HAVE_SCHED_H
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011148 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
11149 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
11150 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011151#ifdef SCHED_SPORADIC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011152 if (PyModule_AddIntMacro(m, SCHED_SPORADIC) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011153#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011154#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011155 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011156#endif
11157#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011158 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011159#endif
11160#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011161 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011162#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011163#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011164 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011165#endif
11166#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011167 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011168#endif
11169#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011170 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011171#endif
11172#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011173 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020011174#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011175#endif
11176
Benjamin Peterson9428d532011-09-14 11:45:52 -040011177#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011178 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
11179 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
11180 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040011181#endif
11182
Victor Stinner8b905bd2011-10-25 13:34:04 +020011183#ifdef RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011184 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011185#endif
11186#ifdef RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011187 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011188#endif
11189#ifdef RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011190 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011191#endif
11192#ifdef RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011193 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011194#endif
11195#ifdef RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011196 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011197#endif
11198#ifdef RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011199 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011200#endif
11201#ifdef RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020011202 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020011203#endif
11204
Victor Stinner8c62be82010-05-06 00:08:46 +000011205 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000011206}
11207
11208
Tim Peters5aa91602002-01-30 05:46:57 +000011209#if (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__)) && !defined(__QNX__)
Martin v. Löwis1a214512008-06-11 05:26:20 +000011210#define INITFUNC PyInit_nt
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011211#define MODNAME "nt"
Tim Peters58e0a8c2001-05-14 22:32:33 +000011212
Guido van Rossum8e9ebfd1997-11-22 21:53:48 +000011213#else
Martin v. Löwis1a214512008-06-11 05:26:20 +000011214#define INITFUNC PyInit_posix
Guido van Rossum0cb96de1997-10-01 04:29:29 +000011215#define MODNAME "posix"
11216#endif
11217
Martin v. Löwis1a214512008-06-11 05:26:20 +000011218static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000011219 PyModuleDef_HEAD_INIT,
11220 MODNAME,
11221 posix__doc__,
11222 -1,
11223 posix_methods,
11224 NULL,
11225 NULL,
11226 NULL,
11227 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +000011228};
11229
11230
Larry Hastings9cf065c2012-06-22 16:30:09 -070011231static char *have_functions[] = {
11232
11233#ifdef HAVE_FACCESSAT
11234 "HAVE_FACCESSAT",
11235#endif
11236
11237#ifdef HAVE_FCHDIR
11238 "HAVE_FCHDIR",
11239#endif
11240
11241#ifdef HAVE_FCHMOD
11242 "HAVE_FCHMOD",
11243#endif
11244
11245#ifdef HAVE_FCHMODAT
11246 "HAVE_FCHMODAT",
11247#endif
11248
11249#ifdef HAVE_FCHOWN
11250 "HAVE_FCHOWN",
11251#endif
11252
11253#ifdef HAVE_FEXECVE
11254 "HAVE_FEXECVE",
11255#endif
11256
11257#ifdef HAVE_FDOPENDIR
11258 "HAVE_FDOPENDIR",
11259#endif
11260
Georg Brandl306336b2012-06-24 12:55:33 +020011261#ifdef HAVE_FPATHCONF
11262 "HAVE_FPATHCONF",
11263#endif
11264
Larry Hastings9cf065c2012-06-22 16:30:09 -070011265#ifdef HAVE_FSTATAT
11266 "HAVE_FSTATAT",
11267#endif
11268
11269#ifdef HAVE_FSTATVFS
11270 "HAVE_FSTATVFS",
11271#endif
11272
Georg Brandl306336b2012-06-24 12:55:33 +020011273#ifdef HAVE_FTRUNCATE
11274 "HAVE_FTRUNCATE",
11275#endif
11276
Larry Hastings9cf065c2012-06-22 16:30:09 -070011277#ifdef HAVE_FUTIMENS
11278 "HAVE_FUTIMENS",
11279#endif
11280
11281#ifdef HAVE_FUTIMES
11282 "HAVE_FUTIMES",
11283#endif
11284
11285#ifdef HAVE_FUTIMESAT
11286 "HAVE_FUTIMESAT",
11287#endif
11288
11289#ifdef HAVE_LINKAT
11290 "HAVE_LINKAT",
11291#endif
11292
11293#ifdef HAVE_LCHFLAGS
11294 "HAVE_LCHFLAGS",
11295#endif
11296
11297#ifdef HAVE_LCHMOD
11298 "HAVE_LCHMOD",
11299#endif
11300
11301#ifdef HAVE_LCHOWN
11302 "HAVE_LCHOWN",
11303#endif
11304
11305#ifdef HAVE_LSTAT
11306 "HAVE_LSTAT",
11307#endif
11308
11309#ifdef HAVE_LUTIMES
11310 "HAVE_LUTIMES",
11311#endif
11312
11313#ifdef HAVE_MKDIRAT
11314 "HAVE_MKDIRAT",
11315#endif
11316
11317#ifdef HAVE_MKFIFOAT
11318 "HAVE_MKFIFOAT",
11319#endif
11320
11321#ifdef HAVE_MKNODAT
11322 "HAVE_MKNODAT",
11323#endif
11324
11325#ifdef HAVE_OPENAT
11326 "HAVE_OPENAT",
11327#endif
11328
11329#ifdef HAVE_READLINKAT
11330 "HAVE_READLINKAT",
11331#endif
11332
11333#ifdef HAVE_RENAMEAT
11334 "HAVE_RENAMEAT",
11335#endif
11336
11337#ifdef HAVE_SYMLINKAT
11338 "HAVE_SYMLINKAT",
11339#endif
11340
11341#ifdef HAVE_UNLINKAT
11342 "HAVE_UNLINKAT",
11343#endif
11344
11345#ifdef HAVE_UTIMENSAT
11346 "HAVE_UTIMENSAT",
11347#endif
11348
11349#ifdef MS_WINDOWS
11350 "MS_WINDOWS",
11351#endif
11352
11353 NULL
11354};
11355
11356
Mark Hammondfe51c6d2002-08-02 02:27:13 +000011357PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000011358INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000011359{
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011361 PyObject *list;
11362 char **trace;
Tim Peters5aa91602002-01-30 05:46:57 +000011363
Brian Curtin52173d42010-12-02 18:29:18 +000011364#if defined(HAVE_SYMLINK) && defined(MS_WINDOWS)
Brian Curtin3b4499c2010-12-28 14:31:47 +000011365 win32_can_symlink = enable_symlink();
Brian Curtin52173d42010-12-02 18:29:18 +000011366#endif
11367
Victor Stinner8c62be82010-05-06 00:08:46 +000011368 m = PyModule_Create(&posixmodule);
11369 if (m == NULL)
11370 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000011371
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 /* Initialize environ dictionary */
11373 v = convertenviron();
11374 Py_XINCREF(v);
11375 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
11376 return NULL;
11377 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000011378
Victor Stinner8c62be82010-05-06 00:08:46 +000011379 if (all_ins(m))
11380 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000011381
Victor Stinner8c62be82010-05-06 00:08:46 +000011382 if (setup_confname_tables(m))
11383 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000011384
Victor Stinner8c62be82010-05-06 00:08:46 +000011385 Py_INCREF(PyExc_OSError);
11386 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000011387
Guido van Rossumb3d39562000-01-31 18:41:26 +000011388#ifdef HAVE_PUTENV
Victor Stinner8c62be82010-05-06 00:08:46 +000011389 if (posix_putenv_garbage == NULL)
11390 posix_putenv_garbage = PyDict_New();
Guido van Rossumb3d39562000-01-31 18:41:26 +000011391#endif
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011392
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 if (!initialized) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020011394#if defined(HAVE_WAITID) && !defined(__APPLE__)
11395 waitid_result_desc.name = MODNAME ".waitid_result";
11396 PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
11397#endif
11398
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 stat_result_desc.name = MODNAME ".stat_result";
11400 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
11401 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
11402 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
11403 PyStructSequence_InitType(&StatResultType, &stat_result_desc);
11404 structseq_new = StatResultType.tp_new;
11405 StatResultType.tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011406
Victor Stinner8c62be82010-05-06 00:08:46 +000011407 statvfs_result_desc.name = MODNAME ".statvfs_result";
11408 PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011409#ifdef NEED_TICKS_PER_SECOND
11410# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011412# elif defined(HZ)
Victor Stinner8c62be82010-05-06 00:08:46 +000011413 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011414# else
Victor Stinner8c62be82010-05-06 00:08:46 +000011415 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000011416# endif
11417#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011418
Benjamin Peterson0163c9a2011-08-02 18:11:38 -050011419#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011420 sched_param_desc.name = MODNAME ".sched_param";
11421 PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
11422 SchedParamType.tp_new = sched_param_new;
11423#endif
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011424
11425 /* initialize TerminalSize_info */
11426 PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
Victor Stinner8c62be82010-05-06 00:08:46 +000011427 }
Ross Lagerwall7807c352011-03-17 20:20:30 +020011428#if defined(HAVE_WAITID) && !defined(__APPLE__)
11429 Py_INCREF((PyObject*) &WaitidResultType);
11430 PyModule_AddObject(m, "waitid_result", (PyObject*) &WaitidResultType);
11431#endif
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 Py_INCREF((PyObject*) &StatResultType);
11433 PyModule_AddObject(m, "stat_result", (PyObject*) &StatResultType);
11434 Py_INCREF((PyObject*) &StatVFSResultType);
11435 PyModule_AddObject(m, "statvfs_result",
11436 (PyObject*) &StatVFSResultType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011437
11438#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
Benjamin Peterson94b580d2011-08-02 17:30:04 -050011439 Py_INCREF(&SchedParamType);
11440 PyModule_AddObject(m, "sched_param", (PyObject *)&SchedParamType);
Benjamin Petersone3298dd2011-08-02 18:40:46 -050011441#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000011442
Larry Hastings605a62d2012-06-24 04:33:36 -070011443 times_result_desc.name = MODNAME ".times_result";
11444 PyStructSequence_InitType(&TimesResultType, &times_result_desc);
11445 PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
11446
11447 uname_result_desc.name = MODNAME ".uname_result";
11448 PyStructSequence_InitType(&UnameResultType, &uname_result_desc);
11449 PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
11450
Thomas Wouters477c8d52006-05-27 19:21:47 +000011451#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000011452 /*
11453 * Step 2 of weak-linking support on Mac OS X.
11454 *
11455 * The code below removes functions that are not available on the
11456 * currently active platform.
11457 *
11458 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070011459 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000011460 * OSX 10.4.
11461 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011462#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011463 if (fstatvfs == NULL) {
11464 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
11465 return NULL;
11466 }
11467 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011468#endif /* HAVE_FSTATVFS */
11469
11470#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000011471 if (statvfs == NULL) {
11472 if (PyObject_DelAttrString(m, "statvfs") == -1) {
11473 return NULL;
11474 }
11475 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011476#endif /* HAVE_STATVFS */
11477
11478# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000011479 if (lchown == NULL) {
11480 if (PyObject_DelAttrString(m, "lchown") == -1) {
11481 return NULL;
11482 }
11483 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000011484#endif /* HAVE_LCHOWN */
11485
11486
11487#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011488
Antoine Pitrou9d20e0e2012-09-12 18:01:36 +020011489 Py_INCREF(&TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010011490 PyModule_AddObject(m, "terminal_size", (PyObject*) &TerminalSizeType);
11491
Larry Hastings6fe20b32012-04-19 15:07:49 -070011492 billion = PyLong_FromLong(1000000000);
11493 if (!billion)
11494 return NULL;
11495
Larry Hastings9cf065c2012-06-22 16:30:09 -070011496 /* suppress "function not used" warnings */
11497 {
11498 int ignored;
11499 fd_specified("", -1);
11500 follow_symlinks_specified("", 1);
11501 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
11502 dir_fd_converter(Py_None, &ignored);
11503 dir_fd_unavailable(Py_None, &ignored);
11504 }
11505
11506 /*
11507 * provide list of locally available functions
11508 * so os.py can populate support_* lists
11509 */
11510 list = PyList_New(0);
11511 if (!list)
11512 return NULL;
11513 for (trace = have_functions; *trace; trace++) {
11514 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
11515 if (!unicode)
11516 return NULL;
11517 if (PyList_Append(list, unicode))
11518 return NULL;
11519 Py_DECREF(unicode);
11520 }
11521 PyModule_AddObject(m, "_have_functions", list);
11522
11523 initialized = 1;
11524
Victor Stinner8c62be82010-05-06 00:08:46 +000011525 return m;
Thomas Wouters477c8d52006-05-27 19:21:47 +000011526
Guido van Rossumb6775db1994-08-01 11:34:53 +000011527}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000011528
11529#ifdef __cplusplus
11530}
11531#endif